From 290734655ce28306ae910b76b8de5fedbd3b4bb8 Mon Sep 17 00:00:00 2001 From: Abdelfadeel Farag Date: Tue, 13 Dec 2022 04:31:33 +0100 Subject: [PATCH] feat: add support for "ssd" disk flag for VM (#181) * feat: add support for ssd flag * update docs, add `ssd` to examples * restore original .md formatting Co-authored-by: Pavel Boldyrev <627562+bpg@users.noreply.github.com> --- docs/resources/virtual_environment_vm.md | 5 ++-- example/resource_virtual_environment_vm.tf | 27 ++++++++++++++------ proxmox/virtual_environment_vm_types.go | 14 +++++++++- proxmox/virtual_environment_vm_types_test.go | 6 +++-- proxmoxtf/resource_virtual_environment_vm.go | 22 ++++++++++++++++ 5 files changed, 61 insertions(+), 13 deletions(-) diff --git a/docs/resources/virtual_environment_vm.md b/docs/resources/virtual_environment_vm.md index 05dc2f24..a7ccfd4e 100644 --- a/docs/resources/virtual_environment_vm.md +++ b/docs/resources/virtual_environment_vm.md @@ -183,6 +183,7 @@ output "ubuntu_vm_public_key" { * `description` - (Optional) The description. * `disk` - (Optional) A disk (multiple blocks supported). * `datastore_id` - (Optional) The identifier for the datastore to create the disk in (defaults to `local-lvm`). + * `discard` - (Optional) Whether to pass discard/trim requests to the underlying storage. Supported values are `on`/`ignore` (defaults to `ignore`) * `file_format` - (Optional) The file format (defaults to `qcow2`). * `qcow2` - QEMU Disk Image v2. * `raw` - Raw Disk Image. @@ -190,14 +191,14 @@ output "ubuntu_vm_public_key" { * `file_id` - (Optional) The file ID for a disk image (experimental - might cause high CPU utilization during import, especially with large disk images). * `interface` - (Required) The disk interface for Proxmox, currently scsi, sata and virtio are supported. + * `iothread` - (Optional) Whether to use iothreads for this disk (defaults to `false`). * `size` - (Optional) The disk size in gigabytes (defaults to `8`). * `speed` - (Optional) The speed limits. * `read` - (Optional) The maximum read speed in megabytes per second. * `read_burstable` - (Optional) The maximum burstable read speed in megabytes per second. * `write` - (Optional) The maximum write speed in megabytes per second. * `write_burstable` - (Optional) The maximum burstable write speed in megabytes per second. - * `iothread` - (Optional) Whether to use iothreads for this disk (defaults to `false`). - * `discard` - (Optional) Whether to pass discard/trim requests to the underlying storage. Supported values are `on`/`ignore` (defaults to `ignore`) + * ssd - (Optional) Whether to use an SSD emulation option for this disk (defaults to `false`). Note that SSD emulation is not supported on VirtIO Block drives. * `initialization` - (Optional) The cloud-init configuration. * `datastore_id` - (Optional) The identifier for the datastore to create the cloud-init disk in (defaults to `local-lvm`). diff --git a/example/resource_virtual_environment_vm.tf b/example/resource_virtual_environment_vm.tf index 720cb21e..6e6cc4f9 100644 --- a/example/resource_virtual_environment_vm.tf +++ b/example/resource_virtual_environment_vm.tf @@ -1,3 +1,7 @@ +locals { + datastore_id = element(data.proxmox_virtual_environment_datastores.example.datastore_ids, index(data.proxmox_virtual_environment_datastores.example.datastore_ids, "local-lvm")) +} + resource "proxmox_virtual_environment_vm" "example_template" { agent { enabled = true @@ -5,14 +9,21 @@ resource "proxmox_virtual_environment_vm" "example_template" { description = "Managed by Terraform" - disk { - datastore_id = element(data.proxmox_virtual_environment_datastores.example.datastore_ids, index(data.proxmox_virtual_environment_datastores.example.datastore_ids, "local-lvm")) - file_id = proxmox_virtual_environment_file.ubuntu_cloud_image.id - interface = "virtio0" - discard = "on" - iothread = true - } +# disk { +# datastore_id = local.datastore_id +# file_id = proxmox_virtual_environment_file.ubuntu_cloud_image.id +# interface = "virtio0" +# iothread = true +# } + disk { + datastore_id = local.datastore_id + file_id = proxmox_virtual_environment_file.ubuntu_cloud_image.id + interface = "scsi0" + discard = "on" + ssd = true + } +# # disk { # datastore_id = "nfs" # interface = "scsi1" @@ -21,7 +32,7 @@ resource "proxmox_virtual_environment_vm" "example_template" { # } initialization { - datastore_id = element(data.proxmox_virtual_environment_datastores.example.datastore_ids, index(data.proxmox_virtual_environment_datastores.example.datastore_ids, "local-lvm")) + datastore_id = local.datastore_id dns { server = "1.1.1.1" diff --git a/proxmox/virtual_environment_vm_types.go b/proxmox/virtual_environment_vm_types.go index 4d7cf81e..5b431106 100644 --- a/proxmox/virtual_environment_vm_types.go +++ b/proxmox/virtual_environment_vm_types.go @@ -163,6 +163,7 @@ type CustomStorageDevice struct { FileVolume string `json:"file" url:"file"` Format *string `json:"format,omitempty" url:"format,omitempty"` IOThread *CustomBool `json:"iothread,omitempty" url:"iothread,omitempty,int"` + SSD *CustomBool `json:"ssd,omitempty" url:"ssd,omitempty,int"` MaxReadSpeedMbps *int `json:"mbps_rd,omitempty" url:"mbps_rd,omitempty"` MaxWriteSpeedMbps *int `json:"mbps_wr,omitempty" url:"mbps_wr,omitempty"` Media *string `json:"media,omitempty" url:"media,omitempty"` @@ -1088,6 +1089,14 @@ func (r CustomStorageDevice) EncodeValues(key string, v *url.Values) error { } } + if r.SSD != nil { + if *r.SSD { + values = append(values, "ssd=1") + } else { + values = append(values, "ssd=0") + } + } + if r.Discard != nil && *r.Discard != "" { values = append(values, fmt.Sprintf("discard=%s", *r.Discard)) } @@ -1451,8 +1460,8 @@ func (r *CustomNetworkDevice) UnmarshalJSON(b []byte) error { if err != nil { return err } - r.RateLimit = &fv + case "tag": iv, err := strconv.Atoi(v[1]) @@ -1631,6 +1640,9 @@ func (r *CustomStorageDevice) UnmarshalJSON(b []byte) error { case "iothread": bv := CustomBool(v[1] == "1") r.IOThread = &bv + case "ssd": + bv := CustomBool(v[1] == "1") + r.SSD = &bv case "discard": r.Discard = &v[1] } diff --git a/proxmox/virtual_environment_vm_types_test.go b/proxmox/virtual_environment_vm_types_test.go index bd3c2a8f..53ef3955 100644 --- a/proxmox/virtual_environment_vm_types_test.go +++ b/proxmox/virtual_environment_vm_types_test.go @@ -14,18 +14,19 @@ func TestCustomStorageDevice_UnmarshalJSON(t *testing.T) { }{ { name: "simple volume", - line: `"local-lvm:vm-2041-disk-0,discard=on,iothread=1,size=8G"`, + line: `"local-lvm:vm-2041-disk-0,discard=on,ssd=1,iothread=1,size=8G"`, want: &CustomStorageDevice{ Discard: strPtr("on"), Enabled: true, FileVolume: "local-lvm:vm-2041-disk-0", IOThread: boolPtr(true), Size: strPtr("8G"), + SSD: boolPtr(true), }, }, { name: "raw volume type", - line: `"nfs:2041/vm-2041-disk-0.raw,discard=ignore,iothread=1,size=8G"`, + line: `"nfs:2041/vm-2041-disk-0.raw,discard=ignore,ssd=1,iothread=1,size=8G"`, want: &CustomStorageDevice{ Discard: strPtr("ignore"), Enabled: true, @@ -33,6 +34,7 @@ func TestCustomStorageDevice_UnmarshalJSON(t *testing.T) { Format: strPtr("raw"), IOThread: boolPtr(true), Size: strPtr("8G"), + SSD: boolPtr(true), }, }, } diff --git a/proxmoxtf/resource_virtual_environment_vm.go b/proxmoxtf/resource_virtual_environment_vm.go index 221cd0fe..2408c09d 100644 --- a/proxmoxtf/resource_virtual_environment_vm.go +++ b/proxmoxtf/resource_virtual_environment_vm.go @@ -52,6 +52,7 @@ const ( dvResourceVirtualEnvironmentVMDiskFileID = "" dvResourceVirtualEnvironmentVMDiskSize = 8 dvResourceVirtualEnvironmentVMDiskIOThread = false + dvResourceVirtualEnvironmentVMDiskSSD = false dvResourceVirtualEnvironmentVMDiskDiscard = "" dvResourceVirtualEnvironmentVMDiskSpeedRead = 0 dvResourceVirtualEnvironmentVMDiskSpeedReadBurstable = 0 @@ -138,6 +139,7 @@ const ( mkResourceVirtualEnvironmentVMDiskFileID = "file_id" mkResourceVirtualEnvironmentVMDiskSize = "size" mkResourceVirtualEnvironmentVMDiskIOThread = "iothread" + mkResourceVirtualEnvironmentVMDiskSSD = "ssd" mkResourceVirtualEnvironmentVMDiskDiscard = "discard" mkResourceVirtualEnvironmentVMDiskSpeed = "speed" mkResourceVirtualEnvironmentVMDiskSpeedRead = "read" @@ -488,6 +490,7 @@ func resourceVirtualEnvironmentVM() *schema.Resource { mkResourceVirtualEnvironmentVMDiskInterface: dvResourceVirtualEnvironmentVMDiskInterface, mkResourceVirtualEnvironmentVMDiskSize: dvResourceVirtualEnvironmentVMDiskSize, mkResourceVirtualEnvironmentVMDiskIOThread: dvResourceVirtualEnvironmentVMDiskIOThread, + mkResourceVirtualEnvironmentVMDiskSSD: dvResourceVirtualEnvironmentVMDiskSSD, mkResourceVirtualEnvironmentVMDiskDiscard: dvResourceVirtualEnvironmentVMDiskDiscard, }, }, nil @@ -534,6 +537,12 @@ func resourceVirtualEnvironmentVM() *schema.Resource { Optional: true, Default: dvResourceVirtualEnvironmentVMDiskIOThread, }, + mkResourceVirtualEnvironmentVMDiskSSD: { + Type: schema.TypeBool, + Description: "Whether to use ssd for this disk drive", + Optional: true, + Default: dvResourceVirtualEnvironmentVMDiskSSD, + }, mkResourceVirtualEnvironmentVMDiskDiscard: { Type: schema.TypeString, Description: "Whether to pass discard/trim requests to the underlying storage.", @@ -1913,6 +1922,7 @@ func resourceVirtualEnvironmentVMCreateCustomDisks(ctx context.Context, d *schem speed := block[mkResourceVirtualEnvironmentVMDiskSpeed].([]interface{}) diskInterface, _ := block[mkResourceVirtualEnvironmentVMDiskInterface].(string) ioThread := proxmox.CustomBool(block[mkResourceVirtualEnvironmentVMDiskIOThread].(bool)) + ssd := proxmox.CustomBool(block[mkResourceVirtualEnvironmentVMDiskSSD].(bool)) discard, _ := block[mkResourceVirtualEnvironmentVMDiskDiscard].(string) if len(speed) == 0 { @@ -1935,6 +1945,10 @@ func resourceVirtualEnvironmentVMCreateCustomDisks(ctx context.Context, d *schem diskOptions += ",iothread=1" } + if ssd { + diskOptions += ",ssd=1" + } + if discard != "" { diskOptions += fmt.Sprintf(",discard=%s", discard) } @@ -2232,6 +2246,7 @@ func resourceVirtualEnvironmentVMGetDiskDeviceObjects(d *schema.ResourceData, di size, _ := block[mkResourceVirtualEnvironmentVMDiskSize].(int) diskInterface, _ := block[mkResourceVirtualEnvironmentVMDiskInterface].(string) ioThread := proxmox.CustomBool(block[mkResourceVirtualEnvironmentVMDiskIOThread].(bool)) + ssd := proxmox.CustomBool(block[mkResourceVirtualEnvironmentVMDiskSSD].(bool)) discard := block[mkResourceVirtualEnvironmentVMDiskDiscard].(string) speedBlock, err := getSchemaBlock(resource, d, []string{mkResourceVirtualEnvironmentVMDisk, mkResourceVirtualEnvironmentVMDiskSpeed}, 0, false) @@ -2253,6 +2268,7 @@ func resourceVirtualEnvironmentVMGetDiskDeviceObjects(d *schema.ResourceData, di diskDevice.Size = &sizeString diskDevice.SizeInt = &size diskDevice.IOThread = &ioThread + diskDevice.SSD = &ssd diskDevice.Discard = &discard if len(speedBlock) > 0 { @@ -2774,6 +2790,12 @@ func resourceVirtualEnvironmentVMReadCustom(ctx context.Context, d *schema.Resou disk[mkResourceVirtualEnvironmentVMDiskIOThread] = false } + if dd.SSD != nil { + disk[mkResourceVirtualEnvironmentVMDiskSSD] = *dd.SSD + } else { + disk[mkResourceVirtualEnvironmentVMDiskSSD] = false + } + if dd.Discard != nil { disk[mkResourceVirtualEnvironmentVMDiskDiscard] = *dd.Discard } else {