Skip to content

Commit

Permalink
fix: specific subnet module
Browse files Browse the repository at this point in the history
  • Loading branch information
mamrajyadav committed Dec 27, 2023
1 parent 1afc359 commit 2495e01
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 58 deletions.
2 changes: 1 addition & 1 deletion _example/name-specific_subnet/example.tf
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ module "name_specific_subnet" {

#subnet
specific_name_subnet = true
specific_subnet_names = "SpecificSubnet"
specific_subnet_names = ["SpecificSubnet"]
subnet_prefixes = ["10.0.1.0/24"]

# route_table
Expand Down
1 change: 0 additions & 1 deletion _example/name-specific_subnet/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,3 @@ output "route_table_associated_subnets" {
value = module.name_specific_subnet[*].route_table_associated_subnets[0]
description = "The collection of Subnets associated with this route table."
}

54 changes: 11 additions & 43 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ module "labels" {

#Subnet
resource "azurerm_subnet" "subnet" {
count = var.enable && var.specific_name_subnet == false ? length(var.subnet_names) : 0
name = "${var.name}-${var.subnet_names[count.index]}"
count = var.enable && var.specific_name_subnet == false ? length(var.subnet_names) : length(var.specific_subnet_names)
name = var.specific_name_subnet == false ? "${var.name}-${element(var.subnet_names, count.index)}" : var.specific_subnet_names[0]
resource_group_name = var.resource_group_name
address_prefixes = [var.subnet_prefixes[count.index]]
virtual_network_name = var.virtual_network_name
service_endpoints = var.service_endpoints
service_endpoint_policy_ids = var.service_endpoint_policy_ids
private_link_service_network_policies_enabled = var.subnet_enforce_private_link_service_network_policies
private_endpoint_network_policies_enabled = lookup(var.subnet_enforce_private_link_endpoint_network_policies, var.subnet_names[count.index], false)
private_endpoint_network_policies_enabled = var.subnet_enforce_private_link_endpoint_network_policies

dynamic "delegation" {
for_each = var.delegation
Expand All @@ -38,35 +38,9 @@ resource "azurerm_subnet" "subnet" {
}
}

resource "azurerm_subnet" "subnet2" {
count = var.enable && var.specific_name_subnet == true ? 1 : 0
name = var.specific_subnet_names
resource_group_name = var.resource_group_name
address_prefixes = [var.subnet_prefixes[count.index]]
virtual_network_name = var.virtual_network_name
service_endpoints = var.service_endpoints
service_endpoint_policy_ids = var.service_endpoint_policy_ids
private_link_service_network_policies_enabled = var.subnet_enforce_private_link_service_network_policies
private_endpoint_network_policies_enabled = lookup(var.subnet_enforce_private_link_endpoint_network_policies, var.specific_subnet_names, false)

dynamic "delegation" {
for_each = var.delegation
content {
name = delegation.key
dynamic "service_delegation" {
for_each = toset(delegation.value)
content {
name = service_delegation.value.name
actions = service_delegation.value.actions
}
}
}
}
}

#Nat Gateway
##Nat Gateway
resource "azurerm_public_ip" "pip" {
count = var.create_nat_gateway ? 1 : 0
count = var.enable && var.create_nat_gateway ? 1 : 0
name = format("%s-nat-gateway-ip", module.labels.id)
allocation_method = var.allocation_method
location = var.location
Expand All @@ -76,7 +50,7 @@ resource "azurerm_public_ip" "pip" {
}

resource "azurerm_nat_gateway" "natgw" {
count = var.create_nat_gateway ? 1 : 0
count = var.enable && var.create_nat_gateway ? 1 : 0
name = format("%s-nat-gateway", module.labels.id)
location = var.location
resource_group_name = var.resource_group_name
Expand All @@ -87,15 +61,15 @@ resource "azurerm_nat_gateway" "natgw" {
}

resource "azurerm_nat_gateway_public_ip_association" "pip_assoc" {
count = var.create_nat_gateway ? 1 : 0
count = var.enable && var.create_nat_gateway ? 1 : 0
nat_gateway_id = join("", azurerm_nat_gateway.natgw[*].id)
public_ip_address_id = azurerm_public_ip.pip[0].id
}

resource "azurerm_subnet_nat_gateway_association" "subnet_assoc" {
count = var.create_nat_gateway ? (var.specific_name_subnet == false ? length(azurerm_subnet.subnet[*].id) : length(azurerm_subnet.subnet2[*].id)) : 0
count = var.enable && var.create_nat_gateway ? var.specific_name_subnet == false ? length(var.subnet_names) : length(var.specific_subnet_names) : 0
nat_gateway_id = join("", azurerm_nat_gateway.natgw[*].id)
subnet_id = var.specific_name_subnet == false ? azurerm_subnet.subnet.*.id[count.index] : azurerm_subnet.subnet2.*.id[count.index]
subnet_id = element(azurerm_subnet.subnet[*].id, count.index)
}

#Route Table
Expand All @@ -119,13 +93,7 @@ resource "azurerm_route_table" "rt" {
}

resource "azurerm_subnet_route_table_association" "main" {
count = var.enable && var.enable_route_table && var.specific_name_subnet == false ? length(var.subnet_prefixes) : 0
count = var.enable && var.specific_name_subnet == false ? length(var.subnet_names) : length(var.specific_subnet_names)
subnet_id = element(azurerm_subnet.subnet[*].id, count.index)
route_table_id = join("", azurerm_route_table.rt[*].id)
}

resource "azurerm_subnet_route_table_association" "main2" {
count = var.enable && var.enable_route_table && var.specific_name_subnet ? length(var.subnet_prefixes) : 0
subnet_id = element(azurerm_subnet.subnet2[*].id, count.index)
route_table_id = join("", azurerm_route_table.rt[*].id)
route_table_id = azurerm_route_table.rt[0].id
}
8 changes: 4 additions & 4 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
output "specific_subnet_name" {
value = azurerm_subnet.subnet2[*].name
value = azurerm_subnet.subnet[*].name
description = "The name of the subnet."
}

output "specific_subnet_id" {
value = azurerm_subnet.subnet2[*].id
description = "The subnet ID."
value = azurerm_subnet.subnet[*].id
description = "The name of the subnet."
}

output "specific_subnet_address_prefixes" {
description = "The address prefixes for the subnet."
value = azurerm_subnet.subnet2[*].address_prefixes
value = azurerm_subnet.subnet[*].address_prefixes
}

output "default_subnet_name" {
Expand Down
18 changes: 9 additions & 9 deletions variables.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
variable "name" {
type = string
default = ""
default = null
description = "Name (e.g. `app` or `cluster`)."
}

Expand All @@ -18,7 +18,7 @@ variable "repository" {

variable "environment" {
type = string
default = ""
default = null
description = "Environment (e.g. `prod`, `dev`, `staging`)."
}

Expand All @@ -42,13 +42,13 @@ variable "enable" {

variable "resource_group_name" {
type = string
default = ""
default = null
description = "The name of an existing resource group to be imported."
}

variable "location" {
type = string
default = ""
default = null
description = "The location/region where the virtual network is created. Changing this forces a new resource to be created."
}

Expand All @@ -65,8 +65,8 @@ variable "subnet_names" {
}

variable "subnet_enforce_private_link_endpoint_network_policies" {
type = map(bool)
default = {}
type = bool
default = false
description = "A map with key (string) `subnet name`, value (bool) `true` or `false` to indicate enable or disable network policies for the private link endpoint on the subnet. Default value is false."
}

Expand Down Expand Up @@ -103,14 +103,14 @@ variable "specific_name_subnet" {
}

variable "specific_subnet_names" {
type = string
default = ""
type = list(string)
default = [""]
description = "A list of subnets inside the vNet."
}

variable "virtual_network_name" {
type = string
default = ""
default = null
description = "The name of the virtual network in which the subnet is created in"
}

Expand Down

0 comments on commit 2495e01

Please sign in to comment.