From 23cfcf0cdd165491c15ee3449b63d471203b540a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antti=20Kivim=C3=A4ki?= Date: Mon, 25 Sep 2023 14:27:14 +0300 Subject: [PATCH] Start using table variables --- examples/terraform-bootstrap/recipe.yml | 21 ++++++++++-------- .../templates/terraform/Taskfile.yml | 8 +++---- .../templates/terraform/state-output.tf | 4 +++- .../templates/terraform/state-storage.tf | 22 ++++++++++++------- 4 files changed, 32 insertions(+), 23 deletions(-) diff --git a/examples/terraform-bootstrap/recipe.yml b/examples/terraform-bootstrap/recipe.yml index 2eaebc72..4c4c1af7 100644 --- a/examples/terraform-bootstrap/recipe.yml +++ b/examples/terraform-bootstrap/recipe.yml @@ -6,14 +6,17 @@ initHelp: Install Task from https://taskfile.dev and run `task init -- dev` in t vars: - name: ENVIRONMENTS description: Comma-separated list of environments to create, e.g. "dev, qa, prod" - regexp: - pattern: "^[a-zA-Z0-9_.()-]+(,\\s*[a-zA-Z0-9_.()-]+)*$" - help: Please enter a comma-separated list of environment names where names consist of letters, digits, underscrores, hyphens, periods or parentheses. + columns: [NAME, RESOURCE_GROUP_NAME] + - name: SERVICE_NAME description: Service name - - name: RESOURCE_GROUP_NAMES - description: Comma-separated list of Azure Resource Group name for each environment - # TODO: Validate comma separated list of names - # regexp: - # pattern: "^[a-zA-Z0-9_.()-]{0,89}[a-zA-Z0-9_()-]$" - # help: Valid resource group names consist of 1-90 underscores, hyphens, periods, parentheses, and letters or digits, and the last character cannot be a period. + + - name: CREATE_RESOURCE_GROUPS + confirm: true + + - name: RESOURCE_GROUP_LOCATION + if: CREATE_RESOURCE_GROUPS == true + options: + - "North Europe" + - "West Europe" + # ... diff --git a/examples/terraform-bootstrap/templates/terraform/Taskfile.yml b/examples/terraform-bootstrap/templates/terraform/Taskfile.yml index 07e928ca..12217903 100644 --- a/examples/terraform-bootstrap/templates/terraform/Taskfile.yml +++ b/examples/terraform-bootstrap/templates/terraform/Taskfile.yml @@ -1,14 +1,12 @@ -{{- $environments := splitList "," .Variables.ENVIRONMENTS -}} version: "3" tasks: init: cmds: - {{- range $index, $env := $environments }} + {{- range $_, $env := .Variables.ENVIRONMENTS }} - task: tf:init-environment vars: - environment: {{ $env | quote }} - storage_account_name: {{ (printf "tfs%.11s" (regexReplaceAll "[^a-z0-9]" (.Variables.SERVICE_NAME | lower) "")) }}{{ stableRandomAlphanumeric 6 .Recipe.Anchor }}{{ $env }} + environment: {{ $env.NAME | quote }} {{- end }} tf:init-environment: deps: [az:login] @@ -46,7 +44,7 @@ tasks: cmds: - task: tf:init-environment vars: - environment: {{ `{{.CLI_ARGS}}` }} + environment: {{ `"{{.CLI_ARGS}}"` }} az:login: cmds: - az login diff --git a/examples/terraform-bootstrap/templates/terraform/state-output.tf b/examples/terraform-bootstrap/templates/terraform/state-output.tf index 24a1e278..05e7736e 100644 --- a/examples/terraform-bootstrap/templates/terraform/state-output.tf +++ b/examples/terraform-bootstrap/templates/terraform/state-output.tf @@ -1,5 +1,7 @@ +{{- $rgRef := ternary "resource" "data" .Variables.CREATE_RESOURCE_GROUPS -}} + output "resource_group_name" { - value = data.azurerm_resource_group.main.name + value = {{ $rgRef }}.azurerm_resource_group.main.name } output "tfstate_storage_account_name" { diff --git a/examples/terraform-bootstrap/templates/terraform/state-storage.tf b/examples/terraform-bootstrap/templates/terraform/state-storage.tf index bf632489..1d6e578f 100644 --- a/examples/terraform-bootstrap/templates/terraform/state-storage.tf +++ b/examples/terraform-bootstrap/templates/terraform/state-storage.tf @@ -1,26 +1,32 @@ -{{- $environments := splitList "," .Variables.ENVIRONMENTS }} -{{- $resource_groups := splitList "," .Variables.RESOURCE_GROUP_NAMES -}} +{{- $rgRef := ternary "resource" "data" .Variables.CREATE_RESOURCE_GROUPS -}} locals { resource_groups = { - {{- range $index, $env := $environments }} - {{ $env | quote }}: {{ (index $resource_groups $index) | quote }} + {{- range $index, $env := .Variables.ENVIRONMENTS }} + {{ $env.NAME | quote }}: {{ $env.RESOURCE_GROUP_NAME | quote }} {{- end }} - "default": {{ (index $resource_groups 0) | quote }} + "default": {{ (index .Variables.ENVIRONMENTS 0).RESOURCE_GROUP_NAME | quote }} } } data "azurerm_client_config" "current" { } +{{ if .Variables.CREATE_RESOURCE_GROUPS -}} +resource "azurerm_resource_group" "main" { + name = local.resource_groups[terraform.workspace] + location = {{ .Variables.RESOURCE_GROUP_LOCATION | quote }} +} +{{- else -}} data "azurerm_resource_group" "main" { name = local.resource_groups[terraform.workspace] } +{{- end }} resource "azurerm_storage_account" "tfstate" { - name = "{{ (printf "tfs%.11s" (regexReplaceAll "[^a-z0-9]" (.Variables.SERVICE_NAME | lower) "")) }}{{ stableRandomAlphanumeric 6 .Recipe.Anchor }}${terraform.workspace}" - resource_group_name = data.azurerm_resource_group.main.name - location = data.azurerm_resource_group.main.location + name = "{{ (printf "tfs%.11s" (regexReplaceAll "[^a-z0-9]" (.Variables.SERVICE_NAME | lower) "")) }}{{ stableRandomAlphanumeric 6 .ID }}${terraform.workspace}" + resource_group_name = {{ $rgRef }}.azurerm_resource_group.main.name + location = {{ $rgRef }}.azurerm_resource_group.main.location account_tier = "Standard" account_replication_type = "LRS" min_tls_version = "TLS1_2"