forked from hashicorp/terraform-provider-google
-
Notifications
You must be signed in to change notification settings - Fork 0
/
self_link_helpers.go
60 lines (48 loc) · 1.46 KB
/
self_link_helpers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package google
import (
"fmt"
"regexp"
"strings"
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/schema"
)
// Compare only the relative path of two self links.
func compareSelfLinkRelativePaths(k, old, new string, d *schema.ResourceData) bool {
oldStripped, err := getRelativePath(old)
if err != nil {
return false
}
newStripped, err := getRelativePath(new)
if err != nil {
return false
}
if oldStripped == newStripped {
return true
}
return false
}
// Use this method when the field accepts either a name or a self_link referencing a global resource.
func compareGlobalSelfLinkOrResourceName(k, old, new string, d *schema.ResourceData) bool {
oldParts := strings.Split(old, "/")
newParts := strings.Split(new, "/")
if oldParts[len(oldParts)-1] == newParts[len(newParts)-1] {
return true
}
return false
}
// Hash the relative path of a self link.
func selfLinkRelativePathHash(selfLink interface{}) int {
path, _ := getRelativePath(selfLink.(string))
return hashcode.String(path)
}
func getRelativePath(selfLink string) (string, error) {
stringParts := strings.SplitAfterN(selfLink, "projects/", 2)
if len(stringParts) != 2 {
return "", fmt.Errorf("String was not a self link: %s", selfLink)
}
return "projects/" + stringParts[1], nil
}
func ConvertSelfLinkToV1(link string) string {
reg := regexp.MustCompile("/compute/[a-zA-Z0-9]*/projects/")
return reg.ReplaceAllString(link, "/compute/v1/projects/")
}