Skip to content

Commit

Permalink
Make google_container_node_pool resources importable. (hashicorp#284)
Browse files Browse the repository at this point in the history
  • Loading branch information
drzero42 authored and Dmitry Vlasov committed Aug 15, 2017
1 parent 92e50ae commit 92bc3db
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
32 changes: 32 additions & 0 deletions google/import_container_node_pool_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package google

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
)

func TestAccGoogleContainerNodePool_import(t *testing.T) {
resourceName := "google_container_node_pool.np"
cluster := fmt.Sprintf("tf-nodepool-test-%s", acctest.RandString(10))
np := fmt.Sprintf("tf-nodepool-test-%s", acctest.RandString(10))
conf := testAccContainerNodePool_basic(cluster, np)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckContainerNodePoolDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: conf,
},

resource.TestStep{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
18 changes: 18 additions & 0 deletions google/resource_container_node_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package google
import (
"fmt"
"log"
"strings"

"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
Expand All @@ -17,6 +18,10 @@ func resourceContainerNodePool() *schema.Resource {
Delete: resourceContainerNodePoolDelete,
Exists: resourceContainerNodePoolExists,

Importer: &schema.ResourceImporter{
State: resourceContainerNodePoolStateImporter,
},

Schema: map[string]*schema.Schema{
"project": &schema.Schema{
Type: schema.TypeString,
Expand Down Expand Up @@ -357,3 +362,16 @@ func resourceContainerNodePoolExists(d *schema.ResourceData, meta interface{}) (
}
return true, nil
}

func resourceContainerNodePoolStateImporter(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
parts := strings.Split(d.Id(), "/")
if len(parts) != 3 {
return nil, fmt.Errorf("Invalid container cluster specifier. Expecting {zone}/{cluster}/{name}")
}

d.Set("zone", parts[0])
d.Set("cluster", parts[1])
d.Set("name", parts[2])

return []*schema.ResourceData{d}, nil
}
8 changes: 8 additions & 0 deletions website/docs/r/container_node_pool.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,11 @@ The `autoscaling` block supports:
<= `maxNodeCount`.

* `maxNodeCount` - (Required) Maximum number of nodes in the NodePool. Must be >= minNodeCount.

## Import

Node pools can be imported using the `zone`, `cluster` and `name`, e.g.

```
$ terraform import google_container_node_pool.mainpool us-east1-a/my-cluster/main-pool
```

0 comments on commit 92bc3db

Please sign in to comment.