diff --git a/modules/workspace/playground/locals.tf b/modules/workspace/playground/locals.tf new file mode 100644 index 0000000..38a84d9 --- /dev/null +++ b/modules/workspace/playground/locals.tf @@ -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 + }) +} diff --git a/modules/workspace/playground/template.tftpl b/modules/workspace/playground/template.tftpl new file mode 100644 index 0000000..9b696ae --- /dev/null +++ b/modules/workspace/playground/template.tftpl @@ -0,0 +1,4 @@ +asdqwe + +%{ for key, value in variables } + ${key} = ${replace(jsonencode(value), "\\", "")}%{ endfor ~} diff --git a/modules/workspace/templates/main.tf.tftpl b/modules/workspace/templates/main.tf.tftpl index ad70a0d..6155497 100644 --- a/modules/workspace/templates/main.tf.tftpl +++ b/modules/workspace/templates/main.tf.tftpl @@ -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 ~} diff --git a/modules/workspace/templates/providers.tf.tftpl b/modules/workspace/templates/providers.tf.tftpl index b5d5eb3..942971a 100644 --- a/modules/workspace/templates/providers.tf.tftpl +++ b/modules/workspace/templates/providers.tf.tftpl @@ -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 ~} @@ -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 ~} diff --git a/tests/basic/1-example.tf b/tests/basic/1-example.tf index 2ab9f59..c4b260d 100644 --- a/tests/basic/1-example.tf +++ b/tests/basic/1-example.tf @@ -2,7 +2,7 @@ module "basic" { source = "../.." org = "dasmeta-testing" - token = "< TFC token >" + token = "ojODA5TvvwpL1A.atlasv1.6ifl0D5Q3zaonS3GPc5aXSLo4HWxCScaXf3u0sSVy4Eb4I62HAcs75W9l4EO9iBkFyE" rootdir = "_terraform" targetdir = "_terraform" @@ -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 = "" diff --git a/tests/basic/Makefile b/tests/basic/Makefile index de8c1ad..ff7aec4 100644 --- a/tests/basic/Makefile +++ b/tests/basic/Makefile @@ -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 diff --git a/tests/debug-escape-issue/0-setup.tf b/tests/debug-escape-issue/0-setup.tf new file mode 100644 index 0000000..49f74c6 --- /dev/null +++ b/tests/debug-escape-issue/0-setup.tf @@ -0,0 +1,8 @@ +terraform { + cloud { + organization = "dasmeta-testing" + workspaces { + name = "terraform-tfe-cloud-test" + } + } +} diff --git a/tests/debug-escape-issue/1-example.tf b/tests/debug-escape-issue/1-example.tf new file mode 100644 index 0000000..7234f38 --- /dev/null +++ b/tests/debug-escape-issue/1-example.tf @@ -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 = "" + } +} diff --git a/tests/debug-escape-issue/example-infra/module-1.yaml b/tests/debug-escape-issue/example-infra/module-1.yaml new file mode 100644 index 0000000..310a2d7 --- /dev/null +++ b/tests/debug-escape-issue/example-infra/module-1.yaml @@ -0,0 +1,2 @@ +source: dasmeta/null/empty +version: 1.2.1 diff --git a/tests/debug-escape-issue/example-infra/module-2.yaml b/tests/debug-escape-issue/example-infra/module-2.yaml new file mode 100644 index 0000000..310a2d7 --- /dev/null +++ b/tests/debug-escape-issue/example-infra/module-2.yaml @@ -0,0 +1,2 @@ +source: dasmeta/null/empty +version: 1.2.1 diff --git a/tests/debug-escape-issue/example-infra/module-3.yaml b/tests/debug-escape-issue/example-infra/module-3.yaml new file mode 100644 index 0000000..ac32ff7 --- /dev/null +++ b/tests/debug-escape-issue/example-infra/module-3.yaml @@ -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" diff --git a/tests/kube-helm-provider/0-setup.tf b/tests/kube-helm-provider/0-setup.tf new file mode 100644 index 0000000..49f74c6 --- /dev/null +++ b/tests/kube-helm-provider/0-setup.tf @@ -0,0 +1,8 @@ +terraform { + cloud { + organization = "dasmeta-testing" + workspaces { + name = "terraform-tfe-cloud-test" + } + } +} diff --git a/tests/kube-helm-provider/1-example.tf b/tests/kube-helm-provider/1-example.tf new file mode 100644 index 0000000..7234f38 --- /dev/null +++ b/tests/kube-helm-provider/1-example.tf @@ -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 = "" + } +} diff --git a/tests/kube-helm-provider/main.yaml b/tests/kube-helm-provider/main.yaml index aa419bf..914d485 100644 --- a/tests/kube-helm-provider/main.yaml +++ b/tests/kube-helm-provider/main.yaml @@ -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" @@ -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 diff --git a/workspaces.tf b/workspaces.tf index e1dc242..cbff30e 100644 --- a/workspaces.tf +++ b/workspaces.tf @@ -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