|
| 1 | +package create |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/stackitcloud/stackit-cli/internal/pkg/args" |
| 8 | + cliErr "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/flags" |
| 11 | + "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" |
| 12 | + "github.com/stackitcloud/stackit-cli/internal/pkg/print" |
| 13 | + "github.com/stackitcloud/stackit-cli/internal/pkg/services/argus/client" |
| 14 | + argusUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/argus/utils" |
| 15 | + |
| 16 | + "github.com/spf13/cobra" |
| 17 | + "github.com/stackitcloud/stackit-sdk-go/services/argus" |
| 18 | +) |
| 19 | + |
| 20 | +const ( |
| 21 | + instanceIdFlag = "instance-id" |
| 22 | + hidePasswordFlag = "hide-password" |
| 23 | +) |
| 24 | + |
| 25 | +type inputModel struct { |
| 26 | + *globalflags.GlobalFlagModel |
| 27 | + |
| 28 | + HidePassword bool |
| 29 | + InstanceId string |
| 30 | +} |
| 31 | + |
| 32 | +func NewCmd(p *print.Printer) *cobra.Command { |
| 33 | + cmd := &cobra.Command{ |
| 34 | + Use: "create", |
| 35 | + Short: "Creates credentials for an Argus instance.", |
| 36 | + Long: "Creates credentials for an Argus instance.", |
| 37 | + Args: args.NoArgs, |
| 38 | + Example: examples.Build( |
| 39 | + examples.NewExample( |
| 40 | + `Create credentials for Argus instance with ID "xxx"`, |
| 41 | + "$ stackit argus credentials create --instance-id xxx"), |
| 42 | + examples.NewExample( |
| 43 | + `Create credentials for Argus instance with ID "xxx" and hide the password in the output`, |
| 44 | + "$ stackit argus credentials create --instance-id xxx --hide-password"), |
| 45 | + ), |
| 46 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 47 | + ctx := context.Background() |
| 48 | + model, err := parseInput(cmd) |
| 49 | + if err != nil { |
| 50 | + return err |
| 51 | + } |
| 52 | + |
| 53 | + // Configure API client |
| 54 | + apiClient, err := client.ConfigureClient(p) |
| 55 | + if err != nil { |
| 56 | + return err |
| 57 | + } |
| 58 | + |
| 59 | + instanceLabel, err := argusUtils.GetInstanceName(ctx, apiClient, model.InstanceId, model.ProjectId) |
| 60 | + if err != nil { |
| 61 | + instanceLabel = model.InstanceId |
| 62 | + } |
| 63 | + |
| 64 | + if !model.AssumeYes { |
| 65 | + prompt := fmt.Sprintf("Are you sure you want to create credentials for instance %q?", instanceLabel) |
| 66 | + err = p.PromptForConfirmation(prompt) |
| 67 | + if err != nil { |
| 68 | + return err |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + // Call API |
| 73 | + req := buildRequest(ctx, model, apiClient) |
| 74 | + if err != nil { |
| 75 | + return err |
| 76 | + } |
| 77 | + resp, err := req.Execute() |
| 78 | + if err != nil { |
| 79 | + return fmt.Errorf("create credentials for Argus instance: %w", err) |
| 80 | + } |
| 81 | + |
| 82 | + p.Outputf("Created credentials for instance %q.\n\n", instanceLabel) |
| 83 | + // The username field cannot be set by the user so we only display it if it's not returned empty |
| 84 | + username := *resp.Credentials.Username |
| 85 | + if username != "" { |
| 86 | + p.Outputf("Username: %s\n", username) |
| 87 | + } |
| 88 | + if model.HidePassword { |
| 89 | + p.Outputf("Password: <hidden>\n") |
| 90 | + } else { |
| 91 | + p.Outputf("Password: %s\n", *resp.Credentials.Password) |
| 92 | + } |
| 93 | + return nil |
| 94 | + }, |
| 95 | + } |
| 96 | + configureFlags(cmd) |
| 97 | + return cmd |
| 98 | +} |
| 99 | + |
| 100 | +func configureFlags(cmd *cobra.Command) { |
| 101 | + cmd.Flags().Var(flags.UUIDFlag(), instanceIdFlag, "Instance ID") |
| 102 | + cmd.Flags().Bool(hidePasswordFlag, false, "Hide password in output") |
| 103 | + |
| 104 | + err := flags.MarkFlagsRequired(cmd, instanceIdFlag) |
| 105 | + cobra.CheckErr(err) |
| 106 | +} |
| 107 | + |
| 108 | +func parseInput(cmd *cobra.Command) (*inputModel, error) { |
| 109 | + globalFlags := globalflags.Parse(cmd) |
| 110 | + if globalFlags.ProjectId == "" { |
| 111 | + return nil, &cliErr.ProjectIdError{} |
| 112 | + } |
| 113 | + |
| 114 | + return &inputModel{ |
| 115 | + GlobalFlagModel: globalFlags, |
| 116 | + InstanceId: flags.FlagToStringValue(cmd, instanceIdFlag), |
| 117 | + HidePassword: flags.FlagToBoolValue(cmd, hidePasswordFlag), |
| 118 | + }, nil |
| 119 | +} |
| 120 | + |
| 121 | +func buildRequest(ctx context.Context, model *inputModel, apiClient *argus.APIClient) argus.ApiCreateCredentialsRequest { |
| 122 | + req := apiClient.CreateCredentials(ctx, model.InstanceId, model.ProjectId) |
| 123 | + return req |
| 124 | +} |
0 commit comments