Make google_container_node_pool resources importable. (#284)

This commit is contained in:
Anders Bruun Olsen 2017-08-07 19:35:39 +02:00 committed by Vincent Roseberry
parent 958f27892b
commit f06970c97a
3 changed files with 58 additions and 0 deletions

View File

@ -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,
},
},
})
}

View File

@ -3,6 +3,7 @@ package google
import (
"fmt"
"log"
"strings"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
@ -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,
@ -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
}

View File

@ -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
```