Skip to content

Commit edb34aa

Browse files
modular-magiciandanawillow
authored andcommitted
Fix redis authorized network and tests. The Redis API currently only accepts partial links. The tests weren't failing because they weren't actually using the network (oops). There were a few other test issues that I fixed while I was there. Fixes #1571. (#1599)
<!-- This change is generated by MagicModules. --> /cc @danawillow
1 parent 55c2c17 commit edb34aa

File tree

2 files changed

+43
-9
lines changed

2 files changed

+43
-9
lines changed

google/resource_redis_instance.go

+10-5
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,11 @@ func resourceRedisInstance() *schema.Resource {
5959
ForceNew: true,
6060
},
6161
"authorized_network": {
62-
Type: schema.TypeString,
63-
Computed: true,
64-
Optional: true,
65-
ForceNew: true,
62+
Type: schema.TypeString,
63+
Computed: true,
64+
Optional: true,
65+
ForceNew: true,
66+
DiffSuppressFunc: compareSelfLinkRelativePaths,
6667
},
6768
"display_name": {
6869
Type: schema.TypeString,
@@ -556,7 +557,11 @@ func expandRedisInstanceAlternativeLocationId(v interface{}, d *schema.ResourceD
556557
}
557558

558559
func expandRedisInstanceAuthorizedNetwork(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) {
559-
return v, nil
560+
fv, err := ParseNetworkFieldValue(v.(string), d, config)
561+
if err != nil {
562+
return nil, err
563+
}
564+
return fv.RelativeLink(), nil
560565
}
561566

562567
func expandRedisInstanceDisplayName(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) {

google/resource_redis_instance_test.go

+33-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ package google
22

33
import (
44
"fmt"
5+
"strings"
56
"testing"
67

78
"github.com/hashicorp/terraform/helper/acctest"
89
"github.com/hashicorp/terraform/helper/resource"
10+
"github.com/hashicorp/terraform/terraform"
911
)
1012

1113
func TestAccRedisInstance_basic(t *testing.T) {
@@ -16,7 +18,7 @@ func TestAccRedisInstance_basic(t *testing.T) {
1618
resource.Test(t, resource.TestCase{
1719
PreCheck: func() { testAccPreCheck(t) },
1820
Providers: testAccProviders,
19-
CheckDestroy: testAccCheckComputeAddressDestroy,
21+
CheckDestroy: testAccCheckRedisInstanceDestroy,
2022
Steps: []resource.TestStep{
2123
resource.TestStep{
2224
Config: testAccRedisInstance_basic(name),
@@ -38,7 +40,7 @@ func TestAccRedisInstance_update(t *testing.T) {
3840
resource.Test(t, resource.TestCase{
3941
PreCheck: func() { testAccPreCheck(t) },
4042
Providers: testAccProviders,
41-
CheckDestroy: testAccCheckComputeAddressDestroy,
43+
CheckDestroy: testAccCheckRedisInstanceDestroy,
4244
Steps: []resource.TestStep{
4345
resource.TestStep{
4446
Config: testAccRedisInstance_update(name),
@@ -69,7 +71,7 @@ func TestAccRedisInstance_full(t *testing.T) {
6971
resource.Test(t, resource.TestCase{
7072
PreCheck: func() { testAccPreCheck(t) },
7173
Providers: testAccProviders,
72-
CheckDestroy: testAccCheckComputeAddressDestroy,
74+
CheckDestroy: testAccCheckRedisInstanceDestroy,
7375
Steps: []resource.TestStep{
7476
resource.TestStep{
7577
Config: testAccRedisInstance_full(name, network),
@@ -83,6 +85,31 @@ func TestAccRedisInstance_full(t *testing.T) {
8385
})
8486
}
8587

88+
func testAccCheckRedisInstanceDestroy(s *terraform.State) error {
89+
config := testAccProvider.Meta().(*Config)
90+
91+
for _, rs := range s.RootModule().Resources {
92+
if rs.Type != "google_redis_instance" {
93+
continue
94+
}
95+
96+
redisIdParts := strings.Split(rs.Primary.ID, "/")
97+
if len(redisIdParts) != 3 {
98+
return fmt.Errorf("Unexpected resource ID %s, expected {project}/{region}/{name}", rs.Primary.ID)
99+
}
100+
101+
project, region, inst := redisIdParts[0], redisIdParts[1], redisIdParts[2]
102+
103+
name := fmt.Sprintf("projects/%s/locations/%s/instances/%s", project, region, inst)
104+
_, err := config.clientRedis.Projects.Locations.Get(name).Do()
105+
if err == nil {
106+
return fmt.Errorf("Redis instance still exists")
107+
}
108+
}
109+
110+
return nil
111+
}
112+
86113
func testAccRedisInstance_basic(name string) string {
87114
return fmt.Sprintf(`
88115
resource "google_redis_instance" "test" {
@@ -130,6 +157,8 @@ resource "google_redis_instance" "test" {
130157
tier = "STANDARD_HA"
131158
memory_size_gb = 1
132159
160+
authorized_network = "${google_compute_network.test.self_link}"
161+
133162
region = "us-central1"
134163
location_id = "us-central1-a"
135164
alternative_location_id = "us-central1-f"
@@ -142,5 +171,5 @@ resource "google_redis_instance" "test" {
142171
my_key = "my_val"
143172
other_key = "other_val"
144173
}
145-
}`, name, network)
174+
}`, network, name)
146175
}

0 commit comments

Comments
 (0)