Skip to content

Commit 0b67483

Browse files
suppressing Project id vs project number diff in forwardingRule.target (#12606) (#20790)
[upstream:890855bb8c655ea02e0014a68f0f83c69d6d3152] Signed-off-by: Modular Magician <magic-modules@google.com>
1 parent e23cc87 commit 0b67483

File tree

6 files changed

+148
-5
lines changed

6 files changed

+148
-5
lines changed

.changelog/12606.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
compute: Allowed Service attachment with Project Number to be used as ForwardingRule.Target
3+
```

google/services/compute/resource_compute_forwarding_rule.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ mode or when creating external forwarding rule with IPv6.`,
497497
"target": {
498498
Type: schema.TypeString,
499499
Optional: true,
500-
DiffSuppressFunc: tpgresource.CompareSelfLinkRelativePaths,
500+
DiffSuppressFunc: tpgresource.CompareSelfLinkRelativePathsIgnoreProjectId,
501501
Description: `The URL of the target resource to receive the matched traffic. For
502502
regional forwarding rules, this target must be in the same region as the
503503
forwarding rule. For global forwarding rules, this target must be a global

google/tpgresource/common_diff_suppress.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,23 @@ func DurationDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
9595
// has the project number instead of the project name
9696
func ProjectNumberDiffSuppress(_, old, new string, _ *schema.ResourceData) bool {
9797
var a2, b2 string
98-
reN := regexp.MustCompile("projects/\\d+")
98+
re := regexp.MustCompile("projects/\\d+")
99+
reN := regexp.MustCompile("projects/[^/]+")
100+
replacement := []byte("projects/equal")
101+
a2 = string(re.ReplaceAll([]byte(old), replacement))
102+
b2 = string(reN.ReplaceAll([]byte(new), replacement))
103+
return a2 == b2
104+
}
105+
106+
// Suppress diffs when the value read from api
107+
// has the project ID instead of the project number
108+
func ProjectIDDiffSuppress(_, old, new string, _ *schema.ResourceData) bool {
109+
var a2, b2 string
99110
re := regexp.MustCompile("projects/[^/]+")
111+
reN := regexp.MustCompile("projects/\\d+")
100112
replacement := []byte("projects/equal")
101-
a2 = string(reN.ReplaceAll([]byte(old), replacement))
102-
b2 = string(re.ReplaceAll([]byte(new), replacement))
113+
a2 = string(re.ReplaceAll([]byte(old), replacement))
114+
b2 = string(reN.ReplaceAll([]byte(new), replacement))
103115
return a2 == b2
104116
}
105117

google/tpgresource/common_diff_suppress_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,53 @@ func TestDurationDiffSuppress(t *testing.T) {
6969
}
7070
}
7171

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

google/tpgresource/self_link_helpers.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func CompareResourceNames(_, old, new string, _ *schema.ResourceData) bool {
1919
}
2020

2121
// Compare only the relative path of two self links.
22-
func CompareSelfLinkRelativePaths(_, old, new string, _ *schema.ResourceData) bool {
22+
func CompareSelfLinkRelativePathsIgnoreProjectId(unused1, old, new string, unused2 *schema.ResourceData) bool {
2323
oldStripped, err := GetRelativePath(old)
2424
if err != nil {
2525
return false
@@ -33,7 +33,24 @@ func CompareSelfLinkRelativePaths(_, old, new string, _ *schema.ResourceData) bo
3333
if oldStripped == newStripped {
3434
return true
3535
}
36+
return ProjectIDDiffSuppress(unused1, oldStripped, newStripped, unused2)
37+
}
38+
39+
// Compare only the relative path of two self links.
40+
func CompareSelfLinkRelativePaths(_, old, new string, _ *schema.ResourceData) bool {
41+
oldStripped, err := GetRelativePath(old)
42+
if err != nil {
43+
return false
44+
}
3645

46+
newStripped, err := GetRelativePath(new)
47+
if err != nil {
48+
return false
49+
}
50+
51+
if oldStripped == newStripped {
52+
return true
53+
}
3754
return false
3855
}
3956

google/tpgresource/self_link_helpers_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,70 @@ func TestCompareSelfLinkOrResourceName(t *testing.T) {
6868
}
6969
}
7070

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

0 commit comments

Comments
 (0)