-
Notifications
You must be signed in to change notification settings - Fork 68
/
config.go
76 lines (65 loc) · 2.22 KB
/
config.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package common
import (
"strings"
"github.com/pkg/errors"
jresource "github.com/upbound/upjet/pkg/resource"
"github.com/crossplane/crossplane-runtime/pkg/fieldpath"
"github.com/crossplane/crossplane-runtime/pkg/reference"
"github.com/crossplane/crossplane-runtime/pkg/resource"
)
const (
// KeyProject is the key for project in Terraform Provider Configuration
KeyProject = "project"
// SelfPackagePath is the golang path for this package.
SelfPackagePath = "github.com/upbound/provider-gcp/config/common"
// ExtractResourceIDFuncPath holds the GCP resource ID extractor func name
ExtractResourceIDFuncPath = "github.com/upbound/provider-gcp/config/common.ExtractResourceID()"
)
var (
// PathSelfLinkExtractor is the golang path to SelfLinkExtractor function
// in this package.
PathSelfLinkExtractor = SelfPackagePath + ".SelfLinkExtractor()"
)
// SelfLinkExtractor extracts URI of the resources from
// "status.atProvider.selfLink" which is quite common among all GCP resources.
func SelfLinkExtractor() reference.ExtractValueFn {
return func(mg resource.Managed) string {
paved, err := fieldpath.PaveObject(mg)
if err != nil {
return ""
}
r, err := paved.GetString("status.atProvider.selfLink")
if err != nil {
return ""
}
return r
}
}
// GetNameFromFullyQualifiedID extracts external-name from GCP ID
// Example: projects/{{project}}/zones/{{zone}}/instances/{{name}}
func GetNameFromFullyQualifiedID(tfstate map[string]interface{}) (string, error) {
id, ok := tfstate["id"].(string)
if !ok {
return "", errors.New("cannot get the id field as string in tfstate")
}
words := strings.Split(id, "/")
return words[len(words)-1], nil
}
// GetField returns the value of field as a string in a map[string]interface{},
//
// fails properly otherwise.
func GetField(from map[string]interface{}, path string) (string, error) {
return fieldpath.Pave(from).GetString(path)
}
// ExtractResourceID extracts the value of `spec.atProvider.id`
// from a Terraformed resource. If mr is not a Terraformed
// resource, returns an empty string.
func ExtractResourceID() reference.ExtractValueFn {
return func(mr resource.Managed) string {
tr, ok := mr.(jresource.Terraformed)
if !ok {
return ""
}
return tr.GetID()
}
}