forked from stackitcloud/stackit-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate.go
More file actions
169 lines (146 loc) · 5.73 KB
/
create.go
File metadata and controls
169 lines (146 loc) · 5.73 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package create
import (
"context"
"fmt"
"github.com/spf13/cobra"
"github.com/stackitcloud/stackit-sdk-go/services/intake"
"github.com/stackitcloud/stackit-sdk-go/services/intake/wait"
"github.com/stackitcloud/stackit-cli/internal/cmd/params"
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
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/print"
"github.com/stackitcloud/stackit-cli/internal/pkg/services/intake/client"
"github.com/stackitcloud/stackit-cli/internal/pkg/spinner"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
)
const (
intakeIdFlag = "intake-id"
displayNameFlag = "display-name"
passwordFlag = "password"
descriptionFlag = "description"
typeFlag = "type"
labelsFlag = "labels"
)
// inputModel struct holds all the input parameters for the command
type inputModel struct {
*globalflags.GlobalFlagModel
IntakeId *string
DisplayName *string
Password *string
Description *string
Type *string
Labels *map[string]string
}
func NewCmd(p *params.CmdParams) *cobra.Command {
cmd := &cobra.Command{
Use: "create",
Short: "Creates a new Intake User",
Long: "Creates a new Intake User, providing secure access credentials for applications to connect to a data stream.",
Args: args.NoArgs,
Example: examples.Build(
examples.NewExample(
`Create a new Intake User with a display name and password for a specific Intake`,
`$ stackit beta intake user create --intake-id xxx --display-name my-intake-user --password "my-secret-password"`),
examples.NewExample(
`Create a new dead-letter queue user with a description and labels`,
`$ stackit beta intake user create --intake-id xxx --display-name my-dlq-reader --password "another-secret" --type "dead-letter" --description "User for reading undelivered messages" --labels "owner=team-alpha,scope=dlq"`),
),
RunE: func(cmd *cobra.Command, _ []string) error {
ctx := context.Background()
model, err := parseInput(p.Printer, cmd)
if err != nil {
return err
}
// Configure API client
apiClient, err := client.ConfigureClient(p.Printer, p.CliVersion)
if err != nil {
return err
}
if !model.AssumeYes {
prompt := fmt.Sprintf("Are you sure you want to create a new User for Intake %q?", *model.IntakeId)
err = p.Printer.PromptForConfirmation(prompt)
if err != nil {
return err
}
}
// Call API
req := buildRequest(ctx, model, apiClient)
resp, err := req.Execute()
if err != nil {
return fmt.Errorf("create Intake User: %w", err)
}
// Wait for async operation, if async mode not enabled
if !model.Async {
s := spinner.New(p.Printer)
s.Start("Creating STACKIT Intake User instance")
_, err = wait.CreateOrUpdateIntakeUserWaitHandler(ctx, apiClient, model.ProjectId, model.Region, *model.IntakeId, resp.GetId()).WaitWithContext(ctx)
if err != nil {
return fmt.Errorf("wait for STACKIT Instance creation: %w", err)
}
s.Stop()
}
return outputResult(p.Printer, model, *model.IntakeId, resp)
},
}
configureFlags(cmd)
return cmd
}
func configureFlags(cmd *cobra.Command) {
cmd.Flags().Var(flags.UUIDFlag(), intakeIdFlag, "ID of the Intake to which the user belongs")
cmd.Flags().String(displayNameFlag, "", "Display name")
cmd.Flags().String(passwordFlag, "", "User password")
cmd.Flags().String(descriptionFlag, "", "Description")
cmd.Flags().String(typeFlag, "", "Type of user, 'intake' for writing to the stream or 'dead-letter' for reading from the dead-letter queue")
cmd.Flags().StringToString(labelsFlag, nil, "Labels in key=value format, separated by commas. Example: --labels \"key1=value1,key2=value2\"")
err := flags.MarkFlagsRequired(cmd, intakeIdFlag, displayNameFlag, passwordFlag)
cobra.CheckErr(err)
}
func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) {
globalFlags := globalflags.Parse(p, cmd)
if globalFlags.ProjectId == "" {
return nil, &cliErr.ProjectIdError{}
}
model := inputModel{
GlobalFlagModel: globalFlags,
IntakeId: flags.FlagToStringPointer(p, cmd, intakeIdFlag),
DisplayName: flags.FlagToStringPointer(p, cmd, displayNameFlag),
Password: flags.FlagToStringPointer(p, cmd, passwordFlag),
Description: flags.FlagToStringPointer(p, cmd, descriptionFlag),
Type: flags.FlagToStringPointer(p, cmd, typeFlag),
Labels: flags.FlagToStringToStringPointer(p, cmd, labelsFlag),
}
p.DebugInputModel(model)
return &model, nil
}
func buildRequest(ctx context.Context, model *inputModel, apiClient *intake.APIClient) intake.ApiCreateIntakeUserRequest {
req := apiClient.CreateIntakeUser(ctx, model.ProjectId, model.Region, *model.IntakeId)
// Build main payload
payload := intake.CreateIntakeUserPayload{
DisplayName: model.DisplayName,
Password: model.Password,
Description: model.Description,
Labels: model.Labels,
}
if model.Type != nil {
payload.Type = (*intake.UserType)(model.Type)
}
req = req.CreateIntakeUserPayload(payload)
return req
}
func outputResult(p *print.Printer, model *inputModel, intakeId string, resp *intake.IntakeUserResponse) error {
return p.OutputResult(model.OutputFormat, resp, func() error {
if resp == nil {
p.Outputf("Created Intake User for Intake %q, but no intake ID was returned.\n", intakeId)
return nil
}
operationState := "Created"
if model.Async {
operationState = "Triggered creation of"
}
p.Outputf("%s Intake User for Intake %q. User ID: %s\n", operationState, intakeId, utils.PtrString(resp.Id))
return nil
})
}