Skip to content

Commit

Permalink
Infer bigtable zone from provider (#4392) (#8222)
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

Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Jan 14, 2021
1 parent 07ae6ae commit 03d2ec2
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 37 deletions.
3 changes: 3 additions & 0 deletions .changelog/4392.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
bigtable: fixed an issue where the `google_bigtable_instance` resource was not inferring the zone from the provider.
```
8 changes: 4 additions & 4 deletions google/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,12 +403,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
34 changes: 28 additions & 6 deletions google/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
1 change: 0 additions & 1 deletion google/resource_bigtable_instance_test.go
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
1 change: 0 additions & 1 deletion website/docs/r/bigtable_gc_policy.html.markdown
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
27 changes: 2 additions & 25 deletions website/docs/r/bigtable_instance.html.markdown
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 03d2ec2

Please sign in to comment.