Skip to content

Commit

Permalink
fix(network): fix "The "count" value depends on resource attributes t…
Browse files Browse the repository at this point in the history
…hat cannot be determined until apply"

Refs: hashicorp/terraform#26755
  • Loading branch information
mrclrchtr committed Mar 21, 2024
1 parent 64b6fee commit e621f9f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
11 changes: 9 additions & 2 deletions network.tf
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@ resource "hcloud_network_subnet" "nodes" {
ip_range = local.node_ipv4_cidr
}

locals {
create_floating_ip = var.enable_floating_ip && var.floating_ip == null
}

resource "hcloud_floating_ip" "control_plane_ipv4" {
count = var.enable_floating_ip ? 1 : 0
count = local.create_floating_ip ? 1 : 0
name = "control-plane-ipv4"
type = "ipv4"
home_location = data.hcloud_location.this.name
Expand All @@ -30,12 +34,15 @@ resource "hcloud_floating_ip" "control_plane_ipv4" {

data "hcloud_floating_ip" "control_plane_ipv4" {
count = var.enable_floating_ip ? 1 : 0
id = coalesce(var.floating_ip_id, hcloud_floating_ip.control_plane_ipv4[0].id)
id = coalesce(var.floating_ip.id, local.create_floating_ip ? hcloud_floating_ip.control_plane_ipv4[0].id : null)
}

resource "hcloud_floating_ip_assignment" "this" {
floating_ip_id = data.hcloud_floating_ip.control_plane_ipv4[0].id
server_id = hcloud_server.control_planes[0].id
depends_on = [
hcloud_server.control_planes,
]
}

resource "hcloud_primary_ip" "control_plane_ipv4" {
Expand Down
7 changes: 5 additions & 2 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,15 @@ variable "enable_floating_ip" {
description = "If true, a floating IP will be created and assigned to the control plane nodes."
}

variable "floating_ip_id" {
type = number
variable "floating_ip" {
type = object({
id = number,
})
default = null
description = <<EOF
The Floating IP (ID) to use for the control plane nodes.
If null (default), a new floating IP will be created.
(using object because of https://github.com/hashicorp/terraform/issues/26755)
EOF
}

Expand Down

0 comments on commit e621f9f

Please sign in to comment.