Skip to content

Commit

Permalink
fix(DMVP-2224): fix backslash issue in generated code and made variab…
Browse files Browse the repository at this point in the history
…les optional
  • Loading branch information
aramkarapetian committed Sep 4, 2023
1 parent 5d8ea12 commit 84a8da6
Show file tree
Hide file tree
Showing 15 changed files with 203 additions and 28 deletions.
55 changes: 55 additions & 0 deletions modules/workspace/playground/locals.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
locals {
module_path = path.module
file_contents = file("${path.module}/../../../tests/debug-escape-issue/example-infra/module-3.yaml")
yaml = yamldecode(local.file_contents)

variables = local.yaml.variables
modules = local.yaml.linked_workspaces
modules_string = join("|", local.modules)

# "${module-1.variable-1}" # => "${data.tfe_outputs.this["module-1"].values.variable-1}"
# module-1 => data.tfe_outputs.this["module-1"].values
# result: "${data.tfe_outputs.this[\"module-1\"].values.result.variable-1}"

modules_with_values = {
for value in local.modules : value => "data.tfe_outputs.this[\"${value}\"].values"
}

variables_encoded = {
for key, value in local.variables : key => jsonencode(value)
}

# variables_example = {
# "variable-1" = "\"${module-1.variable-1}\""
# "variable-2" = "\"something ${module-1.variable-1} and ${module-2.variable-1} else\""
# "variable-3" = "\"module 2 ${module-2.variable-1} else\""
# "variable-4" = "\"module 1 ${module-1.variable-1} else\""
# "variable-7" = "\"module 1 ${module-3.variable-1} else\""
# "variable-list-5" = "[\"module 2 ${module-2.variable-1} else\",\"module 1 ${module-1.variable-1} else\"]"
# "variable-object-6" = "{\"module 2 ${module-2.variable-1} else\":\"module 1 ${module-1.variable-1} else\"}"
# }
whatever = "/(module-1|module-2|module-3)/data.tfe_outputs.this[\"$1\"].values"
# modules_example = {
# "module-1" = "data.tfe_outputs.this[\"module-1\"].values"
# "module-2" = "data.tfe_outputs.this[\"module-2\"].values"
# }

variables_replaced = {
for key, value in local.variables_encoded : key => replace(value, "/(${local.modules_string})/", "data.tfe_outputs.this[\\\"$1\\\"].values")
# reduce(str, [for k, v in local.replacements : {search = k, replace = v}],
# function(acc, values) {
# replace(acc, values.search, values.replace)
# }
# )
}

variables_final = {
for key, value in local.variables_replaced : key => jsondecode(value)
}

json = jsondecode("\"a\\\"b\"")

contents = templatefile("template.tftpl", {
variables = local.variables_final
})
}
4 changes: 4 additions & 0 deletions modules/workspace/playground/template.tftpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
asdqwe

%{ for key, value in variables }
${key} = ${replace(jsonencode(value), "\\", "")}%{ endfor ~}
2 changes: 1 addition & 1 deletion modules/workspace/templates/main.tf.tftpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module "this" {
source = "${source}"
version = "${version}"
%{ for key, value in variables }
${key} = ${jsonencode(value)}%{ endfor ~}
${key} = ${replace(jsonencode(value), "\\", "")}%{ endfor ~}

%{ if module_nested_provider != null ~}
providers = ${jsonencode(module_nested_provider)}%{ endif ~}
Expand Down
8 changes: 4 additions & 4 deletions modules/workspace/templates/providers.tf.tftpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@
alias = "${provider_value.alias}"%{ endif ~}

%{ for key, value in provider_value.variables }
${key} = ${jsonencode(value)}%{ endfor ~}
${key} = ${replace(jsonencode(value), "\\", "")}%{ endfor ~}

%{ for key, value in provider_value.blocks }
${key} {
%{ for nested_key, nested_value in value }

%{ if nested_key != "blocks" ~}
${nested_key} = ${jsonencode(nested_value)}%{ endif ~}%{ endfor ~}
${nested_key} = ${replace(jsonencode(nested_value), "\\", "")}%{ endif ~}%{ endfor ~}

%{ if try(value.blocks,null) != null ~}

%{ for key1, value1 in value.blocks }
${key1} {
%{ for nested_key1, nested_value1 in value1 }
${nested_key1} = ${jsonencode(nested_value1)}
${nested_key1} = ${replace(jsonencode(nested_value1), "\\", "")}
%{ endfor ~}
}
%{ endfor ~}
Expand All @@ -30,7 +30,7 @@ ${key1} {
%{ for key, value in provider_value.custom_var_blocks }
${key} {
%{ for nested_key, nested_value in value }
${nested_key} = ${jsonencode(nested_value)}%{ endfor ~}
${nested_key} = ${replace(jsonencode(nested_value), "\\", "")}%{ endfor ~}

}%{ endfor ~}

Expand Down
4 changes: 2 additions & 2 deletions tests/basic/1-example.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module "basic" {
source = "../.."

org = "dasmeta-testing"
token = "< TFC token >"
token = "ojODA5TvvwpL1A.atlasv1.6ifl0D5Q3zaonS3GPc5aXSLo4HWxCScaXf3u0sSVy4Eb4I62HAcs75W9l4EO9iBkFyE"

rootdir = "_terraform"
targetdir = "_terraform"
Expand All @@ -11,7 +11,7 @@ module "basic" {
git_provider = "github"
git_org = "dasmeta-testing"
git_repo = "test-infrastructure"
git_token = "< github oauth token >"
git_token = "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"

aws = {
access_key_id = ""
Expand Down
21 changes: 14 additions & 7 deletions tests/basic/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
run:
mkdir _tfc
cd _tfc && \
terraform init \
-backend-config="organization=dasmeta-testing" \
-backend-config="workspaces.name=testing-infrastructure" \
-from-module="github.com/dasmeta/terraform-tfe-cloud.git?ref=DMVP-2598-simplify-tfe-use"
run-via-docker:
docker run -it \
-v $(pwd):/build \
-e GIT_TOKEN="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" \
-e TERRAFORM_CLOUD_TOKEN="ojODA5TvvwpL1A.atlasv1.6ifl0D5Q3zaonS3GPc5aXSLo4HWxCScaXf3u0sSVy4Eb4I62HAcs75W9l4EO9iBkFyE" \
-e AWS_ACCESS_KEY_ID="54567898656" \
-e AWS_SECRET_ACCESS_KEY="54567898656" \
-e AWS_DEFAULT_REGION="54567898656" \
dasmeta/meta create

export TF_VAR_git_token=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
export TF_VAR_tfc_token=ojODA5TvvwpL1A.atlasv1.6ifl0D5Q3zaonS3GPc5aXSLo4HWxCScaXf3u0sSVy4Eb4I62HAcs75W9l4EO9iBkFyE
export TFE_TOKEN=ojODA5TvvwpL1A.atlasv1.6ifl0D5Q3zaonS3GPc5aXSLo4HWxCScaXf3u0sSVy4Eb4I62HAcs75W9l4EO9iBkFyE
export TF_TOKEN_app_terraform_io=ojODA5TvvwpL1A.atlasv1.6ifl0D5Q3zaonS3GPc5aXSLo4HWxCScaXf3u0sSVy4Eb4I62HAcs75W9l4EO9iBkFyE
8 changes: 8 additions & 0 deletions tests/debug-escape-issue/0-setup.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
terraform {
cloud {
organization = "dasmeta-testing"
workspaces {
name = "terraform-tfe-cloud-test"
}
}
}
24 changes: 24 additions & 0 deletions tests/debug-escape-issue/1-example.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
variable "tfc_token" {}
variable "git_token" {}

module "basic" {
source = "../.."

org = "dasmeta-testing"
token = var.tfc_token

rootdir = "_terraform"
targetdir = "_terraform"
yamldir = "."

git_provider = "github"
git_org = "dasmeta-testing"
git_repo = "test-infrastructure"
git_token = var.git_token

aws = {
access_key_id = ""
secret_access_key = ""
default_region = ""
}
}
2 changes: 2 additions & 0 deletions tests/debug-escape-issue/example-infra/module-1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
source: dasmeta/null/empty
version: 1.2.1
2 changes: 2 additions & 0 deletions tests/debug-escape-issue/example-infra/module-2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
source: dasmeta/null/empty
version: 1.2.1
42 changes: 42 additions & 0 deletions tests/debug-escape-issue/example-infra/module-3.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
source: dasmeta/null/empty
version: 1.2.1
variables:
static: "Vahagn"
variable-1: "Vahagn ${module-1.variable-1}" # => "Vahagn ${data.tfe_outputs.this["module-1"].values.variable-1}"
# module-1 => data.tfe_outputs.this["module-1"].values
# result: "${data.tfe_outputs.this[\"module-1\"].values.result.variable-1}"
variable-2: "something ${module-1.variable-1} and ${module-2.variable-1} else"
variable-3: "module 2 ${module-2.variable-1} else"
variable-4: "module 1 ${module-1.variable-1} else"
variable-list-5: [
"module 2 ${module-2.variable-1} else",
"module 1 ${module-1.variable-1} else"
]
variable-object-6: {
"module 2 ${module-2.variable-1} else": "module 1 ${module-1.variable-1} else"
}
variable-7: "module 1 ${module-3.variable-1} else" # => "module 1 ${module-3.variable-1} else"
linked_workspaces:
- module-1
- module-2
- 1-environments/dev-1/eks
providers:
- name: kubernetes
version: ~> 2.23
module_nested_provider: true
source: "hashicorp/kubernetes"
variables:
cluster_ca_certificate: ${1-environments/dev-1/eks.cluster_certificate}
host: ${1-environments/dev-1/eks.cluster_host}
token: ${1-environments/dev-1/eks.cluster_token}
blocks:
exec:
api_version: "client.authentication.k8s.io/v1beta1"
args:
- "eks"
- "--region"
- "eu-central-1"
- "get-token"
- "--cluster-name"
- ${1-environments/dev-1/eks.cluster_name}
command: "aws"
8 changes: 8 additions & 0 deletions tests/kube-helm-provider/0-setup.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
terraform {
cloud {
organization = "dasmeta-testing"
workspaces {
name = "terraform-tfe-cloud-test"
}
}
}
24 changes: 24 additions & 0 deletions tests/kube-helm-provider/1-example.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
variable "tfc_token" {}
variable "git_token" {}

module "basic" {
source = "../.."

org = "dasmeta-testing"
token = var.tfc_token

rootdir = "_terraform"
targetdir = "_terraform"
yamldir = "."

git_provider = "github"
git_org = "dasmeta-testing"
git_repo = "test-infrastructure"
git_token = var.git_token

aws = {
access_key_id = ""
secret_access_key = ""
default_region = ""
}
}
25 changes: 12 additions & 13 deletions tests/kube-helm-provider/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ variables:
memory: null
requests:
cpu: null
memory: null
memory: null
readinessprobe_initialdelayseconds: 5
livenessprobe_initialdelayseconds: 30
architecture: "replicaset"
architecture: "replicaset"
providers:
- name: aws
version: ">= 4.0"
Expand Down Expand Up @@ -51,23 +51,22 @@ providers:
"get-token", "--cluster-name", local.cluster_name
]
command: "aws"

- name: helm
version: "2.10.1"
source: "hashicorp/helm"
blocks:
kubernetes:
kubernetes:
cluster_ca_certificate: ${2-product/dasmeta/eks-data.ca_certificate}
host: ${2-product/dasmeta/eks-data.host}
blocks:
exec:
api_version: "client.authentication.k8s.io/v1beta1"
args: [
"eks",
"--region", var.region,
"get-token", "--cluster-name", local.cluster_name
]
command: "aws"
api_version: "client.authentication.k8s.io/v1beta1"
args: [
"eks",
"--region", var.region,
"get-token", "--cluster-name", local.cluster_name
]
command: "aws"
linked_workspaces:
- 2-product/dasmeta/eks-data
- 2-product/dasmeta/secret-reader-infra
- 2-product/dasmeta/eks-data
- 2-product/dasmeta/secret-reader-infra
2 changes: 1 addition & 1 deletion workspaces.tf
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module "workspaces" {
name = each.key
module_source = each.value.source
module_version = each.value.version
module_vars = each.value.variables
module_vars = try(each.value.variables, [])
output = try(each.value.output, null)
target_dir = var.targetdir

Expand Down

0 comments on commit 84a8da6

Please sign in to comment.