Skip to content

Commit

Permalink
Infer bigtable zone from provider (#4392)
Browse files Browse the repository at this point in the history
* infer zone from provider

Co-authored-by: upodroid <[email protected]>

* remove zone from 1 test

* make zone optional
  • Loading branch information
upodroid authored Jan 14, 2021
1 parent 719907f commit 8b6e708
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 37 deletions.
34 changes: 28 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,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": {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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":
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
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 == "" {
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

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

0 comments on commit 8b6e708

Please sign in to comment.