Container cluster importable (#391)

This commit is contained in:
Anders Bruun Olsen 2017-09-07 19:31:58 +02:00 committed by Vincent Roseberry
parent ddc522bfec
commit 67b7b2dd72
4 changed files with 65 additions and 5 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 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,
},
},
})
}

View File

@ -5,6 +5,7 @@ import (
"log"
"net"
"regexp"
"strings"
"time"
"github.com/hashicorp/terraform/helper/resource"
@ -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,
@ -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)
@ -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
}

View File

@ -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"),
@ -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" {

View File

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