-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.tf
54 lines (41 loc) · 1.24 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# ===================================
# Manage volumes in the Hetzner Cloud
# ===================================
# ------------
# Local Values
# ------------
locals {
# Build a map of all provided volume objects, indexed by volume name:
volumes = {
for volume in var.volumes : volume.name => volume
}
# Build a map of all provided volume objects to be attached, indexed
# by volume and server names:
attachments = {
for volume in local.volumes :
"${volume.name}:${volume.server.name}" => merge(volume, {
"volume" = volume.name
}) if(try(volume.server.name, null) != null)
}
}
# -------
# Volumes
# -------
resource "hcloud_volume" "volumes" {
for_each = local.volumes
name = each.value.name
location = each.value.location
size = each.value.size
format = each.value.format
delete_protection = each.value.protection
labels = each.value.labels
}
# ------------------
# Volume Attachments
# ------------------
resource "hcloud_volume_attachment" "attachments" {
for_each = local.attachments
server_id = each.value.server.id
volume_id = hcloud_volume.volumes[each.value.name].id
automount = each.value.server.automount
}