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