a3984bf0cd
A per-site data-source lookup cannot resolve a global object before the global unit has applied it, which deadlocks the first plan. Pass device-type, device role and dhcp IPAM role ids as dependency outputs instead, mocked for plan.
56 lines
1.7 KiB
Terraform
56 lines
1.7 KiB
Terraform
locals {
|
|
# Netmask suffix per subnet, e.g. "24".
|
|
masks = { for k, v in var.subnets : k => split("/", v.prefix)[1] }
|
|
|
|
# Subnets that request a DHCP scope.
|
|
dhcp_subnets = { for k, v in var.subnets : k => v if try(v.dhcp.enabled, false) }
|
|
|
|
# Subnets that declare a gateway.
|
|
gateways = { for k, v in var.subnets : k => v if v.router != null }
|
|
}
|
|
|
|
# The one site this unit owns. Its slug is the config directory
|
|
# (config/<region>/<dc>), never repeated inside the YAML; `name` defaults to it.
|
|
# A site that already exists in NetBox must be imported rather than re-created
|
|
# (NetBox site names are unique).
|
|
resource "netbox_site" "this" {
|
|
name = coalesce(var.site.name, var.site_slug)
|
|
slug = var.site_slug
|
|
description = var.site.description
|
|
}
|
|
|
|
resource "netbox_prefix" "this" {
|
|
for_each = var.subnets
|
|
|
|
prefix = each.value.prefix
|
|
status = "active"
|
|
description = each.value.description
|
|
site_id = tonumber(netbox_site.this.id)
|
|
}
|
|
|
|
resource "netbox_ip_range" "dhcp" {
|
|
for_each = local.dhcp_subnets
|
|
|
|
start_address = "${cidrhost(each.value.prefix, each.value.dhcp.start)}/${local.masks[each.key]}"
|
|
end_address = "${cidrhost(each.value.prefix, each.value.dhcp.stop)}/${local.masks[each.key]}"
|
|
role_id = var.dhcp_role_id
|
|
status = "active"
|
|
description = "DHCP pool for ${each.key}"
|
|
}
|
|
|
|
resource "netbox_ip_address" "gateway" {
|
|
for_each = local.gateways
|
|
|
|
ip_address = "${cidrhost(each.value.prefix, each.value.router)}/${local.masks[each.key]}"
|
|
status = "active"
|
|
description = "gateway"
|
|
}
|
|
|
|
resource "netbox_ip_address" "managed" {
|
|
for_each = { for m in var.managed_ips : m.ip => m }
|
|
|
|
ip_address = each.value.ip
|
|
status = "active"
|
|
description = each.value.description
|
|
}
|