|
| 1 | +// Copyright 2024 KusionStack Authors |
| 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 terraform |
| 16 | + |
| 17 | +import ( |
| 18 | + "errors" |
| 19 | + "fmt" |
| 20 | + "strings" |
| 21 | + |
| 22 | + svchost "github.com/hashicorp/terraform-svchost" |
| 23 | + v1 "kusionstack.io/kusion-api-go/api.kusion.io/v1" |
| 24 | + |
| 25 | + "kusionstack.io/kusion-module-framework/pkg/resources" |
| 26 | +) |
| 27 | + |
| 28 | +// DefaultProviderRegistryHost is the hostname used for provider addresses that do |
| 29 | +// not have an explicit hostname. |
| 30 | +const DefaultProviderRegistryHost = "registry.terraform.io" |
| 31 | + |
| 32 | +var ( |
| 33 | + // errInvalidSource means provider's source is invalid, which must be in [<HOSTNAME>/]<NAMESPACE>/<TYPE> format |
| 34 | + errInvalidSource = errors.New(`invalid provider source string, must be in the format "[hostname/]namespace/name"`) |
| 35 | + // errInvalidVersion means provider's version constraint is invalid, which must not be empty. |
| 36 | + errInvalidVersion = errors.New("invalid provider version constraint") |
| 37 | + // errInvalidResourceTypeOrName resourceType or resourceName is invalid, which must not be empty. |
| 38 | + errInvalidResourceTypeOrName = errors.New("resourceType or resourceName is empty") |
| 39 | +) |
| 40 | + |
| 41 | +// NewProvider constructs a provider instance from given source, version and configuration arguments. |
| 42 | +func NewProvider(providerConfigs map[string]any, source, version string) (Provider, error) { |
| 43 | + var ret Provider |
| 44 | + |
| 45 | + if version == "" { |
| 46 | + return ret, errInvalidVersion |
| 47 | + } |
| 48 | + |
| 49 | + _, err := parseProviderSourceString(source) |
| 50 | + if err != nil { |
| 51 | + return ret, err |
| 52 | + } |
| 53 | + |
| 54 | + ret.Source = source |
| 55 | + ret.Version = version |
| 56 | + ret.ProviderConfigs = providerConfigs |
| 57 | + return ret, nil |
| 58 | +} |
| 59 | + |
| 60 | +// ToKusionResourceID takes provider, resource info and returns string representing Kusion qualified resource ID. |
| 61 | +func ToKusionResourceID(p Provider, resourceType, resourceName string) (string, error) { |
| 62 | + if p.Version == "" { |
| 63 | + return "", errInvalidVersion |
| 64 | + } |
| 65 | + if resourceType == "" || resourceName == "" { |
| 66 | + return "", errInvalidResourceTypeOrName |
| 67 | + } |
| 68 | + tfProvider, err := parseProviderSourceString(p.Source) |
| 69 | + if err != nil { |
| 70 | + return "", err |
| 71 | + } |
| 72 | + |
| 73 | + tfProviderStr := tfProvider.String() |
| 74 | + return strings.Join([]string{tfProviderStr, resourceType, resourceName}, resources.SegmentSeparator), nil |
| 75 | +} |
| 76 | + |
| 77 | +// NewKusionResource creates a Kusion Resource object with the given resourceType, resourceID, attributes. |
| 78 | +func NewKusionResource(p Provider, resourceType, resourceID string, |
| 79 | + attrs map[string]interface{}, dependsOn []string, |
| 80 | +) (*v1.Resource, error) { |
| 81 | + if resourceType == "" { |
| 82 | + return nil, errInvalidResourceTypeOrName |
| 83 | + } |
| 84 | + tfProvider, err := parseProviderSourceString(p.Source) |
| 85 | + if err != nil { |
| 86 | + return nil, err |
| 87 | + } |
| 88 | + tfProvider.Version = p.Version |
| 89 | + |
| 90 | + // put provider info into extensions |
| 91 | + extensions := make(map[string]interface{}, 3) |
| 92 | + extensions["provider"] = tfProvider.String() |
| 93 | + extensions["providerMeta"] = p.ProviderConfigs |
| 94 | + extensions["resourceType"] = resourceType |
| 95 | + |
| 96 | + return &v1.Resource{ |
| 97 | + ID: resourceID, |
| 98 | + Type: v1.Terraform, |
| 99 | + Attributes: attrs, |
| 100 | + DependsOn: dependsOn, |
| 101 | + Extensions: extensions, |
| 102 | + }, nil |
| 103 | +} |
| 104 | + |
| 105 | +// parseProviderSourceString parses the source attribute and returns a terraform provider. |
| 106 | +// |
| 107 | +// The following are valid source string formats: |
| 108 | +// |
| 109 | +// name |
| 110 | +// namespace/name |
| 111 | +// hostname/namespace/name |
| 112 | +func parseProviderSourceString(str string) (TFProvider, error) { |
| 113 | + var ret TFProvider |
| 114 | + |
| 115 | + // split the source string into individual components |
| 116 | + parts := strings.Split(str, "/") |
| 117 | + if len(parts) == 0 || len(parts) > 3 { |
| 118 | + return ret, errInvalidSource |
| 119 | + } |
| 120 | + |
| 121 | + // check for an invalid empty string in any part |
| 122 | + for i := range parts { |
| 123 | + if parts[i] == "" { |
| 124 | + return ret, errInvalidSource |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + // check the 'name' portion, which is always the last part |
| 129 | + givenName := parts[len(parts)-1] |
| 130 | + ret.Type = givenName |
| 131 | + ret.Hostname = DefaultProviderRegistryHost |
| 132 | + |
| 133 | + if len(parts) == 1 { |
| 134 | + ret.Namespace = "hashicorp" |
| 135 | + return ret, nil |
| 136 | + } |
| 137 | + |
| 138 | + if len(parts) >= 2 { |
| 139 | + // the namespace is always the second-to-last part |
| 140 | + givenNamespace := parts[len(parts)-2] |
| 141 | + ret.Namespace = givenNamespace |
| 142 | + } |
| 143 | + |
| 144 | + // Final Case: 3 parts |
| 145 | + if len(parts) == 3 { |
| 146 | + // the namespace is always the first part in a three-part source string |
| 147 | + hn, err := svchost.ForComparison(parts[0]) |
| 148 | + if err != nil { |
| 149 | + return ret, fmt.Errorf("invalid provider source hostname %q in source %q: %s", hn, str, err) |
| 150 | + } |
| 151 | + ret.Hostname = hn |
| 152 | + } |
| 153 | + |
| 154 | + return ret, nil |
| 155 | +} |
0 commit comments