|
| 1 | +package projects |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/MakeNowJust/heredoc/v2" |
| 8 | + "github.com/charmbracelet/log" |
| 9 | + "github.com/ctrlplanedev/cli/internal/api" |
| 10 | + "github.com/spf13/cobra" |
| 11 | + "github.com/spf13/viper" |
| 12 | + "google.golang.org/api/cloudresourcemanager/v1" |
| 13 | +) |
| 14 | + |
| 15 | +// NewSyncProjectsCmd creates a new cobra command for syncing Google Cloud projects |
| 16 | +func NewSyncProjectsCmd() *cobra.Command { |
| 17 | + var name string |
| 18 | + |
| 19 | + cmd := &cobra.Command{ |
| 20 | + Use: "projects", |
| 21 | + Short: "Sync Google Cloud projects into Ctrlplane", |
| 22 | + Example: heredoc.Doc(` |
| 23 | + # Make sure Google Cloud credentials are configured via environment variables or application default credentials |
| 24 | + |
| 25 | + # Sync all projects |
| 26 | + $ ctrlc sync google projects |
| 27 | + `), |
| 28 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 29 | + log.Info("Syncing Google Cloud projects into Ctrlplane") |
| 30 | + |
| 31 | + ctx := context.Background() |
| 32 | + |
| 33 | + // Create Cloud Resource Manager client |
| 34 | + crm, err := cloudresourcemanager.NewService(ctx) |
| 35 | + if err != nil { |
| 36 | + return fmt.Errorf("failed to create Cloud Resource Manager client: %w", err) |
| 37 | + } |
| 38 | + |
| 39 | + // List all projects |
| 40 | + resp, err := crm.Projects.List().Do() |
| 41 | + if err != nil { |
| 42 | + return fmt.Errorf("failed to list projects: %w", err) |
| 43 | + } |
| 44 | + |
| 45 | + resources := []api.CreateResource{} |
| 46 | + |
| 47 | + // Process each project |
| 48 | + for _, project := range resp.Projects { |
| 49 | + metadata := map[string]string{ |
| 50 | + "account/id": project.ProjectId, |
| 51 | + "account/name": project.Name, |
| 52 | + "account/number": fmt.Sprintf("%d", project.ProjectNumber), |
| 53 | + "account/state": project.LifecycleState, |
| 54 | + "account/parent-id": project.Parent.Id, |
| 55 | + "account/parent-type": project.Parent.Type, |
| 56 | + |
| 57 | + "google/project-id": project.ProjectId, |
| 58 | + "google/project-name": project.Name, |
| 59 | + "google/number": fmt.Sprintf("%d", project.ProjectNumber), |
| 60 | + "google/state": project.LifecycleState, |
| 61 | + "google/parent-id": project.Parent.Id, |
| 62 | + "google/parent-type": project.Parent.Type, |
| 63 | + } |
| 64 | + |
| 65 | + // Add labels as metadata |
| 66 | + for key, value := range project.Labels { |
| 67 | + metadata[fmt.Sprintf("labels/%s", key)] = value |
| 68 | + } |
| 69 | + |
| 70 | + resources = append(resources, api.CreateResource{ |
| 71 | + Version: "ctrlplane.dev/cloud/account/v1", |
| 72 | + Kind: "GoogleProject", |
| 73 | + Name: project.Name, |
| 74 | + Identifier: project.ProjectId, |
| 75 | + Config: map[string]any{ |
| 76 | + "id": project.ProjectId, |
| 77 | + "name": project.Name, |
| 78 | + "projectNumber": project.ProjectNumber, |
| 79 | + "state": project.LifecycleState, |
| 80 | + "parent": map[string]any{ |
| 81 | + "id": project.Parent.Id, |
| 82 | + "type": project.Parent.Type, |
| 83 | + }, |
| 84 | + "labels": project.Labels, |
| 85 | + }, |
| 86 | + Metadata: metadata, |
| 87 | + }) |
| 88 | + } |
| 89 | + |
| 90 | + // Upsert resources to Ctrlplane |
| 91 | + if name == "" { |
| 92 | + name = "google-projects" |
| 93 | + } |
| 94 | + |
| 95 | + apiURL := viper.GetString("url") |
| 96 | + apiKey := viper.GetString("api-key") |
| 97 | + workspaceId := viper.GetString("workspace") |
| 98 | + |
| 99 | + ctrlplaneClient, err := api.NewAPIKeyClientWithResponses(apiURL, apiKey) |
| 100 | + if err != nil { |
| 101 | + return fmt.Errorf("failed to create API client: %w", err) |
| 102 | + } |
| 103 | + |
| 104 | + log.Info("Upserting resource provider", "name", name) |
| 105 | + rp, err := api.NewResourceProvider(ctrlplaneClient, workspaceId, name) |
| 106 | + if err != nil { |
| 107 | + return fmt.Errorf("failed to create resource provider: %w", err) |
| 108 | + } |
| 109 | + |
| 110 | + upsertResp, err := rp.UpsertResource(ctx, resources) |
| 111 | + if err != nil { |
| 112 | + return fmt.Errorf("failed to upsert resources: %w", err) |
| 113 | + } |
| 114 | + |
| 115 | + log.Info("Response from upserting resources", "status", upsertResp.Status) |
| 116 | + return nil |
| 117 | + }, |
| 118 | + } |
| 119 | + |
| 120 | + cmd.Flags().StringVarP(&name, "provider", "p", "", "Name of the resource provider") |
| 121 | + |
| 122 | + return cmd |
| 123 | +} |
0 commit comments