From de349523fedac8185468620a7088c55deefdbd79 Mon Sep 17 00:00:00 2001 From: Pavel Boldyrev <627562+bpg@users.noreply.github.com> Date: Mon, 10 Jun 2024 21:40:03 -0400 Subject: [PATCH] feat(vm): add support for `disk.serial` attribute (#1385) * feat(vm): add support for `disk.serial` attribute Signed-off-by: Pavel Boldyrev <627562+bpg@users.noreply.github.com> --- docs/resources/virtual_environment_vm.md | 1 + example/resource_virtual_environment_vm.tf | 1 + fwprovider/test/resource_vm_disks_test.go | 4 ++++ proxmox/nodes/vms/custom_storage_device.go | 8 ++++++++ proxmoxtf/resource/vm/disk/disk.go | 9 +++++++++ proxmoxtf/resource/vm/disk/schema.go | 15 +++++++++++++++ 6 files changed, 38 insertions(+) diff --git a/docs/resources/virtual_environment_vm.md b/docs/resources/virtual_environment_vm.md index 9e86b416..0efeb8d0 100644 --- a/docs/resources/virtual_environment_vm.md +++ b/docs/resources/virtual_environment_vm.md @@ -276,6 +276,7 @@ output "ubuntu_vm_public_key" { - `iothread` - (Optional) Whether to use iothreads for this disk (defaults to `false`). - `replicate` - (Optional) Whether the drive should be considered for replication jobs (defaults to `true`). + - `serial` - (Optional) The serial number of the disk, up to 20 bytes long. - `size` - (Optional) The disk size in gigabytes (defaults to `8`). - `speed` - (Optional) The speed limits. - `iops_read` - (Optional) The maximum read I/O in operations per second. diff --git a/example/resource_virtual_environment_vm.tf b/example/resource_virtual_environment_vm.tf index 39eea863..abf96be7 100644 --- a/example/resource_virtual_environment_vm.tf +++ b/example/resource_virtual_environment_vm.tf @@ -53,6 +53,7 @@ resource "proxmox_virtual_environment_vm" "example_template" { interface = "scsi0" discard = "on" cache = "writeback" + serial = "dead_beef" ssd = true } diff --git a/fwprovider/test/resource_vm_disks_test.go b/fwprovider/test/resource_vm_disks_test.go index 7c1c7259..2c638bb8 100644 --- a/fwprovider/test/resource_vm_disks_test.go +++ b/fwprovider/test/resource_vm_disks_test.go @@ -69,6 +69,7 @@ func TestAccResourceVMDisks(t *testing.T) { file_format = "raw" datastore_id = "local-lvm" interface = "virtio0" + serial = "-dead_beef-" size = 8 replicate = false aio = "native" @@ -93,6 +94,7 @@ func TestAccResourceVMDisks(t *testing.T) { "disk.0.iothread": "false", "disk.0.path_in_datastore": `vm-\d+-disk-\d+`, "disk.0.replicate": "false", + "disk.0.serial": "-dead_beef-", "disk.0.size": "8", "disk.0.ssd": "false", "disk.0.speed.0.iops_read": "100", @@ -122,6 +124,7 @@ func TestAccResourceVMDisks(t *testing.T) { interface = "virtio0" iothread = true discard = "on" + serial = "dead_beef" size = 20 } }`), @@ -134,6 +137,7 @@ func TestAccResourceVMDisks(t *testing.T) { "disk.0.interface": "virtio0", "disk.0.iothread": "true", "disk.0.path_in_datastore": `vm-\d+-disk-\d+`, + "disk.0.serial": "dead_beef", "disk.0.size": "20", "disk.0.ssd": "false", }), diff --git a/proxmox/nodes/vms/custom_storage_device.go b/proxmox/nodes/vms/custom_storage_device.go index 60e9ed21..6244109d 100644 --- a/proxmox/nodes/vms/custom_storage_device.go +++ b/proxmox/nodes/vms/custom_storage_device.go @@ -38,6 +38,7 @@ type CustomStorageDevice struct { MaxWriteSpeedMbps *int `json:"mbps_wr,omitempty" url:"mbps_wr,omitempty"` Media *string `json:"media,omitempty" url:"media,omitempty"` Replicate *types.CustomBool `json:"replicate,omitempty" url:"replicate,omitempty,int"` + Serial *string `json:"serial,omitempty" url:"serial,omitempty"` Size *types.DiskSize `json:"size,omitempty" url:"size,omitempty"` SSD *types.CustomBool `json:"ssd,omitempty" url:"ssd,omitempty,int"` DatastoreID *string `json:"-" url:"-"` @@ -160,6 +161,10 @@ func (d *CustomStorageDevice) EncodeOptions() string { } } + if d.Serial != nil && *d.Serial != "" { + values = append(values, fmt.Sprintf("serial=%s", *d.Serial)) + } + if d.SSD != nil { if *d.SSD { values = append(values, "ssd=1") @@ -346,6 +351,9 @@ func (d *CustomStorageDevice) UnmarshalJSON(b []byte) error { bv := types.CustomBool(v[1] == "1") d.Replicate = &bv + case "serial": + d.Serial = &v[1] + case "size": d.Size = new(types.DiskSize) diff --git a/proxmoxtf/resource/vm/disk/disk.go b/proxmoxtf/resource/vm/disk/disk.go index d2b7fda5..ea27302e 100644 --- a/proxmoxtf/resource/vm/disk/disk.go +++ b/proxmoxtf/resource/vm/disk/disk.go @@ -258,6 +258,7 @@ func GetDiskDeviceObjects( fileID, _ := block[mkDiskFileID].(string) ioThread := types.CustomBool(block[mkDiskIOThread].(bool)) replicate := types.CustomBool(block[mkDiskReplicate].(bool)) + serial := block[mkDiskSerial].(string) size, _ := block[mkDiskSize].(int) ssd := types.CustomBool(block[mkDiskSSD].(bool)) @@ -301,6 +302,7 @@ func GetDiskDeviceObjects( diskDevice.Format = &fileFormat diskDevice.Interface = &diskInterface diskDevice.Replicate = &replicate + diskDevice.Serial = &serial diskDevice.Size = types.DiskSizeFromGigabytes(int64(size)) if !strings.HasPrefix(diskInterface, "virtio") { @@ -535,6 +537,12 @@ func Read( disk[mkDiskReplicate] = true } + if dd.Serial != nil { + disk[mkDiskSerial] = *dd.Serial + } else { + disk[mkDiskSerial] = "" + } + if dd.SSD != nil { disk[mkDiskSSD] = *dd.SSD } else { @@ -700,6 +708,7 @@ func Update( tmp.MaxReadSpeedMbps = disk.MaxReadSpeedMbps tmp.MaxWriteSpeedMbps = disk.MaxWriteSpeedMbps tmp.Replicate = disk.Replicate + tmp.Serial = disk.Serial tmp.SSD = disk.SSD switch prefix { diff --git a/proxmoxtf/resource/vm/disk/schema.go b/proxmoxtf/resource/vm/disk/schema.go index 96f956c8..7fa9cf82 100644 --- a/proxmoxtf/resource/vm/disk/schema.go +++ b/proxmoxtf/resource/vm/disk/schema.go @@ -1,3 +1,9 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + package disk import ( @@ -34,6 +40,7 @@ const ( mkDiskIOThread = "iothread" mkDiskPathInDatastore = "path_in_datastore" mkDiskReplicate = "replicate" + mkDiskSerial = "serial" mkDiskSize = "size" mkDiskSpeed = "speed" mkDiskSpeedRead = "read" @@ -63,6 +70,7 @@ func Schema() map[string]*schema.Schema { mkDiskIOThread: false, mkDiskPathInDatastore: nil, mkDiskReplicate: true, + mkDiskSerial: "", mkDiskSize: dvDiskSize, mkDiskSSD: false, }, @@ -126,6 +134,13 @@ func Schema() map[string]*schema.Schema { Default: "", ValidateDiagFunc: validators.FileID(), }, + mkDiskSerial: { + Type: schema.TypeString, + Description: "The drive’s reported serial number", + Optional: true, + Default: "", + ValidateDiagFunc: validation.ToDiagFunc(validation.StringLenBetween(0, 20)), + }, mkDiskSize: { Type: schema.TypeInt, Description: "The disk size in gigabytes",