|
| 1 | +package cleanup |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/stackitcloud/stackit-cli/internal/pkg/args" |
| 8 | + "github.com/stackitcloud/stackit-cli/internal/pkg/errors" |
| 9 | + "github.com/stackitcloud/stackit-cli/internal/pkg/examples" |
| 10 | + "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" |
| 11 | + "github.com/stackitcloud/stackit-cli/internal/pkg/print" |
| 12 | + "github.com/stackitcloud/stackit-cli/internal/pkg/projectname" |
| 13 | + "github.com/stackitcloud/stackit-cli/internal/pkg/services/load-balancer/client" |
| 14 | + "github.com/stackitcloud/stackit-cli/internal/pkg/services/load-balancer/utils" |
| 15 | + |
| 16 | + "github.com/spf13/cobra" |
| 17 | + "github.com/stackitcloud/stackit-sdk-go/services/loadbalancer" |
| 18 | +) |
| 19 | + |
| 20 | +type inputModel struct { |
| 21 | + *globalflags.GlobalFlagModel |
| 22 | +} |
| 23 | + |
| 24 | +func NewCmd(p *print.Printer) *cobra.Command { |
| 25 | + cmd := &cobra.Command{ |
| 26 | + Use: "cleanup", |
| 27 | + Short: "Deletes observability credentials unused by any Load Balancer", |
| 28 | + Long: "Deletes observability credentials unused by any Load Balancer.", |
| 29 | + Args: args.NoArgs, |
| 30 | + Example: examples.Build( |
| 31 | + examples.NewExample( |
| 32 | + `Delete observability credentials unused by any Load Balancer`, |
| 33 | + "$ stackit load-balancer observability-credentials cleanup"), |
| 34 | + ), |
| 35 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 36 | + ctx := context.Background() |
| 37 | + model, err := parseInput(p, cmd) |
| 38 | + if err != nil { |
| 39 | + return err |
| 40 | + } |
| 41 | + |
| 42 | + // Configure API client |
| 43 | + apiClient, err := client.ConfigureClient(p) |
| 44 | + if err != nil { |
| 45 | + return err |
| 46 | + } |
| 47 | + |
| 48 | + projectLabel, err := projectname.GetProjectName(ctx, p, cmd) |
| 49 | + if err != nil { |
| 50 | + p.Debug(print.ErrorLevel, "get project name: %v", err) |
| 51 | + projectLabel = model.ProjectId |
| 52 | + } |
| 53 | + |
| 54 | + listReq := buildListCredentialsRequest(ctx, model, apiClient) |
| 55 | + resp, err := listReq.Execute() |
| 56 | + if err != nil { |
| 57 | + return fmt.Errorf("list Load Balancer observability credentials: %w", err) |
| 58 | + } |
| 59 | + |
| 60 | + var credentials []loadbalancer.CredentialsResponse |
| 61 | + if resp.Credentials != nil && len(*resp.Credentials) > 0 { |
| 62 | + credentials, err = utils.FilterCredentials(ctx, apiClient, *resp.Credentials, model.ProjectId, utils.OP_FILTER_UNUSED) |
| 63 | + if err != nil { |
| 64 | + return fmt.Errorf("filter Load Balancer observability credentials: %w", err) |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + if len(credentials) == 0 { |
| 69 | + p.Info("No unused observability credentials found on project %q\n", projectLabel) |
| 70 | + return nil |
| 71 | + } |
| 72 | + |
| 73 | + if !model.AssumeYes { |
| 74 | + prompt := "Will delete the following unused observability credentials: \n" |
| 75 | + for _, credential := range credentials { |
| 76 | + if credential.DisplayName == nil || credential.Username == nil { |
| 77 | + return fmt.Errorf("list unused Load Balancer observability credentials: credentials %q missing display name or username", *credential.CredentialsRef) |
| 78 | + } |
| 79 | + name := *credential.DisplayName |
| 80 | + username := *credential.Username |
| 81 | + prompt += fmt.Sprintf(" - %s (username: %q)\n", name, username) |
| 82 | + } |
| 83 | + prompt += fmt.Sprintf("Are you sure you want to delete unused observability credentials on project %q? (This cannot be undone)", projectLabel) |
| 84 | + err = p.PromptForConfirmation(prompt) |
| 85 | + if err != nil { |
| 86 | + return err |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + for _, credential := range credentials { |
| 91 | + if credential.CredentialsRef == nil { |
| 92 | + return fmt.Errorf("delete Load Balancer observability credentials: missing credentials reference") |
| 93 | + } |
| 94 | + credentialsRef := *credential.CredentialsRef |
| 95 | + // Call API |
| 96 | + req := buildDeleteCredentialRequest(ctx, model, apiClient, credentialsRef) |
| 97 | + _, err = req.Execute() |
| 98 | + if err != nil { |
| 99 | + return fmt.Errorf("delete Load Balancer observability credentials: %w", err) |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + p.Info("Deleted unused Load Balancer observability credentials on project %q\n", projectLabel) |
| 104 | + return nil |
| 105 | + }, |
| 106 | + } |
| 107 | + return cmd |
| 108 | +} |
| 109 | + |
| 110 | +func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) { |
| 111 | + globalFlags := globalflags.Parse(p, cmd) |
| 112 | + if globalFlags.ProjectId == "" { |
| 113 | + return nil, &errors.ProjectIdError{} |
| 114 | + } |
| 115 | + |
| 116 | + model := inputModel{ |
| 117 | + GlobalFlagModel: globalFlags, |
| 118 | + } |
| 119 | + |
| 120 | + if p.IsVerbosityDebug() { |
| 121 | + modelStr, err := print.BuildDebugStrFromInputModel(model) |
| 122 | + if err != nil { |
| 123 | + p.Debug(print.ErrorLevel, "convert model to string for debugging: %v", err) |
| 124 | + } else { |
| 125 | + p.Debug(print.DebugLevel, "parsed input values: %s", modelStr) |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + return &model, nil |
| 130 | +} |
| 131 | + |
| 132 | +func buildDeleteCredentialRequest(ctx context.Context, model *inputModel, apiClient *loadbalancer.APIClient, credentialsRef string) loadbalancer.ApiDeleteCredentialsRequest { |
| 133 | + req := apiClient.DeleteCredentials(ctx, model.ProjectId, credentialsRef) |
| 134 | + return req |
| 135 | +} |
| 136 | + |
| 137 | +func buildListCredentialsRequest(ctx context.Context, model *inputModel, apiClient *loadbalancer.APIClient) loadbalancer.ApiListCredentialsRequest { |
| 138 | + req := apiClient.ListCredentials(ctx, model.ProjectId) |
| 139 | + return req |
| 140 | +} |
0 commit comments