Skip to content

Commit

Permalink
suppressing Project id vs project number diff in forwardingRule.target (
Browse files Browse the repository at this point in the history
  • Loading branch information
himanikh authored Dec 26, 2024
1 parent f5e3735 commit 890855b
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 5 deletions.
2 changes: 1 addition & 1 deletion mmv1/products/compute/ForwardingRule.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,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 @@ -93,11 +93,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
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,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 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 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 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

0 comments on commit 890855b

Please sign in to comment.