Skip to content

Commit

Permalink
Start using table variables
Browse files Browse the repository at this point in the history
  • Loading branch information
majori committed Sep 25, 2023
1 parent 1b7a5dc commit 23cfcf0
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 23 deletions.
21 changes: 12 additions & 9 deletions examples/terraform-bootstrap/recipe.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
# ...
8 changes: 3 additions & 5 deletions examples/terraform-bootstrap/templates/terraform/Taskfile.yml
Original file line number Diff line number Diff line change
@@ -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]
Expand Down Expand Up @@ -46,7 +44,7 @@ tasks:
cmds:
- task: tf:init-environment
vars:
environment: {{ `{{.CLI_ARGS}}` }}
environment: {{ `"{{.CLI_ARGS}}"` }}
az:login:
cmds:
- az login
Expand Down
Original file line number Diff line number Diff line change
@@ -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" {
Expand Down
22 changes: 14 additions & 8 deletions examples/terraform-bootstrap/templates/terraform/state-storage.tf
Original file line number Diff line number Diff line change
@@ -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"
Expand Down

0 comments on commit 23cfcf0

Please sign in to comment.