Skip to content

Commit

Permalink
WIP: Ensure that updates to the xenorchestra_vm resource properly han…
Browse files Browse the repository at this point in the history
…dle XO api nullable fields
  • Loading branch information
ddelnano committed Apr 12, 2023
1 parent 60507a7 commit 4cba195
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
8 changes: 6 additions & 2 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 @@ -241,6 +240,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
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 @@ -440,9 +439,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 @@ -778,7 +781,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 @@ -792,6 +794,11 @@ func resourceVmUpdate(d *schema.ResourceData, m interface{}) error {
Value: d.Get("videoram").(int),
},
}

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

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

Expand Down

0 comments on commit 4cba195

Please sign in to comment.