From 15395d3da31373e60ff91c18ce545c8921705a58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Respaut?= Date: Thu, 10 Oct 2024 10:24:52 +0200 Subject: [PATCH] refactor(AZ-1088): apply suggestions --- README.md | 22 ++++++++++++---------- examples/main/base.tf | 5 +++-- examples/main/modules.tf | 4 ++-- locals-naming.tf | 2 +- locals.tf | 4 ++-- outputs.tf | 19 +++++++++++++++---- r-subnet.tf | 22 ++++++++++++++++------ variables-naming.tf | 2 +- variables-subnet.tf | 6 +++--- 9 files changed, 55 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index d5d27b2..c002d96 100644 --- a/README.md +++ b/README.md @@ -71,8 +71,8 @@ module "subnet" { resource_group_name = module.rg.name virtual_network_name = module.vnet.name - subnet_cidr_list = ["10.0.1.0/26"] - subnet_delegation = { + cidrs = ["10.0.1.0/26"] + delegations = { app-service-plan = [ { name = "Microsoft.Web/serverFarms" @@ -105,18 +105,20 @@ No modules. | Name | Type | |------|------| | [azurerm_subnet.main](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/subnet) | resource | -| [azurerm_subnet_network_security_group_association.subnet_association](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/subnet_network_security_group_association) | resource | -| [azurerm_subnet_route_table_association.route_table_association](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/subnet_route_table_association) | resource | +| [azurerm_subnet_network_security_group_association.main](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/subnet_network_security_group_association) | resource | +| [azurerm_subnet_route_table_association.main](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/subnet_route_table_association) | resource | | [azurecaf_name.subnet](https://registry.terraform.io/providers/claranet/azurecaf/latest/docs/data-sources/name) | data source | -| [azurerm_subscription.current](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/data-sources/subscription) | data source | +| [azurerm_subscription.main](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/data-sources/subscription) | data source | ## Inputs | Name | Description | Type | Default | Required | |------|-------------|------|---------|:--------:| +| cidrs | The address prefix list to use for the subnet. | `list(string)` | n/a | yes | | client\_name | Client name/account used in naming. | `string` | n/a | yes | -| custom\_subnet\_name | Optional custom subnet name. | `string` | `null` | no | -| default\_outbound\_access\_enabled | Enable or Disable default\_outbound\_access. See https://learn.microsoft.com/en-us/azure/virtual-network/ip-services/default-outbound-access | `bool` | `false` | no | +| custom\_name | Optional custom subnet name. | `string` | `null` | no | +| default\_outbound\_access\_enabled | Enable or disable `default_outbound_access`. See [documentation](https://learn.microsoft.com/en-us/azure/virtual-network/ip-services/default-outbound-access). | `bool` | `false` | no | +| delegations | Subnet delegations configuration. |
map(list(object({
name = string
actions = list(string)
})))
| `{}` | no | | environment | Project environment. | `string` | n/a | yes | | location\_short | Short string for Azure location. | `string` | n/a | yes | | name\_prefix | Optional prefix for the generated name. | `string` | `""` | no | @@ -132,21 +134,21 @@ No modules. | service\_endpoint\_policy\_ids | The list of IDs of Service Endpoint Policies to associate with the subnet. | `list(string)` | `null` | no | | service\_endpoints | The list of Service endpoints to associate with the subnet. | `list(string)` | `[]` | no | | stack | Project stack name. | `string` | n/a | yes | -| subnet\_cidr\_list | The address prefix list to use for the subnet. | `list(string)` | n/a | yes | -| subnet\_delegation | Subnet delegations configuration. |
map(list(object({
name = string
actions = list(string)
})))
| `{}` | no | | virtual\_network\_name | Virtual network name. | `string` | n/a | yes | ## Outputs | Name | Description | |------|-------------| -| cidrs\_list | CIDR list of the created subnets. | +| cidrs | CIDR list of the created subnets. | | cidrs\_map | Map with names and CIDRs of the created subnets. | | id | ID of the created subnet. | | ips | The collection of IPs within this subnet. | | name | Name of the created subnet. | +| nsg\_association | Subnet network security group association resource object. | | nsg\_association\_id | Subnet network security group association ID. | | resource | Subnet resource object. | +| rt\_association | Subnet route table association resource object. | | rt\_association\_id | Subnet route table association ID. | ## Related documentation diff --git a/examples/main/base.tf b/examples/main/base.tf index 418dc16..553440b 100644 --- a/examples/main/base.tf +++ b/examples/main/base.tf @@ -16,6 +16,7 @@ module "rg" { environment = var.environment stack = var.stack } + module "vnet" { source = "claranet/vnet/azurerm" version = "x.x.x" @@ -25,7 +26,7 @@ module "vnet" { location_short = module.azure_region.location_short client_name = var.client_name stack = var.stack - resource_group_name = module.rg.resource_group_name + resource_group_name = module.rg.name - vnet_cidr = ["10.0.1.0/24"] + cidrs = ["10.0.1.0/24"] } diff --git a/examples/main/modules.tf b/examples/main/modules.tf index 78b20ee..adc5898 100644 --- a/examples/main/modules.tf +++ b/examples/main/modules.tf @@ -34,8 +34,8 @@ module "subnet" { resource_group_name = module.rg.name virtual_network_name = module.vnet.name - subnet_cidr_list = ["10.0.1.0/26"] - subnet_delegation = { + cidrs = ["10.0.1.0/26"] + delegations = { app-service-plan = [ { name = "Microsoft.Web/serverFarms" diff --git a/locals-naming.tf b/locals-naming.tf index e010edc..9a01537 100644 --- a/locals-naming.tf +++ b/locals-naming.tf @@ -3,5 +3,5 @@ locals { name_prefix = lower(var.name_prefix) name_suffix = lower(var.name_suffix) - subnet_name = coalesce(var.custom_subnet_name, data.azurecaf_name.subnet.result) + name = coalesce(var.custom_name, data.azurecaf_name.subnet.result) } diff --git a/locals.tf b/locals.tf index d988ee2..f8649a5 100644 --- a/locals.tf +++ b/locals.tf @@ -2,7 +2,7 @@ locals { network_security_group_rg = coalesce(var.network_security_group_rg, var.resource_group_name) route_table_rg = coalesce(var.route_table_rg, var.resource_group_name) - network_security_group_id = format("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/networkSecurityGroups/%s", data.azurerm_subscription.current.subscription_id, local.network_security_group_rg, coalesce(var.network_security_group_name, "fake")) + network_security_group_id = format("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/networkSecurityGroups/%s", data.azurerm_subscription.main.subscription_id, local.network_security_group_rg, coalesce(var.network_security_group_name, "fake")) - route_table_id = format("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/routeTables/%s", data.azurerm_subscription.current.subscription_id, local.route_table_rg, coalesce(var.route_table_name, "fake")) + route_table_id = format("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/routeTables/%s", data.azurerm_subscription.main.subscription_id, local.route_table_rg, coalesce(var.route_table_name, "fake")) } diff --git a/outputs.tf b/outputs.tf index 52d45f5..98a2a0a 100644 --- a/outputs.tf +++ b/outputs.tf @@ -8,7 +8,7 @@ output "id" { value = azurerm_subnet.main.id } -output "cidrs_list" { +output "cidrs" { description = "CIDR list of the created subnets." value = azurerm_subnet.main.address_prefixes } @@ -27,15 +27,26 @@ output "name" { output "ips" { description = "The collection of IPs within this subnet." - value = var.subnet_cidr_list[*] + value = var.cidrs[*] +} + +output "nsg_association" { + description = "Subnet network security group association resource object." + value = one(azurerm_subnet_network_security_group_association.main[*]) } output "nsg_association_id" { description = "Subnet network security group association ID." - value = one(azurerm_subnet_network_security_group_association.subnet_association[*].id) + value = one(azurerm_subnet_network_security_group_association.main[*].id) +} + +output "rt_association" { + description = "Subnet route table association resource object." + value = one(azurerm_subnet_route_table_association.main[*]) + } output "rt_association_id" { description = "Subnet route table association ID." - value = one(azurerm_subnet_route_table_association.route_table_association[*].id) + value = one(azurerm_subnet_route_table_association.main[*].id) } diff --git a/r-subnet.tf b/r-subnet.tf index 94637c6..571af64 100644 --- a/r-subnet.tf +++ b/r-subnet.tf @@ -4,16 +4,16 @@ moved { } resource "azurerm_subnet" "main" { - name = local.subnet_name + name = local.name resource_group_name = var.resource_group_name virtual_network_name = var.virtual_network_name - address_prefixes = var.subnet_cidr_list + address_prefixes = var.cidrs service_endpoints = var.service_endpoints service_endpoint_policy_ids = var.service_endpoint_policy_ids dynamic "delegation" { - for_each = var.subnet_delegation + for_each = var.delegations content { name = delegation.key dynamic "service_delegation" { @@ -32,19 +32,29 @@ resource "azurerm_subnet" "main" { default_outbound_access_enabled = var.default_outbound_access_enabled } -resource "azurerm_subnet_network_security_group_association" "subnet_association" { +resource "azurerm_subnet_network_security_group_association" "main" { count = var.network_security_group_name == null ? 0 : 1 subnet_id = azurerm_subnet.main.id network_security_group_id = local.network_security_group_id } -resource "azurerm_subnet_route_table_association" "route_table_association" { +moved { + from = azurerm_subnet_network_security_group_association.subnet_association + to = azurerm_subnet_network_security_group_association.main +} + +resource "azurerm_subnet_route_table_association" "main" { count = var.route_table_name == null ? 0 : 1 subnet_id = azurerm_subnet.main.id route_table_id = local.route_table_id } -data "azurerm_subscription" "current" { +moved { + from = azurerm_subnet_route_table_association.route_table_association + to = azurerm_subnet_route_table_association.main +} + +data "azurerm_subscription" "main" { } diff --git a/variables-naming.tf b/variables-naming.tf index 79342b9..39f4faa 100644 --- a/variables-naming.tf +++ b/variables-naming.tf @@ -12,7 +12,7 @@ variable "name_suffix" { } # Custom naming override -variable "custom_subnet_name" { +variable "custom_name" { description = "Optional custom subnet name." type = string default = null diff --git a/variables-subnet.tf b/variables-subnet.tf index 6aa499d..35bf06d 100644 --- a/variables-subnet.tf +++ b/variables-subnet.tf @@ -3,7 +3,7 @@ variable "virtual_network_name" { type = string } -variable "subnet_cidr_list" { +variable "cidrs" { description = "The address prefix list to use for the subnet." type = list(string) } @@ -62,7 +62,7 @@ variable "private_link_service_enabled" { default = null } -variable "subnet_delegation" { +variable "delegations" { description = "Subnet delegations configuration." type = map(list(object({ name = string @@ -73,7 +73,7 @@ variable "subnet_delegation" { } variable "default_outbound_access_enabled" { - description = "Enable or Disable default_outbound_access. See https://learn.microsoft.com/en-us/azure/virtual-network/ip-services/default-outbound-access" + description = "Enable or disable `default_outbound_access`. See [documentation](https://learn.microsoft.com/en-us/azure/virtual-network/ip-services/default-outbound-access)." type = bool default = false }