canonical / terraform-provider-maas

Terraform MAAS provider
Mozilla Public License 2.0
60 stars 43 forks source link

bug: maas_machine resource leaves unsuccessfully added machines in New state instead of deleting them #153

Open vishvikkrishnan opened 7 months ago

vishvikkrishnan commented 7 months ago

The maas_machine resource does not remove unsuccessfully added machines (with a wrong 'power_address', previously existing 'pxe_mac_address', or duplicate 'hostname'), resulting in those machines showing as New in MAAS. The destroy action for such wrongly added machines does nothing.

Error: ServerError: 503 Service Unavailable (No rack controllers can access the BMC of node node01)
│ 
│   with maas_machine.node["node01"],
│   on main.tf line 1, in resource "maas_machine" "node":
│    1: resource "maas_machine" "node" {

However, when the correct details are passed and the resource is reapplied, errors are thrown because the wrongly added machines haven't yet been deleted.

 Error: ServerError: 400 Bad Request ({"mac_addresses": ["MAC address b8:ae:ed:73:38:40 already in use on node01."], "power_type": ["Can't set power type to amt without network interfaces"], "hostname": ["Node with this Hostname already exists."]})
│ 
│   with maas_machine.node["node01"],
│   on main.tf line 1, in resource "maas_machine" "node":
│    1: resource "maas_machine" "node" {

The resource should clean up any failed nodes that weren't added to ensure resource idempotency. Currently, I have to manually remove them MAAS before reattempting a terraform apply.

Tested with the following maas_machine resource on the TF MAAS provider version 2.0:

resource "maas_machine" "node" {
  for_each   = var.machines
  power_type = each.value.power_type
  power_parameters = jsonencode({
    power_address = each.value.bmc_address
    power_pass    = each.value.bmc_password
  })
  pxe_mac_address = each.value.mac_address
  hostname        = each.value.name
  zone            = each.value.zone
}
spdfnet commented 2 months ago

So you have a variable defined from where you can remove the faulty one, apply, then add it again.

Feels to me like usual OpenTofu behavior mixed with a lack of state flow support in the provider. But how would you model the latter? I feel the MAAS API is not ready for it either.