Skip to content

Commit 837a5d8

Browse files
authored
Support for creating instance with custom volume type (#475)
* Support for creating instance with custom volume type
1 parent 6ecc10d commit 837a5d8

File tree

5 files changed

+33
-4
lines changed

5 files changed

+33
-4
lines changed

cmd/instance/instance.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ func init() {
5959
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")
6060
instanceCreateCmd.Flags().BoolVar(&skipShebangCheck, "skip-shebang-check", false, "skip the shebang line check when passing a user init script")
6161
instanceCreateCmd.Flags().StringSliceVarP(&volumes, "volumes", "v", []string{}, "List of volumes to attach at boot")
62+
instanceCreateCmd.Flags().StringVarP(&volumetype, "volume-type", "", "", "Specify the volume type for the instance")
6263

6364
instanceStopCmd.Flags().BoolVarP(&waitStop, "wait", "w", false, "wait until the instance's is stoped")
6465
}

cmd/instance/instance_create.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
)
1919

2020
var wait bool
21-
var hostnameCreate, size, diskimage, publicip, initialuser, sshkey, tags, network, privateIPv4, reservedIPv4, firewall string
21+
var hostnameCreate, size, diskimage, publicip, initialuser, sshkey, tags, network, privateIPv4, reservedIPv4, firewall, volumetype string
2222
var script string
2323
var skipShebangCheck bool
2424
var volumes []string
@@ -150,6 +150,13 @@ If you wish to use a custom format, the available fields are:
150150
config.ReservedIPv4 = reservedIPv4
151151
}
152152

153+
if volumetype != "" {
154+
if !validateAndSetVolumeType(client, volumetype, config) {
155+
utility.Error("The provided volume type is not valid")
156+
os.Exit(1)
157+
}
158+
}
159+
153160
// Set private_ipv4 if provided
154161
if privateIPv4 != "" {
155162
config.PrivateIPv4 = privateIPv4
@@ -347,3 +354,23 @@ If you wish to use a custom format, the available fields are:
347354
}
348355
},
349356
}
357+
358+
// Helper function to validate volume type and set it in config
359+
func validateAndSetVolumeType(client *civogo.Client, volumetype string, config *civogo.InstanceConfig) bool {
360+
// Fetch volume types from Civo API
361+
volumeTypes, err := client.ListVolumeTypes()
362+
if err != nil {
363+
utility.Error("Unable to list volume types %s", err)
364+
os.Exit(1)
365+
}
366+
367+
// Check if the provided volume type is valid
368+
for _, v := range volumeTypes {
369+
if v.Name == volumetype {
370+
config.VolumeType = v.Name
371+
return true // Volume type is valid
372+
}
373+
}
374+
375+
return false // Volume type is not valid
376+
}

cmd/instance/instance_show.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ If you wish to use a custom format, the available fields are:
6969
ow.AppendDataWithLabel("hostname", instance.Hostname, "Hostname")
7070
ow.AppendDataWithLabel("status", utility.ColorStatus(instance.Status), "Status")
7171
ow.AppendDataWithLabel("size", instance.Size, "Size")
72+
ow.AppendDataWithLabel("volume-type", instance.VolumeType, "Volume Type")
7273
ow.AppendDataWithLabel("cpu_cores", strconv.Itoa(instance.CPUCores), "Cpu Cores")
7374
ow.AppendDataWithLabel("ram_mb", strconv.Itoa(instance.RAMMegabytes), "Ram")
7475
ow.AppendDataWithLabel("disk_gb", strconv.Itoa(instance.DiskGigabytes), "SSD disk")

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ require (
99
github.com/bradfitz/iter v0.0.0-20191230175014-e8f45d346db8 // indirect
1010
github.com/briandowns/spinner v1.11.1
1111
github.com/c4milo/unpackit v0.0.0-20170704181138-4ed373e9ef1c // indirect
12-
github.com/civo/civogo v0.3.82
12+
github.com/civo/civogo v0.3.84
1313
github.com/dsnet/compress v0.0.1 // indirect
1414
github.com/fatih/color v1.13.0 // indirect
1515
github.com/google/go-github v17.0.0+incompatible // indirect

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ github.com/briandowns/spinner v1.11.1/go.mod h1:QOuQk7x+EaDASo80FEXwlwiA+j/PPIcX
5454
github.com/c4milo/unpackit v0.0.0-20170704181138-4ed373e9ef1c h1:aprLqMn7gSPT+vdDSl+/E6NLEuArwD/J7IWd8bJt5lQ=
5555
github.com/c4milo/unpackit v0.0.0-20170704181138-4ed373e9ef1c/go.mod h1:Ie6SubJv/NTO9Q0UBH0QCl3Ve50lu9hjbi5YJUw03TE=
5656
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
57-
github.com/civo/civogo v0.3.82 h1:5UD1BrYzJ851BCnaViAW9GvrCxzU0Dgz9gFEMgI/2zY=
58-
github.com/civo/civogo v0.3.82/go.mod h1:7UCYX+qeeJbrG55E1huv+0ySxcHTqq/26FcHLVelQJM=
57+
github.com/civo/civogo v0.3.84 h1:jf5IT7VJFPaReO6g8B0zqKhsYCIizaGo4PjDLY7Sl6Y=
58+
github.com/civo/civogo v0.3.84/go.mod h1:7UCYX+qeeJbrG55E1huv+0ySxcHTqq/26FcHLVelQJM=
5959
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
6060
github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk=
6161
github.com/coreos/etcd v3.3.13+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=

0 commit comments

Comments
 (0)