mirror of
https://github.com/letic/terraform-provider-google.git
synced 2024-07-01 07:42:40 +00:00
add hash function for self link name (#1836)
This commit is contained in:
parent
830ffeb7f3
commit
c5388d27c4
|
@ -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/")
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user