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

fallback to region from redis location_id when region isn't specified #847

Merged
merged 1 commit into from
Jun 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions google-beta/resource_redis_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
41 changes: 41 additions & 0 deletions google-beta/resource_redis_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down Expand Up @@ -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)
}