From 1950c5bf39db58d687b5f2dfa55db8d6820aabc3 Mon Sep 17 00:00:00 2001 From: Robert Kaussow Date: Sun, 9 Oct 2022 16:53:45 +0200 Subject: [PATCH] refactor: remove data sources from module --- data.tf | 18 ------------------ main.tf | 37 +++++++++++++------------------------ outputs.tf | 8 -------- providers.tf | 19 ------------------- variables.tf | 4 ++-- 5 files changed, 15 insertions(+), 71 deletions(-) delete mode 100644 data.tf delete mode 100644 providers.tf diff --git a/data.tf b/data.tf deleted file mode 100644 index b33f3e3..0000000 --- a/data.tf +++ /dev/null @@ -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" -} diff --git a/main.tf b/main.tf index e620a67..c85a4f0 100644 --- a/main.tf +++ b/main.tf @@ -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 { server_volumes = flatten([ for server_key, server in var.server : [ @@ -31,19 +19,12 @@ locals { record_type = domain.type record_value = domain.value 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" { for_each = { for row in var.server : row.name => row } depends_on = [hcloud_ssh_key.key] @@ -55,7 +36,7 @@ resource "hcloud_server" "server" { ssh_keys = var.server_keys backups = try(each.value.backups, 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 { 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)}" } +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" { for_each = { for row in var.server : row.name => row } @@ -112,7 +101,7 @@ resource "hcloud_rdns" "serverv6" { resource "cloudflare_record" "serverv4" { 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 value = hcloud_server.server[each.value.name].ipv4_address type = "A" @@ -122,7 +111,7 @@ resource "cloudflare_record" "serverv4" { resource "cloudflare_record" "serverv6" { 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 value = hcloud_server.server[each.value.name].ipv6_address type = "AAAA" @@ -144,7 +133,7 @@ resource "restapi_object" "ucs_server" { path = "/dns/host_record/" 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" : { "name" : each.value.name, "a" : [ diff --git a/outputs.tf b/outputs.tf index 353c6e0..d99791c 100644 --- a/outputs.tf +++ b/outputs.tf @@ -3,11 +3,3 @@ output "output" { 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 -} diff --git a/providers.tf b/providers.tf deleted file mode 100644 index 1865ad2..0000000 --- a/providers.tf +++ /dev/null @@ -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" - } -} diff --git a/variables.tf b/variables.tf index ac8104e..3a91b17 100644 --- a/variables.tf +++ b/variables.tf @@ -13,7 +13,7 @@ variable "cloudflare_api_token" { } variable "cloudflare_zones" { - type = list(string) + type = any } // Univention @@ -30,7 +30,7 @@ variable "ucs_api_password" { } variable "ucs_zones" { - type = list(string) + type = any } // Module