From 853bc7c14bd89720a5375862401b23c0f47ab977 Mon Sep 17 00:00:00 2001 From: Abdelfadeel Farag Date: Sat, 10 Dec 2022 20:15:37 +0100 Subject: [PATCH] feat: add support for ssd flag --- proxmox/virtual_environment_vm_types.go | 12 +++++++++++ proxmox/virtual_environment_vm_types_test.go | 6 ++++-- proxmoxtf/resource_virtual_environment_vm.go | 22 ++++++++++++++++++++ 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/proxmox/virtual_environment_vm_types.go b/proxmox/virtual_environment_vm_types.go index 7b1809e0e..4ad6de7b6 100644 --- a/proxmox/virtual_environment_vm_types.go +++ b/proxmox/virtual_environment_vm_types.go @@ -164,6 +164,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"` @@ -1079,6 +1080,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)) } @@ -1630,6 +1639,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 bd3c2a8fd..53ef39558 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 7c7571599..b1e8842d9 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 @@ -139,6 +140,7 @@ const ( mkResourceVirtualEnvironmentVMDiskFileID = "file_id" mkResourceVirtualEnvironmentVMDiskSize = "size" mkResourceVirtualEnvironmentVMDiskIOThread = "iothread" + mkResourceVirtualEnvironmentVMDiskSSD = "ssd" mkResourceVirtualEnvironmentVMDiskDiscard = "discard" mkResourceVirtualEnvironmentVMDiskSpeed = "speed" mkResourceVirtualEnvironmentVMDiskSpeedRead = "read" @@ -490,6 +492,7 @@ func resourceVirtualEnvironmentVM() *schema.Resource { mkResourceVirtualEnvironmentVMDiskInterface: dvResourceVirtualEnvironmentVMDiskInterface, mkResourceVirtualEnvironmentVMDiskSize: dvResourceVirtualEnvironmentVMDiskSize, mkResourceVirtualEnvironmentVMDiskIOThread: dvResourceVirtualEnvironmentVMDiskIOThread, + mkResourceVirtualEnvironmentVMDiskSSD: dvResourceVirtualEnvironmentVMDiskSSD, mkResourceVirtualEnvironmentVMDiskDiscard: dvResourceVirtualEnvironmentVMDiskDiscard, }, }, nil @@ -536,6 +539,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.", @@ -1860,6 +1869,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 { @@ -1882,6 +1892,10 @@ func resourceVirtualEnvironmentVMCreateCustomDisks(ctx context.Context, d *schem diskOptions += ",iothread=1" } + if ssd { + diskOptions += ",ssd=1" + } + if discard != "" { diskOptions += fmt.Sprintf(",discard=%s", discard) } @@ -2179,6 +2193,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) @@ -2200,6 +2215,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 { @@ -2726,6 +2742,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 {