Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Container cluster importable #391

Merged
merged 11 commits into from
Sep 7, 2017
31 changes: 31 additions & 0 deletions google/import_container_cluster_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
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.np"
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,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
19 changes: 18 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 @@ -32,6 +33,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 @@ -521,7 +526,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", flattenClusterNodeConfig(cluster.NodeConfig))
d.Set("node_pool", flattenClusterNodePools(d, cluster.NodePools))
Expand Down Expand Up @@ -742,3 +747,15 @@ func generateNodePoolName(prefix string, d *schema.ResourceData) (string, error)
return resource.UniqueId(), nil
}
}

func resourceContainerClusterStateImporter(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test test is failling:
--- FAIL: TestAccGoogleContainerCluster_import (275.85s)
testing.go:428: Step 1 error: Resource specified by ResourceName couldn't be found: google_container_cluster.np

The ID should be set to the name only. See the create method above (line 474).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh.. Should be fixed now :)

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])

return []*schema.ResourceData{d}, nil
}
13 changes: 8 additions & 5 deletions google/resource_container_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import (

"strconv"

"regexp"

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

func TestAccContainerCluster_basic(t *testing.T) {
Expand All @@ -22,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 @@ -493,12 +494,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 @@ -212,3 +212,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
```