|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package v1alpha1 |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + "strings" |
| 21 | + |
| 22 | + "github.com/GoogleCloudPlatform/k8s-config-connector/apis/common" |
| 23 | + refs "github.com/GoogleCloudPlatform/k8s-config-connector/apis/refs/v1beta1" |
| 24 | + "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct" |
| 25 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 26 | +) |
| 27 | + |
| 28 | +type EnvgroupAttachmentIdentity struct { |
| 29 | + parent *EnvgroupAttachmentParent |
| 30 | + id string |
| 31 | +} |
| 32 | + |
| 33 | +func (i *EnvgroupAttachmentIdentity) String() string { |
| 34 | + return fmt.Sprintf("%s/attachments/%s", i.parent, i.id) |
| 35 | +} |
| 36 | + |
| 37 | +func (i *EnvgroupAttachmentIdentity) ID() string { |
| 38 | + return i.id |
| 39 | +} |
| 40 | + |
| 41 | +func (i *EnvgroupAttachmentIdentity) Parent() *EnvgroupAttachmentParent { |
| 42 | + return i.parent |
| 43 | +} |
| 44 | + |
| 45 | +type EnvgroupAttachmentParent struct { |
| 46 | + Organization string |
| 47 | + Envgroup string |
| 48 | +} |
| 49 | + |
| 50 | +func (p *EnvgroupAttachmentParent) String() string { |
| 51 | + return "organizations/" + p.Organization + "/envgroups/" + p.Envgroup |
| 52 | +} |
| 53 | + |
| 54 | +func NewEnvgroupAttachmentIdentity(ctx context.Context, reader client.Reader, obj *ApigeeEnvgroupAttachment) (*EnvgroupAttachmentIdentity, error) { |
| 55 | + if obj == nil { |
| 56 | + return nil, fmt.Errorf("object cannot be nil") |
| 57 | + } |
| 58 | + |
| 59 | + // Get Parent |
| 60 | + orgRef := obj.Spec.OrganizationRef |
| 61 | + if orgRef == nil { |
| 62 | + return nil, fmt.Errorf("no parent organization") |
| 63 | + } |
| 64 | + orgExternal, err := orgRef.NormalizedExternal(ctx, reader, obj.Namespace) |
| 65 | + if err != nil { |
| 66 | + return nil, fmt.Errorf("cannot resolve organization: %w", err) |
| 67 | + } |
| 68 | + |
| 69 | + org, err := refs.ParseApigeeOrganizationExternal(orgExternal) |
| 70 | + if err != nil { |
| 71 | + return nil, fmt.Errorf("cannot parse external organization: %w", err) |
| 72 | + } |
| 73 | + |
| 74 | + envgroupRef := obj.Spec.EnvgroupRef |
| 75 | + if envgroupRef == nil { |
| 76 | + return nil, fmt.Errorf("no envgroup reference") |
| 77 | + } |
| 78 | + envgroupExternal, err := envgroupRef.NormalizedExternal(ctx, reader, obj.Namespace) |
| 79 | + if err != nil { |
| 80 | + return nil, fmt.Errorf("cannot resolve envgroup: %w", err) |
| 81 | + } |
| 82 | + _, envgroupID, err := ParseEnvironmentGroupExternal(envgroupExternal) |
| 83 | + if err != nil { |
| 84 | + return nil, fmt.Errorf("cannot parse envgroup external: %w", err) |
| 85 | + } |
| 86 | + |
| 87 | + resourceID := direct.ValueOf(obj.Spec.ResourceID) |
| 88 | + if resourceID == "" { |
| 89 | + resourceID = obj.GetName() |
| 90 | + } |
| 91 | + if resourceID == "" { |
| 92 | + return nil, fmt.Errorf("cannot resolve resource ID") |
| 93 | + } |
| 94 | + |
| 95 | + externalRef := common.ValueOf(obj.Status.ExternalRef) |
| 96 | + if externalRef != "" { |
| 97 | + actualParent, actualResourceID, err := ParseEnvgroupAttachmentExternalRef(externalRef) |
| 98 | + if err != nil { |
| 99 | + return nil, err |
| 100 | + } |
| 101 | + |
| 102 | + if actualParent.Organization != org { |
| 103 | + return nil, fmt.Errorf("spec.organizationRef changed, expect %s, got %s", actualParent.Organization, org) |
| 104 | + } |
| 105 | + if actualParent.Envgroup != envgroupID { |
| 106 | + return nil, fmt.Errorf("spec.envgroup changed, expect %s, got %s", actualParent.Envgroup, obj.Spec.EnvgroupRef) |
| 107 | + } |
| 108 | + if actualResourceID != actualResourceID { |
| 109 | + return nil, fmt.Errorf("spec.resourceID changed, expect %s, got %s", actualResourceID, resourceID) |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + return &EnvgroupAttachmentIdentity{ |
| 114 | + parent: &EnvgroupAttachmentParent{ |
| 115 | + Organization: org, |
| 116 | + Envgroup: envgroupID, |
| 117 | + }, |
| 118 | + id: resourceID, |
| 119 | + }, nil |
| 120 | +} |
| 121 | + |
| 122 | +func ParseEnvgroupAttachmentExternalRef(external string) (parent *EnvgroupAttachmentParent, resourceID string, err error) { |
| 123 | + tokens := strings.Split(external, "/") |
| 124 | + if len(tokens) != 6 { |
| 125 | + return nil, "", fmt.Errorf("invalid external format: %s, expecting organizations/{{organizationID}}/envgroups/{{envgroupID}}/attachments/{{attachmentID}} ", external) |
| 126 | + } |
| 127 | + |
| 128 | + parent = &EnvgroupAttachmentParent{ |
| 129 | + Organization: tokens[1], |
| 130 | + Envgroup: tokens[3], |
| 131 | + } |
| 132 | + resourceID = tokens[5] |
| 133 | + return parent, resourceID, nil |
| 134 | +} |
0 commit comments