From 380fcff20c16602e650fd1e381797d09fc335ae3 Mon Sep 17 00:00:00 2001 From: unkin-agent Date: Sat, 3 Oct 2026 15:41:38 +1000 Subject: [PATCH] Create NetBox sites rather than requiring them to pre-exist The syd1 site does not exist, so the site data source failed the plan. Sites now follow the same declared-by-reference rule as device roles, types and manufacturers. --- README.md | 4 ++++ modules/netbox/devices.tf | 2 +- modules/netbox/main.tf | 12 +++++++++--- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 41f9663..2c96dff 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,10 @@ ci/puppetdb_backfill.py # reality backfill generator `make plan` / `make apply` reconcile the YAML against NetBox. State lives in Consul under `infra/terraform/netbox///state`. +Sites, device roles, device types and manufacturers are all declared by being +referenced: any slug a subnet or device names is created. A site that already +exists in NetBox must be imported, not re-created, since site names are unique. + ## Subnet schema (`subnets/.yaml`) ```yaml diff --git a/modules/netbox/devices.tf b/modules/netbox/devices.tf index f9a7027..0ecb8fa 100644 --- a/modules/netbox/devices.tf +++ b/modules/netbox/devices.tf @@ -52,7 +52,7 @@ resource "netbox_device" "this" { name = each.key device_type_id = netbox_device_type.this[each.value.model_hint].id role_id = netbox_device_role.this[each.value.role].id - site_id = tonumber(data.netbox_site.this[each.value.site].id) + site_id = tonumber(netbox_site.this[each.value.site].id) status = each.value.pxe ? "staged" : "active" platform_id = try(netbox_platform.this[each.value.provision.platform].id, null) diff --git a/modules/netbox/main.tf b/modules/netbox/main.tf index 5adcb27..30038fc 100644 --- a/modules/netbox/main.tf +++ b/modules/netbox/main.tf @@ -28,9 +28,15 @@ locals { } } -data "netbox_site" "this" { +# Sites are declared by being referenced: every slug named by a subnet or a +# device is created, exactly as device roles, types and manufacturers are. A +# site that already exists in NetBox must be imported rather than re-created +# (NetBox site names are unique). +resource "netbox_site" "this" { for_each = local.sites - slug = each.value + + name = each.value + slug = each.value } # One tag per network so prefixes can be discovered by network membership. @@ -47,7 +53,7 @@ resource "netbox_prefix" "this" { prefix = each.value.prefix status = "active" description = each.value.description - site_id = each.value.site != null ? tonumber(data.netbox_site.this[each.value.site].id) : null + site_id = each.value.site != null ? tonumber(netbox_site.this[each.value.site].id) : null tags = [for t in local.prefix_net_tags[each.key] : t] depends_on = [netbox_tag.network]