-
Notifications
You must be signed in to change notification settings - Fork 39
Onboard Secrets Manager (ACL): update command #167
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
DiogoFerrao
merged 3 commits into
feature-secrets-manager-acls
from
df/onboard-secrets-manager-acls-update
Mar 19, 2024
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
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 secrets-manager instance update | ||
|
|
||
| Updates a Secrets Manager instance | ||
|
|
||
| ### Synopsis | ||
|
|
||
| Updates a Secrets Manager instance. | ||
|
|
||
| ``` | ||
| stackit secrets-manager instance update INSTANCE_ID [flags] | ||
| ``` | ||
|
|
||
| ### Examples | ||
|
|
||
| ``` | ||
| Update the range of IPs allowed to access a Secrets Manager instance with ID "xxx" | ||
| $ stackit secrets-manager instance update xxx --acl 1.2.3.0/24 | ||
| ``` | ||
|
|
||
| ### Options | ||
|
|
||
| ``` | ||
| --acl strings List of IP networks in CIDR notation which are allowed to access this instance (default []) | ||
| -h, --help Help for "stackit secrets-manager instance update" | ||
| ``` | ||
|
|
||
| ### 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"] | ||
| -p, --project-id string Project ID | ||
| ``` | ||
|
|
||
| ### SEE ALSO | ||
|
|
||
| * [stackit secrets-manager instance](./stackit_secrets-manager_instance.md) - Provides functionality for Secrets Manager instances | ||
|
|
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
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,124 @@ | ||
| package update | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| "github.com/stackitcloud/stackit-cli/internal/pkg/args" | ||
| "github.com/stackitcloud/stackit-cli/internal/pkg/confirm" | ||
| cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors" | ||
| "github.com/stackitcloud/stackit-cli/internal/pkg/examples" | ||
| "github.com/stackitcloud/stackit-cli/internal/pkg/flags" | ||
|
|
||
| "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" | ||
| "github.com/stackitcloud/stackit-cli/internal/pkg/services/secrets-manager/client" | ||
| secretsManagerUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/secrets-manager/utils" | ||
| "github.com/stackitcloud/stackit-cli/internal/pkg/utils" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| "github.com/stackitcloud/stackit-sdk-go/services/secretsmanager" | ||
| ) | ||
|
|
||
| const ( | ||
| instanceIdArg = "INSTANCE_ID" | ||
|
|
||
| aclFlag = "acl" | ||
| ) | ||
|
|
||
| type inputModel struct { | ||
| *globalflags.GlobalFlagModel | ||
| InstanceId string | ||
|
|
||
| Acls *[]string | ||
| } | ||
|
|
||
| func NewCmd() *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: fmt.Sprintf("update %s", instanceIdArg), | ||
| Short: "Updates a Secrets Manager instance", | ||
| Long: "Updates a Secrets Manager instance.", | ||
| Args: args.SingleArg(instanceIdArg, utils.ValidateUUID), | ||
| Example: examples.Build( | ||
| examples.NewExample( | ||
| `Update the range of IPs allowed to access a Secrets Manager instance with ID "xxx"`, | ||
| "$ stackit secrets-manager instance update xxx --acl 1.2.3.0/24"), | ||
| ), | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| ctx := context.Background() | ||
| model, err := parseInput(cmd, args) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // Configure API client | ||
| apiClient, err := client.ConfigureClient(cmd) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| instanceLabel, err := secretsManagerUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.InstanceId) | ||
| if err != nil { | ||
| instanceLabel = model.InstanceId | ||
| } | ||
|
|
||
| if !model.AssumeYes { | ||
| prompt := fmt.Sprintf("Are you sure you want to update instance %q?", instanceLabel) | ||
| err = confirm.PromptForConfirmation(cmd, prompt) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| // Call API | ||
| req := buildRequest(ctx, model, apiClient) | ||
| err = req.Execute() | ||
| if err != nil { | ||
| return fmt.Errorf("update Secrets Manager instance: %w", err) | ||
| } | ||
|
|
||
| cmd.Printf("Updated instance %q\n", instanceLabel) | ||
| return nil | ||
| }, | ||
| } | ||
| configureFlags(cmd) | ||
| return cmd | ||
| } | ||
|
|
||
| func configureFlags(cmd *cobra.Command) { | ||
| cmd.Flags().Var(flags.CIDRSliceFlag(), aclFlag, "List of IP networks in CIDR notation which are allowed to access this instance") | ||
|
DiogoFerrao marked this conversation as resolved.
|
||
| } | ||
|
|
||
| func parseInput(cmd *cobra.Command, inputArgs []string) (*inputModel, error) { | ||
| instanceId := inputArgs[0] | ||
|
|
||
| globalFlags := globalflags.Parse(cmd) | ||
| if globalFlags.ProjectId == "" { | ||
| return nil, &cliErr.ProjectIdError{} | ||
| } | ||
|
|
||
| acls := flags.FlagToStringSlicePointer(cmd, aclFlag) | ||
|
|
||
| if acls == nil { | ||
| return nil, &cliErr.EmptyUpdateError{} | ||
| } | ||
|
|
||
| return &inputModel{ | ||
| GlobalFlagModel: globalFlags, | ||
| InstanceId: instanceId, | ||
| Acls: acls, | ||
| }, nil | ||
| } | ||
|
|
||
| func buildRequest(ctx context.Context, model *inputModel, apiClient *secretsmanager.APIClient) secretsmanager.ApiUpdateACLsRequest { | ||
| req := apiClient.UpdateACLs(ctx, model.ProjectId, model.InstanceId) | ||
|
|
||
| cidrs := []secretsmanager.AclUpdate{} | ||
|
|
||
| for _, acl := range *model.Acls { | ||
| cidrs = append(cidrs, secretsmanager.AclUpdate{Cidr: utils.Ptr(acl)}) | ||
| } | ||
|
|
||
| req = req.UpdateACLsPayload(secretsmanager.UpdateACLsPayload{Cidrs: &cidrs}) | ||
|
|
||
| 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.