Skip to content

Commit

Permalink
feat: add support for ssd flag
Browse files Browse the repository at this point in the history
  • Loading branch information
abdo-farag committed Dec 10, 2022
1 parent ad2c618 commit 853bc7c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
12 changes: 12 additions & 0 deletions proxmox/virtual_environment_vm_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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))
}
Expand Down Expand Up @@ -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]
}
Expand Down
6 changes: 4 additions & 2 deletions proxmox/virtual_environment_vm_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,27 @@ 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,
FileVolume: "nfs:2041/vm-2041-disk-0.raw",
Format: strPtr("raw"),
IOThread: boolPtr(true),
Size: strPtr("8G"),
SSD: boolPtr(true),
},
},
}
Expand Down
22 changes: 22 additions & 0 deletions proxmoxtf/resource_virtual_environment_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const (
dvResourceVirtualEnvironmentVMDiskFileID = ""
dvResourceVirtualEnvironmentVMDiskSize = 8
dvResourceVirtualEnvironmentVMDiskIOThread = false
dvResourceVirtualEnvironmentVMDiskSSD = false
dvResourceVirtualEnvironmentVMDiskDiscard = ""
dvResourceVirtualEnvironmentVMDiskSpeedRead = 0
dvResourceVirtualEnvironmentVMDiskSpeedReadBurstable = 0
Expand Down Expand Up @@ -139,6 +140,7 @@ const (
mkResourceVirtualEnvironmentVMDiskFileID = "file_id"
mkResourceVirtualEnvironmentVMDiskSize = "size"
mkResourceVirtualEnvironmentVMDiskIOThread = "iothread"
mkResourceVirtualEnvironmentVMDiskSSD = "ssd"
mkResourceVirtualEnvironmentVMDiskDiscard = "discard"
mkResourceVirtualEnvironmentVMDiskSpeed = "speed"
mkResourceVirtualEnvironmentVMDiskSpeedRead = "read"
Expand Down Expand Up @@ -490,6 +492,7 @@ func resourceVirtualEnvironmentVM() *schema.Resource {
mkResourceVirtualEnvironmentVMDiskInterface: dvResourceVirtualEnvironmentVMDiskInterface,
mkResourceVirtualEnvironmentVMDiskSize: dvResourceVirtualEnvironmentVMDiskSize,
mkResourceVirtualEnvironmentVMDiskIOThread: dvResourceVirtualEnvironmentVMDiskIOThread,
mkResourceVirtualEnvironmentVMDiskSSD: dvResourceVirtualEnvironmentVMDiskSSD,
mkResourceVirtualEnvironmentVMDiskDiscard: dvResourceVirtualEnvironmentVMDiskDiscard,
},
}, nil
Expand Down Expand Up @@ -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.",
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 853bc7c

Please sign in to comment.