Skip to content

Commit 24ff156

Browse files
authored
Revert "Fix permadiff that reorders stateful_external_ip blocks on google_compute_instance_group_manager and google_compute_region_instance_group_manager resources" (#9754)
This reverts commit de43d70.
1 parent 89f2396 commit 24ff156

6 files changed

+102
-460
lines changed

mmv1/third_party/terraform/services/compute/resource_compute_instance_group_manager.go.erb

+19-51
Original file line numberDiff line numberDiff line change
@@ -778,10 +778,10 @@ func resourceComputeInstanceGroupManagerRead(d *schema.ResourceData, meta interf
778778
if err = d.Set("stateful_disk", flattenStatefulPolicy(manager.StatefulPolicy)); err != nil {
779779
return fmt.Errorf("Error setting stateful_disk in state: %s", err.Error())
780780
}
781-
if err = d.Set("stateful_internal_ip", flattenStatefulPolicyStatefulInternalIps(d, manager.StatefulPolicy)); err != nil {
781+
if err = d.Set("stateful_internal_ip", flattenStatefulPolicyStatefulInternalIps(manager.StatefulPolicy)); err != nil {
782782
return fmt.Errorf("Error setting stateful_internal_ip in state: %s", err.Error())
783783
}
784-
if err = d.Set("stateful_external_ip", flattenStatefulPolicyStatefulExternalIps(d, manager.StatefulPolicy)); err != nil {
784+
if err = d.Set("stateful_external_ip", flattenStatefulPolicyStatefulExternalIps(manager.StatefulPolicy)); err != nil {
785785
return fmt.Errorf("Error setting stateful_external_ip in state: %s", err.Error())
786786
}
787787
if err := d.Set("fingerprint", manager.Fingerprint); err != nil {
@@ -1306,68 +1306,36 @@ func flattenStatefulPolicy(statefulPolicy *compute.StatefulPolicy) []map[string]
13061306
return result
13071307
}
13081308

1309-
func flattenStatefulPolicyStatefulInternalIps(d *schema.ResourceData, statefulPolicy *compute.StatefulPolicy) []map[string]interface{} {
1309+
func flattenStatefulPolicyStatefulInternalIps(statefulPolicy *compute.StatefulPolicy) []map[string]interface{} {
13101310
if statefulPolicy == nil || statefulPolicy.PreservedState == nil || statefulPolicy.PreservedState.InternalIPs == nil {
13111311
return make([]map[string]interface{}, 0, 0)
13121312
}
1313+
result := make([]map[string]interface{}, 0, len(statefulPolicy.PreservedState.InternalIPs))
1314+
for interfaceName, internalIp := range statefulPolicy.PreservedState.InternalIPs {
1315+
data := map[string]interface{}{
1316+
"interface_name": interfaceName,
1317+
"delete_rule": internalIp.AutoDelete,
1318+
}
13131319

1314-
return flattenStatefulPolicyStatefulIps(d, "stateful_internal_ip", statefulPolicy.PreservedState.InternalIPs)
1315-
}
1316-
1317-
func flattenStatefulPolicyStatefulExternalIps(d *schema.ResourceData, statefulPolicy *compute.StatefulPolicy) []map[string]interface{} {
1318-
if statefulPolicy == nil || statefulPolicy.PreservedState == nil || statefulPolicy.PreservedState.ExternalIPs == nil {
1319-
return make([]map[string]interface{}, 0)
1320+
result = append(result, data)
13201321
}
1321-
1322-
return flattenStatefulPolicyStatefulIps(d, "stateful_external_ip", statefulPolicy.PreservedState.ExternalIPs)
1322+
return result
13231323
}
13241324

1325-
func flattenStatefulPolicyStatefulIps(d *schema.ResourceData, ipfieldName string, ips map[string]compute.StatefulPolicyPreservedStateNetworkIp) []map[string]interface{} {
1326-
1327-
// statefulPolicy.PreservedState.ExternalIPs and statefulPolicy.PreservedState.InternalIPs are affected by API-side reordering
1328-
// of external/internal IPs, where ordering is done by the interface_name value.
1329-
// Below we intend to reorder the IPs to match the order in the config.
1330-
// Also, data is converted from a map (client library's statefulPolicy.PreservedState.ExternalIPs, or .InternalIPs) to a slice (stored in state).
1331-
// Any IPs found from the API response that aren't in the config are appended to the end of the slice.
1332-
1333-
configIpOrder := d.Get(ipfieldName).([]interface{})
1334-
order := map[string]int{} // record map of interface name to index
1335-
for i, el := range configIpOrder {
1336-
ip := el.(map[string]interface{})
1337-
interfaceName := ip["interface_name"].(string)
1338-
order[interfaceName] = i
1325+
func flattenStatefulPolicyStatefulExternalIps(statefulPolicy *compute.StatefulPolicy) []map[string]interface{} {
1326+
if statefulPolicy == nil || statefulPolicy.PreservedState == nil || statefulPolicy.PreservedState.ExternalIPs == nil {
1327+
return make([]map[string]interface{}, 0, 0)
13391328
}
1340-
1341-
orderedResult := make([]map[string]interface{}, len(configIpOrder))
1342-
unexpectedIps := []map[string]interface{}{}
1343-
for interfaceName, ip := range ips {
1329+
result := make([]map[string]interface{}, 0, len(statefulPolicy.PreservedState.ExternalIPs))
1330+
for interfaceName, externalIp := range statefulPolicy.PreservedState.ExternalIPs {
13441331
data := map[string]interface{}{
13451332
"interface_name": interfaceName,
1346-
"delete_rule": ip.AutoDelete,
1347-
}
1348-
1349-
index, found := order[interfaceName]
1350-
if !found {
1351-
unexpectedIps = append(unexpectedIps, data)
1352-
continue
1333+
"delete_rule": externalIp.AutoDelete,
13531334
}
1354-
orderedResult[index] = data // Put elements from API response in order that matches the config
1355-
}
13561335

1357-
// Remove any nils from the ordered list. This can occur if the API doesn't include an interface present in the config.
1358-
finalResult := []map[string]interface{}{}
1359-
for _, item := range orderedResult {
1360-
if item != nil {
1361-
finalResult = append(finalResult, item)
1362-
}
1363-
}
1364-
1365-
if len(unexpectedIps) > 0 {
1366-
// Additional IPs returned from API but not in the config are appended to the end of the slice
1367-
finalResult = append(finalResult, unexpectedIps...)
1336+
result = append(result, data)
13681337
}
1369-
1370-
return finalResult
1338+
return result
13711339
}
13721340

13731341
func flattenUpdatePolicy(updatePolicy *compute.InstanceGroupManagerUpdatePolicy) []map[string]interface{} {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package compute
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestInstanceGroupManager_parseUniqueId(t *testing.T) {
8+
expectations := map[string][]string{
9+
"projects/imre-test/global/instanceTemplates/example-template-custom?uniqueId=123": {"projects/imre-test/global/instanceTemplates/example-template-custom", "123"},
10+
"https://www.googleapis.com/compute/v1/projects/imre-test/global/instanceTemplates/example-template-custom?uniqueId=123": {"https://www.googleapis.com/compute/v1/projects/imre-test/global/instanceTemplates/example-template-custom", "123"},
11+
"projects/imre-test/global/instanceTemplates/example-template-custom": {"projects/imre-test/global/instanceTemplates/example-template-custom", ""},
12+
"https://www.googleapis.com/compute/v1/projects/imre-test/global/instanceTemplates/example-template-custom": {"https://www.googleapis.com/compute/v1/projects/imre-test/global/instanceTemplates/example-template-custom", ""},
13+
"example-template-custom?uniqueId=123": {"example-template-custom", "123"},
14+
15+
// this test demonstrates that uniqueIds can't override eachother
16+
"projects/imre-test/global/instanceTemplates/example?uniqueId=123?uniqueId=456": {"projects/imre-test/global/instanceTemplates/example", "123?uniqueId=456"},
17+
}
18+
19+
for k, v := range expectations {
20+
aName, aUniqueId := parseUniqueId(k)
21+
if v[0] != aName {
22+
t.Errorf("parseUniqueId failed; name of %v should be %v, not %v", k, v[0], aName)
23+
}
24+
if v[1] != aUniqueId {
25+
t.Errorf("parseUniqueId failed; uniqueId of %v should be %v, not %v", k, v[1], aUniqueId)
26+
}
27+
}
28+
}
29+
30+
func TestInstanceGroupManager_compareInstanceTemplate(t *testing.T) {
31+
shouldAllMatch := []string{
32+
// uniqueId not present
33+
"https://www.googleapis.com/compute/v1/projects/imre-test/global/instanceTemplates/example-template-custom",
34+
"projects/imre-test/global/instanceTemplates/example-template-custom",
35+
// uniqueId present
36+
"https://www.googleapis.com/compute/v1/projects/imre-test/global/instanceTemplates/example-template-custom?uniqueId=123",
37+
"projects/imre-test/global/instanceTemplates/example-template-custom?uniqueId=123",
38+
}
39+
shouldNotMatch := map[string]string{
40+
// mismatching name
41+
"https://www.googleapis.com/compute/v1/projects/imre-test/global/instanceTemplates/example-template-custom": "projects/imre-test/global/instanceTemplates/example-template-custom2",
42+
"projects/imre-test/global/instanceTemplates/example-template-custom": "https://www.googleapis.com/compute/v1/projects/imre-test/global/instanceTemplates/example-template-custom2",
43+
// matching name, but mismatching uniqueId
44+
"https://www.googleapis.com/compute/v1/projects/imre-test/global/instanceTemplates/example-template-custom?uniqueId=123": "projects/imre-test/global/instanceTemplates/example-template-custom?uniqueId=1234",
45+
"projects/imre-test/global/instanceTemplates/example-template-custom?uniqueId=123": "https://www.googleapis.com/compute/v1/projects/imre-test/global/instanceTemplates/example-template-custom?uniqueId=1234",
46+
}
47+
for _, v1 := range shouldAllMatch {
48+
for _, v2 := range shouldAllMatch {
49+
if !compareSelfLinkRelativePathsIgnoreParams("", v1, v2, nil) {
50+
t.Fatalf("compareSelfLinkRelativePathsIgnoreParams did not match (and should have) %v and %v", v1, v2)
51+
}
52+
}
53+
}
54+
55+
for v1, v2 := range shouldNotMatch {
56+
if compareSelfLinkRelativePathsIgnoreParams("", v1, v2, nil) {
57+
t.Fatalf("compareSelfLinkRelativePathsIgnoreParams did match (and shouldn't) %v and %v", v1, v2)
58+
}
59+
}
60+
}
61+
62+
func TestInstanceGroupManager_convertUniqueId(t *testing.T) {
63+
matches := map[string]string{
64+
// uniqueId not present (should return the same)
65+
"https://www.googleapis.com/compute/v1/projects/imre-test/global/instanceTemplates/example-template-custom": "https://www.googleapis.com/compute/v1/projects/imre-test/global/instanceTemplates/example-template-custom",
66+
"projects/imre-test/global/instanceTemplates/example-template-custom": "projects/imre-test/global/instanceTemplates/example-template-custom",
67+
// uniqueId present (should return the last component replaced)
68+
"https://www.googleapis.com/compute/v1/projects/imre-test/global/instanceTemplates/example-template-custom?uniqueId=123": "https://www.googleapis.com/compute/v1/projects/imre-test/global/instanceTemplates/123",
69+
"projects/imre-test/global/instanceTemplates/example-template-custom?uniqueId=123": "projects/imre-test/global/instanceTemplates/123",
70+
"tf-test-igm-8amncgtq22?uniqueId=8361222501423044003": "8361222501423044003",
71+
}
72+
for input, expected := range matches {
73+
actual := ConvertToUniqueIdWhenPresent(input)
74+
if actual != expected {
75+
t.Fatalf("invalid return value by ConvertToUniqueIdWhenPresent for input %v; expected: %v, actual: %v", input, expected, actual)
76+
}
77+
}
78+
}

0 commit comments

Comments
 (0)