Skip to content

Commit

Permalink
fallback to region from redis location_id when region isn't specified (
Browse files Browse the repository at this point in the history
…#1918)

Merged PR #1918.
  • Loading branch information
danawillow authored and modular-magician committed Jun 13, 2019
1 parent caf4024 commit 726a8f7
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 3 deletions.
2 changes: 1 addition & 1 deletion build/terraform
2 changes: 1 addition & 1 deletion build/terraform-beta
2 changes: 1 addition & 1 deletion build/terraform-mapper
2 changes: 2 additions & 0 deletions products/redis/terraform.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
@@ -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
41 changes: 41 additions & 0 deletions third_party/terraform/tests/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)
}

0 comments on commit 726a8f7

Please sign in to comment.