From 3e8b454da40f0559603f86a97d5184e97e765cca Mon Sep 17 00:00:00 2001 From: mmaring Date: Wed, 10 Apr 2024 10:47:11 +0200 Subject: [PATCH] Refactor public IP --- locals.tf | 2 +- main.tf | 4 ++-- variables.tf | 15 +++------------ 3 files changed, 6 insertions(+), 15 deletions(-) diff --git a/locals.tf b/locals.tf index c07017b..5a8ab15 100644 --- a/locals.tf +++ b/locals.tf @@ -1,6 +1,6 @@ locals { public_ip = { - name = coalesce(var.name_overrides.public_ip, "pip-${var.public_ip_config.stage}-${var.virtual_machine_config.hostname}-01-${var.virtual_machine_config.location}") + name = var.public_ip_config != null ? coalesce(var.name_overrides.public_ip, "pip-${var.public_ip_config.stage}-${var.virtual_machine_config.hostname}-01-${var.virtual_machine_config.location}") : "" } nic = { diff --git a/main.tf b/main.tf index 4f21979..c4e24c0 100644 --- a/main.tf +++ b/main.tf @@ -1,5 +1,5 @@ resource "azurerm_public_ip" "this" { - count = var.public_ip_config.enabled ? 1 : 0 + count = var.public_ip_config != null ? 1 : 0 name = local.public_ip.name resource_group_name = var.resource_group_name location = var.virtual_machine_config.location @@ -20,7 +20,7 @@ resource "azurerm_network_interface" "this" { subnet_id = var.subnet.id private_ip_address_allocation = var.nic_config.private_ip == null ? "Dynamic" : "Static" private_ip_address = var.nic_config.private_ip - public_ip_address_id = var.public_ip_config.enabled ? azurerm_public_ip.this[0].id : null + public_ip_address_id = var.public_ip_config != null ? azurerm_public_ip.this[0].id : null } } diff --git a/variables.tf b/variables.tf index 27fe5fb..536fd2c 100644 --- a/variables.tf +++ b/variables.tf @@ -1,24 +1,15 @@ variable "public_ip_config" { type = object({ - enabled = bool allocation_method = optional(string, "Static") - stage = optional(string) + stage = string }) - default = { - enabled = false - } - nullable = false + default = null validation { - condition = contains(["Static", "Dynamic"], var.public_ip_config.allocation_method) + condition = var.public_ip_config != null ? contains(["Static", "Dynamic"], var.public_ip_config.allocation_method) : true error_message = "Allocation method must be Static or Dynamic" } - validation { - condition = var.public_ip_config.enabled == true ? var.public_ip_config.stage != null : true - error_message = "If public ip is enabled, stage must be set." - } description = <<-DOC ``` - enabled: Optionally select true if a public ip should be created. Defaults to false. allocation_method: The allocation method of the public ip that will be created. Defaults to static. stage: The stage of this PIP. Ex: prd, dev, tst, ... ```