diff --git a/go.mod b/go.mod index 064878dc..90550283 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.21 toolchain go1.21.0 require ( - github.com/Telmate/proxmox-api-go v0.0.0-20240727211724-bdb805f525a0 + github.com/Telmate/proxmox-api-go v0.0.0-20240815210158-602cfe748bba github.com/google/uuid v1.6.0 github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320 github.com/hashicorp/terraform-plugin-sdk/v2 v2.34.0 diff --git a/go.sum b/go.sum index bdd85581..c5d78277 100644 --- a/go.sum +++ b/go.sum @@ -4,8 +4,8 @@ github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migc github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM= github.com/ProtonMail/go-crypto v1.1.0-alpha.2-proton h1:HKz85FwoXx86kVtTvFke7rgHvq/HoloSUvW5semjFWs= github.com/ProtonMail/go-crypto v1.1.0-alpha.2-proton/go.mod h1:rA3QumHc/FZ8pAHreoekgiAbzpNsfQAosU5td4SnOrE= -github.com/Telmate/proxmox-api-go v0.0.0-20240727211724-bdb805f525a0 h1:mPpJzWJBvxyqePn7lxxb7j3F3Q+IazGUiPv0OStlgmY= -github.com/Telmate/proxmox-api-go v0.0.0-20240727211724-bdb805f525a0/go.mod h1:O6yNUi0hG9GQLMBgpikSvbnuek1OMweFtbac1sfGuUs= +github.com/Telmate/proxmox-api-go v0.0.0-20240815210158-602cfe748bba h1:rbztGEr4+G00B0Thkq8QGhgyFcgvlOoCNhgu+8Pq+is= +github.com/Telmate/proxmox-api-go v0.0.0-20240815210158-602cfe748bba/go.mod h1:Gu6n6vEn1hlyFUkjrvU+X1fdgaSXLoM9HKYYJqy1fsY= github.com/agext/levenshtein v1.2.3 h1:YB2fHEn0UJagG8T1rrWknE3ZQzWM06O8AMAatNn7lmo= github.com/agext/levenshtein v1.2.3/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558= github.com/apparentlymart/go-textseg/v12 v12.0.0/go.mod h1:S/4uRK2UtaQttw1GenVJEynmyUenKwP++x/+DdGV/Ec= diff --git a/proxmox/resource_vm_qemu.go b/proxmox/resource_vm_qemu.go index b26a5a51..de87ed12 100755 --- a/proxmox/resource_vm_qemu.go +++ b/proxmox/resource_vm_qemu.go @@ -878,6 +878,7 @@ func resourceVmQemuCreate(ctx context.Context, d *schema.ResourceData, meta inte config := pxapi.ConfigQemu{ Name: vmName, + CPU: mapToSDK_CPU(d), Description: pointer(d.Get("desc").(string)), Pool: pointer(pxapi.PoolName(d.Get("pool").(string))), Bios: d.Get("bios").(string), @@ -890,11 +891,6 @@ func resourceVmQemuCreate(ctx context.Context, d *schema.ResourceData, meta inte Agent: mapToStruct_QemuGuestAgent(d), Memory: mapToSDK_Memory(d), Machine: d.Get("machine").(string), - QemuCores: d.Get("cores").(int), - QemuSockets: d.Get("sockets").(int), - QemuVcpus: d.Get("vcpus").(int), - QemuCpu: d.Get("cpu").(string), - QemuNuma: pointer(d.Get("numa").(bool)), QemuKVM: pointer(d.Get("kvm").(bool)), Hotplug: d.Get("hotplug").(string), Scsihw: d.Get("scsihw").(string), @@ -1139,6 +1135,7 @@ func resourceVmQemuUpdate(ctx context.Context, d *schema.ResourceData, meta inte config := pxapi.ConfigQemu{ Name: d.Get("name").(string), + CPU: mapToSDK_CPU(d), Description: pointer(d.Get("desc").(string)), Pool: pointer(pxapi.PoolName(d.Get("pool").(string))), Bios: d.Get("bios").(string), @@ -1151,11 +1148,6 @@ func resourceVmQemuUpdate(ctx context.Context, d *schema.ResourceData, meta inte Agent: mapToStruct_QemuGuestAgent(d), Memory: mapToSDK_Memory(d), Machine: d.Get("machine").(string), - QemuCores: d.Get("cores").(int), - QemuSockets: d.Get("sockets").(int), - QemuVcpus: d.Get("vcpus").(int), - QemuCpu: d.Get("cpu").(string), - QemuNuma: pointer(d.Get("numa").(bool)), QemuKVM: pointer(d.Get("kvm").(bool)), Hotplug: d.Get("hotplug").(string), Scsihw: d.Get("scsihw").(string), @@ -1435,11 +1427,6 @@ func resourceVmQemuRead(ctx context.Context, d *schema.ResourceData, meta interf d.Set("boot", config.Boot) d.Set("bootdisk", config.BootDisk) d.Set("machine", config.Machine) - d.Set("cores", config.QemuCores) - d.Set("sockets", config.QemuSockets) - d.Set("vcpus", config.QemuVcpus) - d.Set("cpu", config.QemuCpu) - d.Set("numa", config.QemuNuma) d.Set("kvm", config.QemuKVM) d.Set("hotplug", config.Hotplug) d.Set("scsihw", config.Scsihw) @@ -1452,6 +1439,7 @@ func resourceVmQemuRead(ctx context.Context, d *schema.ResourceData, meta interf d.Set("linked_vmid", config.LinkedVmId) d.Set("disks", mapFromStruct_ConfigQemu(config.Disks)) mapFromStruct_QemuGuestAgent(d, config.Agent) + mapToTerraform_CPU(config.CPU, d) mapToTerraform_CloudInit(config.CloudInit, d) mapToTerraform_Memory(config.Memory, d) @@ -1990,6 +1978,27 @@ func mapToTerraform_CloudInitNetworkConfig(config pxapi.CloudInitNetworkConfig) return "" } +func mapToTerraform_CPU(config *pxapi.QemuCPU, d *schema.ResourceData) { + if config == nil { + return + } + if config.Cores != nil { + d.Set("cores", int(*config.Cores)) + } + if config.Numa != nil { + d.Set("numa", *config.Numa) + } + if config.Sockets != nil { + d.Set("sockets", int(*config.Sockets)) + } + if config.Type != nil { + d.Set("cpu", string(*config.Type)) + } + if config.VirtualCores != nil { + d.Set("vcpus", int(*config.VirtualCores)) + } +} + func mapToTerraform_Description(description *string) string { if description != nil { return *description @@ -2476,15 +2485,22 @@ func mapToSDK_CloudInitSnippet(param string) *pxapi.CloudInitSnippet { } func mapToSDK_Memory(d *schema.ResourceData) *pxapi.QemuMemory { - capacity := pxapi.QemuMemoryCapacity(d.Get("memory").(int)) - balloon := pxapi.QemuMemoryBalloonCapacity(d.Get("balloon").(int)) - var shares pxapi.QemuMemoryShares return &pxapi.QemuMemory{ - CapacityMiB: &capacity, - MinimumCapacityMiB: &balloon, - Shares: &shares, + CapacityMiB: pointer(pxapi.QemuMemoryCapacity(d.Get("memory").(int))), + MinimumCapacityMiB: pointer(pxapi.QemuMemoryBalloonCapacity(d.Get("balloon").(int))), + Shares: pointer(pxapi.QemuMemoryShares(0)), } } + +func mapToSDK_CPU(d *schema.ResourceData) *pxapi.QemuCPU { + return &pxapi.QemuCPU{ + Cores: pointer(pxapi.QemuCpuCores(d.Get("cores").(int))), + Numa: pointer(d.Get("numa").(bool)), + Sockets: pointer(pxapi.QemuCpuSockets(d.Get("sockets").(int))), + Type: pointer(pxapi.CpuType(d.Get("cpu").(string))), + VirtualCores: pointer(pxapi.CpuVirtualCores(d.Get("vcpus").(int)))} +} + func mapToStruct_QemuCdRom(schema map[string]interface{}) (cdRom *pxapi.QemuCdRom) { schemaItem, ok := schema["cdrom"].([]interface{}) if !ok { diff --git a/proxmox/util.go b/proxmox/util.go index 08d13ef4..f9bf962f 100644 --- a/proxmox/util.go +++ b/proxmox/util.go @@ -480,6 +480,7 @@ func testOptionalArguments(t *testing.T, s *schema.Resource) { } } +// Gets inlined by the compiler, so it's not a performance hit func pointer[T any](item T) *T { return &item }