From 56f1715158906a2439e26814b2d0799b372d9551 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Respaut?= Date: Tue, 29 Mar 2022 16:05:53 +0200 Subject: [PATCH] AZ-515: Option to use Azure CAF naming provider to name resources --- .gitlab-ci.yml | 2 +- CHANGELOG.md | 4 ++++ README.md | 6 +++++- locals-naming.tf | 7 +++++++ locals.tf | 5 ----- r-naming.tf | 9 +++++++++ terraform.tfvars.ci | 16 ++++++++-------- variables-naming.tf | 25 +++++++++++++++++++++++++ variables.tf | 12 ------------ versions.tf | 6 +++++- 10 files changed, 64 insertions(+), 28 deletions(-) create mode 100644 locals-naming.tf create mode 100644 r-naming.tf create mode 100644 variables-naming.tf diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 18b6a0c..00b16c7 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,6 +1,6 @@ --- variables: - TF_MIN_VERSION: "0.12" + TF_MIN_VERSION: "0.13" AZURERM_PROVIDER_MIN_VERSION: "2.8.0" include: diff --git a/CHANGELOG.md b/CHANGELOG.md index bcee50b..79380ab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index a8d99aa..71969a8 100644 --- a/README.md +++ b/README.md @@ -111,6 +111,7 @@ module "azure_network_subnet" { | Name | Version | |------|---------| +| azurecaf | ~> 1.1 | | azurerm | >= 2.8.0 | ## Modules @@ -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 | @@ -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 | @@ -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
object({
name = object({
name = string,
actions = list(string)
})
}) | `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 diff --git a/locals-naming.tf b/locals-naming.tf new file mode 100644 index 0000000..32c1769 --- /dev/null +++ b/locals-naming.tf @@ -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) +} diff --git a/locals.tf b/locals.tf index 62ff7ec..d988ee2 100644 --- a/locals.tf +++ b/locals.tf @@ -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) diff --git a/r-naming.tf b/r-naming.tf new file mode 100644 index 0000000..1ab8772 --- /dev/null +++ b/r-naming.tf @@ -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 = "-" +} diff --git a/terraform.tfvars.ci b/terraform.tfvars.ci index d8eea97..6468173 100644 --- a/terraform.tfvars.ci +++ b/terraform.tfvars.ci @@ -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"] diff --git a/variables-naming.tf b/variables-naming.tf new file mode 100644 index 0000000..e288a4f --- /dev/null +++ b/variables-naming.tf @@ -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 +} diff --git a/variables.tf b/variables.tf index 4052ae4..daea4ed 100644 --- a/variables.tf +++ b/variables.tf @@ -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 diff --git a/versions.tf b/versions.tf index 464e8bc..f154e0c 100644 --- a/versions.tf +++ b/versions.tf @@ -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" + } } }