-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathcreate.go
More file actions
349 lines (312 loc) · 14.6 KB
/
create.go
File metadata and controls
349 lines (312 loc) · 14.6 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
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package create
import (
"context"
"fmt"
"github.com/stackitcloud/stackit-cli/internal/pkg/types"
"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/projectname"
"github.com/stackitcloud/stackit-cli/internal/pkg/services/iaas/client"
"github.com/stackitcloud/stackit-cli/internal/pkg/spinner"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
"github.com/stackitcloud/stackit-sdk-go/services/iaas"
"github.com/stackitcloud/stackit-sdk-go/services/iaas/wait"
"github.com/spf13/cobra"
)
const (
nameFlag = "name"
machineTypeFlag = "machine-type"
affinityGroupFlag = "affinity-group"
availabilityZoneFlag = "availability-zone"
agentProvisionedFlag = "agent-provisioned"
bootVolumeSourceIdFlag = "boot-volume-source-id"
bootVolumeSourceTypeFlag = "boot-volume-source-type"
bootVolumeSizeFlag = "boot-volume-size"
bootVolumePerformanceClassFlag = "boot-volume-performance-class"
bootVolumeDeleteOnTerminationFlag = "boot-volume-delete-on-termination"
imageIdFlag = "image-id"
keypairNameFlag = "keypair-name"
labelFlag = "labels"
networkIdFlag = "network-id"
networkInterfaceIdsFlag = "network-interface-ids"
securityGroupsFlag = "security-groups"
serviceAccountEmailsFlag = "service-account-emails"
userDataFlag = "user-data"
volumesFlag = "volumes"
)
type inputModel struct {
*globalflags.GlobalFlagModel
Name *string
MachineType *string
AffinityGroup *string
AvailabilityZone *string
AgentProvisioned *bool
BootVolumeSourceId *string
BootVolumeSourceType *string
BootVolumeSize *int64
BootVolumePerformanceClass *string
BootVolumeDeleteOnTermination *bool
ImageId *string
KeypairName *string
Labels *map[string]string
NetworkId *string
NetworkInterfaceIds *[]string
SecurityGroups *[]string
ServiceAccountMails *[]string
UserData *string
Volumes *[]string
}
func NewCmd(params *types.CmdParams) *cobra.Command {
cmd := &cobra.Command{
Use: "create",
Short: "Creates a server",
Long: "Creates a server.",
Args: args.NoArgs,
Example: examples.Build(
examples.NewExample(
`Create a server from an image with id xxx`,
`$ stackit server create --machine-type t1.1 --name server1 --image-id xxx`,
),
examples.NewExample(
`Create a server with labels from an image with id xxx`,
`$ stackit server create --machine-type t1.1 --name server1 --image-id xxx --labels key=value,foo=bar`,
),
examples.NewExample(
`Create a server with a boot volume`,
`$ stackit server create --machine-type t1.1 --name server1 --boot-volume-source-id xxx --boot-volume-source-type image --boot-volume-size 64`,
),
examples.NewExample(
`Create a server with a boot volume from an existing volume`,
`$ stackit server create --machine-type t1.1 --name server1 --boot-volume-source-id xxx --boot-volume-source-type volume`,
),
examples.NewExample(
`Create a server with a keypair`,
`$ stackit server create --machine-type t1.1 --name server1 --image-id xxx --keypair-name example`,
),
examples.NewExample(
`Create a server with a network`,
`$ stackit server create --machine-type t1.1 --name server1 --image-id xxx --network-id yyy`,
),
examples.NewExample(
`Create a server with a network interface`,
`$ stackit server create --machine-type t1.1 --name server1 --boot-volume-source-id xxx --boot-volume-source-type image --boot-volume-size 64 --network-interface-ids yyy`,
),
examples.NewExample(
`Create a server with an attached volume`,
`$ stackit server create --machine-type t1.1 --name server1 --boot-volume-source-id xxx --boot-volume-source-type image --boot-volume-size 64 --volumes yyy`,
),
examples.NewExample(
`Create a server with user data (cloud-init)`,
`$ stackit server create --machine-type t1.1 --name server1 --boot-volume-source-id xxx --boot-volume-source-type image --boot-volume-size 64 --user-data @path/to/file.yaml`,
),
examples.NewExample(
`Create a server with provisioned agent`,
`$ stackit server create --machine-type t1.1 --name server1 --boot-volume-source-id xxx --boot-volume-source-type image --boot-volume-size 64 --network-id yyy --agent-provisioned=true`,
),
),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
model, err := parseInput(params.Printer, cmd, args)
if err != nil {
return err
}
// Configure API client
apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion)
if err != nil {
return err
}
projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd)
if err != nil {
params.Printer.Debug(print.ErrorLevel, "get project name: %v", err)
projectLabel = model.ProjectId
}
if !model.AssumeYes {
prompt := fmt.Sprintf("Are you sure you want to create a server for project %q?", projectLabel)
err = params.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 server : %w", err)
}
serverId := *resp.Id
// Wait for async operation, if async mode not enabled
if !model.Async {
s := spinner.New(params.Printer)
s.Start("Creating server")
_, err = wait.CreateServerWaitHandler(ctx, apiClient, model.ProjectId, model.Region, serverId).WaitWithContext(ctx)
if err != nil {
return fmt.Errorf("wait for server creation: %w", err)
}
s.Stop()
}
return outputResult(params.Printer, model.OutputFormat, projectLabel, resp)
},
}
configureFlags(cmd)
return cmd
}
func configureFlags(cmd *cobra.Command) {
cmd.Flags().StringP(nameFlag, "n", "", "Server name")
cmd.Flags().String(machineTypeFlag, "", "Name of the type of the machine for the server. Possible values are documented in https://docs.stackit.cloud/products/compute-engine/server/basics/machine-types/")
cmd.Flags().String(affinityGroupFlag, "", "The affinity group the server is assigned to")
cmd.Flags().String(availabilityZoneFlag, "", "The availability zone of the server")
cmd.Flags().Bool(agentProvisionedFlag, false, "Whether to provision an agent on server creation")
cmd.Flags().String(bootVolumeSourceIdFlag, "", "ID of the source object of boot volume. It can be either an image or volume ID")
cmd.Flags().String(bootVolumeSourceTypeFlag, "", "Type of the source object of boot volume. It can be either 'image' or 'volume'")
cmd.Flags().Int64(bootVolumeSizeFlag, 0, "The size of the boot volume in GB. Must be provided when 'boot-volume-source-type' is 'image'")
cmd.Flags().String(bootVolumePerformanceClassFlag, "", "Boot volume performance class")
cmd.Flags().Bool(bootVolumeDeleteOnTerminationFlag, false, "Delete the volume during the termination of the server. Defaults to false")
cmd.Flags().String(imageIdFlag, "", "The image ID to be used for an ephemeral disk on the server. Either 'image-id' or 'boot-volume-...' flags are required")
cmd.Flags().String(keypairNameFlag, "", "The name of the SSH keypair used during the server creation")
cmd.Flags().StringToString(labelFlag, nil, "Labels are key-value string pairs which can be attached to a server. E.g. '--labels key1=value1,key2=value2,...'")
cmd.Flags().String(networkIdFlag, "", "ID of the network for the initial networking setup for the server creation")
cmd.Flags().StringSlice(networkInterfaceIdsFlag, []string{}, "List of network interface IDs for the initial networking setup for the server creation")
cmd.Flags().StringSlice(securityGroupsFlag, []string{}, "The initial security groups for the server creation")
cmd.Flags().StringSlice(serviceAccountEmailsFlag, []string{}, "List of the service account mails")
cmd.Flags().Var(flags.ReadFromFileFlag(), userDataFlag, "User data that is passed via cloud-init to the server")
cmd.Flags().StringSlice(volumesFlag, []string{}, "The list of volumes attached to the server")
err := flags.MarkFlagsRequired(cmd, nameFlag, machineTypeFlag)
cmd.MarkFlagsMutuallyExclusive(imageIdFlag, bootVolumeSourceIdFlag)
cmd.MarkFlagsMutuallyExclusive(imageIdFlag, bootVolumeSourceTypeFlag)
cmd.MarkFlagsMutuallyExclusive(networkIdFlag, networkInterfaceIdsFlag)
cmd.MarkFlagsOneRequired(networkIdFlag, networkInterfaceIdsFlag)
cobra.CheckErr(err)
}
func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel, error) {
globalFlags := globalflags.Parse(p, cmd)
if globalFlags.ProjectId == "" {
return nil, &cliErr.ProjectIdError{}
}
bootVolumeSourceId := flags.FlagToStringPointer(p, cmd, bootVolumeSourceIdFlag)
bootVolumeSourceType := flags.FlagToStringPointer(p, cmd, bootVolumeSourceTypeFlag)
bootVolumeSize := flags.FlagToInt64Pointer(p, cmd, bootVolumeSizeFlag)
imageId := flags.FlagToStringPointer(p, cmd, imageIdFlag)
if imageId == nil && bootVolumeSourceId == nil && bootVolumeSourceType == nil {
return nil, &cliErr.ServerCreateMissingFlagsError{
Cmd: cmd,
}
}
if imageId == nil {
err := flags.MarkFlagsRequired(cmd, bootVolumeSourceIdFlag, bootVolumeSourceTypeFlag)
cobra.CheckErr(err)
}
if bootVolumeSourceId != nil && bootVolumeSourceType == nil {
err := cmd.MarkFlagRequired(bootVolumeSourceTypeFlag)
cobra.CheckErr(err)
return nil, &cliErr.ServerCreateMissingVolumeTypeError{
Cmd: cmd,
}
}
if bootVolumeSourceType != nil {
if bootVolumeSourceId == nil {
err := cmd.MarkFlagRequired(bootVolumeSourceIdFlag)
cobra.CheckErr(err)
return nil, &cliErr.ServerCreateMissingVolumeIdError{
Cmd: cmd,
}
}
if *bootVolumeSourceType == "image" && bootVolumeSize == nil {
err := cmd.MarkFlagRequired(bootVolumeSizeFlag)
cobra.CheckErr(err)
return nil, &cliErr.ServerCreateError{
Cmd: cmd,
}
}
}
if bootVolumeSourceId == nil && bootVolumeSourceType == nil {
err := cmd.MarkFlagRequired(imageIdFlag)
cobra.CheckErr(err)
}
model := inputModel{
GlobalFlagModel: globalFlags,
Name: flags.FlagToStringPointer(p, cmd, nameFlag),
MachineType: flags.FlagToStringPointer(p, cmd, machineTypeFlag),
AffinityGroup: flags.FlagToStringPointer(p, cmd, affinityGroupFlag),
AvailabilityZone: flags.FlagToStringPointer(p, cmd, availabilityZoneFlag),
AgentProvisioned: flags.FlagToBoolPointer(p, cmd, agentProvisionedFlag),
BootVolumeSourceId: flags.FlagToStringPointer(p, cmd, bootVolumeSourceIdFlag),
BootVolumeSourceType: flags.FlagToStringPointer(p, cmd, bootVolumeSourceTypeFlag),
BootVolumeSize: flags.FlagToInt64Pointer(p, cmd, bootVolumeSizeFlag),
BootVolumePerformanceClass: flags.FlagToStringPointer(p, cmd, bootVolumePerformanceClassFlag),
BootVolumeDeleteOnTermination: flags.FlagToBoolPointer(p, cmd, bootVolumeDeleteOnTerminationFlag),
ImageId: flags.FlagToStringPointer(p, cmd, imageIdFlag),
KeypairName: flags.FlagToStringPointer(p, cmd, keypairNameFlag),
Labels: flags.FlagToStringToStringPointer(p, cmd, labelFlag),
NetworkId: flags.FlagToStringPointer(p, cmd, networkIdFlag),
NetworkInterfaceIds: flags.FlagToStringSlicePointer(p, cmd, networkInterfaceIdsFlag),
SecurityGroups: flags.FlagToStringSlicePointer(p, cmd, securityGroupsFlag),
ServiceAccountMails: flags.FlagToStringSlicePointer(p, cmd, serviceAccountEmailsFlag),
UserData: flags.FlagToStringPointer(p, cmd, userDataFlag),
Volumes: flags.FlagToStringSlicePointer(p, cmd, volumesFlag),
}
p.DebugInputModel(model)
return &model, nil
}
func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APIClient) iaas.ApiCreateServerRequest {
req := apiClient.CreateServer(ctx, model.ProjectId, model.Region)
var userData *[]byte
if model.UserData != nil {
userData = utils.Ptr([]byte(*model.UserData))
}
payload := iaas.CreateServerPayload{
Name: model.Name,
MachineType: model.MachineType,
AffinityGroup: model.AffinityGroup,
AvailabilityZone: model.AvailabilityZone,
ImageId: model.ImageId,
KeypairName: model.KeypairName,
SecurityGroups: model.SecurityGroups,
ServiceAccountMails: model.ServiceAccountMails,
UserData: userData,
Volumes: model.Volumes,
Labels: utils.ConvertStringMapToInterfaceMap(model.Labels),
}
if model.AgentProvisioned != nil {
payload.Agent = &iaas.ServerAgent{
Provisioned: model.AgentProvisioned,
}
}
if model.BootVolumePerformanceClass != nil || model.BootVolumeSize != nil || model.BootVolumeDeleteOnTermination != nil || model.BootVolumeSourceId != nil || model.BootVolumeSourceType != nil {
payload.BootVolume = &iaas.ServerBootVolume{
PerformanceClass: model.BootVolumePerformanceClass,
Size: model.BootVolumeSize,
DeleteOnTermination: model.BootVolumeDeleteOnTermination,
Source: &iaas.BootVolumeSource{
Id: model.BootVolumeSourceId,
Type: model.BootVolumeSourceType,
},
}
}
if model.NetworkInterfaceIds != nil || model.NetworkId != nil {
payload.Networking = &iaas.CreateServerPayloadAllOfNetworking{}
if model.NetworkInterfaceIds != nil {
payload.Networking.CreateServerNetworkingWithNics = &iaas.CreateServerNetworkingWithNics{
NicIds: model.NetworkInterfaceIds,
}
}
if model.NetworkId != nil {
payload.Networking.CreateServerNetworking = &iaas.CreateServerNetworking{
NetworkId: model.NetworkId,
}
}
}
return req.CreateServerPayload(payload)
}
func outputResult(p *print.Printer, outputFormat, projectLabel string, server *iaas.Server) error {
if server == nil {
return fmt.Errorf("server response is empty")
}
return p.OutputResult(outputFormat, server, func() error {
p.Outputf("Created server for project %q.\nServer ID: %s\n", projectLabel, utils.PtrString(server.Id))
return nil
})
}