Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* fix suppress func

* update func

* update to fix func errors

* update regex and tests

---------

Co-authored-by: Edward Sun <sunedward@google.com>
  • Loading branch information
2 people authored and mdtro committed Mar 2, 2023
1 parent 6477e34 commit 9f1d450
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,43 @@ func validateResourceCloudFunctionsFunctionName(v interface{}, k string) (ws []s
return validateRegexp(re)(v, k)
}

func partsCompare(a, b, reg string) bool {

regex := regexp.MustCompile(reg)
if regex.MatchString(a) && regex.MatchString(b) {
aParts := regex.FindStringSubmatch(a)
bParts := regex.FindStringSubmatch(b)
for i := 0; i < len(aParts); i++ {
if aParts[i] != bParts[i] {
return false
}
}
} else if regex.MatchString(a) {
aParts := regex.FindStringSubmatch(a)
if aParts[len(aParts)-1] != b {
return false
}
} else if regex.MatchString(b) {
bParts := regex.FindStringSubmatch(b)
if bParts[len(bParts)-1] != a {
return false
}
} else {
if a != b {
return false
}
}

return true
}

// based on compareSelfLinkOrResourceName, but less reusable and allows multi-/
// strings in the new state (config) part
func compareSelfLinkOrResourceNameWithMultipleParts(_, old, new string, _ *schema.ResourceData) bool {
return strings.HasSuffix(old, new)
// two formats based on expandEventTrigger()
regex1 := "projects/(.+)/databases/\\(default\\)/documents/(.+)"
regex2 := "projects/(.+)/(.+)/(.+)"
return partsCompare(old, new, regex1) || partsCompare(old, new, regex2)
}

func ResourceCloudFunctionsFunction() *schema.Resource {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,136 @@ func TestValidLabelKeys(t *testing.T) {
}
}

func TestCompareSelfLinkOrResourceNameWithMultipleParts(t *testing.T) {
cases := map[string]struct {
Old, New string
ExpectDiffSuppress bool
}{
"projects to no projects doc": {
Old: "projects/myproject/databases/default/documents/resource",
New: "resource",
ExpectDiffSuppress: true,
},
"no projects to projects doc": {
Old: "resource",
New: "projects/myproject/databases/default/documents/resource",
ExpectDiffSuppress: true,
},
"projects to projects doc": {
Old: "projects/myproject/databases/default/documents/resource",
New: "projects/myproject/databases/default/documents/resource",
ExpectDiffSuppress: true,
},
"multi messages doc": {
Old: "messages/{messageId}",
New: "projects/myproject/databases/(default)/documents/messages/{messageId}",
ExpectDiffSuppress: true,
},
"multi messages 2 doc": {
Old: "projects/myproject/databases/(default)/documents/messages/{messageId}",
New: "messages/{messageId}",
ExpectDiffSuppress: true,
},
"projects to no projects topics": {
Old: "projects/myproject/topics/resource",
New: "resource",
ExpectDiffSuppress: true,
},
"no projects to projects topics": {
Old: "resource",
New: "projects/myproject/topics/resource",
ExpectDiffSuppress: true,
},
"projects to projects topics": {
Old: "projects/myproject/topics/resource",
New: "projects/myproject/topics/resource",
ExpectDiffSuppress: true,
},

"unmatched projects to no projects doc": {
Old: "projects/myproject/databases/default/documents/resource",
New: "resourcex",
ExpectDiffSuppress: false,
},
"unmatched no projects to projects doc": {
Old: "resourcex",
New: "projects/myproject/databases/default/documents/resource",
ExpectDiffSuppress: false,
},
"unmatched projects to projects doc": {
Old: "projects/myproject/databases/default/documents/resource",
New: "projects/myproject/databases/default/documents/resourcex",
ExpectDiffSuppress: false,
},
"unmatched projects to projects 2 doc": {
Old: "projects/myprojectx/databases/default/documents/resource",
New: "projects/myproject/databases/default/documents/resource",
ExpectDiffSuppress: false,
},
"unmatched projects to empty doc": {
Old: "",
New: "projects/myproject/databases/default/documents/resource",
ExpectDiffSuppress: false,
},
"unmatched empty to projects 2 doc": {
Old: "projects/myprojectx/databases/default/documents/resource",
New: "",
ExpectDiffSuppress: false,
},
"unmatched default to default2 doc": {
Old: "projects/myproject/databases/default/documents/resource",
New: "projects/myproject/databases/default2/documents/resource",
ExpectDiffSuppress: false,
},
"unmatched projects to no projects topics": {
Old: "projects/myproject/topics/resource",
New: "resourcex",
ExpectDiffSuppress: false,
},
"unmatched no projects to projects topics": {
Old: "resourcex",
New: "projects/myproject/topics/resource",
ExpectDiffSuppress: false,
},
"unmatched projects to projects topics": {
Old: "projects/myproject/topics/resource",
New: "projects/myproject/topics/resourcex",
ExpectDiffSuppress: false,
},
"unmatched projects to projects 2 topics": {
Old: "projects/myprojectx/topics/resource",
New: "projects/myproject/topics/resource",
ExpectDiffSuppress: false,
},
"unmatched projects to empty topics": {
Old: "projects/myproject/topics/resource",
New: "",
ExpectDiffSuppress: false,
},
"unmatched empty to projects topics": {
Old: "",
New: "projects/myproject/topics/resource",
ExpectDiffSuppress: false,
},
"unmatched resource to resource-partial": {
Old: "resource",
New: "resource-partial",
ExpectDiffSuppress: false,
},
"unmatched resource-partial to projects": {
Old: "resource-partial",
New: "projects/myproject/topics/resource",
ExpectDiffSuppress: false,
},
}

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

func TestAccCloudFunctionsFunction_basic(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit 9f1d450

Please sign in to comment.