@@ -6,6 +6,10 @@ terraform {
66 coder = {
77 source = " coder/coder"
88 }
9+ http = {
10+ source = " hashicorp/http"
11+ version = " ~> 3.0"
12+ }
913 }
1014}
1115
@@ -17,36 +21,42 @@ provider "hcloud" {
1721 token = var. hcloud_token
1822}
1923
24+ data "http" "hcloud_locations" {
25+ url = " https://api.hetzner.cloud/v1/locations"
26+
27+ request_headers = {
28+ Authorization = " Bearer ${ var . hcloud_token } "
29+ Accept = " application/json"
30+ }
31+ }
32+
33+ data "http" "hcloud_server_types" {
34+ url = " https://api.hetzner.cloud/v1/server_types"
35+
36+ request_headers = {
37+ Authorization = " Bearer ${ var . hcloud_token } "
38+ Accept = " application/json"
39+ }
40+ }
41+
2042# Available locations: https://docs.hetzner.com/cloud/general/locations/
2143data "coder_parameter" "hcloud_location" {
2244 name = " hcloud_location"
2345 display_name = " Hetzner Location"
2446 description = " Select the Hetzner Cloud location for your workspace."
2547 type = " string"
26- default = " fsn1"
27- option {
28- name = " DE Falkenstein"
29- value = " fsn1"
30- }
31- option {
32- name = " US Ashburn, VA"
33- value = " ash"
34- }
35- option {
36- name = " US Hillsboro, OR"
37- value = " hil"
38- }
39- option {
40- name = " SG Singapore"
41- value = " sin"
42- }
43- option {
44- name = " DE Nuremberg"
45- value = " nbg1"
46- }
47- option {
48- name = " FI Helsinki"
49- value = " hel1"
48+
49+ dynamic "option" {
50+ for_each = local. hcloud_locations
51+ content {
52+ name = format (
53+ " %s (%s, %s)" ,
54+ upper (option. value . name ),
55+ option. value . city ,
56+ option. value . country
57+ )
58+ value = option. value . name
59+ }
5060 }
5161}
5262
@@ -109,17 +119,48 @@ resource "hcloud_volume_attachment" "home_volume_attachment" {
109119locals {
110120 username = lower (data. coder_workspace_owner . me . name )
111121
112- # Data source: local JSON file under the module directory
113- # Check API for latest server types & availability: https://docs.hetzner.cloud/reference/cloud#server-types
114- hcloud_server_types_data = jsondecode (file (" ${ path . module } /hetzner_server_types.json" ))
115- hcloud_server_type_meta = local. hcloud_server_types_data . type_meta
116- hcloud_server_types_by_location = local. hcloud_server_types_data . availability
122+ # --------------------
123+ # Locations
124+ # --------------------
125+ hcloud_locations = [
126+ for loc in jsondecode (data. http . hcloud_locations . response_body ). locations : {
127+ name = loc . name
128+ city = loc . city
129+ country = loc . country
130+ description = loc . description
131+ }
132+ ]
133+
134+ # --------------------
135+ # Server Types
136+ # --------------------
137+ hcloud_server_types = {
138+ for st in jsondecode (data. http . hcloud_server_types . response_body ). server_types :
139+ st.name = > {
140+ cores = st.cores
141+ memory_gb = st.memory
142+ disk_gb = st.disk
143+ locations = [for l in st . locations : l . name ]
144+ deprecated = st.deprecated
145+ }
146+ if st . deprecated == false
147+ }
117148
118149 hcloud_server_type_options_for_selected_location = [
119- for type_name in lookup (local. hcloud_server_types_by_location , data. coder_parameter . hcloud_location . value , []) : {
120- 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 )
121- value = type_name
150+ for name , meta in local . hcloud_server_types : {
151+ name = format (
152+ " %s (%d vCPU, %dGB RAM, %dGB)" ,
153+ upper (name),
154+ meta. cores ,
155+ meta. memory_gb ,
156+ meta. disk_gb
157+ )
158+ value = name
122159 }
160+ if contains (
161+ meta. locations ,
162+ data. coder_parameter . hcloud_location . value
163+ )
123164 ]
124165}
125166
0 commit comments