Skip to content

Commit

Permalink
Ensure xenorchestra_vm resource updates properly handle nullable XO…
Browse files Browse the repository at this point in the history
… 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)
  • Loading branch information
ddelnano authored Aug 15, 2023
1 parent f87dece commit 7e47f7f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
4 changes: 3 additions & 1 deletion client/vif.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ func (c *Client) CreateVIF(vm *Vm, vif *VIF) (*VIF, error) {
params := map[string]interface{}{
"network": vif.Network,
"vm": vm.Id,
"mac": vif.MacAddress,
}
if vif.MacAddress != "" {
params["mac"] = vif.MacAddress
}
err := c.Call("vm.createInterface", params, &id)

Expand Down
18 changes: 15 additions & 3 deletions client/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ type Vm struct {
Boot Boot `json:"boot,omitempty"`
Type string `json:"type,omitempty"`
Id string `json:"id,omitempty"`
AffinityHost string `json:"affinityHost,omitempty"`
AffinityHost *string `json:"affinityHost,omitempty"`
NameDescription string `json:"name_description"`
NameLabel string `json:"name_label"`
CPUs CPUs `json:"CPUs"`
Expand Down Expand Up @@ -212,7 +212,6 @@ func (c *Client) CreateVm(vmReq Vm, createTime time.Duration) (*Vm, error) {
}

params := map[string]interface{}{
"affinityHost": vmReq.AffinityHost,
"bootAfterCreate": true,
"name_label": vmReq.NameLabel,
"name_description": vmReq.NameDescription,
Expand Down Expand Up @@ -243,6 +242,11 @@ func (c *Client) CreateVm(vmReq Vm, createTime time.Duration) (*Vm, error) {
params["hvmBootFirmware"] = firmware
}

affinityHost := vmReq.AffinityHost
if affinityHost != nil {
params["affinityHost"] = affinityHost
}

vga := vmReq.Vga
if vga != "" {
params["vga"] = vga
Expand Down Expand Up @@ -319,7 +323,6 @@ func createVdiMap(disk Disk) map[string]interface{} {
func (c *Client) UpdateVm(vmReq Vm) (*Vm, error) {
params := map[string]interface{}{
"id": vmReq.Id,
"affinityHost": vmReq.AffinityHost,
"name_label": vmReq.NameLabel,
"name_description": vmReq.NameDescription,
"auto_poweron": vmReq.AutoPoweron,
Expand All @@ -341,6 +344,15 @@ func (c *Client) UpdateVm(vmReq Vm) (*Vm, error) {
// coresPerSocket is null or a number of cores per socket. Putting an invalid value doesn't seem to cause an error :(
}

affinityHost := vmReq.AffinityHost
if affinityHost != nil {
if *affinityHost == "" {
params["affinityHost"] = nil
} else {
params["affinityHost"] = *affinityHost
}
}

videoram := vmReq.Videoram.Value
if videoram != 0 {
params["videoram"] = videoram
Expand Down
19 changes: 13 additions & 6 deletions xoa/resource_xenorchestra_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,8 +407,7 @@ func resourceVmCreate(d *schema.ResourceData, m interface{}) error {
Id: rsId.(string),
}
}
vm, err := c.CreateVm(client.Vm{
AffinityHost: d.Get("affinity_host").(string),
createVmParams := client.Vm{
BlockedOperations: blockedOperations,
Boot: client.Boot{
Firmware: d.Get("hvm_boot_firmware").(string),
Expand Down Expand Up @@ -442,9 +441,13 @@ func resourceVmCreate(d *schema.ResourceData, m interface{}) error {
Value: d.Get("videoram").(int),
},
Vga: d.Get("vga").(string),
},
d.Timeout(schema.TimeoutCreate),
)
}

affinityHost := d.Get("affinity_host").(string)
if affinityHost != "" {
createVmParams.AffinityHost = &affinityHost
}
vm, err := c.CreateVm(createVmParams, d.Timeout(schema.TimeoutCreate))

if err != nil {
return err
Expand Down Expand Up @@ -780,7 +783,6 @@ func resourceVmUpdate(d *schema.ResourceData, m interface{}) error {
HA: ha,
ResourceSet: rs,
AutoPoweron: autoPowerOn,
AffinityHost: affinityHost,
BlockedOperations: blockOperations,
ExpNestedHvm: d.Get("exp_nested_hvm").(bool),
StartDelay: d.Get("start_delay").(int),
Expand All @@ -794,6 +796,11 @@ func resourceVmUpdate(d *schema.ResourceData, m interface{}) error {
Value: d.Get("videoram").(int),
},
}

if d.HasChange("affinity_host") {
vmReq.AffinityHost = &affinityHost
}

if haltForUpdates {
err := c.HaltVm(id)

Expand Down

0 comments on commit 7e47f7f

Please sign in to comment.