Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

suppressing Project id vs project number diff in forwardingRule.target #12606

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mmv1/products/compute/ForwardingRule.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ properties:
For Private Service Connect forwarding rules that forward traffic to managed services, the target must be a service attachment.
update_url: 'projects/{{project}}/regions/{{region}}/forwardingRules/{{name}}/setTarget'
update_verb: 'POST'
diff_suppress_func: 'tpgresource.CompareSelfLinkRelativePaths'
diff_suppress_func: 'tpgresource.CompareSelfLinkRelativePathsIgnoreProjectId'
custom_expand: 'templates/terraform/custom_expand/self_link_from_name.tmpl'
- name: 'labelFingerprint'
type: Fingerprint
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ func ProjectNumberDiffSuppress(_, old, new string, _ *schema.ResourceData) bool
reN := regexp.MustCompile("projects/\\d+")
re := regexp.MustCompile("projects/[^/]+")
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
19 changes: 18 additions & 1 deletion mmv1/third_party/terraform/tpgresource/self_link_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,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 @@ -31,7 +31,24 @@ func CompareSelfLinkRelativePaths(_, old, new string, _ *schema.ResourceData) bo
if oldStripped == newStripped {
return true
}
return ProjectNumberDiffSuppress(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 mmv1/third_party/terraform/tpgresource/self_link_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,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
Loading