From 1c7fd5c86cf55334883584e14dac51013de883b1 Mon Sep 17 00:00:00 2001 From: "Alexandru Vlad (Scai)" <59282365+alexevladgabriel@users.noreply.github.com> Date: Fri, 19 Dec 2025 18:51:16 +0200 Subject: [PATCH 1/3] feat: get locations & server types from API --- .../templates/hetzner-linux/main.tf | 105 ++++++++++++------ 1 file changed, 73 insertions(+), 32 deletions(-) diff --git a/registry/Excellencedev/templates/hetzner-linux/main.tf b/registry/Excellencedev/templates/hetzner-linux/main.tf index 4b71e4561..f2ee8e4a4 100644 --- a/registry/Excellencedev/templates/hetzner-linux/main.tf +++ b/registry/Excellencedev/templates/hetzner-linux/main.tf @@ -6,6 +6,10 @@ terraform { coder = { source = "coder/coder" } + http = { + source = "hashicorp/http" + version = "~> 3.0" + } } } @@ -17,36 +21,42 @@ provider "hcloud" { token = var.hcloud_token } +data "http" "hcloud_locations" { + url = "https://api.hetzner.cloud/v1/locations" + + request_headers = { + Authorization = "Bearer ${var.hcloud_token}" + Accept = "application/json" + } +} + +data "http" "hcloud_server_types" { + url = "https://api.hetzner.cloud/v1/server_types" + + request_headers = { + Authorization = "Bearer ${var.hcloud_token}" + Accept = "application/json" + } +} + # Available locations: https://docs.hetzner.com/cloud/general/locations/ data "coder_parameter" "hcloud_location" { name = "hcloud_location" display_name = "Hetzner Location" description = "Select the Hetzner Cloud location for your workspace." type = "string" - default = "fsn1" - option { - name = "DE Falkenstein" - value = "fsn1" - } - option { - name = "US Ashburn, VA" - value = "ash" - } - option { - name = "US Hillsboro, OR" - value = "hil" - } - option { - name = "SG Singapore" - value = "sin" - } - option { - name = "DE Nuremberg" - value = "nbg1" - } - option { - name = "FI Helsinki" - value = "hel1" + + dynamic "option" { + for_each = local.hcloud_locations + content { + name = format( + "%s (%s, %s)", + upper(option.value.name), + option.value.city, + option.value.country + ) + value = option.value.name + } } } @@ -109,17 +119,48 @@ resource "hcloud_volume_attachment" "home_volume_attachment" { locals { username = lower(data.coder_workspace_owner.me.name) - # Data source: local JSON file under the module directory - # Check API for latest server types & availability: https://docs.hetzner.cloud/reference/cloud#server-types - hcloud_server_types_data = jsondecode(file("${path.module}/hetzner_server_types.json")) - hcloud_server_type_meta = local.hcloud_server_types_data.type_meta - hcloud_server_types_by_location = local.hcloud_server_types_data.availability + # -------------------- + # Locations + # -------------------- + hcloud_locations = [ + for loc in jsondecode(data.http.hcloud_locations.response_body).locations : { + name = loc.name + city = loc.city + country = loc.country + description = loc.description + } + ] + + # -------------------- + # Server Types + # -------------------- + hcloud_server_types = { + for st in jsondecode(data.http.hcloud_server_types.response_body).server_types : + st.name => { + cores = st.cores + memory_gb = st.memory + disk_gb = st.disk + locations = [for l in st.locations : l.name] + deprecated = st.deprecated + } + if st.deprecated == false + } hcloud_server_type_options_for_selected_location = [ - for type_name in lookup(local.hcloud_server_types_by_location, data.coder_parameter.hcloud_location.value, []) : { - name = format("%s (%d vCPU, %dGB RAM, %dGB)", upper(type_name), local.hcloud_server_type_meta[type_name].cores, local.hcloud_server_type_meta[type_name].memory_gb, local.hcloud_server_type_meta[type_name].disk_gb) - value = type_name + for name, meta in local.hcloud_server_types : { + name = format( + "%s (%d vCPU, %dGB RAM, %dGB)", + upper(name), + meta.cores, + meta.memory_gb, + meta.disk_gb + ) + value = name } + if contains( + meta.locations, + data.coder_parameter.hcloud_location.value + ) ] } From f0efdb1b538488dea81ad42e7b8de41f83502cd0 Mon Sep 17 00:00:00 2001 From: "Alexandru Vlad (Scai)" <59282365+alexevladgabriel@users.noreply.github.com> Date: Fri, 19 Dec 2025 18:51:25 +0200 Subject: [PATCH 2/3] chore: remove unused file --- .../hetzner_server_types.json\342\200\216" | 27 ------------------- 1 file changed, 27 deletions(-) delete mode 100644 "registry/Excellencedev/templates/hetzner-linux/hetzner_server_types.json\342\200\216" diff --git "a/registry/Excellencedev/templates/hetzner-linux/hetzner_server_types.json\342\200\216" "b/registry/Excellencedev/templates/hetzner-linux/hetzner_server_types.json\342\200\216" deleted file mode 100644 index 6be0938a7..000000000 --- "a/registry/Excellencedev/templates/hetzner-linux/hetzner_server_types.json\342\200\216" +++ /dev/null @@ -1,27 +0,0 @@ -{ - "type_meta": { - "cx22": { "cores": 2, "memory_gb": 4, "disk_gb": 40 }, - "cx32": { "cores": 4, "memory_gb": 8, "disk_gb": 80 }, - "cx42": { "cores": 8, "memory_gb": 16, "disk_gb": 160 }, - "cx52": { "cores": 16, "memory_gb": 32, "disk_gb": 320 }, - "cpx11": { "cores": 2, "memory_gb": 2, "disk_gb": 40 }, - "cpx21": { "cores": 3, "memory_gb": 4, "disk_gb": 80 }, - "cpx31": { "cores": 4, "memory_gb": 8, "disk_gb": 160 }, - "cpx41": { "cores": 8, "memory_gb": 16, "disk_gb": 240 }, - "cpx51": { "cores": 16, "memory_gb": 32, "disk_gb": 360 }, - "ccx13": { "cores": 2, "memory_gb": 8, "disk_gb": 80 }, - "ccx23": { "cores": 4, "memory_gb": 16, "disk_gb": 160 }, - "ccx33": { "cores": 8, "memory_gb": 32, "disk_gb": 240 }, - "ccx43": { "cores": 16, "memory_gb": 64, "disk_gb": 360 }, - "ccx53": { "cores": 32, "memory_gb": 128, "disk_gb": 600 }, - "ccx63": { "cores": 48, "memory_gb": 192, "disk_gb": 960 } - }, - "availability": { - "fsn1": ["cpx11", "cpx21", "cpx31", "cpx41", "cpx51", "ccx13", "ccx23", "ccx33"], - "ash": ["cpx11", "cpx21", "cpx31", "cpx41", "cpx51", "ccx13", "ccx23", "ccx33"], - "hel1": ["cx22", "cpx11", "cpx21", "cpx31", "cpx41", "cpx51", "ccx13", "ccx23", "ccx33"], - "hil": ["cpx11", "cpx21", "cpx31", "cpx41", "ccx13", "ccx23", "ccx33"], - "nbg1": ["cx22", "cx32", "cx42", "cx52", "cpx11", "cpx21", "cpx31", "cpx41", "cpx51", "ccx13", "ccx23", "ccx33"], - "sin": ["cpx11", "cpx21", "cpx31", "cpx41", "cpx51", "ccx13", "ccx23", "ccx33"] - } -} From f08be6e05d82aa523c82ec9ab76f1b9989884950 Mon Sep 17 00:00:00 2001 From: "Alexandru Vlad (Scai)" <59282365+alexevladgabriel@users.noreply.github.com> Date: Fri, 19 Dec 2025 18:58:23 +0200 Subject: [PATCH 3/3] chore: lint files --- registry/Excellencedev/templates/hetzner-linux/main.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/registry/Excellencedev/templates/hetzner-linux/main.tf b/registry/Excellencedev/templates/hetzner-linux/main.tf index f2ee8e4a4..1feea4f95 100644 --- a/registry/Excellencedev/templates/hetzner-linux/main.tf +++ b/registry/Excellencedev/templates/hetzner-linux/main.tf @@ -49,7 +49,7 @@ data "coder_parameter" "hcloud_location" { dynamic "option" { for_each = local.hcloud_locations content { - name = format( + name = format( "%s (%s, %s)", upper(option.value.name), option.value.city,