Skip to content

Commit

Permalink
Container cluster importable (hashicorp#391)
Browse files Browse the repository at this point in the history
  • Loading branch information
drzero42 authored and rosbo committed Sep 7, 2017
1 parent ec66a47 commit 4fdfe1c
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 5 deletions.
32 changes: 32 additions & 0 deletions google/import_container_cluster_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 TestAccGoogleContainerCluster_import(t *testing.T) {
resourceName := "google_container_cluster.primary"
name := fmt.Sprintf("tf-cluster-test-%s", acctest.RandString(10))
conf := testAccContainerCluster_basic(name)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckContainerClusterDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: conf,
},

resource.TestStep{
ResourceName: resourceName,
ImportStateIdPrefix: "us-central1-a/",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
20 changes: 19 additions & 1 deletion google/resource_container_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"
"net"
"regexp"
"strings"
"time"

"github.com/hashicorp/terraform/helper/resource"
Expand Down Expand Up @@ -33,6 +34,10 @@ func resourceContainerCluster() *schema.Resource {
SchemaVersion: 1,
MigrateState: resourceContainerClusterMigrateState,

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

Schema: map[string]*schema.Schema{
"master_auth": {
Type: schema.TypeList,
Expand Down Expand Up @@ -492,7 +497,7 @@ func resourceContainerClusterRead(d *schema.ResourceData, meta interface{}) erro
d.Set("enable_legacy_abac", cluster.LegacyAbac.Enabled)
d.Set("logging_service", cluster.LoggingService)
d.Set("monitoring_service", cluster.MonitoringService)
d.Set("network", d.Get("network").(string))
d.Set("network", cluster.Network)
d.Set("subnetwork", cluster.Subnetwork)
d.Set("node_config", flattenNodeConfig(cluster.NodeConfig))
nps, err := flattenClusterNodePools(d, config, cluster.NodePools)
Expand Down Expand Up @@ -779,3 +784,16 @@ func generateNodePoolName(prefix string, d *schema.ResourceData) (string, error)
return resource.UniqueId(), nil
}
}

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

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

return []*schema.ResourceData{d}, nil
}
10 changes: 6 additions & 4 deletions google/resource_container_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestAccContainerCluster_basic(t *testing.T) {
CheckDestroy: testAccCheckContainerClusterDestroy,
Steps: []resource.TestStep{
{
Config: testAccContainerCluster_basic,
Config: testAccContainerCluster_basic(fmt.Sprintf("cluster-test-%s", acctest.RandString(10))),
Check: resource.ComposeTestCheckFunc(
testAccCheckContainerCluster(
"google_container_cluster.primary"),
Expand Down Expand Up @@ -623,12 +623,14 @@ func matchError(attr, tf interface{}, gcp interface{}) string {
return fmt.Sprintf("Cluster has mismatched %s.\nTF State: %+v\nGCP State: %+v", attr, tf, gcp)
}

var testAccContainerCluster_basic = fmt.Sprintf(`
func testAccContainerCluster_basic(name string) string {
return fmt.Sprintf(`
resource "google_container_cluster" "primary" {
name = "cluster-test-%s"
name = "%s"
zone = "us-central1-a"
initial_node_count = 3
}`, acctest.RandString(10))
}`, name)
}

var testAccContainerCluster_withTimeout = fmt.Sprintf(`
resource "google_container_cluster" "primary" {
Expand Down
8 changes: 8 additions & 0 deletions website/docs/r/container_cluster.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,11 @@ exported:
- `create` - (Default `30 minutes`) Used for clusters
- `update` - (Default `10 minutes`) Used for updates to clusters
- `delete` - (Default `10 minutes`) Used for destroying clusters.

## Import

Container clusters can be imported using the `zone`, and `name`, e.g.

```
$ terraform import google_container_cluster.mycluster us-east1-a/my-cluster
```

0 comments on commit 4fdfe1c

Please sign in to comment.