Skip to content

Commit

Permalink
Merge pull request #1168 from Tinyblargon/cpu_type
Browse files Browse the repository at this point in the history
refactor: move cpu code to package & deprecate `cpu`
  • Loading branch information
Tinyblargon authored Nov 23, 2024
2 parents e2ed468 + 118836c commit 1478348
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 62 deletions.
2 changes: 1 addition & 1 deletion docs/guides/cloud_init.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion docs/resources/vm_qemu.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`. |
Expand Down
2 changes: 1 addition & 1 deletion examples/cloudinit_example.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
2 changes: 1 addition & 1 deletion examples/pxe_example.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
82 changes: 82 additions & 0 deletions proxmox/Internal/resource/guest/qemu/cpu/schema.go
Original file line number Diff line number Diff line change
@@ -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
},
}
}
23 changes: 23 additions & 0 deletions proxmox/Internal/resource/guest/qemu/cpu/sdk.go
Original file line number Diff line number Diff line change
@@ -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)))}
}
26 changes: 26 additions & 0 deletions proxmox/Internal/resource/guest/qemu/cpu/terraform.go
Original file line number Diff line number Diff line change
@@ -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))
}
}
74 changes: 16 additions & 58 deletions proxmox/resource_vm_qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 1478348

Please sign in to comment.