-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcreate.go
237 lines (208 loc) · 7.68 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package create
import (
"context"
"encoding/json"
"fmt"
"regexp"
"github.com/goccy/go-yaml"
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
"github.com/stackitcloud/stackit-cli/internal/pkg/auth"
"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/resourcemanager/client"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
"github.com/spf13/cobra"
"github.com/stackitcloud/stackit-sdk-go/services/resourcemanager"
)
const (
parentIdFlag = "parent-id"
nameFlag = "name"
labelFlag = "label"
networkAreaIdFlag = "network-area-id"
ownerRole = "project.owner"
labelKeyRegex = `[A-ZÄÜÖa-zäüöß0-9_-]{1,64}`
labelValueRegex = `^$|[A-ZÄÜÖa-zäüöß0-9_-]{1,64}`
networkAreaLabel = "networkArea"
)
type inputModel struct {
*globalflags.GlobalFlagModel
ParentId *string
Name *string
Labels *map[string]string
NetworkAreaId *string
}
func NewCmd(p *print.Printer) *cobra.Command {
cmd := &cobra.Command{
Use: "create",
Short: "Creates a STACKIT project",
Long: fmt.Sprintf("%s\n%s\n%s\n%s\n%s\n",
"Creates a STACKIT project.",
"You can associate a project with a STACKIT Network Area (SNA) by providing the ID of the SNA.",
"The STACKIT Network Area (SNA) allows projects within an organization to be connected to each other on a network level.",
"This makes it possible to connect various resources of the projects within an SNA and also simplifies the connection with on-prem environments (hybrid cloud).",
"The network type can no longer be changed after the project has been created. If you require a different network type, you must create a new project.",
),
Args: args.NoArgs,
Example: examples.Build(
examples.NewExample(
`Create a STACKIT project`,
"$ stackit project create --parent-id xxxx --name my-project"),
examples.NewExample(
`Create a STACKIT project with a set of labels`,
"$ stackit project create --parent-id xxxx --name my-project --label key=value --label foo=bar"),
examples.NewExample(
`Create a STACKIT project with a network area`,
"$ stackit project create --parent-id xxxx --name my-project --network-area-id yyyy"),
),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
model, err := parseInput(p, cmd)
if err != nil {
return err
}
// Configure API client
apiClient, err := client.ConfigureClient(p)
if err != nil {
return err
}
if !model.AssumeYes {
prompt := fmt.Sprintf("Are you sure you want to create a project under the parent with ID %q?", *model.ParentId)
err = p.PromptForConfirmation(prompt)
if err != nil {
return err
}
}
// Call API
req, err := buildRequest(ctx, model, apiClient)
if err != nil {
return fmt.Errorf("build project creation request: %w", err)
}
resp, err := req.Execute()
if err != nil {
return fmt.Errorf("create project: %w", err)
}
return outputResult(p, model, resp)
},
}
configureFlags(cmd)
return cmd
}
func configureFlags(cmd *cobra.Command) {
cmd.Flags().String(parentIdFlag, "", "Parent resource identifier. Both container ID (user-friendly) and UUID are supported")
cmd.Flags().String(nameFlag, "", "Project name")
cmd.Flags().StringToString(labelFlag, nil, "Labels are key-value string pairs which can be attached to a project. A label can be provided with the format key=value and the flag can be used multiple times to provide a list of labels")
cmd.Flags().Var(flags.UUIDFlag(), networkAreaIdFlag, "ID of a STACKIT Network Area (SNA) to associate with the project.")
err := flags.MarkFlagsRequired(cmd, parentIdFlag, nameFlag)
cobra.CheckErr(err)
}
func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) {
globalFlags := globalflags.Parse(p, cmd)
labels := flags.FlagToStringToStringPointer(p, cmd, labelFlag)
if labels != nil {
labelKeyRegex := regexp.MustCompile(labelKeyRegex)
labelValueRegex := regexp.MustCompile(labelValueRegex)
for key, value := range *labels {
if !labelKeyRegex.MatchString(key) {
return nil, &errors.FlagValidationError{
Flag: labelFlag,
Details: fmt.Sprintf("label key %s didn't match the required regex expression %s", key, labelKeyRegex),
}
}
if !labelValueRegex.MatchString(value) {
return nil, &errors.FlagValidationError{
Flag: labelFlag,
Details: fmt.Sprintf("label value %s for key %s didn't match the required regex expression %s", value, key, labelValueRegex),
}
}
}
}
model := inputModel{
GlobalFlagModel: globalFlags,
ParentId: flags.FlagToStringPointer(p, cmd, parentIdFlag),
Name: flags.FlagToStringPointer(p, cmd, nameFlag),
Labels: labels,
NetworkAreaId: flags.FlagToStringPointer(p, cmd, networkAreaIdFlag),
}
if p.IsVerbosityDebug() {
modelStr, err := print.BuildDebugStrFromInputModel(model)
if err != nil {
p.Debug(print.ErrorLevel, "convert model to string for debugging: %v", err)
} else {
p.Debug(print.DebugLevel, "parsed input values: %s", modelStr)
}
}
return &model, nil
}
func buildRequest(ctx context.Context, model *inputModel, apiClient *resourcemanager.APIClient) (resourcemanager.ApiCreateProjectRequest, error) {
req := apiClient.CreateProject(ctx)
authFlow, err := auth.GetAuthFlow()
if err != nil {
return req, fmt.Errorf("get authentication flow: %w", err)
}
var email string
switch authFlow {
case auth.AUTH_FLOW_SERVICE_ACCOUNT_TOKEN:
email, err = auth.GetAuthField(auth.SERVICE_ACCOUNT_EMAIL)
if err != nil {
return req, fmt.Errorf("get email of the service account that was used to authenticate: %w", err)
}
case auth.AUTH_FLOW_SERVICE_ACCOUNT_KEY:
email, err = auth.GetAuthField(auth.SERVICE_ACCOUNT_EMAIL)
if err != nil {
return req, fmt.Errorf("get email of the service account that was used to authenticate: %w", err)
}
case auth.AUTH_FLOW_USER_TOKEN:
email, err = auth.GetAuthField(auth.USER_EMAIL)
if err != nil {
return req, fmt.Errorf("get your user email from configuration: %w", err)
}
default:
return req, fmt.Errorf("the configured authentication flow (%s) is not supported, please report this issue", authFlow)
}
if email == "" {
return req, fmt.Errorf("the authenticated subject email cannot be empty, please report this issue")
}
labels := model.Labels
if model.NetworkAreaId != nil {
if labels == nil {
labels = &map[string]string{}
}
(*labels)[networkAreaLabel] = *model.NetworkAreaId
}
req = req.CreateProjectPayload(resourcemanager.CreateProjectPayload{
ContainerParentId: model.ParentId,
Name: model.Name,
Labels: labels,
Members: &[]resourcemanager.Member{
{
Role: utils.Ptr(ownerRole),
Subject: utils.Ptr(email),
},
},
})
return req, nil
}
func outputResult(p *print.Printer, model *inputModel, resp *resourcemanager.Project) error {
switch model.OutputFormat {
case print.JSONOutputFormat:
details, err := json.MarshalIndent(resp, "", " ")
if err != nil {
return fmt.Errorf("marshal project: %w", err)
}
p.Outputln(string(details))
return nil
case print.YAMLOutputFormat:
details, err := yaml.MarshalWithOptions(resp, yaml.IndentSequence(true))
if err != nil {
return fmt.Errorf("marshal project: %w", err)
}
p.Outputln(string(details))
return nil
default:
p.Outputf("Created project under the parent with ID %q. Project ID: %s\n", *model.ParentId, *resp.ProjectId)
return nil
}
}