|
| 1 | +package apply |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "path/filepath" |
| 8 | + |
| 9 | + "os" |
| 10 | + |
| 11 | + "github.com/charmbracelet/log" |
| 12 | + "github.com/ctrlplanedev/cli/internal/api" |
| 13 | + "github.com/google/uuid" |
| 14 | + "github.com/spf13/cobra" |
| 15 | + "github.com/spf13/viper" |
| 16 | + |
| 17 | + "gopkg.in/yaml.v3" |
| 18 | +) |
| 19 | + |
| 20 | +// Config represents the structure of the YAML file |
| 21 | +type Config struct { |
| 22 | + Systems map[string]System `yaml:"systems"` |
| 23 | +} |
| 24 | + |
| 25 | +type System struct { |
| 26 | + Name string `yaml:"name"` |
| 27 | + Description string `yaml:"description"` |
| 28 | + Deployments map[string]Deployment `yaml:"deployments"` |
| 29 | +} |
| 30 | + |
| 31 | +type Deployment struct { |
| 32 | + Name string `yaml:"name"` |
| 33 | + Description string `yaml:"description"` |
| 34 | + SystemName string `yaml:"systemName"` |
| 35 | + Metadata map[string]string `yaml:"metadata"` |
| 36 | +} |
| 37 | + |
| 38 | +// NewApplyCommand creates a new apply command |
| 39 | +func NewApplyCmd() *cobra.Command { |
| 40 | + var filePath string |
| 41 | + |
| 42 | + cmd := &cobra.Command{ |
| 43 | + Use: "apply", |
| 44 | + Short: "Apply a YAML configuration file to create systems and deployments", |
| 45 | + Long: `Apply a YAML configuration file to create systems and deployments in Ctrlplane`, |
| 46 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 47 | + return runApply(filePath) |
| 48 | + }, |
| 49 | + } |
| 50 | + |
| 51 | + cmd.Flags().StringVarP(&filePath, "file", "f", "", "Path to the YAML configuration file (required)") |
| 52 | + cmd.MarkFlagRequired("file") |
| 53 | + |
| 54 | + return cmd |
| 55 | +} |
| 56 | + |
| 57 | +func runApply(filePath string) error { |
| 58 | + ctx := context.Background() |
| 59 | + logger := log.FromContext(ctx) |
| 60 | + |
| 61 | + // Read and parse the YAML file |
| 62 | + config, err := readConfigFile(filePath) |
| 63 | + if err != nil { |
| 64 | + return fmt.Errorf("failed to read config file: %w", err) |
| 65 | + } |
| 66 | + |
| 67 | + // Create API client |
| 68 | + apiURL := viper.GetString("url") |
| 69 | + apiKey := viper.GetString("api-key") |
| 70 | + workspace := viper.GetString("workspace") |
| 71 | + |
| 72 | + workspaceID, err := uuid.Parse(workspace) |
| 73 | + if err != nil { |
| 74 | + return fmt.Errorf("invalid workspace ID: %w", err) |
| 75 | + } |
| 76 | + |
| 77 | + client, err := api.NewAPIKeyClientWithResponses(apiURL, apiKey) |
| 78 | + if err != nil { |
| 79 | + return fmt.Errorf("failed to create API client: %w", err) |
| 80 | + } |
| 81 | + |
| 82 | + for slug, system := range config.Systems { |
| 83 | + logger.Info("Upserting system", "name", system.Name) |
| 84 | + |
| 85 | + systemID, err := upsertSystem(ctx, client, workspaceID, slug, system) |
| 86 | + if err != nil { |
| 87 | + logger.Error("Failed to upsert system", "name", system.Name, "error", err) |
| 88 | + continue |
| 89 | + } |
| 90 | + logger.Info("System created successfully", "name", system.Name, "id", systemID) |
| 91 | + |
| 92 | + systemIDUUID, err := uuid.Parse(systemID) |
| 93 | + if err != nil { |
| 94 | + return fmt.Errorf("invalid system ID: %w", err) |
| 95 | + } |
| 96 | + |
| 97 | + for deploymentSlug, deployment := range system.Deployments { |
| 98 | + logger.Info("Creating deployment", "name", deployment.Name) |
| 99 | + _, err := upsertDeployment(ctx, client, systemIDUUID, deploymentSlug, deployment) |
| 100 | + if err != nil { |
| 101 | + logger.Error("Failed to create deployment", "name", deployment.Name, "error", err) |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + return nil |
| 107 | +} |
| 108 | + |
| 109 | +func readConfigFile(filePath string) (*Config, error) { |
| 110 | + // Resolve absolute path |
| 111 | + absPath, err := filepath.Abs(filePath) |
| 112 | + if err != nil { |
| 113 | + return nil, fmt.Errorf("failed to resolve file path: %w", err) |
| 114 | + } |
| 115 | + |
| 116 | + // Read file |
| 117 | + data, err := os.ReadFile(absPath) |
| 118 | + if err != nil { |
| 119 | + return nil, fmt.Errorf("failed to read file: %w", err) |
| 120 | + } |
| 121 | + |
| 122 | + // Parse YAML |
| 123 | + var config Config |
| 124 | + if err := yaml.Unmarshal(data, &config); err != nil { |
| 125 | + return nil, fmt.Errorf("failed to parse YAML: %w", err) |
| 126 | + } |
| 127 | + |
| 128 | + return &config, nil |
| 129 | +} |
| 130 | + |
| 131 | +func upsertSystem( |
| 132 | + ctx context.Context, |
| 133 | + client *api.ClientWithResponses, |
| 134 | + workspaceID uuid.UUID, |
| 135 | + slug string, |
| 136 | + system System, |
| 137 | +) (string, error) { |
| 138 | + resp, err := client.CreateSystemWithResponse(ctx, api.CreateSystemJSONRequestBody{ |
| 139 | + Slug: slug, |
| 140 | + WorkspaceId: workspaceID, |
| 141 | + Name: system.Name, |
| 142 | + Description: &system.Description, |
| 143 | + }) |
| 144 | + |
| 145 | + if err != nil { |
| 146 | + return "", fmt.Errorf("API request failed: %w", err) |
| 147 | + } |
| 148 | + |
| 149 | + if resp.StatusCode() >= 400 { |
| 150 | + return "", fmt.Errorf("API returned error status: %d", resp.StatusCode()) |
| 151 | + } |
| 152 | + |
| 153 | + if resp.JSON200 != nil { |
| 154 | + return resp.JSON200.Id.String(), nil |
| 155 | + } |
| 156 | + |
| 157 | + if resp.JSON201 != nil { |
| 158 | + return resp.JSON201.Id.String(), nil |
| 159 | + } |
| 160 | + |
| 161 | + return "", fmt.Errorf("unexpected response format") |
| 162 | +} |
| 163 | + |
| 164 | +func upsertDeployment( |
| 165 | + ctx context.Context, |
| 166 | + client *api.ClientWithResponses, |
| 167 | + systemID uuid.UUID, |
| 168 | + deploymentSlug string, |
| 169 | + deployment Deployment, |
| 170 | +) (string, error) { |
| 171 | + resp, err := client.CreateDeploymentWithResponse(ctx, api.CreateDeploymentJSONRequestBody{ |
| 172 | + SystemId: systemID, |
| 173 | + Slug: deploymentSlug, |
| 174 | + Name: deployment.Name, |
| 175 | + Description: &deployment.Description, |
| 176 | + }) |
| 177 | + |
| 178 | + if err != nil { |
| 179 | + return "", fmt.Errorf("API request failed: %w", err) |
| 180 | + } |
| 181 | + |
| 182 | + if resp.StatusCode() >= 400 { |
| 183 | + return "", fmt.Errorf("API returned error status: %d", resp.StatusCode()) |
| 184 | + } |
| 185 | + |
| 186 | + if resp.JSON200 != nil { |
| 187 | + return resp.JSON200.Id.String(), nil |
| 188 | + } |
| 189 | + |
| 190 | + if resp.JSON201 != nil { |
| 191 | + return resp.JSON201.Id.String(), nil |
| 192 | + } |
| 193 | + |
| 194 | + return "", fmt.Errorf("unexpected response format") |
| 195 | +} |
0 commit comments