Skip to content

Commit

Permalink
fixed diff on last slash of uri
Browse files Browse the repository at this point in the history
  • Loading branch information
Edward Sun committed Jul 1, 2022
1 parent 2bc98f8 commit b4195c7
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
2 changes: 2 additions & 0 deletions mmv1/products/cloudscheduler/terraform.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ overrides: !ruby/object:Overrides::ResourceOverrides
diff_suppress_func: 'authHeaderDiffSuppress'
httpTarget.oidcToken: !ruby/object:Overrides::Terraform::PropertyOverride
diff_suppress_func: 'authHeaderDiffSuppress'
httpTarget.uri: !ruby/object:Overrides::Terraform::PropertyOverride
diff_suppress_func: 'lastSlashDiffSuppress'
appEngineHttpTarget.headers: !ruby/object:Overrides::Terraform::PropertyOverride
custom_flatten: 'templates/terraform/custom_flatten/http_headers.erb'
validation: !ruby/object:Provider::Terraform::Validation
Expand Down
14 changes: 14 additions & 0 deletions mmv1/third_party/terraform/utils/common_diff_suppress.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,17 @@ func compareOptionalSubnet(_, old, new string, _ *schema.ResourceData) bool {
// otherwise compare as self links
return compareSelfLinkOrResourceName("", old, new, nil)
}

// Suppress diffs in below cases
// "https://hello-rehvs75zla-uc.a.run.app/" -> "https://hello-rehvs75zla-uc.a.run.app"
// "https://hello-rehvs75zla-uc.a.run.app" -> "https://hello-rehvs75zla-uc.a.run.app/"
func lastSlashDiffSuppress(_, old, new string, _ *schema.ResourceData) bool {
if last := len(new) - 1; last >= 0 && new[last] == '/' {
new = new[:last]
}

if last := len(old) - 1; last >= 0 && old[last] == '/' {
old = old[:last]
}
return new == old
}
34 changes: 34 additions & 0 deletions mmv1/third_party/terraform/utils/common_diff_suppress_test.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -285,3 +285,37 @@ func TestDurationDiffSuppress(t *testing.T) {
}
}
}

func TestLastSlashDiffSuppress(t *testing.T) {
cases := map[string]struct {
Old, New string
ExpectDiffSuppress bool
}{
"locations to zones": {
Old: "https://hello-rehvs75zla-uc.a.run.app/",
New: "https://hello-rehvs75zla-uc.a.run.app",
ExpectDiffSuppress: true,
},
"regions to locations": {
Old: "https://hello-rehvs75zla-uc.a.run.app",
New: "https://hello-rehvs75zla-uc.a.run.app/",
ExpectDiffSuppress: true,
},
"locations to locations": {
Old: "https://hello-rehvs75zla-uc.a.run.app/",
New: "https://hello-rehvs75zla-uc.a.run.app/",
ExpectDiffSuppress: true,
},
"zones to regions": {
Old: "https://x.a.run.app/",
New: "https://y.a.run.app",
ExpectDiffSuppress: false,
},
}

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

0 comments on commit b4195c7

Please sign in to comment.