-
Notifications
You must be signed in to change notification settings - Fork 20
Onboard load-balancer observability-credentials cleanup
#311
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
docs/stackit_load-balancer_observability-credentials_cleanup.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
## stackit load-balancer observability-credentials cleanup | ||
|
||
Deletes observability credentials unused by any Load Balancer | ||
|
||
### Synopsis | ||
|
||
Deletes observability credentials unused by any Load Balancer. | ||
|
||
``` | ||
stackit load-balancer observability-credentials cleanup [flags] | ||
``` | ||
|
||
### Examples | ||
|
||
``` | ||
Delete observability credentials unused by any Load Balancer | ||
$ stackit load-balancer observability-credentials cleanup | ||
``` | ||
|
||
### Options | ||
|
||
``` | ||
-h, --help Help for "stackit load-balancer observability-credentials cleanup" | ||
``` | ||
|
||
### Options inherited from parent commands | ||
|
||
``` | ||
-y, --assume-yes If set, skips all confirmation prompts | ||
--async If set, runs the command asynchronously | ||
-o, --output-format string Output format, one of ["json" "pretty" "none"] | ||
-p, --project-id string Project ID | ||
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info") | ||
``` | ||
|
||
### SEE ALSO | ||
|
||
* [stackit load-balancer observability-credentials](./stackit_load-balancer_observability-credentials.md) - Provides functionality for Load Balancer observability credentials | ||
|
140 changes: 140 additions & 0 deletions
140
internal/cmd/load-balancer/observability-credentials/cleanup/cleanup.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
package cleanup | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/stackitcloud/stackit-cli/internal/pkg/args" | ||
"github.com/stackitcloud/stackit-cli/internal/pkg/errors" | ||
"github.com/stackitcloud/stackit-cli/internal/pkg/examples" | ||
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" | ||
"github.com/stackitcloud/stackit-cli/internal/pkg/print" | ||
"github.com/stackitcloud/stackit-cli/internal/pkg/projectname" | ||
"github.com/stackitcloud/stackit-cli/internal/pkg/services/load-balancer/client" | ||
"github.com/stackitcloud/stackit-cli/internal/pkg/services/load-balancer/utils" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/stackitcloud/stackit-sdk-go/services/loadbalancer" | ||
) | ||
|
||
type inputModel struct { | ||
*globalflags.GlobalFlagModel | ||
} | ||
|
||
func NewCmd(p *print.Printer) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "cleanup", | ||
Short: "Deletes observability credentials unused by any Load Balancer", | ||
Long: "Deletes observability credentials unused by any Load Balancer.", | ||
Args: args.NoArgs, | ||
Example: examples.Build( | ||
examples.NewExample( | ||
`Delete observability credentials unused by any Load Balancer`, | ||
"$ stackit load-balancer observability-credentials cleanup"), | ||
), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
ctx := context.Background() | ||
model, err := parseInput(p, cmd) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Configure API client | ||
apiClient, err := client.ConfigureClient(p) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
projectLabel, err := projectname.GetProjectName(ctx, p, cmd) | ||
if err != nil { | ||
p.Debug(print.ErrorLevel, "get project name: %v", err) | ||
projectLabel = model.ProjectId | ||
} | ||
|
||
listReq := buildListCredentialsRequest(ctx, model, apiClient) | ||
resp, err := listReq.Execute() | ||
if err != nil { | ||
return fmt.Errorf("list Load Balancer observability credentials: %w", err) | ||
} | ||
|
||
var credentials []loadbalancer.CredentialsResponse | ||
if resp.Credentials != nil && len(*resp.Credentials) > 0 { | ||
credentials, err = utils.FilterCredentials(ctx, apiClient, *resp.Credentials, model.ProjectId, utils.OP_FILTER_UNUSED) | ||
if err != nil { | ||
return fmt.Errorf("filter Load Balancer observability credentials: %w", err) | ||
} | ||
} | ||
|
||
if len(credentials) == 0 { | ||
p.Info("No unused observability credentials found on project %q\n", projectLabel) | ||
return nil | ||
} | ||
|
||
if !model.AssumeYes { | ||
prompt := "Will delete the following unused observability credentials: \n" | ||
for _, credential := range credentials { | ||
if credential.DisplayName == nil || credential.Username == nil { | ||
return fmt.Errorf("list unused Load Balancer observability credentials: credentials %q missing display name or username", *credential.CredentialsRef) | ||
} | ||
name := *credential.DisplayName | ||
username := *credential.Username | ||
prompt += fmt.Sprintf(" - %s (username: %q)\n", name, username) | ||
} | ||
prompt += fmt.Sprintf("Are you sure you want to delete unused observability credentials on project %q? (This cannot be undone)", projectLabel) | ||
err = p.PromptForConfirmation(prompt) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
for _, credential := range credentials { | ||
if credential.CredentialsRef == nil { | ||
return fmt.Errorf("delete Load Balancer observability credentials: missing credentials reference") | ||
} | ||
credentialsRef := *credential.CredentialsRef | ||
// Call API | ||
req := buildDeleteCredentialRequest(ctx, model, apiClient, credentialsRef) | ||
_, err = req.Execute() | ||
if err != nil { | ||
return fmt.Errorf("delete Load Balancer observability credentials: %w", err) | ||
} | ||
} | ||
|
||
p.Info("Deleted unused Load Balancer observability credentials on project %q\n", projectLabel) | ||
return nil | ||
}, | ||
} | ||
return cmd | ||
} | ||
|
||
func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) { | ||
globalFlags := globalflags.Parse(p, cmd) | ||
if globalFlags.ProjectId == "" { | ||
return nil, &errors.ProjectIdError{} | ||
} | ||
|
||
model := inputModel{ | ||
GlobalFlagModel: globalFlags, | ||
} | ||
|
||
if p.IsVerbosityDebug() { | ||
modelStr, err := print.BuildDebugStrFromInputModel(model) | ||
if err != nil { | ||
p.Debug(print.ErrorLevel, "convert model to string for debugging: %v", err) | ||
} else { | ||
p.Debug(print.DebugLevel, "parsed input values: %s", modelStr) | ||
} | ||
} | ||
|
||
return &model, nil | ||
} | ||
|
||
func buildDeleteCredentialRequest(ctx context.Context, model *inputModel, apiClient *loadbalancer.APIClient, credentialsRef string) loadbalancer.ApiDeleteCredentialsRequest { | ||
req := apiClient.DeleteCredentials(ctx, model.ProjectId, credentialsRef) | ||
return req | ||
} | ||
|
||
func buildListCredentialsRequest(ctx context.Context, model *inputModel, apiClient *loadbalancer.APIClient) loadbalancer.ApiListCredentialsRequest { | ||
req := apiClient.ListCredentials(ctx, model.ProjectId) | ||
return req | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.