From 949e27e32be007e42d54722bf46c2bb08127d827 Mon Sep 17 00:00:00 2001 From: Dana Hoffman Date: Thu, 13 Jun 2019 19:28:59 +0000 Subject: [PATCH] fallback to region from redis location_id when region isn't specified Signed-off-by: Modular Magician --- google-beta/resource_redis_instance.go | 20 ++++++++++ google-beta/resource_redis_instance_test.go | 41 +++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/google-beta/resource_redis_instance.go b/google-beta/resource_redis_instance.go index 9cc17db7a2..14325cb568 100644 --- a/google-beta/resource_redis_instance.go +++ b/google-beta/resource_redis_instance.go @@ -208,6 +208,11 @@ func resourceRedisInstanceCreate(d *schema.ResourceData, meta interface{}) error obj["tier"] = tierProp } + obj, err = resourceRedisInstanceEncoder(d, meta, obj) + if err != nil { + return err + } + url, err := replaceVars(d, config, "{{RedisBasePath}}projects/{{project}}/locations/{{region}}/instances?instanceId={{name}}") if err != nil { return err @@ -352,6 +357,11 @@ func resourceRedisInstanceUpdate(d *schema.ResourceData, meta interface{}) error obj["memorySizeGb"] = memorySizeGbProp } + obj, err = resourceRedisInstanceEncoder(d, meta, obj) + if err != nil { + return err + } + url, err := replaceVars(d, config, "{{RedisBasePath}}projects/{{project}}/locations/{{region}}/instances/{{name}}") if err != nil { return err @@ -597,3 +607,13 @@ func expandRedisInstanceReservedIpRange(v interface{}, d TerraformResourceData, func expandRedisInstanceTier(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { return v, nil } + +func resourceRedisInstanceEncoder(d *schema.ResourceData, meta interface{}, obj map[string]interface{}) (map[string]interface{}, error) { + 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/google-beta/resource_redis_instance_test.go b/google-beta/resource_redis_instance_test.go index f92d595fbb..b7e5e4dab6 100644 --- a/google-beta/resource_redis_instance_test.go +++ b/google-beta/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) +}