/
image.go
922 lines (848 loc) · 35.9 KB
/
image.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
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
package commands
import (
"bufio"
"context"
"crypto/x509"
"errors"
"fmt"
"github.com/ionos-cloud/ionosctl/internal/functional"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"time"
"github.com/ionos-cloud/ionosctl/commands/cloudapi-v6/waiter"
"github.com/ionos-cloud/ionosctl/pkg/constants"
"github.com/ionos-cloud/ionosctl/pkg/utils"
"github.com/spf13/pflag"
"go.uber.org/multierr"
"golang.org/x/exp/slices"
"github.com/fatih/structs"
"github.com/ionos-cloud/ionosctl/commands/cloudapi-v6/completer"
"github.com/ionos-cloud/ionosctl/commands/cloudapi-v6/query"
"github.com/ionos-cloud/ionosctl/pkg/core"
"github.com/ionos-cloud/ionosctl/pkg/printer"
cloudapiv6 "github.com/ionos-cloud/ionosctl/services/cloudapi-v6"
"github.com/ionos-cloud/ionosctl/services/cloudapi-v6/resources"
ionoscloud "github.com/ionos-cloud/sdk-go/v6"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"golang.org/x/sync/errgroup"
)
func ImageCmd() *core.Command {
ctx := context.TODO()
imageCmd := &core.Command{
Command: &cobra.Command{
Use: "image",
Aliases: []string{"img"},
Short: "Image Operations",
Long: "The sub-commands of `ionosctl image` allow you to see information about the Images available.",
TraverseChildren: true,
},
}
globalFlags := imageCmd.GlobalFlags()
globalFlags.StringSliceP(constants.ArgCols, "", defaultImageCols, printer.ColsMessage(allImageCols))
_ = viper.BindPFlag(core.GetFlagName(imageCmd.Name(), constants.ArgCols), globalFlags.Lookup(constants.ArgCols))
_ = imageCmd.Command.RegisterFlagCompletionFunc(constants.ArgCols, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return allImageCols, cobra.ShellCompDirectiveNoFileComp
})
/*
List Command
*/
list := core.NewCommand(ctx, imageCmd, core.CommandBuilder{
Namespace: "image",
Resource: "image",
Verb: "list",
Aliases: []string{"l", "ls"},
ShortDesc: "List Images",
LongDesc: "Use this command to get a full list of available public Images.\n\nYou can filter the results using `--filters` option. Use the following format to set filters: `--filters KEY1=VALUE1,KEY2=VALUE2`.\n" + completer.ImagesFiltersUsage(),
Example: listImagesExample,
PreCmdRun: PreRunImageList,
CmdRun: RunImageList,
InitClient: true,
})
list.AddStringFlag(cloudapiv6.ArgType, "", "", "The type of the Image", core.DeprecatedFlagOption())
_ = list.Command.RegisterFlagCompletionFunc(cloudapiv6.ArgType, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return []string{"CDROM", "HDD"}, cobra.ShellCompDirectiveNoFileComp
})
list.AddStringFlag(cloudapiv6.ArgLicenceType, "", "", "The licence type of the Image", core.DeprecatedFlagOption())
_ = list.Command.RegisterFlagCompletionFunc(cloudapiv6.ArgLicenceType, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return []string{"WINDOWS", "WINDOWS2016", "LINUX", "OTHER", "UNKNOWN"}, cobra.ShellCompDirectiveNoFileComp
})
list.AddStringFlag(cloudapiv6.ArgLocation, cloudapiv6.ArgLocationShort, "", "The location of the Image", core.DeprecatedFlagOption())
_ = list.Command.RegisterFlagCompletionFunc(cloudapiv6.ArgLocation, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return completer.LocationIds(os.Stderr), cobra.ShellCompDirectiveNoFileComp
})
list.AddStringFlag(cloudapiv6.ArgImageAlias, "", "", "Image Alias or part of Image Alias to sort Images by", core.DeprecatedFlagOption())
list.AddIntFlag(cloudapiv6.ArgLatest, "", 0, "Show the latest N Images, based on creation date, starting from now in descending order. If it is not set, all Images will be printed", core.DeprecatedFlagOption())
list.AddInt32Flag(cloudapiv6.ArgMaxResults, cloudapiv6.ArgMaxResultsShort, cloudapiv6.DefaultMaxResults, cloudapiv6.ArgMaxResultsDescription)
list.AddInt32Flag(cloudapiv6.ArgDepth, cloudapiv6.ArgDepthShort, cloudapiv6.DefaultListDepth, cloudapiv6.ArgDepthDescription)
list.AddStringFlag(cloudapiv6.ArgOrderBy, "", "", cloudapiv6.ArgOrderByDescription)
_ = list.Command.RegisterFlagCompletionFunc(cloudapiv6.ArgOrderBy, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return completer.ImagesFilters(), cobra.ShellCompDirectiveNoFileComp
})
list.AddStringSliceFlag(cloudapiv6.ArgFilters, cloudapiv6.ArgFiltersShort, []string{""}, cloudapiv6.ArgFiltersDescription)
_ = list.Command.RegisterFlagCompletionFunc(cloudapiv6.ArgFilters, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return completer.ImagesFilters(), cobra.ShellCompDirectiveNoFileComp
})
list.AddBoolFlag(constants.ArgNoHeaders, "", false, cloudapiv6.ArgNoHeadersDescription)
/*
Get Command
*/
get := core.NewCommand(ctx, imageCmd, core.CommandBuilder{
Namespace: "image",
Resource: "image",
Verb: "get",
Aliases: []string{"g"},
ShortDesc: "Get a specified Image",
LongDesc: "Use this command to get information about a specified Image.\n\nRequired values to run command:\n\n* Image Id",
Example: getImageExample,
PreCmdRun: PreRunImageId,
CmdRun: RunImageGet,
InitClient: true,
})
get.AddUUIDFlag(cloudapiv6.ArgImageId, cloudapiv6.ArgIdShort, "", cloudapiv6.ImageId, core.RequiredFlagOption())
_ = get.Command.RegisterFlagCompletionFunc(cloudapiv6.ArgImageId, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return completer.ImageIds(os.Stderr), cobra.ShellCompDirectiveNoFileComp
})
get.AddBoolFlag(constants.ArgNoHeaders, "", false, cloudapiv6.ArgNoHeadersDescription)
get.AddInt32Flag(cloudapiv6.ArgDepth, cloudapiv6.ArgDepthShort, cloudapiv6.DefaultGetDepth, cloudapiv6.ArgDepthDescription)
update := core.NewCommand(ctx, imageCmd, core.CommandBuilder{
Namespace: "image",
Resource: "image",
Verb: "update",
Aliases: []string{"u", "up"},
ShortDesc: "Update a specified Image",
LongDesc: "Use this command to update information about a specified Image.\n\nRequired values to run command:\n\n* Image Id",
Example: "ionosctl image update --image-id IMAGE_ID",
PreCmdRun: PreRunImageId,
CmdRun: RunImageUpdate,
InitClient: true,
})
update.AddUUIDFlag(cloudapiv6.ArgImageId, cloudapiv6.ArgIdShort, "", cloudapiv6.ImageId, core.RequiredFlagOption())
_ = update.Command.RegisterFlagCompletionFunc(cloudapiv6.ArgImageId, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return completer.ImageIds(os.Stderr), cobra.ShellCompDirectiveNoFileComp
})
update.Command.Flags().SortFlags = false // Hot Plugs generate a lot of flags to scroll through, put them at the end
update.AddBoolFlag(constants.ArgWaitForRequest, constants.ArgWaitForRequestShort, constants.DefaultWait, "Wait for the Request for Image update to be executed")
update.AddIntFlag(constants.ArgTimeout, constants.ArgTimeoutShort, constants.DefaultTimeoutSeconds, "Timeout option for Request for Image update [seconds]")
update.AddBoolFlag(constants.ArgNoHeaders, "", false, cloudapiv6.ArgNoHeadersDescription)
update.AddInt32Flag(cloudapiv6.ArgDepth, cloudapiv6.ArgDepthShort, cloudapiv6.DefaultGetDepth, cloudapiv6.ArgDepthDescription)
addPropertiesFlags := func(command *core.Command) {
command.AddStringFlag(cloudapiv6.ArgName, cloudapiv6.ArgNameShort, "", "Name of the Image")
command.AddStringFlag(cloudapiv6.ArgDescription, cloudapiv6.ArgDescriptionShort, "", "Description of the Image")
command.AddSetFlag(cloudapiv6.ArgLicenceType, "", "UNKNOWN", []string{"UNKNOWN", "WINDOWS", "WINDOWS2016", "WINDOWS2022", "LINUX", "OTHER"}, "The OS type of this image")
command.AddSetFlag("cloud-init", "", "V1", []string{"V1", "NONE"}, "Cloud init compatibility")
command.AddBoolFlag(cloudapiv6.ArgCpuHotPlug, "", true, "'Hot-Plug' CPU. It is not possible to have a hot-unplug CPU which you previously did not hot-plug")
command.AddBoolFlag(cloudapiv6.ArgRamHotPlug, "", true, "'Hot-Plug' RAM")
command.AddBoolFlag(cloudapiv6.ArgNicHotPlug, "", true, "'Hot-Plug' NIC")
command.AddBoolFlag(cloudapiv6.ArgDiscVirtioHotPlug, "", true, "'Hot-Plug' Virt-IO drive")
command.AddBoolFlag(cloudapiv6.ArgDiscScsiHotPlug, "", true, "'Hot-Plug' SCSI drive")
command.AddBoolFlag(cloudapiv6.ArgCpuHotUnplug, "", false, "'Hot-Unplug' CPU. It is not possible to have a hot-unplug CPU which you previously did not hot-plug")
command.AddBoolFlag(cloudapiv6.ArgRamHotUnplug, "", false, "'Hot-Unplug' RAM")
command.AddBoolFlag(cloudapiv6.ArgNicHotUnplug, "", false, "'Hot-Unplug' NIC")
command.AddBoolFlag(cloudapiv6.ArgDiscVirtioHotUnplug, "", false, "'Hot-Unplug' Virt-IO drive")
command.AddBoolFlag(cloudapiv6.ArgDiscScsiHotUnplug, "", false, "'Hot-Unplug' SCSI drive")
}
addPropertiesFlags(update)
/*
Delete Command
*/
deleteCmd := core.NewCommand(ctx, imageCmd, core.CommandBuilder{
Namespace: "image",
Resource: "image",
Verb: "delete",
Aliases: []string{"d"},
ShortDesc: "Delete an image",
LongDesc: "Use this command to delete a specified Image.\n\nRequired values to run command:\n\n* Image Id",
Example: "ionosctl image delete --image-id IMAGE_ID",
PreCmdRun: PreRunImageDelete,
CmdRun: RunImageDelete,
InitClient: true,
})
deleteCmd.AddUUIDFlag(cloudapiv6.ArgImageId, cloudapiv6.ArgIdShort, "", cloudapiv6.ImageId, core.RequiredFlagOption())
_ = deleteCmd.Command.RegisterFlagCompletionFunc(cloudapiv6.ArgImageId, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return completer.ImageIds(os.Stderr), cobra.ShellCompDirectiveNoFileComp
})
deleteCmd.AddBoolFlag(constants.ArgAll, constants.ArgAllShort, false, "Delete all non-public images")
deleteCmd.AddBoolFlag(constants.ArgWaitForRequest, constants.ArgWaitForRequestShort, constants.DefaultWait, "Wait for the Request for Image update to be executed")
deleteCmd.AddIntFlag(constants.ArgTimeout, constants.ArgTimeoutShort, constants.DefaultTimeoutSeconds, "Timeout option for Request for Image update [seconds]")
deleteCmd.AddBoolFlag(constants.ArgNoHeaders, "", false, cloudapiv6.ArgNoHeadersDescription)
deleteCmd.AddInt32Flag(cloudapiv6.ArgDepth, cloudapiv6.ArgDepthShort, cloudapiv6.DefaultGetDepth, cloudapiv6.ArgDepthDescription)
/*
upload Command
Perform upload to given FTP server. All locations from `location list` + karlsruhe (fkb)
- ftp://ftp-fkb.ionos.com/hdd-images
- ftp://ftp-fkb.ionos.com/iso-images
https://docs.ionos.com/cloud/compute-engine/block-storage/block-storage-faq#how-do-i-upload-my-own-images-with-ftp
*/
upload := core.NewCommand(ctx, imageCmd, core.CommandBuilder{
Namespace: "image",
Resource: "image",
Verb: "upload",
Aliases: []string{"ftp-upload", "ftp", "upl"},
ShortDesc: "Upload an image to FTP server",
LongDesc: "Use this command to upload an HDD or ISO image.\n\nRequired values to run command:\n\n* Location\n",
Example: "ionosctl img u -i kolibri.iso -l fkb,fra,vit",
PreCmdRun: PreRunImageUpload,
CmdRun: RunImageUpload,
InitClient: true,
})
upload.AddStringSliceFlag(cloudapiv6.ArgLocation, cloudapiv6.ArgLocationShort, []string{"fra"}, "Location to upload to. Must be an array containing only fra, fkb, txl, lhr, las, ewr, vit", core.RequiredFlagOption())
upload.AddStringSliceFlag("image", "i", nil, "Slice of paths to images, can be absolute path or relative to current working directory", core.RequiredFlagOption())
upload.AddStringFlag("ftp-url", "", "ftp-%s.ionos.com", "URL of FTP server, with %s flag if location is embedded into url")
upload.AddBoolFlag("skip-verify", "", false, "Skip verification of server certificate, useful if using a custom ftp-url. WARNING: You can be the target of a man-in-the-middle attack!")
upload.AddBoolFlag("skip-update", "", false, "After the image is uploaded to the FTP server, send a PATCH to the API with the contents of the image properties flags and emulate a \"create\" command.")
upload.AddStringFlag("crt-path", "", "", "(Unneeded for IONOS FTP Servers) Path to file containing server certificate. If your FTP server is self-signed, you need to add the server certificate to the list of certificate authorities trusted by the client.")
upload.AddStringSliceFlag(cloudapiv6.ArgImageAlias, cloudapiv6.ArgImageAliasShort, nil, "Rename the uploaded images. These names should not contain any extension. By default, this is the base of the image path")
upload.AddIntFlag(constants.ArgTimeout, constants.ArgTimeoutShort, 300, "(seconds) Context Deadline. FTP connection will time out after this many seconds")
addPropertiesFlags(upload)
upload.Command.Flags().SortFlags = false // Hot Plugs generate a lot of flags to scroll through, put them at the end
return imageCmd
}
func PreRunImageDelete(c *core.PreCommandConfig) error {
return core.CheckRequiredFlagsSets(c.Command, c.NS, []string{cloudapiv6.ArgImageId}, []string{cloudapiv6.ArgAll})
}
func RunImageDelete(c *core.CommandConfig) error {
if viper.GetBool(core.GetFlagName(c.NS, cloudapiv6.ArgAll)) {
if err := DeleteAllNonPublicImages(c); err != nil {
return err
}
return c.Printer.Print(printer.Result{Resource: c.Resource, Verb: c.Verb})
} else {
listQueryParams, err := query.GetListQueryParams(c)
if err != nil {
return err
}
queryParams := listQueryParams.QueryParams
if err := utils.AskForConfirm(c.Stdin, c.Printer, "delete image"); err != nil {
return err
}
imgId := viper.GetString(core.GetFlagName(c.NS, cloudapiv6.ArgImageId))
c.Printer.Verbose("Starting deletion on image with ID: %v...", imgId)
resp, err := c.CloudApiV6Services.Images().Delete(imgId, queryParams)
if resp != nil && printer.GetId(resp) != "" {
c.Printer.Verbose(constants.MessageRequestInfo, printer.GetId(resp), resp.RequestTime)
}
if err != nil {
return err
}
if err = utils.WaitForRequest(c, waiter.RequestInterrogator, printer.GetId(resp)); err != nil {
return err
}
return c.Printer.Print(getImagePrint(resp, c, nil))
}
}
// Util func - Given a slice of public & non-public images, return only those images that are non-public.
// If any image in the slice has null properties, or "Properties.Public" field is nil, the image is skipped (and a verbose message is shown)
func getNonPublicImages(imgs []ionoscloud.Image, printService printer.PrintService) ([]ionoscloud.Image, error) {
var nonPublicImgs []ionoscloud.Image
for _, i := range imgs {
properties, ok := i.GetPropertiesOk()
if !ok {
printService.Verbose("skipping %s: properties are nil\n", *i.GetId())
continue
}
isPublic, ok := properties.GetPublicOk()
if !ok {
printService.Verbose("skipping %s: field `public` is nil\n", *i.GetId())
continue
}
if !*isPublic {
nonPublicImgs = append(nonPublicImgs, i)
}
}
return nonPublicImgs, nil
}
// deletes non-public images, as deleting public images is forbidden by the API.
func DeleteAllNonPublicImages(c *core.CommandConfig) error {
depth := int32(1)
images, resp, err := c.CloudApiV6Services.Images().List(
resources.ListQueryParams{QueryParams: resources.QueryParams{Depth: &depth}},
)
if err != nil {
return err
}
allItems, ok := images.GetItemsOk()
if !(ok && len(*allItems) > 0 && allItems != nil) {
return errors.New("could not retrieve images")
}
items, err := getNonPublicImages(*allItems, c.Printer)
if err != nil {
return err
}
if len(items) < 1 {
return errors.New("no non-public images found")
}
_ = c.Printer.Warn("Images to be deleted:")
// TODO: this is duplicated across all resources - refactor this (across all resources)
for _, img := range items {
delIdAndName := ""
if id, ok := img.GetIdOk(); ok && id != nil {
delIdAndName += "ID: `" + *id
}
if properties, ok := img.GetPropertiesOk(); ok && properties != nil {
if name, ok := properties.GetNameOk(); ok && name != nil {
delIdAndName += "`, Name: " + *name
}
}
_ = c.Printer.Warn(delIdAndName)
}
if err = utils.AskForConfirm(c.Stdin, c.Printer, "delete all the images"); err != nil {
return err
}
c.Printer.Verbose("Deleting all the images...")
var multiErr error
for _, img := range items {
if id, ok := img.GetIdOk(); ok && id != nil {
c.Printer.Verbose("Starting deleting image with id: %v...", *id)
resp, err = c.CloudApiV6Services.Images().Delete(*id, resources.QueryParams{})
if resp != nil && printer.GetId(resp) != "" {
c.Printer.Verbose(constants.MessageRequestInfo, printer.GetId(resp), resp.RequestTime)
}
if err != nil {
multiErr = multierr.Append(multiErr, fmt.Errorf(constants.ErrDeleteAll, c.Resource, *id, err))
continue
} else {
_ = c.Printer.Warn(fmt.Sprintf(constants.MessageDeletingAll, c.Resource, *id))
}
if err = utils.WaitForRequest(c, waiter.RequestInterrogator, printer.GetId(resp)); err != nil {
multiErr = multierr.Append(multiErr, fmt.Errorf(constants.ErrDeleteAll, c.Resource, *id, err))
continue
}
}
}
if multiErr != nil {
return multiErr
}
return nil
}
// returns an ImageProperties object which reflects the currently set flags
func getDesiredImageAfterPatch(c *core.CommandConfig) resources.ImageProperties {
input := resources.ImageProperties{}
c.Command.Command.Flags().VisitAll(func(flag *pflag.Flag) {
val := flag.Value.String()
if val == "" {
return
}
boolval, _ := strconv.ParseBool(val)
switch flag.Name {
case cloudapiv6.ArgName:
input.SetName(val)
break
case cloudapiv6.ArgDescription:
input.SetDescription(val)
break
case "cloud-init":
input.SetCloudInit(val)
break
case cloudapiv6.ArgLicenceType:
input.SetLicenceType(val)
break
case cloudapiv6.ArgCpuHotPlug:
input.SetCpuHotPlug(boolval)
break
case cloudapiv6.ArgRamHotPlug:
input.SetRamHotPlug(boolval)
break
case cloudapiv6.ArgNicHotPlug:
input.SetNicHotPlug(boolval)
break
case cloudapiv6.ArgDiscVirtioHotPlug:
input.SetDiscVirtioHotPlug(boolval)
break
case cloudapiv6.ArgDiscScsiHotPlug:
input.SetDiscScsiHotPlug(boolval)
break
case cloudapiv6.ArgCpuHotUnplug:
input.SetCpuHotUnplug(boolval)
break
case cloudapiv6.ArgRamHotUnplug:
input.SetRamHotUnplug(boolval)
break
case cloudapiv6.ArgNicHotUnplug:
input.SetNicHotUnplug(boolval)
break
case cloudapiv6.ArgDiscVirtioHotUnplug:
input.SetDiscVirtioHotUnplug(boolval)
break
case cloudapiv6.ArgDiscScsiHotUnplug:
input.SetDiscScsiHotUnplug(boolval)
break
default:
// --image-id, verbose, filters, depth, etc
break
}
c.Printer.Verbose(fmt.Sprintf("Property %s set: %s", flag.Name, flag.Value))
})
return input
}
func RunImageUpdate(c *core.CommandConfig) error {
listQueryParams, err := query.GetListQueryParams(c)
if err != nil {
return err
}
queryParams := listQueryParams.QueryParams
input := getDesiredImageAfterPatch(c)
img, resp, err := c.CloudApiV6Services.Images().Update(
viper.GetString(core.GetFlagName(c.NS, cloudapiv6.ArgImageId)),
input,
queryParams,
)
if resp != nil && printer.GetId(resp) != "" {
c.Printer.Verbose(constants.MessageRequestInfo, printer.GetId(resp), resp.RequestTime)
}
if err != nil {
return err
}
if err = utils.WaitForRequest(c, waiter.RequestInterrogator, printer.GetId(resp)); err != nil {
return err
}
return c.Printer.Print(getImagePrint(resp, c, []resources.Image{*img}))
}
func PreRunImageUpload(c *core.PreCommandConfig) error {
allSliceFlagValuesInSet := func(flagVals []string, set []string) error {
for _, val := range flagVals {
if !(slices.Contains(set, val)) {
return fmt.Errorf("%s is not one of: %s", val, strings.Join(set, ", "))
}
}
return nil
}
err := c.Command.Command.MarkFlagRequired("image")
if err != nil {
return err
}
validImageExtensions := []string{".iso", ".img", ".vmdk", ".vhd", ".vhdx", ".cow", ".qcow", ".qcow2", ".raw", ".vpc", ".vdi"}
images := viper.GetStringSlice(core.GetFlagName(c.NS, "image"))
// All images in 'images' slice should have extensions in set of validImageExtensions
err = allSliceFlagValuesInSet(
functional.Map(images, func(s string) string {
return filepath.Ext(s)
}),
validImageExtensions,
)
if err != nil {
return err
}
// "Locations" flag only required if ftp-url custom flag contains a %s in which to add the location ID
if strings.Contains(viper.GetString(core.GetFlagName(c.NS, "ftp-url")), "%s") {
err = c.Command.Command.MarkFlagRequired(cloudapiv6.ArgLocation)
if err != nil {
return err
}
validLocations := []string{"fra", "fkb", "txl", "lhr", "las", "ewr", "vit"}
err = allSliceFlagValuesInSet(viper.GetStringSlice(cloudapiv6.ArgLocation), validLocations)
if err != nil {
return err
}
}
aliases := viper.GetStringSlice(core.GetFlagName(c.NS, cloudapiv6.ArgImageAlias))
if len(aliases) != 0 && len(aliases) != len(images) {
return fmt.Errorf("slices of image files and image aliases are of different lengths. Uploading multiple images with the same alias is forbidden")
}
return nil
}
// Reads server certificate at given path.
// If path unset, returns nil.
// Otherwise, returns certificate pool containing server certificate
func getCertificate(path string) (*x509.CertPool, error) {
if path == "" {
return nil, nil
}
caCert, err := os.ReadFile(path)
if err != nil {
return nil, err
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)
return caCertPool, nil
}
func updateImagesAfterUpload(c *core.CommandConfig, diffImgs []resources.Image, properties resources.ImageProperties) ([]resources.Image, error) {
// do a patch on the uploaded images
var imgs []resources.Image
for _, diffImg := range diffImgs {
img, _, err := c.CloudApiV6Services.Images().Update(*diffImg.GetId(), properties, cloudapiv6.ParentResourceQueryParams)
imgs = append(imgs, *img)
if err != nil {
return nil, err
}
}
return imgs, nil
}
func getDiffUploadedImages(c *core.CommandConfig, names, locations []string) ([]resources.Image, error) {
// Wait for images uploaded to ftp server to register on `ionosctl image list`.
// Currently "hacky" code, until SDK-1163 is fixed.
// TODO: Replace the nested for-loops when support for filtering by multiple values per key is added
const attempts = 6
const timeoutPerAttempt = 10
attempt := 0
time.Sleep(timeoutPerAttempt * time.Second)
var diffImgs []resources.Image
c.Printer.Verbose("Will iterate over %+v\n", names)
for attempt < attempts {
diffImgs = []resources.Image{} // throw away old iteration images
for _, n := range names { // TODO: See SDK-1163. These nested for-loops could be O(1) instead!
for _, l := range locations { // TODO: See SDK-1163. These nested for-loops could be O(1) instead!
filters := map[string]string{
"name": n,
"public": "false",
"location": l,
}
c.Printer.Verbose("Filtering by name %s and location %s\n", n, l)
depth := int32(1)
imgs, _, err := c.CloudApiV6Services.Images().List(resources.ListQueryParams{Filters: &filters, QueryParams: resources.QueryParams{Depth: &depth}})
if err != nil {
return nil, fmt.Errorf("failed listing uploaded images")
}
c.Printer.Verbose("Got images by listing: %+v\n", getImages(imgs))
if len(getImages(imgs)) < 1 {
break // should have one for each location. If less, just break here. Clearly we won't have enough.
}
diffImgs = append(diffImgs, getImages(imgs)...)
c.Printer.Verbose("Total images: %+v\n", diffImgs)
}
}
if len(diffImgs) == len(names)*len(locations) {
c.Printer.Verbose("Success! found images %+v\n", diffImgs)
// Success!
break
}
if attempt >= attempts {
return nil, fmt.Errorf("failed retrieving all uploaded images: timeout %ds", timeoutPerAttempt*attempts)
}
// New attempt...
c.Printer.Verbose("Attempt %d failed", attempt)
time.Sleep(timeoutPerAttempt * time.Second)
attempt += 1
}
return diffImgs, nil
}
func RunImageUpload(c *core.CommandConfig) error {
certPool, err := getCertificate(viper.GetString(core.GetFlagName(c.NS, "crt-path")))
if err != nil {
return err
}
images := viper.GetStringSlice(core.GetFlagName(c.NS, "image"))
aliases := viper.GetStringSlice(core.GetFlagName(c.NS, cloudapiv6.ArgImageAlias))
locations := viper.GetStringSlice(core.GetFlagName(c.NS, cloudapiv6.ArgLocation))
skipVerify := viper.GetBool(core.GetFlagName(c.NS, "skip-verify"))
timeout := viper.GetInt(core.GetFlagName(c.NS, constants.ArgTimeout))
var eg errgroup.Group
for _, loc := range locations {
for imgIdx, img := range images {
url := viper.GetString(core.GetFlagName(c.NS, "ftp-url"))
if strings.Contains(url, "%s") {
url = fmt.Sprintf(url, loc) // Add the location modifier, if the URL supports it
}
c.Printer.Verbose("Uploading %s to %s", img, url)
var isoOrHdd string
if ext := filepath.Ext(img); ext == ".iso" || ext == ".img" {
isoOrHdd = "iso"
} else {
isoOrHdd = "hdd"
}
serverFilePath := fmt.Sprintf("%s-images/", isoOrHdd) // iso-images / hdd-images
if len(aliases) == 0 {
serverFilePath += filepath.Base(img) // If no custom alias, use the filename
} else {
serverFilePath += aliases[imgIdx] + filepath.Ext(img) // Use custom alias
}
file, err := os.Open(img)
if err != nil {
return err
}
data := bufio.NewReader(file)
// Catching error from goroutines. https://stackoverflow.com/questions/62387307/how-to-catch-errors-from-goroutines
// Uploads each image to each location.
eg.Go(func() error {
err := c.CloudApiV6Services.Images().Upload(
resources.UploadProperties{
FTPServerProperties: resources.FTPServerProperties{Url: url, Port: 21, SkipVerify: skipVerify, ServerCertificate: certPool, Timeout: timeout},
ImageFileProperties: resources.ImageFileProperties{Path: serverFilePath, DataBuffer: data},
},
)
if err != nil {
return err
}
return file.Close()
})
}
}
if err := eg.Wait(); err != nil {
return err
}
if viper.GetBool(core.GetFlagName(c.NS, "skip-update")) {
c.Printer.Verbose("Successfully uploaded images")
return nil
}
names := images
if len(aliases) != 0 {
// Returns a slice containing `alias[i] + filepath.Ext(images[i])`
// (i.e it gets the extensions from `images` flag, and appends them to each elem `image-alias`)
// Resulting slice is the full image names, as returned by `ionosctl image list` on the Name column
names = functional.MapIdx(aliases, func(k int, v string) string {
return v + filepath.Ext(images[k])
})
}
diffImgs, err := getDiffUploadedImages(c, names, locations) // Get UUIDs of uploaded images
if err != nil {
return fmt.Errorf("%e, however the upload was successful. Please run `ionosctl image update` manually", err)
}
properties := getDesiredImageAfterPatch(c)
imgs, err := updateImagesAfterUpload(c, diffImgs, properties)
if err != nil {
return fmt.Errorf("%e, however the upload was successful. Please run `ionosctl image update` manually", err)
}
c.Printer.Verbose("Successfully uploaded and updated images")
return c.Printer.Print(getImagePrint(nil, c, imgs))
}
func PreRunImageList(c *core.PreCommandConfig) error {
if viper.IsSet(core.GetFlagName(c.NS, cloudapiv6.ArgFilters)) {
return query.ValidateFilters(c, completer.ImagesFilters(), completer.ImagesFiltersUsage())
}
return nil
}
func PreRunImageId(c *core.PreCommandConfig) error {
return core.CheckRequiredFlags(c.Command, c.NS, cloudapiv6.ArgImageId)
}
func RunImageList(c *core.CommandConfig) error {
_ = c.Printer.Warn("WARNING: The following flags are deprecated:" + c.Command.GetAnnotationsByKey(core.DeprecatedFlagsAnnotation) + ". Use --filters --order-by --max-results options instead!")
// Add Query Parameters for GET Requests
listQueryParams, err := query.GetListQueryParams(c)
if err != nil {
return err
}
images, resp, err := c.CloudApiV6Services.Images().List(listQueryParams)
if resp != nil {
c.Printer.Verbose(constants.MessageRequestTime, resp.RequestTime)
}
if err != nil {
return err
}
if viper.IsSet(core.GetFlagName(c.NS, cloudapiv6.ArgLocation)) {
images = sortImagesByLocation(images, viper.GetString(core.GetFlagName(c.NS, cloudapiv6.ArgLocation)))
}
if viper.IsSet(core.GetFlagName(c.NS, cloudapiv6.ArgLicenceType)) {
images = sortImagesByLicenceType(images, strings.ToUpper(viper.GetString(core.GetFlagName(c.NS, cloudapiv6.ArgLicenceType))))
}
if viper.IsSet(core.GetFlagName(c.NS, cloudapiv6.ArgType)) {
images = sortImagesByType(images, strings.ToUpper(viper.GetString(core.GetFlagName(c.NS, cloudapiv6.ArgType))))
}
if viper.IsSet(core.GetFlagName(c.NS, cloudapiv6.ArgImageAlias)) {
images = sortImagesByAlias(images, viper.GetString(core.GetFlagName(c.NS, cloudapiv6.ArgImageAlias)))
}
if viper.IsSet(core.GetFlagName(c.NS, cloudapiv6.ArgLatest)) {
images = sortImagesByTime(images, viper.GetInt(core.GetFlagName(c.NS, cloudapiv6.ArgLatest)))
}
if itemsOk, ok := images.GetItemsOk(); ok && itemsOk != nil {
if len(*itemsOk) == 0 {
return errors.New("error getting images based on given criteria")
}
}
return c.Printer.Print(getImagePrint(nil, c, getImages(images)))
}
func RunImageGet(c *core.CommandConfig) error {
listQueryParams, err := query.GetListQueryParams(c)
if err != nil {
return err
}
queryParams := listQueryParams.QueryParams
c.Printer.Verbose("Image with id: %v is getting...", viper.GetString(core.GetFlagName(c.NS, cloudapiv6.ArgImageId)))
img, resp, err := c.CloudApiV6Services.Images().Get(viper.GetString(core.GetFlagName(c.NS, cloudapiv6.ArgImageId)), queryParams)
if resp != nil {
c.Printer.Verbose(constants.MessageRequestTime, resp.RequestTime)
}
if err != nil {
return err
}
return c.Printer.Print(getImagePrint(nil, c, getImage(img)))
}
// Output Printing
var (
defaultImageCols = []string{"ImageId", "Name", "ImageAliases", "Location", "LicenceType", "ImageType", "CloudInit", "CreatedDate"}
allImageCols = []string{"ImageId", "Name", "ImageAliases", "Location", "Size", "LicenceType", "ImageType", "Description", "Public", "CloudInit", "CreatedDate", "CreatedBy", "CreatedByUserId"}
)
type ImagePrint struct {
ImageId string `json:"ImageId,omitempty"`
Name string `json:"Name,omitempty"`
Description string `json:"Description,omitempty"`
Location string `json:"Location,omitempty"`
Size string `json:"Size,omitempty"`
LicenceType string `json:"LicenceType,omitempty"`
ImageType string `json:"ImageType,omitempty"`
Public bool `json:"Public,omitempty"`
ImageAliases []string `json:"ImageAliases,omitempty"`
CloudInit string `json:"CloudInit,omitempty"`
CreatedBy string `json:"CreatedBy,omitempty"`
CreatedByUserId string `json:"CreatedByUserId,omitempty"`
CreatedDate time.Time `json:"CreatedDate,omitempty"`
}
func getImagePrint(resp *resources.Response, c *core.CommandConfig, imgs []resources.Image) printer.Result {
r := printer.Result{}
if c != nil {
if resp != nil {
r.ApiResponse = resp
r.Resource = c.Resource
r.Verb = c.Verb
r.WaitForRequest = viper.GetBool(core.GetFlagName(c.NS, constants.ArgWaitForRequest))
}
if imgs != nil {
r.OutputJSON = imgs
r.KeyValue = getImagesKVMaps(imgs)
r.Columns = printer.GetHeaders(allImageCols, defaultImageCols, viper.GetStringSlice(core.GetFlagName(c.Resource, constants.ArgCols)))
}
}
return r
}
func getImages(images resources.Images) []resources.Image {
imgs := make([]resources.Image, 0)
if items, ok := images.GetItemsOk(); ok && items != nil {
for _, d := range *items {
imgs = append(imgs, resources.Image{Image: d})
}
}
return imgs
}
func getImage(image *resources.Image) []resources.Image {
imgs := make([]resources.Image, 0)
if image != nil {
imgs = append(imgs, resources.Image{Image: image.Image})
}
return imgs
}
func getImagesKVMaps(imgs []resources.Image) []map[string]interface{} {
out := make([]map[string]interface{}, 0, len(imgs))
for _, img := range imgs {
o := getImageKVMap(img)
out = append(out, o)
}
return out
}
func getImageKVMap(img resources.Image) map[string]interface{} {
var imgPrint ImagePrint
if idOk, ok := img.GetIdOk(); ok && idOk != nil {
imgPrint.ImageId = *idOk
}
if propertiesOk, ok := img.GetPropertiesOk(); ok && propertiesOk != nil {
if name, ok := propertiesOk.GetNameOk(); ok && name != nil {
imgPrint.Name = *name
}
if description, ok := propertiesOk.GetDescriptionOk(); ok && description != nil {
imgPrint.Description = *description
}
if loc, ok := propertiesOk.GetLocationOk(); ok && loc != nil {
imgPrint.Location = *loc
}
if size, ok := propertiesOk.GetSizeOk(); ok && size != nil {
imgPrint.Size = fmt.Sprintf("%vGB", *size)
}
if licType, ok := propertiesOk.GetLicenceTypeOk(); ok && licType != nil {
imgPrint.LicenceType = *licType
}
if imgType, ok := propertiesOk.GetImageTypeOk(); ok && imgType != nil {
imgPrint.ImageType = *imgType
}
if public, ok := propertiesOk.GetPublicOk(); ok && public != nil {
imgPrint.Public = *public
}
if aliases, ok := propertiesOk.GetImageAliasesOk(); ok && aliases != nil {
imgPrint.ImageAliases = *aliases
}
if cloudInit, ok := propertiesOk.GetCloudInitOk(); ok && cloudInit != nil {
imgPrint.CloudInit = *cloudInit
}
}
if metadataOk, ok := img.GetMetadataOk(); ok && metadataOk != nil {
if createdDateOk, ok := metadataOk.GetCreatedDateOk(); ok && createdDateOk != nil {
imgPrint.CreatedDate = *createdDateOk
}
if createdByOk, ok := metadataOk.GetCreatedByOk(); ok && createdByOk != nil {
imgPrint.CreatedBy = *createdByOk
}
if createdByUserIdOk, ok := metadataOk.GetCreatedByUserIdOk(); ok && createdByUserIdOk != nil {
imgPrint.CreatedByUserId = *createdByUserIdOk
}
}
return structs.Map(imgPrint)
}
// Output Columns Sorting
func sortImagesByLocation(images resources.Images, location string) resources.Images {
imgLocationItems := make([]ionoscloud.Image, 0)
if items, ok := images.GetItemsOk(); ok && items != nil {
for _, img := range *items {
properties := img.GetProperties()
if loc, ok := properties.GetLocationOk(); ok && loc != nil {
if *loc == location {
imgLocationItems = append(imgLocationItems, img)
}
}
}
}
images.Items = &imgLocationItems
return images
}
func sortImagesByLicenceType(images resources.Images, licenceType string) resources.Images {
imgLicenceTypeItems := make([]ionoscloud.Image, 0)
if items, ok := images.GetItemsOk(); ok && items != nil {
for _, img := range *items {
properties := img.GetProperties()
if imgLicenceType, ok := properties.GetLicenceTypeOk(); ok && imgLicenceType != nil {
if *imgLicenceType == licenceType {
imgLicenceTypeItems = append(imgLicenceTypeItems, img)
}
}
}
}
images.Items = &imgLicenceTypeItems
return images
}
func sortImagesByType(images resources.Images, imgType string) resources.Images {
imgTypeItems := make([]ionoscloud.Image, 0)
if items, ok := images.GetItemsOk(); ok && items != nil {
for _, img := range *items {
properties := img.GetProperties()
if t, ok := properties.GetImageTypeOk(); ok && t != nil {
if *t == imgType {
imgTypeItems = append(imgTypeItems, img)
}
}
}
}
images.Items = &imgTypeItems
return images
}
func sortImagesByAlias(images resources.Images, alias string) resources.Images {
imgTypeItems := make([]ionoscloud.Image, 0)
if items, ok := images.GetItemsOk(); ok && items != nil {
for _, img := range *items {
properties := img.GetProperties()
if imageAliasesOk, ok := properties.GetImageAliasesOk(); ok && imageAliasesOk != nil {
for _, imageAliaseOk := range *imageAliasesOk {
if strings.Contains(imageAliaseOk, alias) {
imgTypeItems = append(imgTypeItems, img)
}
}
}
}
}
images.Items = &imgTypeItems
return images
}
func sortImagesByTime(images resources.Images, n int) resources.Images {
if items, ok := images.GetItemsOk(); ok && items != nil {
imageItems := *items
if len(imageItems) > 0 {
// Sort Requests using time.Time, in descending order
sort.SliceStable(imageItems, func(i, j int) bool {
return imageItems[i].Metadata.CreatedDate.Time.After(imageItems[j].Metadata.CreatedDate.Time)
})
}
if len(imageItems) >= n {
imageItems = imageItems[:n]
}
images.Items = &imageItems
}
return images
}