Skip to content

Commit

Permalink
fixed diff on last slash of uri (#6211)
Browse files Browse the repository at this point in the history
* fixed diff on last slash of uri

* update names of tests

* fix a typo

Co-authored-by: Edward Sun <sunedward@google.com>
  • Loading branch information
edwardmedia and Edward Sun authored Jul 6, 2022
1 parent dd11612 commit 967cebf
Show file tree
Hide file tree
Showing 3 changed files with 55 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
}
39 changes: 39 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,42 @@ func TestDurationDiffSuppress(t *testing.T) {
}
}
}

func TestLastSlashDiffSuppress(t *testing.T) {
cases := map[string]struct {
Old, New string
ExpectDiffSuppress bool
}{
"slash to no slash": {
Old: "https://hello-rehvs75zla-uc.a.run.app/",
New: "https://hello-rehvs75zla-uc.a.run.app",
ExpectDiffSuppress: true,
},
"no slash to slash": {
Old: "https://hello-rehvs75zla-uc.a.run.app",
New: "https://hello-rehvs75zla-uc.a.run.app/",
ExpectDiffSuppress: true,
},
"slash to slash": {
Old: "https://hello-rehvs75zla-uc.a.run.app/",
New: "https://hello-rehvs75zla-uc.a.run.app/",
ExpectDiffSuppress: true,
},
"no slash to no slash": {
Old: "https://hello-rehvs75zla-uc.a.run.app",
New: "https://hello-rehvs75zla-uc.a.run.app",
ExpectDiffSuppress: true,
},
"different domains": {
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 967cebf

Please sign in to comment.