|
| 1 | +package build |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "regexp" |
| 8 | + "strconv" |
| 9 | + |
| 10 | + "github.com/nitrictech/suga/cli/internal/api" |
| 11 | + "github.com/nitrictech/suga/cli/internal/version" |
| 12 | + "github.com/nitrictech/suga/engines/terraform" |
| 13 | +) |
| 14 | + |
| 15 | +// Repository fetches both platform and plugins in a single API call at construction time |
| 16 | +type Repository struct { |
| 17 | + apiClient *api.SugaApiClient |
| 18 | + currentTeam string |
| 19 | + // The platform reference this repository was created for |
| 20 | + platformRef string |
| 21 | + // Cached platform spec and plugin manifests |
| 22 | + platformSpec *terraform.PlatformSpec |
| 23 | + pluginManifests map[string]map[string]any |
| 24 | +} |
| 25 | + |
| 26 | +var _ terraform.PlatformRepository = (*Repository)(nil) |
| 27 | +var _ terraform.PluginRepository = (*Repository)(nil) |
| 28 | + |
| 29 | +// NewRepository creates a repository and fetches the platform and all its plugins upfront |
| 30 | +func NewRepository(apiClient *api.SugaApiClient, currentTeam, platformRef string) (*Repository, error) { |
| 31 | + repo := &Repository{ |
| 32 | + apiClient: apiClient, |
| 33 | + currentTeam: currentTeam, |
| 34 | + platformRef: platformRef, |
| 35 | + } |
| 36 | + |
| 37 | + // Fetch platform and plugins immediately |
| 38 | + if err := repo.fetchPlatformAndPlugins(platformRef); err != nil { |
| 39 | + return nil, err |
| 40 | + } |
| 41 | + |
| 42 | + return repo, nil |
| 43 | +} |
| 44 | + |
| 45 | +// GetPlatform validates that the requested platform matches the one this repository was created for |
| 46 | +func (r *Repository) GetPlatform(name string) (*terraform.PlatformSpec, error) { |
| 47 | + // Validate that the requested platform matches what we fetched |
| 48 | + if name != r.platformRef { |
| 49 | + return nil, fmt.Errorf("repository was created for platform %s but %s was requested", r.platformRef, name) |
| 50 | + } |
| 51 | + |
| 52 | + return r.platformSpec, nil |
| 53 | +} |
| 54 | + |
| 55 | +// fetchPlatformAndPlugins fetches the platform and all its plugins in a single API call |
| 56 | +func (r *Repository) fetchPlatformAndPlugins(name string) error { |
| 57 | + // Parse the platform name <team>/<platform>@<revision> |
| 58 | + re := regexp.MustCompile(`^(?P<team>[a-z][a-z0-9-]*)/(?P<platform>[a-z][a-z0-9-]*)@(?P<revision>\d+)$`) |
| 59 | + matches := re.FindStringSubmatch(name) |
| 60 | + |
| 61 | + if matches == nil { |
| 62 | + return fmt.Errorf("invalid platform name format: %s. Expected format: <team>/<platform>@<revision> e.g. %s/aws@1", name, version.CommandName) |
| 63 | + } |
| 64 | + |
| 65 | + team := matches[re.SubexpIndex("team")] |
| 66 | + platform := matches[re.SubexpIndex("platform")] |
| 67 | + revisionStr := matches[re.SubexpIndex("revision")] |
| 68 | + |
| 69 | + revision, err := strconv.Atoi(revisionStr) |
| 70 | + if err != nil { |
| 71 | + return fmt.Errorf("invalid revision format: %s. Expected integer", revisionStr) |
| 72 | + } |
| 73 | + |
| 74 | + // Smart ordering: try public first if the platform team doesn't match current user's team |
| 75 | + var platformSpec *terraform.PlatformSpec |
| 76 | + var plugins map[string]map[string]any |
| 77 | + |
| 78 | + if team != r.currentTeam { |
| 79 | + // Try public access first |
| 80 | + platformSpec, plugins, err = r.apiClient.GetPublicBuildManifest(team, platform, revision) |
| 81 | + if err == nil { |
| 82 | + r.platformSpec = platformSpec |
| 83 | + r.pluginManifests = plugins |
| 84 | + return nil |
| 85 | + } |
| 86 | + |
| 87 | + // If public fails with 404, it's definitely not found |
| 88 | + if errors.Is(err, api.ErrNotFound) { |
| 89 | + return terraform.ErrPlatformNotFound |
| 90 | + } |
| 91 | + |
| 92 | + // If public fails for other reasons, try authenticated access |
| 93 | + platformSpec, plugins, err = r.apiClient.GetBuildManifest(team, platform, revision) |
| 94 | + if err != nil { |
| 95 | + if errors.Is(err, api.ErrNotFound) { |
| 96 | + return terraform.ErrPlatformNotFound |
| 97 | + } |
| 98 | + if errors.Is(err, api.ErrUnauthenticated) { |
| 99 | + return terraform.ErrUnauthenticated |
| 100 | + } |
| 101 | + return err |
| 102 | + } |
| 103 | + r.platformSpec = platformSpec |
| 104 | + r.pluginManifests = plugins |
| 105 | + return nil |
| 106 | + } |
| 107 | + |
| 108 | + // Try authenticated access first |
| 109 | + platformSpec, plugins, err = r.apiClient.GetBuildManifest(team, platform, revision) |
| 110 | + if err != nil { |
| 111 | + // If authentication failed, try public platform access |
| 112 | + if errors.Is(err, api.ErrUnauthenticated) || errors.Is(err, api.ErrNotFound) { |
| 113 | + platformSpec, plugins, err = r.apiClient.GetPublicBuildManifest(team, platform, revision) |
| 114 | + if err != nil { |
| 115 | + // If public access also fails with 404, return platform not found |
| 116 | + if errors.Is(err, api.ErrNotFound) { |
| 117 | + return terraform.ErrPlatformNotFound |
| 118 | + } |
| 119 | + // Return the original authentication error for other public access failures |
| 120 | + return terraform.ErrUnauthenticated |
| 121 | + } |
| 122 | + r.platformSpec = platformSpec |
| 123 | + r.pluginManifests = plugins |
| 124 | + return nil |
| 125 | + } |
| 126 | + |
| 127 | + // If its a 404, then return platform not found error |
| 128 | + if errors.Is(err, api.ErrNotFound) { |
| 129 | + return terraform.ErrPlatformNotFound |
| 130 | + } |
| 131 | + |
| 132 | + // return the original error to the engine |
| 133 | + return err |
| 134 | + } |
| 135 | + |
| 136 | + r.platformSpec = platformSpec |
| 137 | + r.pluginManifests = plugins |
| 138 | + return nil |
| 139 | +} |
| 140 | + |
| 141 | +// GetResourcePlugin retrieves a cached plugin manifest |
| 142 | +func (r *Repository) GetResourcePlugin(team, libname, version, name string) (*terraform.ResourcePluginManifest, error) { |
| 143 | + pluginManifest, err := r.getPluginManifest(team, libname, version, name) |
| 144 | + if err != nil { |
| 145 | + return nil, err |
| 146 | + } |
| 147 | + |
| 148 | + resourcePluginManifest, ok := pluginManifest.(*terraform.ResourcePluginManifest) |
| 149 | + if !ok { |
| 150 | + return nil, fmt.Errorf("encountered malformed manifest for plugin %s/%s/%s@%s", team, libname, name, version) |
| 151 | + } |
| 152 | + |
| 153 | + return resourcePluginManifest, nil |
| 154 | +} |
| 155 | + |
| 156 | +// GetIdentityPlugin retrieves a cached plugin manifest |
| 157 | +func (r *Repository) GetIdentityPlugin(team, libname, version, name string) (*terraform.IdentityPluginManifest, error) { |
| 158 | + pluginManifest, err := r.getPluginManifest(team, libname, version, name) |
| 159 | + if err != nil { |
| 160 | + return nil, err |
| 161 | + } |
| 162 | + |
| 163 | + identityPluginManifest, ok := pluginManifest.(*terraform.IdentityPluginManifest) |
| 164 | + if !ok { |
| 165 | + return nil, fmt.Errorf("encountered malformed manifest for plugin %s/%s/%s@%s", team, libname, name, version) |
| 166 | + } |
| 167 | + |
| 168 | + return identityPluginManifest, nil |
| 169 | +} |
| 170 | + |
| 171 | +// getPluginManifest retrieves a plugin manifest from the cache |
| 172 | +func (r *Repository) getPluginManifest(team, libname, version, name string) (any, error) { |
| 173 | + if r.pluginManifests == nil { |
| 174 | + return nil, fmt.Errorf("no plugin manifests cached. GetPlatform must be called first") |
| 175 | + } |
| 176 | + |
| 177 | + key := fmt.Sprintf("%s/%s/%s/%s", team, libname, version, name) |
| 178 | + manifestData, ok := r.pluginManifests[key] |
| 179 | + if !ok { |
| 180 | + return nil, fmt.Errorf("plugin %s not found in build manifest", key) |
| 181 | + } |
| 182 | + |
| 183 | + // Check the type field to determine which manifest type to use |
| 184 | + pluginType, ok := manifestData["type"].(string) |
| 185 | + if !ok { |
| 186 | + return nil, fmt.Errorf("plugin manifest missing type field for %s", key) |
| 187 | + } |
| 188 | + |
| 189 | + if pluginType == "identity" { |
| 190 | + var identityManifest terraform.IdentityPluginManifest |
| 191 | + if err := remapToStruct(manifestData, &identityManifest); err != nil { |
| 192 | + return nil, fmt.Errorf("failed to parse identity plugin manifest for %s: %w", key, err) |
| 193 | + } |
| 194 | + return &identityManifest, nil |
| 195 | + } |
| 196 | + |
| 197 | + var resourceManifest terraform.ResourcePluginManifest |
| 198 | + if err := remapToStruct(manifestData, &resourceManifest); err != nil { |
| 199 | + return nil, fmt.Errorf("failed to parse resource plugin manifest for %s: %w", key, err) |
| 200 | + } |
| 201 | + return &resourceManifest, nil |
| 202 | +} |
| 203 | + |
| 204 | +// remapToStruct is a helper to convert map[string]any to a struct |
| 205 | +// This is a simple implementation using JSON marshaling |
| 206 | +func remapToStruct(m map[string]any, target any) error { |
| 207 | + bytes, err := json.Marshal(m) |
| 208 | + if err != nil { |
| 209 | + return err |
| 210 | + } |
| 211 | + return json.Unmarshal(bytes, target) |
| 212 | +} |
0 commit comments