From 8a06e287b36cace110c9e88e39e1c3bccf29d6aa Mon Sep 17 00:00:00 2001 From: Dan Petersen Date: Tue, 31 Dec 2019 07:06:03 +0100 Subject: [PATCH] Add tablet_device argument to VM resource --- CHANGELOG.md | 4 ++- README.md | 1 + example/resource_virtual_environment_vm.tf | 4 +++ proxmoxtf/resource_virtual_environment_vm.go | 28 +++++++++++++++++-- .../resource_virtual_environment_vm_test.go | 3 ++ 5 files changed, 36 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 12e7f92e..73dcfbc0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,9 @@ ENHANCEMENTS: -resource/virtual_environment_vm: Add `cpu.flags`, `cpu.type`, `cpu.units` and `vga` arguments +resource/virtual_environment_vm: Add `cpu.flags`, `cpu.type` and `cpu.units` arguments +resource/virtual_environment_vm: Add `tablet_device` argument +resource/virtual_environment_vm: Add `vga` argument ## 0.1.0 diff --git a/README.md b/README.md index be1a6839..179b23ea 100644 --- a/README.md +++ b/README.md @@ -478,6 +478,7 @@ This resource doesn't expose any additional attributes. * `wxp` - Windows XP * `pool_id` - (Optional) The ID of a pool to assign the virtual machine to * `started` - (Optional) Whether to start the virtual machine (defaults to `true`) +* `tablet_device` - (Optional) Whether to enable the USB tablet device (defaults to `true`) * `vga` - (Optional) The VGA configuration * `enabled` - (Optional) Whether to enable the VGA device (defaults to `true`) * `memory` - (Optional) The VGA memory in megabytes (defaults to `16`) diff --git a/example/resource_virtual_environment_vm.tf b/example/resource_virtual_environment_vm.tf index 27563a38..1f374c95 100644 --- a/example/resource_virtual_environment_vm.tf +++ b/example/resource_virtual_environment_vm.tf @@ -23,11 +23,15 @@ resource "proxmox_virtual_environment_vm" "example" { user_data_file_id = "${proxmox_virtual_environment_file.cloud_config.id}" } + 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}" } + name = "terraform-provider-proxmox-example" + network_device {} node_name = "${data.proxmox_virtual_environment_nodes.example.names[0]}" diff --git a/proxmoxtf/resource_virtual_environment_vm.go b/proxmoxtf/resource_virtual_environment_vm.go index 8ded7cee..a046db4e 100644 --- a/proxmoxtf/resource_virtual_environment_vm.go +++ b/proxmoxtf/resource_virtual_environment_vm.go @@ -53,6 +53,7 @@ const ( dvResourceVirtualEnvironmentVMOSType = "other" dvResourceVirtualEnvironmentVMPoolID = "" dvResourceVirtualEnvironmentVMStarted = true + dvResourceVirtualEnvironmentVMTabletDevice = true dvResourceVirtualEnvironmentVMVGAEnabled = true dvResourceVirtualEnvironmentVMVGAMemory = 16 dvResourceVirtualEnvironmentVMVGAType = "std" @@ -120,6 +121,7 @@ const ( mkResourceVirtualEnvironmentVMOSType = "os_type" mkResourceVirtualEnvironmentVMPoolID = "pool_id" mkResourceVirtualEnvironmentVMStarted = "started" + mkResourceVirtualEnvironmentVMTabletDevice = "tablet_device" mkResourceVirtualEnvironmentVMVGA = "vga" mkResourceVirtualEnvironmentVMVGAEnabled = "enabled" mkResourceVirtualEnvironmentVMVGAMemory = "memory" @@ -707,6 +709,12 @@ func resourceVirtualEnvironmentVM() *schema.Resource { Optional: true, Default: dvResourceVirtualEnvironmentVMStarted, }, + mkResourceVirtualEnvironmentVMTabletDevice: { + Type: schema.TypeBool, + Description: "Whether to enable the USB tablet device", + Optional: true, + Default: dvResourceVirtualEnvironmentVMTabletDevice, + }, mkResourceVirtualEnvironmentVMVGA: &schema.Schema{ Type: schema.TypeList, Description: "The VGA configuration", @@ -853,6 +861,7 @@ func resourceVirtualEnvironmentVMCreate(d *schema.ResourceData, m interface{}) e osType := d.Get(mkResourceVirtualEnvironmentVMOSType).(string) poolID := d.Get(mkResourceVirtualEnvironmentVMPoolID).(string) started := proxmox.CustomBool(d.Get(mkResourceVirtualEnvironmentVMStarted).(bool)) + tabletDevice := proxmox.CustomBool(d.Get(mkResourceVirtualEnvironmentVMTabletDevice).(bool)) vgaDevice, err := resourceVirtualEnvironmentVMGetVGADeviceObject(d, m) @@ -911,7 +920,6 @@ func resourceVirtualEnvironmentVMCreate(d *schema.ResourceData, m interface{}) e } scsiHardware := "virtio-scsi-pci" - tabletDeviceEnabled := proxmox.CustomBool(true) body := &proxmox.VirtualEnvironmentVMCreateRequestBody{ Agent: &proxmox.CustomAgent{ @@ -941,7 +949,7 @@ func resourceVirtualEnvironmentVMCreate(d *schema.ResourceData, m interface{}) e SerialDevices: []string{"socket"}, SharedMemory: memorySharedObject, StartOnBoot: &started, - TabletDeviceEnabled: &tabletDeviceEnabled, + TabletDeviceEnabled: &tabletDevice, VGADevice: vgaDevice, VMID: &vmID, } @@ -1639,7 +1647,7 @@ func resourceVirtualEnvironmentVMRead(d *schema.ResourceData, m interface{}) err d.Set(mkResourceVirtualEnvironmentVMCPU, []interface{}{cpu}) } - // Compare the description and keyboard layout to the values stored in the state. + // Compare some primitive arguments to the values stored in the state. if vmConfig.Description != nil { d.Set(mkResourceVirtualEnvironmentVMDescription, *vmConfig.Description) } else { @@ -1652,6 +1660,12 @@ func resourceVirtualEnvironmentVMRead(d *schema.ResourceData, m interface{}) err d.Set(mkResourceVirtualEnvironmentVMKeyboardLayout, "") } + if vmConfig.TabletDeviceEnabled != nil { + d.Set(mkResourceVirtualEnvironmentVMTabletDevice, bool(*vmConfig.TabletDeviceEnabled)) + } else { + d.Set(mkResourceVirtualEnvironmentVMTabletDevice, false) + } + // Compare the disks to those stored in the state. currentDiskList := d.Get(mkResourceVirtualEnvironmentVMDisk).([]interface{}) @@ -2018,6 +2032,7 @@ func resourceVirtualEnvironmentVMUpdate(d *schema.ResourceData, m interface{}) e keyboardLayout := d.Get(mkResourceVirtualEnvironmentVMKeyboardLayout).(string) name := d.Get(mkResourceVirtualEnvironmentVMName).(string) osType := d.Get(mkResourceVirtualEnvironmentVMOSType).(string) + tabletDevice := proxmox.CustomBool(d.Get(mkResourceVirtualEnvironmentVMTabletDevice).(bool)) if description != "" { body.Description = &description @@ -2030,6 +2045,13 @@ func resourceVirtualEnvironmentVMUpdate(d *schema.ResourceData, m interface{}) e } body.OSType = &osType + body.TabletDeviceEnabled = &tabletDevice + + if d.HasChange(mkResourceVirtualEnvironmentVMKeyboardLayout) || + d.HasChange(mkResourceVirtualEnvironmentVMOSType) || + d.HasChange(mkResourceVirtualEnvironmentVMTabletDevice) { + rebootRequired = true + } // Prepare the new agent configuration. if d.HasChange(mkResourceVirtualEnvironmentVMAgent) { diff --git a/proxmoxtf/resource_virtual_environment_vm_test.go b/proxmoxtf/resource_virtual_environment_vm_test.go index 4288a597..e9442e6c 100644 --- a/proxmoxtf/resource_virtual_environment_vm_test.go +++ b/proxmoxtf/resource_virtual_environment_vm_test.go @@ -40,6 +40,7 @@ func TestResourceVirtualEnvironmentVMSchema(t *testing.T) { mkResourceVirtualEnvironmentVMOSType, mkResourceVirtualEnvironmentVMPoolID, mkResourceVirtualEnvironmentVMStarted, + mkResourceVirtualEnvironmentVMTabletDevice, mkResourceVirtualEnvironmentVMVMID, }) @@ -67,6 +68,7 @@ func TestResourceVirtualEnvironmentVMSchema(t *testing.T) { mkResourceVirtualEnvironmentVMOSType, mkResourceVirtualEnvironmentVMPoolID, mkResourceVirtualEnvironmentVMStarted, + mkResourceVirtualEnvironmentVMTabletDevice, mkResourceVirtualEnvironmentVMVMID, }, []schema.ValueType{ schema.TypeList, @@ -85,6 +87,7 @@ func TestResourceVirtualEnvironmentVMSchema(t *testing.T) { schema.TypeString, schema.TypeString, schema.TypeBool, + schema.TypeBool, schema.TypeInt, })