equinix / terraform-provider-equinix

Terraform Equinix provider
https://deploy.equinix.com/labs/terraform-provider-equinix/
MIT License
47 stars 44 forks source link

Expose customdata in equinix_metal_project #145

Open displague opened 2 years ago

displague commented 2 years ago

Metal projects offer a customdata field which can be used to store arbitrary JSON.

curl -H "X-Auth-Token: $METAL_AUTH_TOKEN" \
     -H 'Content-type: application/json' \
     -X PUT \
     -d '{"customdata": {"any":["json"]}' \
    https://api.equinix.com/metal/v1/projects/$METAL_PROJECT_ID

In Terraform, the above could translate to stash project scoped state, for example, for IPAM data:

variable "ipam" {
  description = "project scoped ipam (this is stored in the Metal Project API as customdata)"
  default = {vlans:{"vlan1001" = { cidr = "192.168.1/24", hosts = 20, offset = 5 }}
  }
}

resource "equinix_metal_project" "foo" {
  customdata = jsonencode(var.ipam)
}

resource "equinix_metal_device" "foo" {
  count = var.ipam.vlan1001.hosts
  userdata = templatefile("foo.yaml", { ip = cidrhost(var.ipam.vlan1001.cidr, count.index + var.ipam.vlan1001.offset) })
}

// equinix_metal_vlan to create vlan1001
// equinix_metal_port to attach each device to vlan1001

This is contrived, there are certainly other approaches that may be more effective. Alternatives could include SSH provisioners templated similarly to userdata above, triggered on node count changes.

Nonetheless, the API exposes a project customdata field and Terraform does not offer access to this field potentially keeping users from creative solutioning.

displague commented 2 years ago

This will depend on packngo support for Project.CustomData. See https://github.com/packethost/packngo/pull/225