Skip to content

Commit

Permalink
Merge pull request #1 from gsoft-inc/feature/per-module-suffix
Browse files Browse the repository at this point in the history
Feature/per module suffix
  • Loading branch information
yohanb authored Mar 29, 2020
2 parents e95d8be + 39a4fbc commit f1ef833
Show file tree
Hide file tree
Showing 45 changed files with 153 additions and 32 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
.vscode
.DS_Store
node_modules/
node_modules/
*.tfstate
*.tfstate.backup
.terraform/
25 changes: 5 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,23 @@ _Note: Since this module is comprised of multiple sub-modules, you need to [refe
## Example Usage
```hcl
provider "azurerm" {
version = "~>1.35.0"
}
provider "random" {
version = "~>2.2"
version = "=2.1.0"
}
variable "location" {
default = "eastus2"
}
resource "random_string" "suffix" {
length = 13
upper = false
lower = true
number = true
special = false
keepers = {
region = var.location
}
}
module "resource_group_name" {
source = "gsoft-inc/naming/azurerm//modules/general/resource_group"
name = "example"
prefixes = ["organization", "project", "production"]
suffixes = [random_string.suffix.result]
}
module "storage_account_name" {
source = "gsoft-inc/naming/azurerm//modules/storage/storage_account"
name = "example"
prefixes = ["org", "proj", "prod"]
suffixes = [random_string.suffix.result]
}
resource "azurerm_resource_group" "example" {
Expand All @@ -66,7 +49,9 @@ resource "azurerm_storage_account" "example" {
This example would result into something like this:

- `organization-project-production-example-35xvzaq251lja` (resource group)
- `org0proj0prod0example035` (storage account)
- `org0proj0prod0example01x` (storage account)

*Note: Random suffix is automatically generated but can but overriden.*

## Updating the module
```hcl
Expand All @@ -75,4 +60,4 @@ terraform get -update

## License

Copyright © 2019, GSoft inc. This code is licensed under the Apache License, Version 2.0. You may obtain a copy of this license at https://github.com/gsoft-inc/gsoft-license/blob/master/LICENSE.
Copyright © 2020, GSoft inc. This code is licensed under the Apache License, Version 2.0. You may obtain a copy of this license at https://github.com/gsoft-inc/gsoft-license/blob/master/LICENSE.
15 changes: 13 additions & 2 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
locals {
prefix = "${join(var.separator, var.prefixes)}"
suffix = "${join(var.separator, var.suffixes)}"
prefix = join(var.separator, var.prefixes)
suffix = length(var.suffixes) == 0 ? random_string.suffix.result : join(var.separator, var.suffixes)
separated_name = "${var.separator}${var.name}${var.separator}"
}

resource "random_string" "suffix" {
length = 13
upper = false
lower = true
number = true
special = false
keepers = {
name = var.name
}
}

data "null_data_source" "names" {
count = var.nb_instances
inputs = {
Expand Down
1 change: 1 addition & 0 deletions modules/compute/vm_linux/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ variable "prefixes" {
variable "suffixes" {
description = "List of suffixes to append at the end of the resource name."
type = list(string)
default = []
}

variable "nb_instances" {
Expand Down
1 change: 1 addition & 0 deletions modules/compute/vm_windows/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ variable "prefixes" {
variable "suffixes" {
description = "List of suffixes to append at the end of the resource name."
type = list(string)
default = []
}

variable "nb_instances" {
Expand Down
1 change: 1 addition & 0 deletions modules/containers/container_registry/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ variable "prefixes" {
variable "suffixes" {
description = "List of suffixes to append at the end of the resource name."
type = list(string)
default = []
}

variable "nb_instances" {
Expand Down
13 changes: 13 additions & 0 deletions modules/general/api_management/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,18 @@ module "api_management" {
name = var.name
prefixes = var.prefixes
suffixes = var.suffixes
separator = "-"
max_length = 50
nb_instances = var.nb_instances
}

data "null_data_source" "names" {
count = var.nb_instances
inputs = {
result = var.nb_instances > 1 ? regex("^[a-zA-Z0-9]{1}[a-zA-Z0-9-]*$", module.api_management.results[count.index]) : regex("^[a-zA-Z0-9]{1}[a-zA-Z0-9-]*$", module.api_management.result)
}
}

locals {
results = data.null_data_source.names.*.outputs.result
}
9 changes: 7 additions & 2 deletions modules/general/api_management/outputs.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
output "result" {
description = "The generated API management name."
value = regex("^[a-zA-Z0-9-]*$", module.api_management.result)
description = "The generated api management name."
value = local.results[0]
}

output "results" {
description = "The generated api management names."
value = local.results
}
7 changes: 6 additions & 1 deletion modules/general/api_management/variables.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
variable "name" {
description = "API management name."
description = "api management name."
type = string
}

Expand All @@ -11,4 +11,9 @@ variable "prefixes" {
variable "suffixes" {
description = "List of suffixes to append at the end of the resource name."
type = list(string)
default = []
}

variable "nb_instances" {
default = 1
}
1 change: 0 additions & 1 deletion modules/general/api_management/versions.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

terraform {
required_version = ">= 0.12"
}
13 changes: 13 additions & 0 deletions modules/general/key_vault/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,18 @@ module "key_vault" {
name = var.name
prefixes = var.prefixes
suffixes = var.suffixes
separator = "-"
max_length = 24
nb_instances = var.nb_instances
}

data "null_data_source" "names" {
count = var.nb_instances
inputs = {
result = var.nb_instances > 1 ? regex("^[a-zA-Z0-9]{1}[a-zA-Z0-9-]*$", module.key_vault.results[count.index]) : regex("^[a-zA-Z0-9]{1}[a-zA-Z0-9-]*$", module.key_vault.result)
}
}

locals {
results = data.null_data_source.names.*.outputs.result
}
7 changes: 6 additions & 1 deletion modules/general/key_vault/outputs.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
output "result" {
description = "The generated key vault name."
value = regex("^[a-zA-Z]{1}[a-zA-Z0-9-]*$", module.key_vault.result)
value = local.results[0]
}

output "results" {
description = "The generated key vault names."
value = local.results
}
5 changes: 5 additions & 0 deletions modules/general/key_vault/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,9 @@ variable "prefixes" {
variable "suffixes" {
description = "List of suffixes to append at the end of the resource name."
type = list(string)
default = []
}

variable "nb_instances" {
default = 1
}
1 change: 0 additions & 1 deletion modules/general/key_vault/versions.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

terraform {
required_version = ">= 0.12"
}
13 changes: 13 additions & 0 deletions modules/general/resource_group/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,18 @@ module "resource_group" {
name = var.name
prefixes = var.prefixes
suffixes = var.suffixes
separator = "-"
max_length = 90
nb_instances = var.nb_instances
}

data "null_data_source" "names" {
count = var.nb_instances
inputs = {
result = var.nb_instances > 1 ? regex("^[a-zA-Z0-9]{1}[a-zA-Z0-9-]*$", module.resource_group.results[count.index]) : regex("^[a-zA-Z0-9]{1}[a-zA-Z0-9-]*$", module.resource_group.result)
}
}

locals {
results = data.null_data_source.names.*.outputs.result
}
7 changes: 6 additions & 1 deletion modules/general/resource_group/outputs.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
output "result" {
description = "The generated resource group name."
value = regex("^[-\\w\\._\\(\\)]+$", module.resource_group.result)
value = local.results[0]
}

output "results" {
description = "The generated resource group names."
value = local.results
}
7 changes: 6 additions & 1 deletion modules/general/resource_group/variables.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
variable "name" {
description = "Resource group name."
description = "resource group name."
type = string
}

Expand All @@ -11,4 +11,9 @@ variable "prefixes" {
variable "suffixes" {
description = "List of suffixes to append at the end of the resource name."
type = list(string)
default = []
}

variable "nb_instances" {
default = 1
}
1 change: 0 additions & 1 deletion modules/general/resource_group/versions.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

terraform {
required_version = ">= 0.12"
}
1 change: 1 addition & 0 deletions modules/messaging/event_hub/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ variable "prefixes" {
variable "suffixes" {
description = "List of suffixes to append at the end of the resource name."
type = list(string)
default = []
}

variable "nb_instances" {
Expand Down
1 change: 1 addition & 0 deletions modules/messaging/event_hubs_namespace/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ variable "prefixes" {
variable "suffixes" {
description = "List of suffixes to append at the end of the resource name."
type = list(string)
default = []
}

variable "nb_instances" {
Expand Down
1 change: 1 addition & 0 deletions modules/messaging/service_bus_namespace/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ variable "prefixes" {
variable "suffixes" {
description = "List of suffixes to append at the end of the resource name."
type = list(string)
default = []
}

variable "nb_instances" {
Expand Down
1 change: 1 addition & 0 deletions modules/networking/application_gateway/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ variable "prefixes" {
variable "suffixes" {
description = "List of suffixes to append at the end of the resource name."
type = list(string)
default = []
}

variable "nb_instances" {
Expand Down
1 change: 1 addition & 0 deletions modules/networking/load_balancer/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ variable "prefixes" {
variable "suffixes" {
description = "List of suffixes to append at the end of the resource name."
type = list(string)
default = []
}

variable "nb_instances" {
Expand Down
1 change: 1 addition & 0 deletions modules/networking/load_balancer_rules_config/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ variable "prefixes" {
variable "suffixes" {
description = "List of suffixes to append at the end of the resource name."
type = list(string)
default = []
}

variable "nb_instances" {
Expand Down
1 change: 1 addition & 0 deletions modules/networking/network_interface/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ variable "prefixes" {
variable "suffixes" {
description = "List of suffixes to append at the end of the resource name."
type = list(string)
default = []
}

variable "nb_instances" {
Expand Down
1 change: 1 addition & 0 deletions modules/networking/network_security_group/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ variable "prefixes" {
variable "suffixes" {
description = "List of suffixes to append at the end of the resource name."
type = list(string)
default = []
}

variable "nb_instances" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ variable "prefixes" {
variable "suffixes" {
description = "List of suffixes to append at the end of the resource name."
type = list(string)
default = []
}

variable "nb_instances" {
Expand Down
1 change: 1 addition & 0 deletions modules/networking/public_ip_address/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ variable "prefixes" {
variable "suffixes" {
description = "List of suffixes to append at the end of the resource name."
type = list(string)
default = []
}

variable "nb_instances" {
Expand Down
1 change: 1 addition & 0 deletions modules/networking/subnet/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ variable "prefixes" {
variable "suffixes" {
description = "List of suffixes to append at the end of the resource name."
type = list(string)
default = []
}

variable "nb_instances" {
Expand Down
1 change: 1 addition & 0 deletions modules/networking/traffic_manager_profile/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ variable "prefixes" {
variable "suffixes" {
description = "List of suffixes to append at the end of the resource name."
type = list(string)
default = []
}

variable "nb_instances" {
Expand Down
1 change: 1 addition & 0 deletions modules/networking/virtual_network/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ variable "prefixes" {
variable "suffixes" {
description = "List of suffixes to append at the end of the resource name."
type = list(string)
default = []
}

variable "nb_instances" {
Expand Down
1 change: 1 addition & 0 deletions modules/storage/container_name/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ variable "prefixes" {
variable "suffixes" {
description = "List of suffixes to append at the end of the resource name."
type = list(string)
default = []
}

variable "nb_instances" {
Expand Down
1 change: 1 addition & 0 deletions modules/storage/data_lake_storage/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ variable "prefixes" {
variable "suffixes" {
description = "List of suffixes to append at the end of the resource name."
type = list(string)
default = []
}

variable "nb_instances" {
Expand Down
Loading

0 comments on commit f1ef833

Please sign in to comment.