|
| 1 | +package rbsvolumes |
| 2 | + |
| 3 | +import ( |
| 4 | + serverscom "github.com/serverscom/serverscom-go-client/pkg" |
| 5 | + "github.com/serverscom/srvctl/cmd/base" |
| 6 | + "github.com/spf13/cobra" |
| 7 | +) |
| 8 | + |
| 9 | +type AddFlags struct { |
| 10 | + Skeleton bool |
| 11 | + InputPath string |
| 12 | + Name string |
| 13 | + Size int64 |
| 14 | + LocationID int |
| 15 | + FlavorID int |
| 16 | + Labels []string |
| 17 | +} |
| 18 | + |
| 19 | +func newAddCmd(cmdContext *base.CmdContext) *cobra.Command { |
| 20 | + flags := &AddFlags{} |
| 21 | + |
| 22 | + cmd := &cobra.Command{ |
| 23 | + Use: "add", |
| 24 | + Short: "Add a RBS volume", |
| 25 | + Long: "Add a new Remote Block Storage volume", |
| 26 | + Args: cobra.ExactArgs(0), |
| 27 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 28 | + formatter := cmdContext.GetOrCreateFormatter(cmd) |
| 29 | + |
| 30 | + if flags.Skeleton { |
| 31 | + return formatter.FormatSkeleton("rbs-volumes/add.json") |
| 32 | + } |
| 33 | + |
| 34 | + manager := cmdContext.GetManager() |
| 35 | + |
| 36 | + ctx, cancel := base.SetupContext(cmd, manager) |
| 37 | + defer cancel() |
| 38 | + |
| 39 | + base.SetupProxy(cmd, manager) |
| 40 | + |
| 41 | + input := &serverscom.RemoteBlockStorageVolumeCreateInput{} |
| 42 | + |
| 43 | + if flags.InputPath != "" { |
| 44 | + if err := base.ReadInputJSON(flags.InputPath, cmd.InOrStdin(), input); err != nil { |
| 45 | + return err |
| 46 | + } |
| 47 | + } else { |
| 48 | + required := []string{"name", "size", "flavor-id"} |
| 49 | + if err := base.ValidateFlags(cmd, required); err != nil { |
| 50 | + return err |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + if err := flags.FillInput(cmd, input); err != nil { |
| 55 | + return err |
| 56 | + } |
| 57 | + |
| 58 | + scClient := cmdContext.GetClient().SetVerbose(manager.GetVerbose(cmd)).GetScClient() |
| 59 | + volume, err := scClient.RemoteBlockStorageVolumes.Create(ctx, *input) |
| 60 | + if err != nil { |
| 61 | + return err |
| 62 | + } |
| 63 | + |
| 64 | + if volume != nil { |
| 65 | + return formatter.Format(volume) |
| 66 | + } |
| 67 | + return nil |
| 68 | + }, |
| 69 | + } |
| 70 | + |
| 71 | + cmd.Flags().StringVarP(&flags.InputPath, "input", "i", "", "path to input file or '-' to read from stdin") |
| 72 | + cmd.Flags().BoolVarP(&flags.Skeleton, "skeleton", "s", false, "JSON object with structure that is required to be passed") |
| 73 | + |
| 74 | + cmd.Flags().StringVarP(&flags.Name, "name", "n", "", "name of the RBS volume") |
| 75 | + cmd.Flags().Int64Var(&flags.Size, "size", 0, "size of the volume in GB") |
| 76 | + cmd.Flags().IntVar(&flags.LocationID, "location-id", 0, "ID of the location") |
| 77 | + cmd.Flags().IntVar(&flags.FlavorID, "flavor-id", 0, "ID of the flavor") |
| 78 | + cmd.Flags().StringArrayVarP(&flags.Labels, "label", "l", []string{}, "string in key=value format") |
| 79 | + |
| 80 | + return cmd |
| 81 | +} |
| 82 | + |
| 83 | +func (f *AddFlags) FillInput(cmd *cobra.Command, input *serverscom.RemoteBlockStorageVolumeCreateInput) error { |
| 84 | + if cmd.Flags().Changed("name") { |
| 85 | + input.Name = f.Name |
| 86 | + } |
| 87 | + if cmd.Flags().Changed("size") { |
| 88 | + input.Size = f.Size |
| 89 | + } |
| 90 | + if cmd.Flags().Changed("location-id") { |
| 91 | + input.LocationID = f.LocationID |
| 92 | + } |
| 93 | + if cmd.Flags().Changed("flavor-id") { |
| 94 | + input.FlavorID = f.FlavorID |
| 95 | + } |
| 96 | + if cmd.Flags().Changed("label") { |
| 97 | + labelsMap, err := base.ParseLabels(f.Labels) |
| 98 | + if err != nil { |
| 99 | + return err |
| 100 | + } |
| 101 | + input.Labels = labelsMap |
| 102 | + } |
| 103 | + |
| 104 | + return nil |
| 105 | +} |
0 commit comments