Skip to content

Commit 890855b

Browse files
authored
suppressing Project id vs project number diff in forwardingRule.target (#12606)
1 parent f5e3735 commit 890855b

File tree

5 files changed

+145
-5
lines changed

5 files changed

+145
-5
lines changed

mmv1/products/compute/ForwardingRule.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ properties:
485485
For Private Service Connect forwarding rules that forward traffic to managed services, the target must be a service attachment.
486486
update_url: 'projects/{{project}}/regions/{{region}}/forwardingRules/{{name}}/setTarget'
487487
update_verb: 'POST'
488-
diff_suppress_func: 'tpgresource.CompareSelfLinkRelativePaths'
488+
diff_suppress_func: 'tpgresource.CompareSelfLinkRelativePathsIgnoreProjectId'
489489
custom_expand: 'templates/terraform/custom_expand/self_link_from_name.tmpl'
490490
- name: 'labelFingerprint'
491491
type: Fingerprint

mmv1/third_party/terraform/tpgresource/common_diff_suppress.go.tmpl

+15-3
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,23 @@ func DurationDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
9393
// has the project number instead of the project name
9494
func ProjectNumberDiffSuppress(_, old, new string, _ *schema.ResourceData) bool {
9595
var a2, b2 string
96-
reN := regexp.MustCompile("projects/\\d+")
96+
re := regexp.MustCompile("projects/\\d+")
97+
reN := regexp.MustCompile("projects/[^/]+")
98+
replacement := []byte("projects/equal")
99+
a2 = string(re.ReplaceAll([]byte(old), replacement))
100+
b2 = string(reN.ReplaceAll([]byte(new), replacement))
101+
return a2 == b2
102+
}
103+
104+
// Suppress diffs when the value read from api
105+
// has the project ID instead of the project number
106+
func ProjectIDDiffSuppress(_, old, new string, _ *schema.ResourceData) bool {
107+
var a2, b2 string
97108
re := regexp.MustCompile("projects/[^/]+")
109+
reN := regexp.MustCompile("projects/\\d+")
98110
replacement := []byte("projects/equal")
99-
a2 = string(reN.ReplaceAll([]byte(old), replacement))
100-
b2 = string(re.ReplaceAll([]byte(new), replacement))
111+
a2 = string(re.ReplaceAll([]byte(old), replacement))
112+
b2 = string(reN.ReplaceAll([]byte(new), replacement))
101113
return a2 == b2
102114
}
103115

mmv1/third_party/terraform/tpgresource/common_diff_suppress_test.go

+47
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,53 @@ func TestDurationDiffSuppress(t *testing.T) {
6767
}
6868
}
6969

70+
func TestProjectNumberDiffSuppress(t *testing.T) {
71+
cases := map[string]struct {
72+
Old, New string
73+
ExpectDiffSuppress bool
74+
}{
75+
"different project identifiers": {
76+
Old: "projects/1234/locations/abc/serviceAttachments/xyz",
77+
New: "projects/ten-tp/locations/abc/serviceAttachments/xyz",
78+
ExpectDiffSuppress: true,
79+
},
80+
"different resources": {
81+
Old: "projects/1234/locations/abc/serviceAttachments/jkl",
82+
New: "projects/ten-tp/locations/abc/serviceAttachments/xyz",
83+
ExpectDiffSuppress: false,
84+
},
85+
}
86+
87+
for tn, tc := range cases {
88+
if ProjectNumberDiffSuppress("diffSuppress", tc.Old, tc.New, nil) != tc.ExpectDiffSuppress {
89+
t.Fatalf("bad: %s, '%s' => '%s' expect %t", tn, tc.Old, tc.New, tc.ExpectDiffSuppress)
90+
}
91+
}
92+
}
93+
94+
func TestProjectIDDiffSuppress(t *testing.T) {
95+
cases := map[string]struct {
96+
Old, New string
97+
ExpectDiffSuppress bool
98+
}{
99+
"different project identifiers": {
100+
Old: "projects/ten-tp/locations/abc/serviceAttachments/xyz",
101+
New: "projects/1234/locations/abc/serviceAttachments/xyz",
102+
ExpectDiffSuppress: true,
103+
},
104+
"different resources": {
105+
Old: "projects/ten-tp/locations/abc/serviceAttachments/xyz",
106+
New: "projects/1234/locations/abc/serviceAttachments/jkl",
107+
ExpectDiffSuppress: false,
108+
},
109+
}
110+
111+
for tn, tc := range cases {
112+
if ProjectIDDiffSuppress("diffSuppress", tc.Old, tc.New, nil) != tc.ExpectDiffSuppress {
113+
t.Fatalf("bad: %s, '%s' => '%s' expect %t", tn, tc.Old, tc.New, tc.ExpectDiffSuppress)
114+
}
115+
}
116+
}
70117
func TestEmptyOrUnsetBlockDiffSuppress(t *testing.T) {
71118
cases := map[string]struct {
72119
Key, Old, New string

mmv1/third_party/terraform/tpgresource/self_link_helpers.go

+18-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func CompareResourceNames(_, old, new string, _ *schema.ResourceData) bool {
1717
}
1818

1919
// Compare only the relative path of two self links.
20-
func CompareSelfLinkRelativePaths(_, old, new string, _ *schema.ResourceData) bool {
20+
func CompareSelfLinkRelativePathsIgnoreProjectId(unused1, old, new string, unused2 *schema.ResourceData) bool {
2121
oldStripped, err := GetRelativePath(old)
2222
if err != nil {
2323
return false
@@ -31,7 +31,24 @@ func CompareSelfLinkRelativePaths(_, old, new string, _ *schema.ResourceData) bo
3131
if oldStripped == newStripped {
3232
return true
3333
}
34+
return ProjectIDDiffSuppress(unused1, oldStripped, newStripped, unused2)
35+
}
36+
37+
// Compare only the relative path of two self links.
38+
func CompareSelfLinkRelativePaths(_, old, new string, _ *schema.ResourceData) bool {
39+
oldStripped, err := GetRelativePath(old)
40+
if err != nil {
41+
return false
42+
}
3443

44+
newStripped, err := GetRelativePath(new)
45+
if err != nil {
46+
return false
47+
}
48+
49+
if oldStripped == newStripped {
50+
return true
51+
}
3552
return false
3653
}
3754

mmv1/third_party/terraform/tpgresource/self_link_helpers_test.go

+64
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,70 @@ func TestCompareSelfLinkOrResourceName(t *testing.T) {
6666
}
6767
}
6868

69+
func TestCompareSelfLinkRelativePathsIgnoreProjectId(t *testing.T) {
70+
cases := map[string]struct {
71+
Old, New string
72+
Expect bool
73+
}{
74+
"full path, project number": {
75+
Old: "https://www.googleapis.com/compute/v1/projects/your-project/global/networks/a-network",
76+
New: "https://www.googleapis.com/compute/v1/projects/1234/global/networks/a-network",
77+
Expect: true,
78+
},
79+
"partial path, project number": {
80+
Old: "https://www.googleapis.com/compute/v1/projects/your-project/global/networks/a-network",
81+
New: "projects/1234/global/networks/a-network",
82+
Expect: true,
83+
},
84+
"partial path, same": {
85+
Old: "https://www.googleapis.com/compute/v1/projects/your-project/global/networks/a-network",
86+
New: "projects/your-project/global/networks/a-network",
87+
Expect: true,
88+
},
89+
"partial path, different name": {
90+
Old: "https://www.googleapis.com/compute/v1/projects/your-project/global/networks/a-network",
91+
New: "projects/your-project/global/networks/another-network",
92+
Expect: false,
93+
},
94+
"partial path, different project": {
95+
Old: "https://www.googleapis.com/compute/v1/projects/your-project/global/networks/a-network",
96+
New: "projects/another-project/global/networks/a-network",
97+
Expect: false,
98+
},
99+
"full path, different name": {
100+
Old: "https://www.googleapis.com/compute/v1/projects/your-project/global/networks/a-network",
101+
New: "https://www.googleapis.com/compute/v1/projects/your-project/global/networks/another-network",
102+
Expect: false,
103+
},
104+
"full path, different project": {
105+
Old: "https://www.googleapis.com/compute/v1/projects/your-project/global/networks/a-network",
106+
New: "https://www.googleapis.com/compute/v1/projects/another-project/global/networks/a-network",
107+
Expect: false,
108+
},
109+
"beta full path, same": {
110+
Old: "https://www.googleapis.com/compute/v1/projects/your-project/global/networks/a-network",
111+
New: "https://www.googleapis.com/compute/beta/projects/your-project/global/networks/a-network",
112+
Expect: true,
113+
},
114+
"beta full path, different name": {
115+
Old: "https://www.googleapis.com/compute/v1/projects/your-project/global/networks/a-network",
116+
New: "https://www.googleapis.com/compute/beta/projects/your-project/global/networks/another-network",
117+
Expect: false,
118+
},
119+
"beta full path, different project": {
120+
Old: "https://www.googleapis.com/compute/v1/projects/your-project/global/networks/a-network",
121+
New: "https://www.googleapis.com/compute/beta/projects/another-project/global/networks/a-network",
122+
Expect: false,
123+
},
124+
}
125+
126+
for tn, tc := range cases {
127+
if CompareSelfLinkRelativePathsIgnoreProjectId("", tc.Old, tc.New, nil) != tc.Expect {
128+
t.Errorf("bad: %s, expected %t for old = %q and new = %q", tn, tc.Expect, tc.Old, tc.New)
129+
}
130+
}
131+
}
132+
69133
func TestGetResourceNameFromSelfLink(t *testing.T) {
70134
cases := map[string]struct {
71135
SelfLink, ExpectedName string

0 commit comments

Comments
 (0)