From f3eeaa4b40fc656eb648af286b9c4ab959cff444 Mon Sep 17 00:00:00 2001 From: Dana Hoffman Date: Thu, 13 Jun 2019 11:00:29 -0700 Subject: [PATCH 1/2] fallback to region from redis location_id when region isn't specified --- products/redis/terraform.yaml | 2 + ...redis_location_id_for_fallback_zone.go.erb | 21 ++++++++++ .../tests/resource_redis_instance_test.go | 41 +++++++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 templates/terraform/encoders/redis_location_id_for_fallback_zone.go.erb diff --git a/products/redis/terraform.yaml b/products/redis/terraform.yaml index f3e3b326ef18..ca3ea15e40a9 100644 --- a/products/redis/terraform.yaml +++ b/products/redis/terraform.yaml @@ -21,6 +21,8 @@ overrides: !ruby/object:Overrides::ResourceOverrides autogen_async: true id_format: "{{project}}/{{region}}/{{name}}" import_format: ["projects/{{project}}/locations/{{region}}/instances/{{name}}"] + custom_code: !ruby/object:Provider::Terraform::CustomCode + encoder: templates/terraform/encoders/redis_location_id_for_fallback_zone.go.erb examples: - !ruby/object:Provider::Terraform::Examples name: "redis_instance_basic" diff --git a/templates/terraform/encoders/redis_location_id_for_fallback_zone.go.erb b/templates/terraform/encoders/redis_location_id_for_fallback_zone.go.erb new file mode 100644 index 000000000000..05398f172b70 --- /dev/null +++ b/templates/terraform/encoders/redis_location_id_for_fallback_zone.go.erb @@ -0,0 +1,21 @@ +<%# The license inside this block applies to this file. + # Copyright 2019 Google Inc. + # Licensed under the Apache License, Version 2.0 (the "License"); + # you may not use this file except in compliance with the License. + # You may obtain a copy of the License at + # + # http://www.apache.org/licenses/LICENSE-2.0 + # + # Unless required by applicable law or agreed to in writing, software + # distributed under the License is distributed on an "AS IS" BASIS, + # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + # See the License for the specific language governing permissions and + # limitations under the License. +-%> +config := meta.(*Config) +region, err := getRegionFromSchema("region", "location_id", d, config) +if err != nil { + return nil, err +} +d.Set("region", region) +return obj, nil diff --git a/third_party/terraform/tests/resource_redis_instance_test.go b/third_party/terraform/tests/resource_redis_instance_test.go index f92d595fbb80..b7e5e4dab656 100644 --- a/third_party/terraform/tests/resource_redis_instance_test.go +++ b/third_party/terraform/tests/resource_redis_instance_test.go @@ -38,6 +38,38 @@ func TestAccRedisInstance_update(t *testing.T) { }) } +func TestAccRedisInstance_regionFromLocation(t *testing.T) { + t.Parallel() + + name := acctest.RandomWithPrefix("tf-test") + + // Pick a zone that isn't in the provider-specified region so we know we + // didn't fall back to that one. + region := "us-west1" + zone := "us-west1-b" + if getTestRegionFromEnv() == "us-west1" { + region = "us-central1" + zone = "us-central1-a" + } + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckRedisInstanceDestroy, + Steps: []resource.TestStep{ + { + Config: testAccRedisInstance_regionFromLocation(name, zone), + Check: resource.TestCheckResourceAttr("google_redis_instance.test", "region", region), + }, + { + ResourceName: "google_redis_instance.test", + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + func testAccRedisInstance_update(name string) string { return fmt.Sprintf(` resource "google_redis_instance" "test" { @@ -76,3 +108,12 @@ resource "google_redis_instance" "test" { } }`, name) } + +func testAccRedisInstance_regionFromLocation(name, zone string) string { + return fmt.Sprintf(` +resource "google_redis_instance" "test" { + name = "%s" + memory_size_gb = 1 + location_id = "%s" +}`, name, zone) +} From 8890f44fc5dda4fc2d8c4afff37e3554166fdd20 Mon Sep 17 00:00:00 2001 From: Modular Magician Date: Thu, 13 Jun 2019 20:56:46 +0000 Subject: [PATCH 2/2] Update tracked submodules -> HEAD on Thu Jun 13 20:56:46 UTC 2019 Tracked submodules are build/terraform-beta build/terraform-mapper build/terraform build/ansible build/inspec. --- build/terraform | 2 +- build/terraform-beta | 2 +- build/terraform-mapper | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/build/terraform b/build/terraform index 2a40e5a749e5..f9ce6c371963 160000 --- a/build/terraform +++ b/build/terraform @@ -1 +1 @@ -Subproject commit 2a40e5a749e5058ce1406b708d14d75855e8607f +Subproject commit f9ce6c3719639844f7e1ac5c5f53253778b98229 diff --git a/build/terraform-beta b/build/terraform-beta index 7be1b03663d6..3e2e4cc315e8 160000 --- a/build/terraform-beta +++ b/build/terraform-beta @@ -1 +1 @@ -Subproject commit 7be1b03663d6c315b7e1f5d6fad369f2c32cab64 +Subproject commit 3e2e4cc315e8ad212ef95c324baa796389fdbdf3 diff --git a/build/terraform-mapper b/build/terraform-mapper index 9af028d29b62..d72e1eed02db 160000 --- a/build/terraform-mapper +++ b/build/terraform-mapper @@ -1 +1 @@ -Subproject commit 9af028d29b62cf4e25278a9f747625ef690ffd8b +Subproject commit d72e1eed02db7def154b18957826439502913876