refactor: remove data sources from module
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Robert Kaussow 2022-10-09 16:53:45 +02:00
parent b0fd947abf
commit 1950c5bf39
Signed by: xoxys
GPG Key ID: 4E692A2EAECC03C0
5 changed files with 15 additions and 71 deletions

18
data.tf
View File

@ -1,18 +0,0 @@
data "cloudflare_zones" "zones" {
for_each = toset(try(var.cloudflare_zones, []))
filter {
name = each.key
status = "active"
}
}
data "restapi_object" "ucs_zones" {
for_each = toset(try(var.ucs_zones, []))
query_string = "filter=univentionObjectType=dns/forward_zone"
path = "/dns/forward_zone/"
search_key = "id"
search_value = each.key
results_key = "_embedded/udm:object"
}

37
main.tf
View File

@ -1,15 +1,3 @@
locals {
cloudflare_zones = {
for zone in try(data.cloudflare_zones.zones, []) : zone.zones[0].name => zone.zones[0].id
}
}
locals {
ucs_zones = {
for zone in try(data.restapi_object.ucs_zones, []) : zone.api_data.id => zone.api_data.dn
}
}
locals { locals {
server_volumes = flatten([ server_volumes = flatten([
for server_key, server in var.server : [ for server_key, server in var.server : [
@ -31,19 +19,12 @@ locals {
record_type = domain.type record_type = domain.type
record_value = domain.value record_value = domain.value
record_ttl = try(domain.ttl, 1) record_ttl = try(domain.ttl, 1)
zone_id = local.cloudflare_zones[try(domain.zone_name, var.server_dns_zone)] zone_id = var.cloudflare_zones[try(domain.zone_name, var.server_dns_zone)]
} }
] ]
]) ])
} }
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" { resource "hcloud_server" "server" {
for_each = { for row in var.server : row.name => row } for_each = { for row in var.server : row.name => row }
depends_on = [hcloud_ssh_key.key] depends_on = [hcloud_ssh_key.key]
@ -55,7 +36,7 @@ resource "hcloud_server" "server" {
ssh_keys = var.server_keys ssh_keys = var.server_keys
backups = try(each.value.backups, false) backups = try(each.value.backups, false)
keep_disk = try(each.value.keep_disk, false) keep_disk = try(each.value.keep_disk, false)
placement_group_id = try(hcloud_placement_group.group[each.value.placement_group].id, null) placement_group_id = each.value.placement_group
lifecycle { lifecycle {
ignore_changes = [ssh_keys] ignore_changes = [ssh_keys]
@ -93,6 +74,14 @@ resource "hcloud_ssh_key" "key" {
public_key = "${element(split(" ", each.value), 0)} ${element(split(" ", each.value), 1)}" public_key = "${element(split(" ", each.value), 0)} ${element(split(" ", each.value), 1)}"
} }
resource "hcloud_server_network" "network" {
for_each = { for row in var.server : row.name => row if contains(keys(row), "network") }
server_id = hcloud_server.server[each.value.name].id
network_id = each.value.network
ip = each.value.network_ip
}
resource "hcloud_rdns" "serverv4" { resource "hcloud_rdns" "serverv4" {
for_each = { for row in var.server : row.name => row } for_each = { for row in var.server : row.name => row }
@ -112,7 +101,7 @@ resource "hcloud_rdns" "serverv6" {
resource "cloudflare_record" "serverv4" { resource "cloudflare_record" "serverv4" {
for_each = { for row in var.server : row.name => row } for_each = { for row in var.server : row.name => row }
zone_id = local.cloudflare_zones[try(each.value.dns_zone, var.server_dns_zone)] zone_id = var.cloudflare_zones[try(each.value.dns_zone, var.server_dns_zone)]
name = each.value.name name = each.value.name
value = hcloud_server.server[each.value.name].ipv4_address value = hcloud_server.server[each.value.name].ipv4_address
type = "A" type = "A"
@ -122,7 +111,7 @@ resource "cloudflare_record" "serverv4" {
resource "cloudflare_record" "serverv6" { resource "cloudflare_record" "serverv6" {
for_each = { for row in var.server : row.name => row } for_each = { for row in var.server : row.name => row }
zone_id = local.cloudflare_zones[try(each.value.dns_zone, var.server_dns_zone)] zone_id = var.cloudflare_zones[try(each.value.dns_zone, var.server_dns_zone)]
name = each.value.name name = each.value.name
value = hcloud_server.server[each.value.name].ipv6_address value = hcloud_server.server[each.value.name].ipv6_address
type = "AAAA" type = "AAAA"
@ -144,7 +133,7 @@ resource "restapi_object" "ucs_server" {
path = "/dns/host_record/" path = "/dns/host_record/"
data = jsonencode({ data = jsonencode({
"position" : local.ucs_zones[try(each.value.dns_zone, var.server_dns_zone)], "position" : var.ucs_zones[try(each.value.dns_zone, var.server_dns_zone)],
"properties" : { "properties" : {
"name" : each.value.name, "name" : each.value.name,
"a" : [ "a" : [

View File

@ -3,11 +3,3 @@ output "output" {
for server_key, server in hcloud_server.server : server_key => server for server_key, server in hcloud_server.server : server_key => server
} }
} }
output "cloudflare_zones" {
value = local.cloudflare_zones
}
output "ucs_zones" {
value = local.ucs_zones
}

View File

@ -1,19 +0,0 @@
provider "hcloud" {
token = var.hcloud_token
}
provider "cloudflare" {
api_token = var.cloudflare_api_token
}
provider "restapi" {
uri = var.ucs_api_url
username = var.ucs_api_username
password = var.ucs_api_password
id_attribute = "dn"
debug = true
create_returns_object = true
headers = {
accept = "application/json"
}
}

View File

@ -13,7 +13,7 @@ variable "cloudflare_api_token" {
} }
variable "cloudflare_zones" { variable "cloudflare_zones" {
type = list(string) type = any
} }
// Univention // Univention
@ -30,7 +30,7 @@ variable "ucs_api_password" {
} }
variable "ucs_zones" { variable "ucs_zones" {
type = list(string) type = any
} }
// Module // Module