Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue13513 #7355

Merged
merged 4 commits into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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