Skip to content

Commit

Permalink
refactor(AZ-1088): apply suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
Shr3ps committed Nov 6, 2024
1 parent 90b68d5 commit 15395d3
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 31 deletions.
22 changes: 12 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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. | <pre>map(list(object({<br/> name = string<br/> actions = list(string)<br/> })))</pre> | `{}` | 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 |
Expand All @@ -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. | <pre>map(list(object({<br/> name = string<br/> actions = list(string)<br/> })))</pre> | `{}` | 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. |
<!-- END_TF_DOCS -->
## Related documentation
Expand Down
5 changes: 3 additions & 2 deletions examples/main/base.tf
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module "rg" {
environment = var.environment
stack = var.stack
}

module "vnet" {
source = "claranet/vnet/azurerm"
version = "x.x.x"
Expand All @@ -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"]
}
4 changes: 2 additions & 2 deletions examples/main/modules.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion locals-naming.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
4 changes: 2 additions & 2 deletions locals.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
19 changes: 15 additions & 4 deletions outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
}
22 changes: 16 additions & 6 deletions r-subnet.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand All @@ -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" {
}
2 changes: 1 addition & 1 deletion variables-naming.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions variables-subnet.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
Expand All @@ -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
}

0 comments on commit 15395d3

Please sign in to comment.