From aa9dcf7daf166634e887abb6596f68e2597e86f2 Mon Sep 17 00:00:00 2001 From: Julien WITTOUCK Date: Fri, 19 Mar 2021 16:47:59 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20:=20generate=20tfvar=20files=20for?= =?UTF-8?q?=20complex=20variable=20types?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/gaia_app/stacks/bo/Stack.java | 13 +++++- .../java/io/gaia_app/stacks/bo/StackTest.kt | 44 +++++++++++++++++++ 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/gaia_app/stacks/bo/Stack.java b/src/main/java/io/gaia_app/stacks/bo/Stack.java index 6e4988a6f..21b58cbf9 100644 --- a/src/main/java/io/gaia_app/stacks/bo/Stack.java +++ b/src/main/java/io/gaia_app/stacks/bo/Stack.java @@ -188,7 +188,10 @@ public void setCredentialsId(String credentialsId) { * @return a string with the content of the tfvars file */ public String tfvars() { - var varLine = "%s = \"%s\"\n"; + // strings are quoted, other variable types are not + var stringVariableLine = "%s = \"%s\"\n"; + var variableLine = "%s = %s\n"; + var variablesBuilder = new StringBuilder(); module.getVariables().forEach(terraformVariable -> { @@ -198,7 +201,13 @@ public String tfvars() { if (this.variableValues.containsKey(variableName)) { variableValue = this.variableValues.get(variableName); } - variablesBuilder.append(String.format(varLine, variableName, variableValue)); + + if( "string".equals(terraformVariable.getType())){ + variablesBuilder.append(String.format(stringVariableLine, variableName, variableValue)); + } + else { + variablesBuilder.append(String.format(variableLine, variableName, variableValue)); + } }); return variablesBuilder.toString(); } diff --git a/src/test/java/io/gaia_app/stacks/bo/StackTest.kt b/src/test/java/io/gaia_app/stacks/bo/StackTest.kt index fddeefb84..f6b16961a 100644 --- a/src/test/java/io/gaia_app/stacks/bo/StackTest.kt +++ b/src/test/java/io/gaia_app/stacks/bo/StackTest.kt @@ -28,4 +28,48 @@ internal class StackTest { .contains("variableWithDefault = \"myValue\"\n") .contains("mandatoryVariable = \"myOtherValue\"\n") } + + @Test + fun tfvars_shouldGenerateTfvarContents_forComplexTypes() { + // given + val listOfNumbers = Variable("listOfNumbers", "list(number)", "a list variable") + + val module = TerraformModule() + module.variables = listOf(listOfNumbers) + + val stack = Stack() + stack.module = module + stack.variableValues = mapOf("listOfNumbers" to "[1,2]") + + // then + Assertions.assertThat(stack.tfvars()) + .contains("listOfNumbers = [1,2]\n") + } + + @Test + fun tfvars_shouldGenerateTfvarContents_forComplexTypesOnMultilineStrings() { + // given + val mapVariable = Variable("mapVariable", "map", "a map variable") + + val module = TerraformModule() + module.variables = listOf(mapVariable) + + val stack = Stack() + stack.module = module + stack.variableValues = mapOf("mapVariable" to """ + { + name = "Anakin" + age = 45 + } + """.trimIndent()) + + // then + Assertions.assertThat(stack.tfvars()) + .contains(""" + mapVariable = { + name = "Anakin" + age = 45 + } + """.trimIndent()) + } }