Skip to content

Commit

Permalink
bug: commission machines in New state by their PXC MAC
Browse files Browse the repository at this point in the history
  • Loading branch information
skatsaounis committed Jun 17, 2024
1 parent 6152862 commit 264f838
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions maas/resource_maas_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,18 +152,37 @@ func resourceMaasMachine() *schema.Resource {
}

func resourceMachineCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var machine *entity.Machine
client := meta.(*client.Client)

// Create MAAS machine
powerParams, err := getMachinePowerParams(d)
machines, err := client.Machines.Get(&entity.MachinesParams{MACAddress: []string{d.Get("pxe_mac_address").(string)}})
if err != nil {
return diag.FromErr(err)
}
machine, err := client.Machines.Create(getMachineParams(d), powerParams)

powerParams, err := getMachinePowerParams(d)
if err != nil {
return diag.FromErr(err)
}

if len(machines) == 1 && machines[0].StatusName == "New" {
// Update MAAS machine with Terraform configuration
if machine, err = client.Machine.Update(machines[0].SystemID, getMachineParams(d), powerParams); err != nil {
return diag.FromErr(err)
}
// Commission MAAS machine
machine, err = client.Machine.Commission(machine.SystemID, &entity.MachineCommissionParams{})
if err != nil {
return diag.FromErr(err)
}
} else {
// Create MAAS machine
machine, err = client.Machines.Create(getMachineParams(d), powerParams)
if err != nil {
return diag.FromErr(err)
}
}

// Save Id
d.SetId(machine.SystemID)

Expand Down

0 comments on commit 264f838

Please sign in to comment.