Skip to content

Commit

Permalink
AZ-515: Option to use Azure CAF naming provider to name resources
Browse files Browse the repository at this point in the history
  • Loading branch information
Shr3ps committed Mar 29, 2022
1 parent 5226d5c commit 56f1715
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
variables:
TF_MIN_VERSION: "0.12"
TF_MIN_VERSION: "0.13"
AZURERM_PROVIDER_MIN_VERSION: "2.8.0"

include:
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Unreleased

Breaking
* AZ-515: Option to use Azure CAF naming provider to name resources
* AZ-515: Require Terraform 0.13+

Changed
* AZ-572: Revamp examples and improve CI

Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ module "azure_network_subnet" {

| Name | Version |
|------|---------|
| azurecaf | ~> 1.1 |
| azurerm | >= 2.8.0 |

## Modules
Expand All @@ -121,6 +122,7 @@ No modules.

| Name | Type |
|------|------|
| [azurecaf_name.subnet](https://registry.terraform.io/providers/aztfmod/azurecaf/latest/docs/resources/name) | resource |
| [azurerm_subnet.subnet](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 |
Expand All @@ -135,7 +137,8 @@ No modules.
| enforce\_private\_link | Enable or Disable network policies for the private link endpoint on the subnet | `bool` | `false` | no |
| environment | Project environment | `string` | n/a | yes |
| location\_short | Short string for Azure location. | `string` | n/a | yes |
| name\_prefix | Optional prefix for subnet names | `string` | `""` | no |
| name\_prefix | Optional prefix for the generated name | `string` | `""` | no |
| name\_suffix | Optional suffix for the generated name | `string` | `""` | no |
| network\_security\_group\_name | The Network Security Group name to associate with the subnets | `string` | `null` | no |
| network\_security\_group\_rg | The Network Security Group RG to associate with the subnet. Default is the same RG than the subnet. | `string` | `null` | no |
| resource\_group\_name | Resource group name | `string` | n/a | yes |
Expand All @@ -145,6 +148,7 @@ No modules.
| 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 | Configuration delegations on subnet<br>object({<br> name = object({<br> name = string,<br> actions = list(string)<br> })<br>}) | `map(list(any))` | `{}` | no |
| use\_caf\_naming | Use the Azure CAF naming provider to generate default resource name. `custom_subnet_name` override this if set. Legacy default name is used if this is set to `false`. | `bool` | `true` | no |
| virtual\_network\_name | Virtual network name | `string` | n/a | yes |

## Outputs
Expand Down
7 changes: 7 additions & 0 deletions locals-naming.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
locals {
# Naming locals/constants
name_prefix = lower(var.name_prefix)
name_suffix = lower(var.name_suffix)

subnet_name = coalesce(var.custom_subnet_name, azurecaf_name.subnet.result)
}
5 changes: 0 additions & 5 deletions locals.tf
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
locals {
name_prefix = var.name_prefix != "" ? replace(var.name_prefix, "/[a-z0-9]$/", "$0-") : ""
default_name = lower("${local.name_prefix}${var.stack}-${var.client_name}-${var.location_short}-${var.environment}")

subnet_name = coalesce(var.custom_subnet_name, "${local.default_name}-subnet")

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)

Expand Down
9 changes: 9 additions & 0 deletions r-naming.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
resource "azurecaf_name" "subnet" {
name = var.stack
resource_type = "azurerm_subnet"
prefixes = var.name_prefix == "" ? null : [local.name_prefix]
suffixes = compact([var.client_name, var.location_short, var.environment, local.name_suffix, var.use_caf_naming ? "" : "subnet"])
use_slug = var.use_caf_naming
clean_input = true
separator = "-"
}
16 changes: 8 additions & 8 deletions terraform.tfvars.ci
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
location_short="frc"
client_name="test"
environment="test"
stack="ci"
resource_group_name="rg-test"
virtual_network_name="vnet-test"
subnet_cidr_list=["10.10.0.0/24"]
service_endpoints=["Microsoft.AzureActiveDirectory","Microsoft.AzureCosmosDB"]
location_short = "frc"
client_name = "test"
environment = "test"
stack = "ci"
resource_group_name = "rg-test"
virtual_network_name = "vnet-test"
subnet_cidr_list = ["10.10.0.0/24"]
service_endpoints = ["Microsoft.AzureActiveDirectory", "Microsoft.AzureCosmosDB"]
25 changes: 25 additions & 0 deletions variables-naming.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generic naming variables
variable "name_prefix" {
description = "Optional prefix for the generated name"
type = string
default = ""
}

variable "name_suffix" {
description = "Optional suffix for the generated name"
type = string
default = ""
}

variable "use_caf_naming" {
description = "Use the Azure CAF naming provider to generate default resource name. `custom_subnet_name` override this if set. Legacy default name is used if this is set to `false`."
type = bool
default = true
}

# Custom naming override
variable "custom_subnet_name" {
description = "Optional custom subnet name"
type = string
default = null
}
12 changes: 0 additions & 12 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,6 @@ variable "client_name" {
type = string
}

variable "name_prefix" {
description = "Optional prefix for subnet names"
type = string
default = ""
}

variable "custom_subnet_name" {
description = "Optional custom subnet name"
type = string
default = null
}

variable "environment" {
description = "Project environment"
type = string
Expand Down
6 changes: 5 additions & 1 deletion versions.tf
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@

terraform {
required_version = ">= 0.12.26"
required_version = ">= 0.13"
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = ">= 2.8.0"
}
azurecaf = {
source = "aztfmod/azurecaf"
version = "~> 1.1"
}
}
}

0 comments on commit 56f1715

Please sign in to comment.