From 8b6753157daadac1670cb59011f055747b7cc198 Mon Sep 17 00:00:00 2001 From: Uzair Ali <72073401+uzaxirr@users.noreply.github.com> Date: Thu, 3 Oct 2024 13:24:21 +0530 Subject: [PATCH 1/2] Support for creating instance with custom volume type --- cmd/instance/instance.go | 1 + cmd/instance/instance_create.go | 29 ++++++++++++++++++++++++++++- cmd/instance/instance_show.go | 1 + go.mod | 2 ++ go.sum | 2 -- 5 files changed, 32 insertions(+), 3 deletions(-) diff --git a/cmd/instance/instance.go b/cmd/instance/instance.go index 3ac9251e..656aa1f3 100644 --- a/cmd/instance/instance.go +++ b/cmd/instance/instance.go @@ -59,6 +59,7 @@ func init() { instanceCreateCmd.Flags().StringVar(&script, "script", "", "path to a script that will be uploaded to /usr/local/bin/civo-user-init-script on your instance, read/write/executable only by root and then will be executed at the end of the cloud initialization") instanceCreateCmd.Flags().BoolVar(&skipShebangCheck, "skip-shebang-check", false, "skip the shebang line check when passing a user init script") instanceCreateCmd.Flags().StringSliceVarP(&volumes, "volumes", "v", []string{}, "List of volumes to attach at boot") + instanceCreateCmd.Flags().StringVarP(&volumetype, "volume-type", "", "", "Specify the volume type for the instance") instanceStopCmd.Flags().BoolVarP(&waitStop, "wait", "w", false, "wait until the instance's is stoped") } diff --git a/cmd/instance/instance_create.go b/cmd/instance/instance_create.go index c6871e4c..d1b1630a 100644 --- a/cmd/instance/instance_create.go +++ b/cmd/instance/instance_create.go @@ -18,7 +18,7 @@ import ( ) var wait bool -var hostnameCreate, size, diskimage, publicip, initialuser, sshkey, tags, network, privateIPv4, reservedIPv4, firewall string +var hostnameCreate, size, diskimage, publicip, initialuser, sshkey, tags, network, privateIPv4, reservedIPv4, firewall, volumetype string var script string var skipShebangCheck bool var volumes []string @@ -150,6 +150,13 @@ If you wish to use a custom format, the available fields are: config.ReservedIPv4 = reservedIPv4 } + if volumetype != "" { + if !validateAndSetVolumeType(client, volumetype, config) { + utility.Error("The provided volume type is not valid") + os.Exit(1) + } + } + // Set private_ipv4 if provided if privateIPv4 != "" { config.PrivateIPv4 = privateIPv4 @@ -347,3 +354,23 @@ If you wish to use a custom format, the available fields are: } }, } + +// Helper function to validate volume type and set it in config +func validateAndSetVolumeType(client *civogo.Client, volumetype string, config *civogo.InstanceConfig) bool { + // Fetch volume types from Civo API + volumeTypes, err := client.ListVolumeTypes() + if err != nil { + utility.Error("Unable to list volume types %s", err) + os.Exit(1) + } + + // Check if the provided volume type is valid + for _, v := range volumeTypes { + if v.Name == volumetype { + config.VolumeType = v.Name + return true // Volume type is valid + } + } + + return false // Volume type is not valid +} diff --git a/cmd/instance/instance_show.go b/cmd/instance/instance_show.go index 183f5e94..c8ba467e 100644 --- a/cmd/instance/instance_show.go +++ b/cmd/instance/instance_show.go @@ -69,6 +69,7 @@ If you wish to use a custom format, the available fields are: ow.AppendDataWithLabel("hostname", instance.Hostname, "Hostname") ow.AppendDataWithLabel("status", utility.ColorStatus(instance.Status), "Status") ow.AppendDataWithLabel("size", instance.Size, "Size") + ow.AppendDataWithLabel("volume-type", instance.VolumeType, "Volume Type") ow.AppendDataWithLabel("cpu_cores", strconv.Itoa(instance.CPUCores), "Cpu Cores") ow.AppendDataWithLabel("ram_mb", strconv.Itoa(instance.RAMMegabytes), "Ram") ow.AppendDataWithLabel("disk_gb", strconv.Itoa(instance.DiskGigabytes), "SSD disk") diff --git a/go.mod b/go.mod index 6de310ec..c71676bb 100644 --- a/go.mod +++ b/go.mod @@ -80,3 +80,5 @@ require ( sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect ) + +replace github.com/civo/civogo => /Users/uzair/Work/civogo diff --git a/go.sum b/go.sum index b8fda7a8..c0272bce 100644 --- a/go.sum +++ b/go.sum @@ -54,8 +54,6 @@ github.com/briandowns/spinner v1.11.1/go.mod h1:QOuQk7x+EaDASo80FEXwlwiA+j/PPIcX github.com/c4milo/unpackit v0.0.0-20170704181138-4ed373e9ef1c h1:aprLqMn7gSPT+vdDSl+/E6NLEuArwD/J7IWd8bJt5lQ= github.com/c4milo/unpackit v0.0.0-20170704181138-4ed373e9ef1c/go.mod h1:Ie6SubJv/NTO9Q0UBH0QCl3Ve50lu9hjbi5YJUw03TE= github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= -github.com/civo/civogo v0.3.82 h1:5UD1BrYzJ851BCnaViAW9GvrCxzU0Dgz9gFEMgI/2zY= -github.com/civo/civogo v0.3.82/go.mod h1:7UCYX+qeeJbrG55E1huv+0ySxcHTqq/26FcHLVelQJM= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk= github.com/coreos/etcd v3.3.13+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE= From fea21b86697704f2e49e873407472a42256cf2c4 Mon Sep 17 00:00:00 2001 From: Uzair Ali <72073401+uzaxirr@users.noreply.github.com> Date: Mon, 7 Oct 2024 10:44:31 +0530 Subject: [PATCH 2/2] update civogo --- go.mod | 4 +--- go.sum | 2 ++ 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index c71676bb..b0dc2162 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,7 @@ require ( github.com/bradfitz/iter v0.0.0-20191230175014-e8f45d346db8 // indirect github.com/briandowns/spinner v1.11.1 github.com/c4milo/unpackit v0.0.0-20170704181138-4ed373e9ef1c // indirect - github.com/civo/civogo v0.3.82 + github.com/civo/civogo v0.3.84 github.com/dsnet/compress v0.0.1 // indirect github.com/fatih/color v1.13.0 // indirect github.com/google/go-github v17.0.0+incompatible // indirect @@ -80,5 +80,3 @@ require ( sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect ) - -replace github.com/civo/civogo => /Users/uzair/Work/civogo diff --git a/go.sum b/go.sum index c0272bce..f4933630 100644 --- a/go.sum +++ b/go.sum @@ -54,6 +54,8 @@ github.com/briandowns/spinner v1.11.1/go.mod h1:QOuQk7x+EaDASo80FEXwlwiA+j/PPIcX github.com/c4milo/unpackit v0.0.0-20170704181138-4ed373e9ef1c h1:aprLqMn7gSPT+vdDSl+/E6NLEuArwD/J7IWd8bJt5lQ= github.com/c4milo/unpackit v0.0.0-20170704181138-4ed373e9ef1c/go.mod h1:Ie6SubJv/NTO9Q0UBH0QCl3Ve50lu9hjbi5YJUw03TE= github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= +github.com/civo/civogo v0.3.84 h1:jf5IT7VJFPaReO6g8B0zqKhsYCIizaGo4PjDLY7Sl6Y= +github.com/civo/civogo v0.3.84/go.mod h1:7UCYX+qeeJbrG55E1huv+0ySxcHTqq/26FcHLVelQJM= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk= github.com/coreos/etcd v3.3.13+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=