|
| 1 | +package create |
| 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/confirm" |
| 9 | + "github.com/stackitcloud/stackit-cli/internal/pkg/errors" |
| 10 | + "github.com/stackitcloud/stackit-cli/internal/pkg/examples" |
| 11 | + "github.com/stackitcloud/stackit-cli/internal/pkg/flags" |
| 12 | + "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" |
| 13 | + "github.com/stackitcloud/stackit-cli/internal/pkg/services/secrets-manager/client" |
| 14 | + secretsManagerUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/secrets-manager/utils" |
| 15 | + "github.com/stackitcloud/stackit-cli/internal/pkg/utils" |
| 16 | + |
| 17 | + "github.com/spf13/cobra" |
| 18 | + "github.com/stackitcloud/stackit-sdk-go/services/secretsmanager" |
| 19 | +) |
| 20 | + |
| 21 | +const ( |
| 22 | + instanceIdFlag = "instance-id" |
| 23 | + descriptionFlag = "description" |
| 24 | + writeFlag = "write" |
| 25 | + hidePasswordFlag = "hide-password" |
| 26 | +) |
| 27 | + |
| 28 | +type inputModel struct { |
| 29 | + *globalflags.GlobalFlagModel |
| 30 | + |
| 31 | + InstanceId string |
| 32 | + Description *string |
| 33 | + Write *bool |
| 34 | + HidePassword bool |
| 35 | +} |
| 36 | + |
| 37 | +func NewCmd() *cobra.Command { |
| 38 | + cmd := &cobra.Command{ |
| 39 | + Use: "create", |
| 40 | + Short: "Creates a Secrets Manager user", |
| 41 | + Long: fmt.Sprintf("%s\n%s\n%s", |
| 42 | + "Creates a Secrets Manager user.", |
| 43 | + "The username and password are auto-generated and provided upon creation.", |
| 44 | + "A description can be provided to identify a user.", |
| 45 | + ), |
| 46 | + Example: examples.Build( |
| 47 | + examples.NewExample( |
| 48 | + `Create a Secrets Manager user for instance with ID "xxx" and description "yyy"`, |
| 49 | + "$ stackit secrets-manager user create --instance-id xxx --description yyy"), |
| 50 | + examples.NewExample( |
| 51 | + `Create a Secrets Manager user for instance with ID "xxx" and hides the generated password`, |
| 52 | + "$ stackit secrets-manager user create --instance-id xxx --hide-password"), |
| 53 | + examples.NewExample( |
| 54 | + `Create a Secrets Manager user for instance with ID "xxx" with write access to the secrets engine`, |
| 55 | + "$ stackit secrets-manager user create --instance-id xxx --write"), |
| 56 | + ), |
| 57 | + Args: args.NoArgs, |
| 58 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 59 | + ctx := context.Background() |
| 60 | + model, err := parseInput(cmd) |
| 61 | + if err != nil { |
| 62 | + return err |
| 63 | + } |
| 64 | + |
| 65 | + // Configure API client |
| 66 | + apiClient, err := client.ConfigureClient(cmd) |
| 67 | + if err != nil { |
| 68 | + return err |
| 69 | + } |
| 70 | + |
| 71 | + instanceLabel, err := secretsManagerUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.InstanceId) |
| 72 | + if err != nil { |
| 73 | + instanceLabel = model.InstanceId |
| 74 | + } |
| 75 | + |
| 76 | + if !model.AssumeYes { |
| 77 | + prompt := fmt.Sprintf("Are you sure you want to create a user for instance %q?", instanceLabel) |
| 78 | + err = confirm.PromptForConfirmation(cmd, prompt) |
| 79 | + if err != nil { |
| 80 | + return err |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + // Call API |
| 85 | + req := buildRequest(ctx, model, apiClient) |
| 86 | + resp, err := req.Execute() |
| 87 | + if err != nil { |
| 88 | + return fmt.Errorf("create Secrets Manager user: %w", err) |
| 89 | + } |
| 90 | + |
| 91 | + cmd.Printf("Created user for instance %q. User ID: %s\n\n", instanceLabel, *resp.Id) |
| 92 | + cmd.Printf("Username: %s\n", *resp.Username) |
| 93 | + if model.HidePassword { |
| 94 | + cmd.Printf("Password: <hidden>\n") |
| 95 | + } else { |
| 96 | + cmd.Printf("Password: %s\n", *resp.Password) |
| 97 | + } |
| 98 | + cmd.Printf("Description: %s\n", *resp.Description) |
| 99 | + cmd.Printf("Write Access: %t\n", *resp.Write) |
| 100 | + |
| 101 | + return nil |
| 102 | + }, |
| 103 | + } |
| 104 | + |
| 105 | + configureFlags(cmd) |
| 106 | + return cmd |
| 107 | +} |
| 108 | + |
| 109 | +func configureFlags(cmd *cobra.Command) { |
| 110 | + cmd.Flags().Var(flags.UUIDFlag(), instanceIdFlag, "ID of the instance") |
| 111 | + cmd.Flags().String(descriptionFlag, "", "A user chosen description to differentiate between multiple users") |
| 112 | + cmd.Flags().Bool(writeFlag, false, "User write access to the secrets engine. If unset, user is read-only") |
| 113 | + cmd.Flags().Bool(hidePasswordFlag, false, "Hide password in output") |
| 114 | + |
| 115 | + err := flags.MarkFlagsRequired(cmd, instanceIdFlag) |
| 116 | + cobra.CheckErr(err) |
| 117 | +} |
| 118 | + |
| 119 | +func parseInput(cmd *cobra.Command) (*inputModel, error) { |
| 120 | + globalFlags := globalflags.Parse(cmd) |
| 121 | + if globalFlags.ProjectId == "" { |
| 122 | + return nil, &errors.ProjectIdError{} |
| 123 | + } |
| 124 | + |
| 125 | + return &inputModel{ |
| 126 | + GlobalFlagModel: globalFlags, |
| 127 | + InstanceId: flags.FlagToStringValue(cmd, instanceIdFlag), |
| 128 | + Description: utils.Ptr(flags.FlagToStringValue(cmd, descriptionFlag)), |
| 129 | + Write: utils.Ptr(flags.FlagToBoolValue(cmd, writeFlag)), |
| 130 | + HidePassword: flags.FlagToBoolValue(cmd, hidePasswordFlag), |
| 131 | + }, nil |
| 132 | +} |
| 133 | + |
| 134 | +func buildRequest(ctx context.Context, model *inputModel, apiClient *secretsmanager.APIClient) secretsmanager.ApiCreateUserRequest { |
| 135 | + req := apiClient.CreateUser(ctx, model.ProjectId, model.InstanceId) |
| 136 | + req = req.CreateUserPayload(secretsmanager.CreateUserPayload{ |
| 137 | + Description: model.Description, |
| 138 | + Write: model.Write, |
| 139 | + }) |
| 140 | + return req |
| 141 | +} |
0 commit comments