Skip to content

Commit 05eb3d6

Browse files
authored
Onboard load-balancer observability-credentials cleanup (#311)
* command implementation, add testing * rename var, generate docs * address PR comments
1 parent 0578af7 commit 05eb3d6

File tree

5 files changed

+403
-0
lines changed

5 files changed

+403
-0
lines changed

docs/stackit_load-balancer_observability-credentials.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ stackit load-balancer observability-credentials [flags]
3030

3131
* [stackit load-balancer](./stackit_load-balancer.md) - Provides functionality for Load Balancer
3232
* [stackit load-balancer observability-credentials add](./stackit_load-balancer_observability-credentials_add.md) - Adds observability credentials to Load Balancer
33+
* [stackit load-balancer observability-credentials cleanup](./stackit_load-balancer_observability-credentials_cleanup.md) - Deletes observability credentials unused by any Load Balancer
3334
* [stackit load-balancer observability-credentials delete](./stackit_load-balancer_observability-credentials_delete.md) - Deletes observability credentials for Load Balancer
3435
* [stackit load-balancer observability-credentials describe](./stackit_load-balancer_observability-credentials_describe.md) - Shows details of observability credentials for Load Balancer
3536
* [stackit load-balancer observability-credentials list](./stackit_load-balancer_observability-credentials_list.md) - Lists observability credentials for Load Balancer
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
## stackit load-balancer observability-credentials cleanup
2+
3+
Deletes observability credentials unused by any Load Balancer
4+
5+
### Synopsis
6+
7+
Deletes observability credentials unused by any Load Balancer.
8+
9+
```
10+
stackit load-balancer observability-credentials cleanup [flags]
11+
```
12+
13+
### Examples
14+
15+
```
16+
Delete observability credentials unused by any Load Balancer
17+
$ stackit load-balancer observability-credentials cleanup
18+
```
19+
20+
### Options
21+
22+
```
23+
-h, --help Help for "stackit load-balancer observability-credentials cleanup"
24+
```
25+
26+
### Options inherited from parent commands
27+
28+
```
29+
-y, --assume-yes If set, skips all confirmation prompts
30+
--async If set, runs the command asynchronously
31+
-o, --output-format string Output format, one of ["json" "pretty" "none"]
32+
-p, --project-id string Project ID
33+
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
34+
```
35+
36+
### SEE ALSO
37+
38+
* [stackit load-balancer observability-credentials](./stackit_load-balancer_observability-credentials.md) - Provides functionality for Load Balancer observability credentials
39+
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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

Comments
 (0)