From 5099ff372cc706706edca4345e4b1a75b1107a1d Mon Sep 17 00:00:00 2001 From: Dan Petersen Date: Fri, 27 Mar 2020 16:57:54 +0100 Subject: [PATCH] Ignore default value for cpu.architecture when the root account is not being used --- CHANGELOG.md | 6 +++- proxmox/virtual_environment_authentication.go | 5 +++ proxmoxtf/resource_virtual_environment_vm.go | 34 ++++++++++++++++--- 3 files changed, 39 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 926d3862..e9fba064 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,9 +1,13 @@ -## 0.4.0 +## 0.4.0 (UNRELEASED) BUG FIXES: * library/virtual_environment_nodes: Fix node IP address format +WORKAROUNDS: + +* resource/virtual_environment_vm: Ignore default value for `cpu.architecture` when the root account is not being used + ## 0.3.0 ENHANCEMENTS: diff --git a/proxmox/virtual_environment_authentication.go b/proxmox/virtual_environment_authentication.go index 19eb5290..5970c70a 100644 --- a/proxmox/virtual_environment_authentication.go +++ b/proxmox/virtual_environment_authentication.go @@ -13,6 +13,11 @@ import ( "net/url" ) +const ( + // DefaultRootAccount contains the default username and realm for the root account. + DefaultRootAccount = "root@pam" +) + // Authenticate authenticates against the specified endpoint. func (c *VirtualEnvironmentClient) Authenticate(reset bool) error { if c.authenticationData != nil && !reset { diff --git a/proxmoxtf/resource_virtual_environment_vm.go b/proxmoxtf/resource_virtual_environment_vm.go index 36ba9775..50f86a77 100644 --- a/proxmoxtf/resource_virtual_environment_vm.go +++ b/proxmoxtf/resource_virtual_environment_vm.go @@ -1129,7 +1129,11 @@ func resourceVirtualEnvironmentVMCreateClone(d *schema.ResourceData, m interface cpuFlagsConverted[fi] = flag.(string) } - updateBody.CPUArchitecture = &cpuArchitecture + // Only the root account is allowed to change the CPU architecture, which makes this check necessary. + if veClient.Username == proxmox.DefaultRootAccount || cpuArchitecture != dvResourceVirtualEnvironmentVMCPUArchitecture { + updateBody.CPUArchitecture = &cpuArchitecture + } + updateBody.CPUCores = &cpuCores updateBody.CPUEmulation = &proxmox.CustomCPUEmulation{ Flags: &cpuFlagsConverted, @@ -1458,7 +1462,6 @@ func resourceVirtualEnvironmentVMCreateCustom(d *schema.ResourceData, m interfac BootDisk: &bootDisk, BootOrder: &bootOrder, CloudInitConfig: initializationConfig, - CPUArchitecture: &cpuArchitecture, CPUCores: &cpuCores, CPUEmulation: &proxmox.CustomCPUEmulation{ Flags: &cpuFlagsConverted, @@ -1484,6 +1487,11 @@ func resourceVirtualEnvironmentVMCreateCustom(d *schema.ResourceData, m interfac VMID: &vmID, } + // Only the root account is allowed to change the CPU architecture, which makes this check necessary. + if veClient.Username == proxmox.DefaultRootAccount || cpuArchitecture != dvResourceVirtualEnvironmentVMCPUArchitecture { + createBody.CPUArchitecture = &cpuArchitecture + } + if cpuHotplugged > 0 { createBody.VirtualCPUCount = &cpuHotplugged } @@ -2037,7 +2045,14 @@ func resourceVirtualEnvironmentVMRead(d *schema.ResourceData, m interface{}) err } func resourceVirtualEnvironmentVMReadCustom(d *schema.ResourceData, m interface{}, vmID int, vmConfig *proxmox.VirtualEnvironmentVMGetResponseData, vmStatus *proxmox.VirtualEnvironmentVMGetStatusResponseData) error { - err := resourceVirtualEnvironmentVMReadPrimitiveValues(d, m, vmID, vmConfig, vmStatus) + config := m.(providerConfiguration) + veClient, err := config.GetVEClient() + + if err != nil { + return err + } + + err = resourceVirtualEnvironmentVMReadPrimitiveValues(d, m, vmID, vmConfig, vmStatus) if err != nil { return err @@ -2178,7 +2193,12 @@ func resourceVirtualEnvironmentVMReadCustom(d *schema.ResourceData, m interface{ cpu[mkResourceVirtualEnvironmentVMCPUArchitecture] = *vmConfig.CPUArchitecture } else { // Default value of "arch" is "" according to the API documentation. - cpu[mkResourceVirtualEnvironmentVMCPUArchitecture] = "" + // However, assume the provider's default value as a workaround when the root account is not being used. + if veClient.Username != proxmox.DefaultRootAccount { + cpu[mkResourceVirtualEnvironmentVMCPUArchitecture] = dvResourceVirtualEnvironmentVMCPUArchitecture + } else { + cpu[mkResourceVirtualEnvironmentVMCPUArchitecture] = "" + } } if vmConfig.CPUCores != nil { @@ -3047,7 +3067,11 @@ func resourceVirtualEnvironmentVMUpdate(d *schema.ResourceData, m interface{}) e cpuType := cpuBlock[mkResourceVirtualEnvironmentVMCPUType].(string) cpuUnits := cpuBlock[mkResourceVirtualEnvironmentVMCPUUnits].(int) - updateBody.CPUArchitecture = &cpuArchitecture + // Only the root account is allowed to change the CPU architecture, which makes this check necessary. + if veClient.Username == proxmox.DefaultRootAccount || cpuArchitecture != dvResourceVirtualEnvironmentVMCPUArchitecture { + updateBody.CPUArchitecture = &cpuArchitecture + } + updateBody.CPUCores = &cpuCores updateBody.CPUSockets = &cpuSockets updateBody.CPUUnits = &cpuUnits