Skip to content

Commit

Permalink
Refactor public IP
Browse files Browse the repository at this point in the history
  • Loading branch information
mjmar01 committed Apr 10, 2024
1 parent cd11152 commit 3e8b454
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 15 deletions.
2 changes: 1 addition & 1 deletion locals.tf
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down
4 changes: 2 additions & 2 deletions main.tf
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
}
}

Expand Down
15 changes: 3 additions & 12 deletions variables.tf
Original file line number Diff line number Diff line change
@@ -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, ...
```
Expand Down

0 comments on commit 3e8b454

Please sign in to comment.