Skip to content

Commit

Permalink
Support for creating instance with custom volume type (#475)
Browse files Browse the repository at this point in the history
* Support for creating instance with custom volume type
  • Loading branch information
uzaxirr authored Oct 7, 2024
1 parent 6ecc10d commit 837a5d8
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 4 deletions.
1 change: 1 addition & 0 deletions cmd/instance/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
29 changes: 28 additions & 1 deletion cmd/instance/instance_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
1 change: 1 addition & 0 deletions cmd/instance/instance_show.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +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.82 h1:5UD1BrYzJ851BCnaViAW9GvrCxzU0Dgz9gFEMgI/2zY=
github.com/civo/civogo v0.3.82/go.mod h1:7UCYX+qeeJbrG55E1huv+0ySxcHTqq/26FcHLVelQJM=
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=
Expand Down

0 comments on commit 837a5d8

Please sign in to comment.