Skip to content

Commit

Permalink
refactor to use only boot_order argument
Browse files Browse the repository at this point in the history
  • Loading branch information
bpg committed Apr 7, 2023
1 parent 688dc0e commit 1bb68bb
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 25 deletions.
7 changes: 4 additions & 3 deletions docs/resources/virtual_environment_vm.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ output "ubuntu_vm_public_key" {
- `bios` - (Optional) The BIOS implementation (defaults to `seabios`).
- `ovmf` - OVMF (UEFI).
- `seabios` - SeaBIOS.
- `boot_order` - (Optional) Specify the guest boot order (defaults to `c`).
- `boot_disk` - (Optional) Enable booting from specified disk (deprecated).
- `boot_order` - (Optional) Specify a list of devices to boot from in the order
they appear in the list (defaults to `[]`).
- `cdrom` - (Optional) The CDROM configuration.
- `enabled` - (Optional) Whether to enable the CDROM drive (defaults
to `false`).
Expand Down Expand Up @@ -363,7 +363,8 @@ output "ubuntu_vm_public_key" {
- `device` - (Optional) The device (defaults to `socket`).
- `/dev/*` - A host serial device.
- `socket` - A unix socket.
- `scsi_hardware` - (Optional) The SCSI hardware type (defaults to `virtio-scsi-pci`).
- `scsi_hardware` - (Optional) The SCSI hardware type (defaults
to `virtio-scsi-pci`).
- `lsi` - LSI Logic SAS1068E.
- `lsi53c810` - LSI Logic 53C810.
- `virtio-scsi-pci` - VirtIO SCSI.
Expand Down
47 changes: 36 additions & 11 deletions proxmox/virtual_environment_vm_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,17 +240,17 @@ type VirtualEnvironmentVMCreateRequestBody struct {
BackupFile *string `json:"archive,omitempty" url:"archive,omitempty"`
BandwidthLimit *int `json:"bwlimit,omitempty" url:"bwlimit,omitempty"`
BIOS *string `json:"bios,omitempty" url:"bios,omitempty"`
Boot *CustomBoot `json:"boot,omitempty" url:"boot,omitempty"`
CDROM *string `json:"cdrom,omitempty" url:"cdrom,omitempty"`
CloudInitConfig *CustomCloudInitConfig `json:"cloudinit,omitempty" url:"cloudinit,omitempty"`
CPUArchitecture *string `json:"arch,omitempty" url:"arch,omitempty"`
CPUCores *int `json:"cores,omitempty" url:"cores,omitempty"`
CPUEmulation *CustomCPUEmulation `json:"cpu,omitempty" url:"cpu,omitempty"`
CPULimit *int `json:"cpulimit,omitempty" url:"cpulimit,omitempty"`
CPUSockets *int `json:"sockets,omitempty" url:"sockets,omitempty"`
CPUUnits *int `json:"cpuunits,omitempty" url:"cpuunits,omitempty"`
DedicatedMemory *int `json:"memory,omitempty" url:"memory,omitempty"`
Delete []string `json:"delete,omitempty" url:"delete,omitempty,comma"`
Boot *CustomBoot `json:"boot,omitempty" url:"boot,omitempty"`
CDROM *string `json:"cdrom,omitempty" url:"cdrom,omitempty"`
CloudInitConfig *CustomCloudInitConfig `json:"cloudinit,omitempty" url:"cloudinit,omitempty"`
CPUArchitecture *string `json:"arch,omitempty" url:"arch,omitempty"`
CPUCores *int `json:"cores,omitempty" url:"cores,omitempty"`
CPUEmulation *CustomCPUEmulation `json:"cpu,omitempty" url:"cpu,omitempty"`
CPULimit *int `json:"cpulimit,omitempty" url:"cpulimit,omitempty"`
CPUSockets *int `json:"sockets,omitempty" url:"sockets,omitempty"`
CPUUnits *int `json:"cpuunits,omitempty" url:"cpuunits,omitempty"`
DedicatedMemory *int `json:"memory,omitempty" url:"memory,omitempty"`
Delete []string `json:"delete,omitempty" url:"delete,omitempty,comma"`
DeletionProtection *types.CustomBool `json:"protection,omitempty" url:"force,omitempty,int"`
Description *string `json:"description,omitempty" url:"description,omitempty"`
EFIDisk *CustomEFIDisk `json:"efidisk0,omitempty" url:"efidisk0,omitempty"`
Expand Down Expand Up @@ -1310,6 +1310,31 @@ func (r *CustomAudioDevice) UnmarshalJSON(b []byte) error {
return nil
}

// UnmarshalJSON converts a CustomBoot string to an object.
func (r *CustomBoot) UnmarshalJSON(b []byte) error {
var s string

err := json.Unmarshal(b, &s)
if err != nil {
return fmt.Errorf("error unmarshalling CustomBoot: %w", err)
}

pairs := strings.Split(s, ",")

for _, p := range pairs {
v := strings.Split(strings.TrimSpace(p), "=")

if len(v) == 2 {
if v[0] == "order" {
v := strings.Split(strings.TrimSpace(v[1]), ";")
r.Order = &v
}
}
}

return nil
}

// UnmarshalJSON converts a CustomCloudInitFiles string to an object.
func (r *CustomCloudInitFiles) UnmarshalJSON(b []byte) error {
var s string
Expand Down
49 changes: 41 additions & 8 deletions proxmoxtf/resource/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ const (
dvResourceVirtualEnvironmentVMAudioDeviceDriver = "spice"
dvResourceVirtualEnvironmentVMAudioDeviceEnabled = true
dvResourceVirtualEnvironmentVMBIOS = "seabios"
dvResourceVirtualEnvironmentVMBootDevice = "scsi0"
dvResourceVirtualEnvironmentVMCDROMEnabled = false
dvResourceVirtualEnvironmentVMCDROMFileID = ""
dvResourceVirtualEnvironmentVMCloneDatastoreID = ""
Expand Down Expand Up @@ -1926,10 +1925,30 @@ func vmCreateCustom(ctx context.Context, d *schema.ResourceData, m interface{})

var memorySharedObject *proxmox.CustomSharedMemory

var bootOrderConverted []string
if cdromEnabled {
bootOrderConverted = []string{"ide3"}
}
bootOrder := d.Get(mkResourceVirtualEnvironmentVMBootOrder).([]interface{})
bootOrderConverted := make([]string, len(bootOrder))
for i, device := range bootOrder {
bootOrderConverted[i] = device.(string)
//nolint:nestif
if len(bootOrder) == 0 {
if sataDeviceObjects != nil {
bootOrderConverted = append(bootOrderConverted, "sata0")
}
if scsiDeviceObjects != nil {
bootOrderConverted = append(bootOrderConverted, "scsi0")
}
if virtioDeviceObjects != nil {
bootOrderConverted = append(bootOrderConverted, "virtio0")
}
if networkDeviceObjects != nil {
bootOrderConverted = append(bootOrderConverted, "net0")
}
} else {
bootOrderConverted = make([]string, len(bootOrder))
for i, device := range bootOrder {
bootOrderConverted[i] = device.(string)
}
}

cpuFlagsConverted := make([]string, len(cpuFlags))
Expand Down Expand Up @@ -1968,10 +1987,11 @@ func vmCreateCustom(ctx context.Context, d *schema.ResourceData, m interface{})
TrimClonedDisks: &agentTrim,
Type: &agentType,
},
AudioDevices: audioDevices,
BIOS: &bios,
BootDisk: &bootDisk,
BootOrder: &bootOrder,
AudioDevices: audioDevices,
BIOS: &bios,
Boot: &proxmox.CustomBoot{
Order: &bootOrderConverted,
},
CloudInitConfig: initializationConfig,
CPUCores: &cpuCores,
CPUEmulation: &proxmox.CustomCPUEmulation{
Expand Down Expand Up @@ -4045,6 +4065,19 @@ func vmUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.D
rebootRequired = true
}

// Prepare the new boot configuration.
if d.HasChange(mkResourceVirtualEnvironmentVMBootOrder) {
bootOrder := d.Get(mkResourceVirtualEnvironmentVMBootOrder).([]interface{})
bootOrderConverted := make([]string, len(bootOrder))
for i, device := range bootOrder {
bootOrderConverted[i] = device.(string)
}
updateBody.Boot = &proxmox.CustomBoot{
Order: &bootOrderConverted,
}
rebootRequired = true
}

// Prepare the new CDROM configuration.
if d.HasChange(mkResourceVirtualEnvironmentVMCDROM) {
cdromBlock, err := getSchemaBlock(
Expand Down
4 changes: 1 addition & 3 deletions proxmoxtf/resource/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ func TestVMSchema(t *testing.T) {
mkResourceVirtualEnvironmentVMAudioDevice,
mkResourceVirtualEnvironmentVMBIOS,
mkResourceVirtualEnvironmentVMBootOrder,
mkResourceVirtualEnvironmentVMBootDisk,
mkResourceVirtualEnvironmentVMCDROM,
mkResourceVirtualEnvironmentVMClone,
mkResourceVirtualEnvironmentVMCPU,
Expand Down Expand Up @@ -75,8 +74,7 @@ func TestVMSchema(t *testing.T) {
mkResourceVirtualEnvironmentVMAgent: schema.TypeList,
mkResourceVirtualEnvironmentVMAudioDevice: schema.TypeList,
mkResourceVirtualEnvironmentVMBIOS: schema.TypeString,
mkResourceVirtualEnvironmentVMBootOrder: schema.TypeString,
mkResourceVirtualEnvironmentVMBootDisk: schema.TypeString,
mkResourceVirtualEnvironmentVMBootOrder: schema.TypeList,
mkResourceVirtualEnvironmentVMCDROM: schema.TypeList,
mkResourceVirtualEnvironmentVMCPU: schema.TypeList,
mkResourceVirtualEnvironmentVMDescription: schema.TypeString,
Expand Down

0 comments on commit 1bb68bb

Please sign in to comment.