Skip to content

Commit 70a9b68

Browse files
authored
Merge pull request #41 from serverscom/cloud-volumes
Cloud volumes cmd
2 parents 171c190 + 6d8a5a2 commit 70a9b68

25 files changed

Lines changed: 1287 additions & 0 deletions

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ generate: deps
2222
mockgen --destination ./internal/mocks/network_pool_service.go --package=mocks --source ./vendor/github.com/serverscom/serverscom-go-client/pkg/network_pools.go
2323
mockgen --destination ./internal/mocks/cloud_instances_service.go --package=mocks --source ./vendor/github.com/serverscom/serverscom-go-client/pkg/cloud_computing_instances.go
2424
mockgen --destination ./internal/mocks/cloud_computing_regions_service.go --package=mocks --source ./vendor/github.com/serverscom/serverscom-go-client/pkg/cloud_computing_regions.go
25+
mockgen --destination ./internal/mocks/cloud_block_storage_volumes_service.go --package=mocks --source ./vendor/github.com/serverscom/serverscom-go-client/pkg/cloud_block_storage_volumes.go
2526
sed -i '' 's|github.com/serverscom/srvctl/vendor/github.com/serverscom/serverscom-go-client/pkg|github.com/serverscom/serverscom-go-client/pkg|g' \
2627
./internal/mocks/ssh_service.go \
2728
./internal/mocks/hosts_service.go \
@@ -36,5 +37,6 @@ generate: deps
3637
./internal/mocks/kubernetes_clusters_service.go \
3738
./internal/mocks/l2_segment_service.go \
3839
./internal/mocks/network_pool_service.go \
40+
./internal/mocks/cloud_block_storage_volumes_service.go \
3941
./internal/mocks/cloud_instances_service.go \
4042
./internal/mocks/cloud_computing_regions_service.go

cmd/entities/cloud-volumes/add.go

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package cloudvolumes
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+
InputPath string
11+
Name string
12+
RegionID int
13+
Size int
14+
Description string
15+
ImageID string
16+
SnapshotID string
17+
AttachInstanceID string
18+
BackupID string
19+
Labels []string
20+
}
21+
22+
func newAddCmd(cmdContext *base.CmdContext) *cobra.Command {
23+
flags := &AddFlags{}
24+
25+
cmd := &cobra.Command{
26+
Use: "add --input <path>",
27+
Short: "Add a cloud volume",
28+
Long: "Add a new cloud volume to a Cloud region",
29+
Args: cobra.ExactArgs(0),
30+
RunE: func(cmd *cobra.Command, args []string) error {
31+
manager := cmdContext.GetManager()
32+
33+
ctx, cancel := base.SetupContext(cmd, manager)
34+
defer cancel()
35+
36+
base.SetupProxy(cmd, manager)
37+
38+
input := &serverscom.CloudBlockStorageVolumeCreateInput{}
39+
40+
if flags.InputPath != "" {
41+
if err := base.ReadInputJSON(flags.InputPath, cmd.InOrStdin(), input); err != nil {
42+
return err
43+
}
44+
} else {
45+
required := []string{"name", "region-id"}
46+
if err := base.ValidateFlags(cmd, required); err != nil {
47+
return err
48+
}
49+
}
50+
51+
if err := flags.FillInput(cmd, input); err != nil {
52+
return err
53+
}
54+
55+
scClient := cmdContext.GetClient().SetVerbose(manager.GetVerbose(cmd)).GetScClient()
56+
volume, err := scClient.CloudBlockStorageVolumes.Create(ctx, *input)
57+
if err != nil {
58+
return err
59+
}
60+
61+
if volume != nil {
62+
formatter := cmdContext.GetOrCreateFormatter(cmd)
63+
return formatter.Format(volume)
64+
}
65+
return nil
66+
},
67+
}
68+
69+
cmd.Flags().StringVarP(&flags.InputPath, "input", "i", "", "path to input file or '-' to read from stdin")
70+
cmd.Flags().StringVarP(&flags.Name, "name", "n", "", "A name of the cloud volume")
71+
cmd.Flags().IntVar(&flags.RegionID, "region-id", 0, "ID of the cloud region")
72+
cmd.Flags().IntVar(&flags.Size, "size", 0, "Size of the volume in GB")
73+
cmd.Flags().StringVar(&flags.Description, "description", "", "Description of the volume")
74+
cmd.Flags().StringVar(&flags.ImageID, "image-id", "", "ID of the image to create volume from")
75+
cmd.Flags().StringVar(&flags.SnapshotID, "snapshot-id", "", "ID of the snapshot to create volume from")
76+
cmd.Flags().StringVar(&flags.AttachInstanceID, "attach-instance-id", "", "ID of the instance to attach volume to")
77+
cmd.Flags().StringVar(&flags.BackupID, "backup-id", "", "ID of the backup to create volume from")
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.CloudBlockStorageVolumeCreateInput) error {
84+
if cmd.Flags().Changed("name") {
85+
input.Name = f.Name
86+
}
87+
if cmd.Flags().Changed("region-id") {
88+
input.RegionID = f.RegionID
89+
}
90+
if cmd.Flags().Changed("size") {
91+
input.Size = f.Size
92+
}
93+
if cmd.Flags().Changed("description") {
94+
input.Description = f.Description
95+
}
96+
if cmd.Flags().Changed("image-id") {
97+
input.ImageID = f.ImageID
98+
}
99+
if cmd.Flags().Changed("snapshot-id") {
100+
input.SnapshotID = f.SnapshotID
101+
}
102+
if cmd.Flags().Changed("attach-instance-id") {
103+
input.AttachInstanceID = f.AttachInstanceID
104+
}
105+
if cmd.Flags().Changed("backup-id") {
106+
input.BackupID = f.BackupID
107+
}
108+
if cmd.Flags().Changed("label") {
109+
labelsMap, err := base.ParseLabels(f.Labels)
110+
if err != nil {
111+
return err
112+
}
113+
input.Labels = labelsMap
114+
}
115+
116+
return nil
117+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package cloudvolumes
2+
3+
import (
4+
"log"
5+
6+
serverscom "github.com/serverscom/serverscom-go-client/pkg"
7+
"github.com/serverscom/srvctl/cmd/base"
8+
"github.com/serverscom/srvctl/internal/output/entities"
9+
"github.com/spf13/cobra"
10+
)
11+
12+
func NewCmd(cmdContext *base.CmdContext) *cobra.Command {
13+
cloudVolumeEntity, err := entities.Registry.GetEntityFromValue(serverscom.CloudBlockStorageVolume{})
14+
if err != nil {
15+
log.Fatal(err)
16+
}
17+
entitiesMap := make(map[string]entities.EntityInterface)
18+
entitiesMap["cloud-volumes"] = cloudVolumeEntity
19+
cmd := &cobra.Command{
20+
Use: "cloud-volumes",
21+
Short: "Manage cloud volumes",
22+
PersistentPreRunE: base.CombinePreRunE(
23+
base.CheckFormatterFlags(cmdContext, entitiesMap),
24+
base.CheckEmptyContexts(cmdContext),
25+
),
26+
Args: base.NoArgs,
27+
Run: base.UsageRun,
28+
}
29+
30+
cmd.AddCommand(
31+
newListCmd(cmdContext),
32+
newAddCmd(cmdContext),
33+
newGetCmd(cmdContext),
34+
newUpdateCmd(cmdContext),
35+
newDeleteCmd(cmdContext),
36+
newVolumeAttachCmd(cmdContext),
37+
newVolumeDetachCmd(cmdContext),
38+
)
39+
40+
base.AddFormatFlags(cmd)
41+
42+
return cmd
43+
}

0 commit comments

Comments
 (0)