Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
himanikh committed Dec 26, 2024
1 parent d8dac3c commit ba9084b
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,20 @@ 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(re.ReplaceAll([]byte(old), replacement))
b2 = string(reN.ReplaceAll([]byte(new), replacement))
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
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func CompareSelfLinkRelativePathsIgnoreProjectId(unused1, old, new string, unuse
if oldStripped == newStripped {
return true
}
return ProjectNumberDiffSuppress(unused1, oldStripped, newStripped, unused2)
return ProjectIDDiffSuppress(unused1, oldStripped, newStripped, unused2)
}

// Compare only the relative path of two self links.
Expand Down

0 comments on commit ba9084b

Please sign in to comment.