2022-09-01 19:43:01 +00:00
|
|
|
locals {
|
2022-09-03 21:12:06 +00:00
|
|
|
cloudflare_zones = {
|
2022-09-01 19:43:01 +00:00
|
|
|
for zone in try(data.cloudflare_zones.zones, []) : zone.zones[0].name => zone.zones[0].id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-03 21:12:06 +00:00
|
|
|
locals {
|
|
|
|
ucs_zones = {
|
|
|
|
for zone in try(data.restapi_object.ucs_zones, []) : zone.api_data.id => zone.api_data.dn
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-01 19:43:01 +00:00
|
|
|
locals {
|
|
|
|
server_volumes = flatten([
|
|
|
|
for server_key, server in var.server : [
|
|
|
|
for volume_key, volume in try(server.volumes, []) : {
|
|
|
|
volume_name = volume.name
|
|
|
|
volume_size = volume.size
|
|
|
|
server_id = hcloud_server.server[server.name].id
|
|
|
|
}
|
|
|
|
]
|
|
|
|
])
|
|
|
|
}
|
|
|
|
|
|
|
|
locals {
|
|
|
|
server_domains = flatten([
|
|
|
|
for server_key, server in var.server : [
|
|
|
|
for domain_key, domain in try(server.domains, []) : {
|
|
|
|
record_id = try(domain.id, domain.name)
|
|
|
|
record_name = domain.name
|
|
|
|
record_type = domain.type
|
|
|
|
record_value = domain.value
|
|
|
|
record_ttl = try(domain.ttl, 1)
|
2022-09-03 21:12:06 +00:00
|
|
|
zone_id = local.cloudflare_zones[try(domain.zone_name, var.server_dns_zone)]
|
2022-09-01 19:43:01 +00:00
|
|
|
}
|
|
|
|
]
|
|
|
|
])
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "hcloud_placement_group" "group" {
|
|
|
|
for_each = { for key, row in var.server : row.name => row if contains(keys(row), "placement_group") }
|
|
|
|
|
|
|
|
name = each.value.placement_group
|
|
|
|
type = "spread"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "hcloud_server" "server" {
|
|
|
|
for_each = { for row in var.server : row.name => row }
|
|
|
|
depends_on = [hcloud_ssh_key.key]
|
|
|
|
|
|
|
|
name = each.value.name
|
|
|
|
server_type = try(each.value.type, "cx11")
|
|
|
|
image = try(each.value.image, "rocky-9")
|
|
|
|
datacenter = try(each.value.datacenter, "fsn1-dc14")
|
|
|
|
ssh_keys = var.server_keys
|
|
|
|
backups = try(each.value.backups, true)
|
|
|
|
keep_disk = try(each.value.keep_disk, false)
|
|
|
|
placement_group_id = try(hcloud_placement_group.group[each.value.placement_group].id, null)
|
|
|
|
|
|
|
|
lifecycle {
|
|
|
|
ignore_changes = [ssh_keys]
|
|
|
|
}
|
|
|
|
|
|
|
|
labels = merge(
|
|
|
|
{
|
2022-09-04 11:38:44 +00:00
|
|
|
provisioner = "terraform"
|
2022-09-01 19:43:01 +00:00
|
|
|
project = var.hcloud_project
|
|
|
|
},
|
|
|
|
{
|
|
|
|
for index, service in try(each.value.services, []) : "service_${index + 1}" => service
|
|
|
|
},
|
|
|
|
{
|
2022-09-04 12:09:14 +00:00
|
|
|
for index, type in try(each.value.types, []) : "type_${index + 1}" => type
|
2022-09-01 19:43:01 +00:00
|
|
|
},
|
|
|
|
try(each.value.labels, {})
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "hcloud_volume" "volume" {
|
|
|
|
for_each = { for row in local.server_volumes : row.volume_name => row }
|
|
|
|
|
|
|
|
name = each.value.volume_name
|
|
|
|
size = each.value.volume_size
|
|
|
|
server_id = each.value.server_id
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "hcloud_ssh_key" "key" {
|
|
|
|
for_each = { for row in toset(var.ssh_keys) : element(split(" ", row), 2) => row }
|
|
|
|
|
|
|
|
name = each.key
|
|
|
|
public_key = "${element(split(" ", each.value), 0)} ${element(split(" ", each.value), 1)}"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "hcloud_rdns" "serverv4" {
|
|
|
|
for_each = { for row in var.server : row.name => row }
|
|
|
|
|
|
|
|
server_id = hcloud_server.server[each.value.name].id
|
|
|
|
ip_address = hcloud_server.server[each.value.name].ipv4_address
|
2022-09-03 21:12:06 +00:00
|
|
|
dns_ptr = "${each.value.name}.${try(each.value.dns_zone, var.server_dns_zone)}"
|
2022-09-01 19:43:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "hcloud_rdns" "serverv6" {
|
|
|
|
for_each = { for row in var.server : row.name => row }
|
|
|
|
|
|
|
|
server_id = hcloud_server.server[each.value.name].id
|
|
|
|
ip_address = hcloud_server.server[each.value.name].ipv6_address
|
2022-09-03 21:12:06 +00:00
|
|
|
dns_ptr = "${each.value.name}.${try(each.value.dns_zone, var.server_dns_zone)}"
|
2022-09-01 19:43:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "cloudflare_record" "serverv4" {
|
|
|
|
for_each = { for row in var.server : row.name => row }
|
|
|
|
|
2022-09-03 21:12:06 +00:00
|
|
|
zone_id = local.cloudflare_zones[try(each.value.dns_zone, var.server_dns_zone)]
|
2022-09-01 19:43:01 +00:00
|
|
|
name = each.value.name
|
|
|
|
value = hcloud_server.server[each.value.name].ipv4_address
|
|
|
|
type = "A"
|
|
|
|
ttl = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "cloudflare_record" "serverv6" {
|
|
|
|
for_each = { for row in var.server : row.name => row }
|
|
|
|
|
2022-09-03 21:12:06 +00:00
|
|
|
zone_id = local.cloudflare_zones[try(each.value.dns_zone, var.server_dns_zone)]
|
2022-09-01 19:43:01 +00:00
|
|
|
name = each.value.name
|
|
|
|
value = hcloud_server.server[each.value.name].ipv6_address
|
|
|
|
type = "AAAA"
|
|
|
|
ttl = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "cloudflare_record" "record" {
|
|
|
|
for_each = { for row in local.server_domains : row.record_id => row }
|
|
|
|
|
|
|
|
zone_id = each.value.zone_id
|
|
|
|
name = each.value.record_name
|
|
|
|
value = each.value.record_value
|
|
|
|
type = each.value.record_type
|
|
|
|
ttl = each.value.record_ttl
|
|
|
|
}
|
2022-09-03 21:12:06 +00:00
|
|
|
|
|
|
|
resource "restapi_object" "ucs_server" {
|
|
|
|
for_each = { for row in var.server : row.name => row }
|
|
|
|
|
|
|
|
path = "/dns/host_record/"
|
|
|
|
data = jsonencode({
|
|
|
|
"position" : local.ucs_zones[try(each.value.dns_zone, var.server_dns_zone)],
|
|
|
|
"properties" : {
|
|
|
|
"name" : each.value.name,
|
|
|
|
"a" : [
|
|
|
|
hcloud_server.server[each.value.name].ipv4_address,
|
|
|
|
hcloud_server.server[each.value.name].ipv6_address,
|
|
|
|
],
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|