|
13 | 13 | // limitations under the License. |
14 | 14 |
|
15 | 15 | package v1beta1 |
16 | | - |
17 | | -import ( |
18 | | - "context" |
19 | | - "fmt" |
20 | | - |
21 | | - "sigs.k8s.io/controller-runtime/pkg/client" |
22 | | -) |
23 | | - |
24 | | -// TagBindingIdentity defines the resource reference to TagsTagBinding, which "External" field |
25 | | -// holds the GCP identifier for the KRM object. |
26 | | -type TagBindingIdentity struct { |
27 | | - parent string |
28 | | - tagValue string |
29 | | -} |
30 | | - |
31 | | -func (i *TagBindingIdentity) String() string { |
32 | | - return fmt.Sprintf("tagBindings/%s/%s", i.parent, i.tagValue) |
33 | | -} |
34 | | - |
35 | | -func (i *TagBindingIdentity) Parent() string { |
36 | | - return i.parent |
37 | | -} |
38 | | - |
39 | | -func (i *TagBindingIdentity) TagValue() string { |
40 | | - return i.tagValue |
41 | | -} |
42 | | - |
43 | | -// New builds a TagBindingIdentity from the Config Connector TagBinding object. |
44 | | -func NewTagBindingIdentity(ctx context.Context, reader client.Reader, obj *TagsTagBinding) (*TagBindingIdentity, error) { |
45 | | - parent, err := resolveParent(ctx, reader, obj) |
46 | | - if err != nil { |
47 | | - return nil, err |
48 | | - } |
49 | | - |
50 | | - tagValue, err := resolveTagValue(ctx, reader, obj) |
51 | | - if err != nil { |
52 | | - return nil, err |
53 | | - } |
54 | | - |
55 | | - return &TagBindingIdentity{ |
56 | | - parent: parent, |
57 | | - tagValue: tagValue, |
58 | | - }, nil |
59 | | -} |
60 | | - |
61 | | -func resolveParent(ctx context.Context, reader client.Reader, obj *TagsTagBinding) (string, error) { |
62 | | - if obj.Spec.ParentRef.External != "" { |
63 | | - return obj.Spec.ParentRef.External, nil |
64 | | - } |
65 | | - if obj.Spec.ParentRef.Name != "" { |
66 | | - // TODO: This is not quite right, we need to resolve the project number. |
67 | | - // For now, we will just use the name. |
68 | | - return fmt.Sprintf("//cloudresourcemanager.googleapis.com/projects/%s", obj.Spec.ParentRef.Name), nil |
69 | | - } |
70 | | - return "", fmt.Errorf("parentRef is required") |
71 | | -} |
72 | | - |
73 | | -func resolveTagValue(ctx context.Context, reader client.Reader, obj *TagsTagBinding) (string, error) { |
74 | | - if obj.Spec.TagValueRef.External != "" { |
75 | | - return obj.Spec.TagValueRef.External, nil |
76 | | - } |
77 | | - if obj.Spec.TagValueRef.Name != "" { |
78 | | - // TODO: This is not quite right, we need to resolve the tag value name. |
79 | | - // For now, we will just use the name. |
80 | | - return fmt.Sprintf("tagValues/%s", obj.Spec.TagValueRef.Name), nil |
81 | | - } |
82 | | - return "", fmt.Errorf("tagValueRef is required") |
83 | | -} |
0 commit comments