diff --git a/docs/guides/cloud_init.md b/docs/guides/cloud_init.md index 0aba1ed1..12849f8e 100644 --- a/docs/guides/cloud_init.md +++ b/docs/guides/cloud_init.md @@ -189,7 +189,7 @@ resource "proxmox_vm_qemu" "preprovision-test" { sockets = 1 # Same CPU as the Physical host, possible to add cpu flags # Ex: "host,flags=+md-clear;+pcid;+spec-ctrl;+ssbd;+pdpe1gb" - cpu = "host" + cpu_type = "host" numa = false memory = 2560 scsihw = "lsi" diff --git a/docs/resources/vm_qemu.md b/docs/resources/vm_qemu.md index 195cdba9..f1e9ffc8 100644 --- a/docs/resources/vm_qemu.md +++ b/docs/resources/vm_qemu.md @@ -118,7 +118,7 @@ The following arguments are supported in the top level resource block. | `sockets` | `int` | `1` | The number of CPU sockets to allocate to the VM. | | `cores` | `int` | `1` | The number of CPU cores per CPU socket to allocate to the VM. | | `vcpus` | `int` | `0` | The number of vCPUs plugged into the VM when it starts. If `0`, this is set automatically by Proxmox to `sockets * cores`. | -| `cpu` | `str` | `"host"` | The type of CPU to emulate in the Guest. See the [docs about CPU Types](https://pve.proxmox.com/pve-docs/chapter-qm.html#qm_cpu) for more info. | +| `cpu_type` | `str` | `"host"` | The type of CPU to emulate in the Guest. See the [docs about CPU Types](https://pve.proxmox.com/pve-docs/chapter-qm.html#qm_cpu) for more info. | | `numa` | `bool` | `false` | Whether to enable [Non-Uniform Memory Access](https://pve.proxmox.com/pve-docs/chapter-qm.html#qm_cpu) in the guest. | | `hotplug` | `str` | `"network,disk,usb"` | Comma delimited list of hotplug features to enable. Options: `network`, `disk`, `cpu`, `memory`, `usb`. Set to `0` to disable hotplug. | | `scsihw` | `str` | `"lsi"` | The SCSI controller to emulate. Options: `lsi`, `lsi53c810`, `megasas`, `pvscsi`, `virtio-scsi-pci`, `virtio-scsi-single`. | diff --git a/examples/cloudinit_example.tf b/examples/cloudinit_example.tf index b851073a..f0da6c58 100644 --- a/examples/cloudinit_example.tf +++ b/examples/cloudinit_example.tf @@ -27,7 +27,7 @@ resource "proxmox_vm_qemu" "cloudinit-test" { cores = 2 sockets = 1 vcpus = 0 - cpu = "host" + cpu_type = "host" memory = 2048 scsihw = "lsi" diff --git a/examples/pxe_example.tf b/examples/pxe_example.tf index b0ba78d2..a95a26df 100644 --- a/examples/pxe_example.tf +++ b/examples/pxe_example.tf @@ -32,7 +32,7 @@ resource "proxmox_vm_qemu" "pxe-example" { # and future boots will run off the disk boot = "order=scsi0;net0" cores = 2 - cpu = "host" + cpu_type = "host" define_connection_info = true force_create = false hotplug = "network,disk,usb" diff --git a/proxmox/Internal/resource/guest/qemu/cpu/schema.go b/proxmox/Internal/resource/guest/qemu/cpu/schema.go new file mode 100644 index 00000000..324c3a4c --- /dev/null +++ b/proxmox/Internal/resource/guest/qemu/cpu/schema.go @@ -0,0 +1,82 @@ +package cpu + +import ( + pveAPI "github.com/Telmate/proxmox-api-go/proxmox" + "github.com/hashicorp/go-cty/cty" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +const ( + Root string = "cpu" + RootCores string = "cores" + RootCpuType string = "cpu_type" + RootNuma string = "numa" + RootSockets string = "sockets" + RootVirtualCores string = "vcpus" +) + +func SchemaCores() *schema.Schema { + return &schema.Schema{ + Type: schema.TypeInt, + Optional: true, + Default: 1, + ValidateDiagFunc: func(i interface{}, p cty.Path) diag.Diagnostics { + v, ok := i.(int) + if !ok { + return diag.Errorf(RootCores + " must be an integer") + } + if v < 1 { + return diag.Errorf(RootCores + " must be greater than 0") + } + return diag.FromErr(pveAPI.QemuCpuCores(v).Validate()) + }} +} + +func SchemaType(s schema.Schema) *schema.Schema { + s.Type = schema.TypeString + s.Optional = true + return &s +} + +func SchemaNuma() *schema.Schema { + return &schema.Schema{ + Type: schema.TypeBool, + Optional: true, + } +} + +func SchemaSockets() *schema.Schema { + return &schema.Schema{ + Type: schema.TypeInt, + Optional: true, + Default: 1, + ValidateDiagFunc: func(i interface{}, p cty.Path) diag.Diagnostics { + v, ok := i.(int) + if !ok { + return diag.Errorf(RootSockets + " must be an integer") + } + if v < 1 { + return diag.Errorf(RootSockets + " must be greater than 0") + } + return diag.FromErr(pveAPI.QemuCpuSockets(v).Validate()) + }} +} + +func SchemaVirtualCores() *schema.Schema { + return &schema.Schema{ + Type: schema.TypeInt, + Optional: true, + Default: 0, + ValidateDiagFunc: func(i interface{}, p cty.Path) diag.Diagnostics { + v, ok := i.(int) + if !ok { + return diag.Errorf(RootVirtualCores + " must be an integer") + } + if v < 0 { + return diag.Errorf(RootVirtualCores + " must be greater than or equal to 0") + } + return nil + }, + } +} diff --git a/proxmox/Internal/resource/guest/qemu/cpu/sdk.go b/proxmox/Internal/resource/guest/qemu/cpu/sdk.go new file mode 100644 index 00000000..b49809a9 --- /dev/null +++ b/proxmox/Internal/resource/guest/qemu/cpu/sdk.go @@ -0,0 +1,23 @@ +package cpu + +import ( + pveAPI "github.com/Telmate/proxmox-api-go/proxmox" + "github.com/Telmate/terraform-provider-proxmox/v2/proxmox/Internal/util" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +func SDK(d *schema.ResourceData) *pveAPI.QemuCPU { + var cpuType pveAPI.CpuType + if v, ok := d.GetOk(Root); ok { + cpuType = pveAPI.CpuType(v.(string)) + } else { + v := d.Get(RootCpuType) + cpuType = pveAPI.CpuType(v.(string)) + } + return &pveAPI.QemuCPU{ + Cores: util.Pointer(pveAPI.QemuCpuCores(d.Get(RootCores).(int))), + Numa: util.Pointer(d.Get(RootNuma).(bool)), + Sockets: util.Pointer(pveAPI.QemuCpuSockets(d.Get(RootSockets).(int))), + Type: util.Pointer(cpuType), + VirtualCores: util.Pointer(pveAPI.CpuVirtualCores(d.Get(RootVirtualCores).(int)))} +} diff --git a/proxmox/Internal/resource/guest/qemu/cpu/terraform.go b/proxmox/Internal/resource/guest/qemu/cpu/terraform.go new file mode 100644 index 00000000..06608bfc --- /dev/null +++ b/proxmox/Internal/resource/guest/qemu/cpu/terraform.go @@ -0,0 +1,26 @@ +package cpu + +import ( + pveAPI "github.com/Telmate/proxmox-api-go/proxmox" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +func Terraform(config pveAPI.QemuCPU, d *schema.ResourceData) { + if config.Cores != nil { + d.Set(RootCores, int(*config.Cores)) + } + if config.Numa != nil { + d.Set(RootNuma, *config.Numa) + } + if config.Sockets != nil { + d.Set(RootSockets, int(*config.Sockets)) + } + if _, ok := d.GetOk(Root); ok { + d.Set(Root, string(*config.Type)) + } else { + d.Set(RootCpuType, string(*config.Type)) + } + if config.VirtualCores != nil { + d.Set(RootVirtualCores, int(*config.VirtualCores)) + } +} diff --git a/proxmox/resource_vm_qemu.go b/proxmox/resource_vm_qemu.go index 3a03f8a8..2a46cc5c 100755 --- a/proxmox/resource_vm_qemu.go +++ b/proxmox/resource_vm_qemu.go @@ -28,6 +28,7 @@ import ( "github.com/Telmate/terraform-provider-proxmox/v2/proxmox/Internal/pxapi/dns/nameservers" "github.com/Telmate/terraform-provider-proxmox/v2/proxmox/Internal/pxapi/guest/sshkeys" "github.com/Telmate/terraform-provider-proxmox/v2/proxmox/Internal/pxapi/guest/tags" + "github.com/Telmate/terraform-provider-proxmox/v2/proxmox/Internal/resource/guest/qemu/cpu" "github.com/Telmate/terraform-provider-proxmox/v2/proxmox/Internal/resource/guest/qemu/disk" "github.com/Telmate/terraform-provider-proxmox/v2/proxmox/Internal/resource/guest/qemu/network" "github.com/Telmate/terraform-provider-proxmox/v2/proxmox/Internal/resource/guest/qemu/pci" @@ -281,31 +282,16 @@ func resourceVmQemu() *schema.Resource { Optional: true, Default: 0, }, - "cores": { - Type: schema.TypeInt, - Optional: true, - Default: 1, - }, - "sockets": { - Type: schema.TypeInt, - Optional: true, - Default: 1, - }, - "vcpus": { - Type: schema.TypeInt, - Optional: true, - Default: 0, - }, - "cpu": { - Type: schema.TypeString, - Optional: true, - Default: "host", - }, - "numa": { - Type: schema.TypeBool, - Optional: true, - // Default: false, - }, + cpu.Root: cpu.SchemaType(schema.Schema{ + ConflictsWith: []string{cpu.RootCpuType}, + Deprecated: "use '" + cpu.RootCpuType + "' instead"}), + cpu.RootCores: cpu.SchemaCores(), + cpu.RootCpuType: cpu.SchemaType(schema.Schema{ + ConflictsWith: []string{cpu.Root}, + Default: "host"}), + cpu.RootNuma: cpu.SchemaNuma(), + cpu.RootSockets: cpu.SchemaSockets(), + cpu.RootVirtualCores: cpu.SchemaVirtualCores(), "kvm": { Type: schema.TypeBool, Optional: true, @@ -714,7 +700,7 @@ func resourceVmQemuCreate(ctx context.Context, d *schema.ResourceData, meta inte config := pxapi.ConfigQemu{ Name: vmName, - CPU: mapToSDK_CPU(d), + CPU: cpu.SDK(d), Description: util.Pointer(d.Get("desc").(string)), Pool: util.Pointer(pxapi.PoolName(d.Get("pool").(string))), Bios: d.Get("bios").(string), @@ -960,7 +946,7 @@ func resourceVmQemuUpdate(ctx context.Context, d *schema.ResourceData, meta inte config := pxapi.ConfigQemu{ Name: d.Get("name").(string), - CPU: mapToSDK_CPU(d), + CPU: cpu.SDK(d), Description: util.Pointer(d.Get("desc").(string)), Pool: util.Pointer(pxapi.PoolName(d.Get("pool").(string))), Bios: d.Get("bios").(string), @@ -1280,7 +1266,9 @@ func resourceVmQemuRead(ctx context.Context, d *schema.ResourceData, meta interf d.Set("smbios", ReadSmbiosArgs(config.Smbios1)) d.Set("linked_vmid", config.LinkedVmId) mapFromStruct_QemuGuestAgent(d, config.Agent) - mapToTerraform_CPU(config.CPU, d) + if config.CPU != nil { + cpu.Terraform(*config.CPU, d) + } mapToTerraform_CloudInit(config.CloudInit, d) mapToTerraform_Memory(config.Memory, d) if len(config.Networks) != 0 { @@ -1758,27 +1746,6 @@ 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 @@ -1904,15 +1871,6 @@ func mapToSDK_Memory(d *schema.ResourceData) *pxapi.QemuMemory { } } -func mapToSDK_CPU(d *schema.ResourceData) *pxapi.QemuCPU { - return &pxapi.QemuCPU{ - Cores: util.Pointer(pxapi.QemuCpuCores(d.Get("cores").(int))), - Numa: util.Pointer(d.Get("numa").(bool)), - Sockets: util.Pointer(pxapi.QemuCpuSockets(d.Get("sockets").(int))), - Type: util.Pointer(pxapi.CpuType(d.Get("cpu").(string))), - VirtualCores: util.Pointer(pxapi.CpuVirtualCores(d.Get("vcpus").(int)))} -} - func mapToSDK_QemuGuestAgent(d *schema.ResourceData) *pxapi.QemuGuestAgent { var tmpEnable bool if d.Get("agent").(int) == 1 {