Skip to content

Commit

Permalink
suppressing Project id vs project number diff in forwardingRule.targe…
Browse files Browse the repository at this point in the history
…t (#12606) (#8978)

[upstream:890855bb8c655ea02e0014a68f0f83c69d6d3152]

Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Dec 27, 2024
1 parent fdce7dd commit c2891a6
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .changelog/12606.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
compute: Allowed Service attachment with Project Number to be used as ForwardingRule.Target
```
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ mode or when creating external forwarding rule with IPv6.`,
"target": {
Type: schema.TypeString,
Optional: true,
DiffSuppressFunc: tpgresource.CompareSelfLinkRelativePaths,
DiffSuppressFunc: tpgresource.CompareSelfLinkRelativePathsIgnoreProjectId,
Description: `The URL of the target resource to receive the matched traffic. For
regional forwarding rules, this target must be in the same region as the
forwarding rule. For global forwarding rules, this target must be a global
Expand Down
18 changes: 15 additions & 3 deletions google-beta/tpgresource/common_diff_suppress.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,23 @@ func DurationDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
// has the project number instead of the project name
func ProjectNumberDiffSuppress(_, old, new string, _ *schema.ResourceData) bool {
var a2, b2 string
reN := regexp.MustCompile("projects/\\d+")
re := regexp.MustCompile("projects/\\d+")
reN := regexp.MustCompile("projects/[^/]+")
replacement := []byte("projects/equal")
a2 = string(re.ReplaceAll([]byte(old), replacement))
b2 = string(reN.ReplaceAll([]byte(new), replacement))
return a2 == b2
}

// Suppress diffs when the value read from api
// has the project ID instead of the project number
func ProjectIDDiffSuppress(_, old, new string, _ *schema.ResourceData) bool {
var a2, b2 string
re := regexp.MustCompile("projects/[^/]+")
reN := regexp.MustCompile("projects/\\d+")
replacement := []byte("projects/equal")
a2 = string(reN.ReplaceAll([]byte(old), replacement))
b2 = string(re.ReplaceAll([]byte(new), replacement))
a2 = string(re.ReplaceAll([]byte(old), replacement))
b2 = string(reN.ReplaceAll([]byte(new), replacement))
return a2 == b2
}

Expand Down
47 changes: 47 additions & 0 deletions google-beta/tpgresource/common_diff_suppress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,53 @@ func TestDurationDiffSuppress(t *testing.T) {
}
}

func TestProjectNumberDiffSuppress(t *testing.T) {
cases := map[string]struct {
Old, New string
ExpectDiffSuppress bool
}{
"different project identifiers": {
Old: "projects/1234/locations/abc/serviceAttachments/xyz",
New: "projects/ten-tp/locations/abc/serviceAttachments/xyz",
ExpectDiffSuppress: true,
},
"different resources": {
Old: "projects/1234/locations/abc/serviceAttachments/jkl",
New: "projects/ten-tp/locations/abc/serviceAttachments/xyz",
ExpectDiffSuppress: false,
},
}

for tn, tc := range cases {
if ProjectNumberDiffSuppress("diffSuppress", tc.Old, tc.New, nil) != tc.ExpectDiffSuppress {
t.Fatalf("bad: %s, '%s' => '%s' expect %t", tn, tc.Old, tc.New, tc.ExpectDiffSuppress)
}
}
}

func TestProjectIDDiffSuppress(t *testing.T) {
cases := map[string]struct {
Old, New string
ExpectDiffSuppress bool
}{
"different project identifiers": {
Old: "projects/ten-tp/locations/abc/serviceAttachments/xyz",
New: "projects/1234/locations/abc/serviceAttachments/xyz",
ExpectDiffSuppress: true,
},
"different resources": {
Old: "projects/ten-tp/locations/abc/serviceAttachments/xyz",
New: "projects/1234/locations/abc/serviceAttachments/jkl",
ExpectDiffSuppress: false,
},
}

for tn, tc := range cases {
if ProjectIDDiffSuppress("diffSuppress", tc.Old, tc.New, nil) != tc.ExpectDiffSuppress {
t.Fatalf("bad: %s, '%s' => '%s' expect %t", tn, tc.Old, tc.New, tc.ExpectDiffSuppress)
}
}
}
func TestEmptyOrUnsetBlockDiffSuppress(t *testing.T) {
cases := map[string]struct {
Key, Old, New string
Expand Down
19 changes: 18 additions & 1 deletion google-beta/tpgresource/self_link_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func CompareResourceNames(_, old, new string, _ *schema.ResourceData) bool {
}

// Compare only the relative path of two self links.
func CompareSelfLinkRelativePaths(_, old, new string, _ *schema.ResourceData) bool {
func CompareSelfLinkRelativePathsIgnoreProjectId(unused1, old, new string, unused2 *schema.ResourceData) bool {
oldStripped, err := GetRelativePath(old)
if err != nil {
return false
Expand All @@ -33,7 +33,24 @@ func CompareSelfLinkRelativePaths(_, old, new string, _ *schema.ResourceData) bo
if oldStripped == newStripped {
return true
}
return ProjectIDDiffSuppress(unused1, oldStripped, newStripped, unused2)
}

// Compare only the relative path of two self links.
func CompareSelfLinkRelativePaths(_, old, new string, _ *schema.ResourceData) bool {
oldStripped, err := GetRelativePath(old)
if err != nil {
return false
}

newStripped, err := GetRelativePath(new)
if err != nil {
return false
}

if oldStripped == newStripped {
return true
}
return false
}

Expand Down
64 changes: 64 additions & 0 deletions google-beta/tpgresource/self_link_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,70 @@ func TestCompareSelfLinkOrResourceName(t *testing.T) {
}
}

func TestCompareSelfLinkRelativePathsIgnoreProjectId(t *testing.T) {
cases := map[string]struct {
Old, New string
Expect bool
}{
"full path, project number": {
Old: "https://www.googleapis.com/compute/v1/projects/your-project/global/networks/a-network",
New: "https://www.googleapis.com/compute/v1/projects/1234/global/networks/a-network",
Expect: true,
},
"partial path, project number": {
Old: "https://www.googleapis.com/compute/v1/projects/your-project/global/networks/a-network",
New: "projects/1234/global/networks/a-network",
Expect: true,
},
"partial path, same": {
Old: "https://www.googleapis.com/compute/v1/projects/your-project/global/networks/a-network",
New: "projects/your-project/global/networks/a-network",
Expect: true,
},
"partial path, different name": {
Old: "https://www.googleapis.com/compute/v1/projects/your-project/global/networks/a-network",
New: "projects/your-project/global/networks/another-network",
Expect: false,
},
"partial path, different project": {
Old: "https://www.googleapis.com/compute/v1/projects/your-project/global/networks/a-network",
New: "projects/another-project/global/networks/a-network",
Expect: false,
},
"full path, different name": {
Old: "https://www.googleapis.com/compute/v1/projects/your-project/global/networks/a-network",
New: "https://www.googleapis.com/compute/v1/projects/your-project/global/networks/another-network",
Expect: false,
},
"full path, different project": {
Old: "https://www.googleapis.com/compute/v1/projects/your-project/global/networks/a-network",
New: "https://www.googleapis.com/compute/v1/projects/another-project/global/networks/a-network",
Expect: false,
},
"beta full path, same": {
Old: "https://www.googleapis.com/compute/v1/projects/your-project/global/networks/a-network",
New: "https://www.googleapis.com/compute/beta/projects/your-project/global/networks/a-network",
Expect: true,
},
"beta full path, different name": {
Old: "https://www.googleapis.com/compute/v1/projects/your-project/global/networks/a-network",
New: "https://www.googleapis.com/compute/beta/projects/your-project/global/networks/another-network",
Expect: false,
},
"beta full path, different project": {
Old: "https://www.googleapis.com/compute/v1/projects/your-project/global/networks/a-network",
New: "https://www.googleapis.com/compute/beta/projects/another-project/global/networks/a-network",
Expect: false,
},
}

for tn, tc := range cases {
if CompareSelfLinkRelativePathsIgnoreProjectId("", tc.Old, tc.New, nil) != tc.Expect {
t.Errorf("bad: %s, expected %t for old = %q and new = %q", tn, tc.Expect, tc.Old, tc.New)
}
}
}

func TestGetResourceNameFromSelfLink(t *testing.T) {
cases := map[string]struct {
SelfLink, ExpectedName string
Expand Down

0 comments on commit c2891a6

Please sign in to comment.