diff --git a/third_party/terraform/resources/resource_bigtable_instance.go b/third_party/terraform/resources/resource_bigtable_instance.go index bcc6e21e45d3..fea6d9ad3794 100644 --- a/third_party/terraform/resources/resource_bigtable_instance.go +++ b/third_party/terraform/resources/resource_bigtable_instance.go @@ -62,7 +62,8 @@ func resourceBigtableInstance() *schema.Resource { }, "zone": { Type: schema.TypeString, - Required: true, + Computed: true, + Optional: true, Description: `The zone to create the Cloud Bigtable cluster in. Each cluster must have a different zone in the same region. Zones that support Bigtable instances are noted on the Cloud Bigtable locations page.`, }, "num_nodes": { @@ -161,7 +162,10 @@ func resourceBigtableInstanceCreate(d *schema.ResourceData, meta interface{}) er conf.InstanceType = bigtable.PRODUCTION } - conf.Clusters = expandBigtableClusters(d.Get("cluster").([]interface{}), conf.InstanceID) + conf.Clusters, err = expandBigtableClusters(d.Get("cluster").([]interface{}), conf.InstanceID, config) + if err != nil { + return err + } c, err := config.BigTableClientFactory(userAgent).NewInstanceAdminClient(project) if err != nil { @@ -288,7 +292,10 @@ func resourceBigtableInstanceUpdate(d *schema.ResourceData, meta interface{}) er conf.InstanceType = bigtable.PRODUCTION } - conf.Clusters = expandBigtableClusters(d.Get("cluster").([]interface{}), conf.InstanceID) + conf.Clusters, err = expandBigtableClusters(d.Get("cluster").([]interface{}), conf.InstanceID, config) + if err != nil { + return err + } _, err = bigtable.UpdateInstanceAndSyncClusters(ctx, c, conf) if err != nil { @@ -350,11 +357,14 @@ func flattenBigtableCluster(c *bigtable.ClusterInfo) map[string]interface{} { } } -func expandBigtableClusters(clusters []interface{}, instanceID string) []bigtable.ClusterConfig { +func expandBigtableClusters(clusters []interface{}, instanceID string, config *Config) ([]bigtable.ClusterConfig, error) { results := make([]bigtable.ClusterConfig, 0, len(clusters)) for _, c := range clusters { cluster := c.(map[string]interface{}) - zone := cluster["zone"].(string) + zone, err := getBigtableZone(cluster["zone"].(string), config) + if err != nil { + return nil, err + } var storageType bigtable.StorageType switch cluster["storage_type"].(string) { case "SSD": @@ -370,7 +380,19 @@ func expandBigtableClusters(clusters []interface{}, instanceID string) []bigtabl StorageType: storageType, }) } - return results + return results, nil +} + +// getBigtableZone reads the "zone" value from the given resource data and falls back +// to provider's value if not given. If neither is provided, returns an error. +func getBigtableZone(z string, config *Config) (string, error) { + if z == "" { + if config.Zone != "" { + return config.Zone, nil + } + return "", fmt.Errorf("cannot determine zone: set in cluster.0.zone, or set provider-level zone") + } + return GetResourceNameFromSelfLink(z), nil } // resourceBigtableInstanceClusterReorderTypeList causes the cluster block to diff --git a/third_party/terraform/tests/resource_bigtable_instance_test.go b/third_party/terraform/tests/resource_bigtable_instance_test.go index 3de97c5c338b..8a03841842b1 100644 --- a/third_party/terraform/tests/resource_bigtable_instance_test.go +++ b/third_party/terraform/tests/resource_bigtable_instance_test.go @@ -190,7 +190,6 @@ resource "google_bigtable_instance" "instance" { name = "%s" cluster { cluster_id = "%s" - zone = "us-central1-b" num_nodes = %d storage_type = "HDD" } diff --git a/third_party/terraform/utils/provider_test.go.erb b/third_party/terraform/utils/provider_test.go.erb index 1ffdfae55cb6..c87aacc47860 100644 --- a/third_party/terraform/utils/provider_test.go.erb +++ b/third_party/terraform/utils/provider_test.go.erb @@ -419,12 +419,12 @@ func testAccPreCheck(t *testing.T) { t.Fatalf("One of %s must be set for acceptance tests", strings.Join(projectEnvVars, ", ")) } - if v := multiEnvSearch(regionEnvVars); v != "us-central1" { - t.Fatalf("One of %s must be set to us-central1 for acceptance tests", strings.Join(regionEnvVars, ", ")) + if v := multiEnvSearch(regionEnvVars); v == "" { + t.Fatalf("One of %s must be set for acceptance tests", strings.Join(regionEnvVars, ", ")) } - if v := multiEnvSearch(zoneEnvVars); v != "us-central1-a" { - t.Fatalf("One of %s must be set to us-central1-a for acceptance tests", strings.Join(zoneEnvVars, ", ")) + if v := multiEnvSearch(zoneEnvVars); v == "" { + t.Fatalf("One of %s must be set for acceptance tests", strings.Join(zoneEnvVars, ", ")) } } diff --git a/third_party/terraform/website/docs/r/bigtable_gc_policy.html.markdown b/third_party/terraform/website/docs/r/bigtable_gc_policy.html.markdown index 7c6ff3c237cd..e58904790823 100644 --- a/third_party/terraform/website/docs/r/bigtable_gc_policy.html.markdown +++ b/third_party/terraform/website/docs/r/bigtable_gc_policy.html.markdown @@ -21,7 +21,6 @@ resource "google_bigtable_instance" "instance" { name = "tf-instance" cluster { cluster_id = "tf-instance-cluster" - zone = "us-central1-b" num_nodes = 3 storage_type = "HDD" } diff --git a/third_party/terraform/website/docs/r/bigtable_instance.html.markdown b/third_party/terraform/website/docs/r/bigtable_instance.html.markdown index 746153d13169..2a2a31526d11 100644 --- a/third_party/terraform/website/docs/r/bigtable_instance.html.markdown +++ b/third_party/terraform/website/docs/r/bigtable_instance.html.markdown @@ -31,39 +31,16 @@ resource "google_bigtable_instance" "production-instance" { cluster { cluster_id = "tf-instance-cluster" - zone = "us-central1-b" num_nodes = 1 storage_type = "HDD" } - lifecycle { - prevent_destroy = true - } - labels = { my-label = "prod-label" } } ``` -## Example Usage - Development Instance - -```hcl -resource "google_bigtable_instance" "development-instance" { - name = "tf-instance" - instance_type = "DEVELOPMENT" - - cluster { - cluster_id = "tf-instance-cluster" - zone = "us-central1-b" - storage_type = "HDD" - } - - labels = { - my-label = "dev-label" - } -} -``` ## Argument Reference @@ -99,8 +76,8 @@ The `cluster` block supports the following arguments: * `cluster_id` - (Required) The ID of the Cloud Bigtable cluster. -* `zone` - (Required) The zone to create the Cloud Bigtable cluster in. Each -cluster must have a different zone in the same region. Zones that support +* `zone` - (Optional) The zone to create the Cloud Bigtable cluster in. If it not +specified, the provider zone is used. Each cluster must have a different zone in the same region. Zones that support Bigtable instances are noted on the [Cloud Bigtable locations page](https://cloud.google.com/bigtable/docs/locations). * `num_nodes` - (Optional) The number of nodes in your Cloud Bigtable cluster.