add hash function for self link name (#1836)

This commit is contained in:
Dana Hoffman 2018-08-03 16:23:21 -07:00 committed by GitHub
parent 830ffeb7f3
commit c5388d27c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 0 deletions

View File

@ -60,6 +60,12 @@ func getRelativePath(selfLink string) (string, error) {
return "projects/" + stringParts[1], nil
}
// Hash the name path of a self link.
func selfLinkNameHash(selfLink interface{}) int {
name := GetResourceNameFromSelfLink(selfLink.(string))
return hashcode.String(name)
}
func ConvertSelfLinkToV1(link string) string {
reg := regexp.MustCompile("/compute/[a-zA-Z0-9]*/projects/")
return reg.ReplaceAllString(link, "/compute/v1/projects/")

View File

@ -86,3 +86,27 @@ func TestGetResourceNameFromSelfLink(t *testing.T) {
}
}
}
func TestSelfLinkNameHash(t *testing.T) {
cases := map[string]struct {
SelfLink, Name string
Expect bool
}{
"same": {
SelfLink: "https://www.googleapis.com/compute/v1/projects/your-project/global/networks/a-network",
Name: "a-network",
Expect: true,
},
"different": {
SelfLink: "https://www.googleapis.com/compute/v1/projects/your-project/global/networks/a-network",
Name: "another-network",
Expect: false,
},
}
for tn, tc := range cases {
if (selfLinkNameHash(tc.SelfLink) == selfLinkNameHash(tc.Name)) != tc.Expect {
t.Errorf("%s: expected %t for whether hashes matched for self link = %q, name = %q", tn, tc.Expect, tc.SelfLink, tc.Name)
}
}
}