Skip to content

Commit 7e47f7f

Browse files
authored
Ensure xenorchestra_vm resource updates properly handle nullable XO api fields (#239)
* WIP: Ensure that updates to the xenorchestra_vm resource properly handle XO api nullable fields (cherry picked from commit 4cba195) * Address issues with affinity_host on update and mac_address on VIF creation (cherry picked from commit 07c7e10f3cba8f7a1fd12ecececd4c656dc9ae32)
1 parent f87dece commit 7e47f7f

File tree

3 files changed

+31
-10
lines changed

3 files changed

+31
-10
lines changed

client/vif.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ func (c *Client) CreateVIF(vm *Vm, vif *VIF) (*VIF, error) {
7474
params := map[string]interface{}{
7575
"network": vif.Network,
7676
"vm": vm.Id,
77-
"mac": vif.MacAddress,
77+
}
78+
if vif.MacAddress != "" {
79+
params["mac"] = vif.MacAddress
7880
}
7981
err := c.Call("vm.createInterface", params, &id)
8082

client/vm.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ type Vm struct {
9595
Boot Boot `json:"boot,omitempty"`
9696
Type string `json:"type,omitempty"`
9797
Id string `json:"id,omitempty"`
98-
AffinityHost string `json:"affinityHost,omitempty"`
98+
AffinityHost *string `json:"affinityHost,omitempty"`
9999
NameDescription string `json:"name_description"`
100100
NameLabel string `json:"name_label"`
101101
CPUs CPUs `json:"CPUs"`
@@ -212,7 +212,6 @@ func (c *Client) CreateVm(vmReq Vm, createTime time.Duration) (*Vm, error) {
212212
}
213213

214214
params := map[string]interface{}{
215-
"affinityHost": vmReq.AffinityHost,
216215
"bootAfterCreate": true,
217216
"name_label": vmReq.NameLabel,
218217
"name_description": vmReq.NameDescription,
@@ -243,6 +242,11 @@ func (c *Client) CreateVm(vmReq Vm, createTime time.Duration) (*Vm, error) {
243242
params["hvmBootFirmware"] = firmware
244243
}
245244

245+
affinityHost := vmReq.AffinityHost
246+
if affinityHost != nil {
247+
params["affinityHost"] = affinityHost
248+
}
249+
246250
vga := vmReq.Vga
247251
if vga != "" {
248252
params["vga"] = vga
@@ -319,7 +323,6 @@ func createVdiMap(disk Disk) map[string]interface{} {
319323
func (c *Client) UpdateVm(vmReq Vm) (*Vm, error) {
320324
params := map[string]interface{}{
321325
"id": vmReq.Id,
322-
"affinityHost": vmReq.AffinityHost,
323326
"name_label": vmReq.NameLabel,
324327
"name_description": vmReq.NameDescription,
325328
"auto_poweron": vmReq.AutoPoweron,
@@ -341,6 +344,15 @@ func (c *Client) UpdateVm(vmReq Vm) (*Vm, error) {
341344
// coresPerSocket is null or a number of cores per socket. Putting an invalid value doesn't seem to cause an error :(
342345
}
343346

347+
affinityHost := vmReq.AffinityHost
348+
if affinityHost != nil {
349+
if *affinityHost == "" {
350+
params["affinityHost"] = nil
351+
} else {
352+
params["affinityHost"] = *affinityHost
353+
}
354+
}
355+
344356
videoram := vmReq.Videoram.Value
345357
if videoram != 0 {
346358
params["videoram"] = videoram

xoa/resource_xenorchestra_vm.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -407,8 +407,7 @@ func resourceVmCreate(d *schema.ResourceData, m interface{}) error {
407407
Id: rsId.(string),
408408
}
409409
}
410-
vm, err := c.CreateVm(client.Vm{
411-
AffinityHost: d.Get("affinity_host").(string),
410+
createVmParams := client.Vm{
412411
BlockedOperations: blockedOperations,
413412
Boot: client.Boot{
414413
Firmware: d.Get("hvm_boot_firmware").(string),
@@ -442,9 +441,13 @@ func resourceVmCreate(d *schema.ResourceData, m interface{}) error {
442441
Value: d.Get("videoram").(int),
443442
},
444443
Vga: d.Get("vga").(string),
445-
},
446-
d.Timeout(schema.TimeoutCreate),
447-
)
444+
}
445+
446+
affinityHost := d.Get("affinity_host").(string)
447+
if affinityHost != "" {
448+
createVmParams.AffinityHost = &affinityHost
449+
}
450+
vm, err := c.CreateVm(createVmParams, d.Timeout(schema.TimeoutCreate))
448451

449452
if err != nil {
450453
return err
@@ -780,7 +783,6 @@ func resourceVmUpdate(d *schema.ResourceData, m interface{}) error {
780783
HA: ha,
781784
ResourceSet: rs,
782785
AutoPoweron: autoPowerOn,
783-
AffinityHost: affinityHost,
784786
BlockedOperations: blockOperations,
785787
ExpNestedHvm: d.Get("exp_nested_hvm").(bool),
786788
StartDelay: d.Get("start_delay").(int),
@@ -794,6 +796,11 @@ func resourceVmUpdate(d *schema.ResourceData, m interface{}) error {
794796
Value: d.Get("videoram").(int),
795797
},
796798
}
799+
800+
if d.HasChange("affinity_host") {
801+
vmReq.AffinityHost = &affinityHost
802+
}
803+
797804
if haltForUpdates {
798805
err := c.HaltVm(id)
799806

0 commit comments

Comments
 (0)