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

Infer bigtable zone from provider #4392

Merged
merged 11 commits into from
Jan 14, 2021
Merged
33 changes: 27 additions & 6 deletions third_party/terraform/resources/resource_bigtable_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func resourceBigtableInstance() *schema.Resource {
},
"zone": {
Type: schema.TypeString,
Required: true,
Computed: 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": {
Expand Down Expand Up @@ -160,7 +160,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 {
Expand Down Expand Up @@ -287,7 +290,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 {
Expand Down Expand Up @@ -349,11 +355,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":
Expand All @@ -369,7 +378,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
melinath marked this conversation as resolved.
Show resolved Hide resolved
}

// resourceBigtableInstanceClusterReorderTypeList causes the cluster block to
Expand Down
8 changes: 4 additions & 4 deletions third_party/terraform/utils/provider_test.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "" {
melinath marked this conversation as resolved.
Show resolved Hide resolved
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, ", "))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
melinath marked this conversation as resolved.
Show resolved Hide resolved

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

Expand Down Expand Up @@ -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.
Expand Down