diff --git a/src/main/java/io/codeka/gaia/runner/StackCommandBuilder.java b/src/main/java/io/codeka/gaia/runner/StackCommandBuilder.java index 9262fe9d9..6fa815684 100644 --- a/src/main/java/io/codeka/gaia/runner/StackCommandBuilder.java +++ b/src/main/java/io/codeka/gaia/runner/StackCommandBuilder.java @@ -7,6 +7,9 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; +import java.util.LinkedList; +import java.util.List; + /** * A builder class to create stack commands */ @@ -35,21 +38,24 @@ String buildApplyScript(Stack stack, TerraformModule module){ System.out.println(applyCommand); - var commands = new String[]{ + var commands = new LinkedList<>(List.of( "set -ex", String.format("git clone %s module", module.getGitRepositoryUrl()), - "cd module", + "cd module")); + + if(module.getDirectory() != null && !module.getDirectory().isBlank()) { + commands.add(String.format("cd %s", module.getDirectory())); + } + + commands.addAll(List.of( "echo 'generating backend configuration'", backendGenCommand, "cat backend.tf", "terraform version", "terraform init", - applyCommand - }; - - // generate a backend.tf config ! + applyCommand)); - return String.join("\n", commands); + return String.join("\n", commands.toArray(new String[]{})); } /** @@ -63,25 +69,28 @@ String buildPlanScript(Stack stack, TerraformModule module){ "\t}\n" + "}\n\" > backend.tf", stack.getId()); - String applyCommand = buildPlanCommand(stack, module); + String planCommand = buildPlanCommand(stack, module); - System.out.println(applyCommand); + System.out.println(planCommand); - var commands = new String[]{ + var commands = new LinkedList<>(List.of( "set -ex", String.format("git clone %s module", module.getGitRepositoryUrl()), - "cd module", + "cd module")); + + if(module.getDirectory() != null && !module.getDirectory().isBlank()) { + commands.add(String.format("cd %s", module.getDirectory())); + } + + commands.addAll(List.of( "echo 'generating backend configuration'", backendGenCommand, "cat backend.tf", "terraform version", "terraform init", - applyCommand - }; - - // generate a backend.tf config ! + planCommand)); - return String.join("\n", commands); + return String.join("\n", commands.toArray(new String[]{})); } String buildApplyCommand(Stack stack, TerraformModule module) { diff --git a/src/test/java/io/codeka/gaia/runner/StackCommandBuilderTest.java b/src/test/java/io/codeka/gaia/runner/StackCommandBuilderTest.java index 8f328e370..8195dfdcf 100644 --- a/src/test/java/io/codeka/gaia/runner/StackCommandBuilderTest.java +++ b/src/test/java/io/codeka/gaia/runner/StackCommandBuilderTest.java @@ -4,13 +4,15 @@ import io.codeka.gaia.bo.Stack; import io.codeka.gaia.bo.TerraformModule; import io.codeka.gaia.bo.TerraformVariable; +import org.jetbrains.annotations.NotNull; import org.junit.jupiter.api.Test; import java.util.Collections; import java.util.List; import java.util.Map; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; class StackCommandBuilderTest { @@ -82,4 +84,81 @@ void buildApplyCommand_shouldUseDefaultVariableValues(){ assertEquals("terraform apply --auto-approve -var \"test=defaultValue\" ", applyCommand); } + @Test + void buildApplyScript_shouldGenerateAFullScript(){ + var stackCommandBuilder = new StackCommandBuilder(new Settings()); + + TerraformModule module = moduleWithDirectory(); + + var stack = new Stack(); + var script = stackCommandBuilder.buildApplyScript(stack, module); + + assertTrue(script.contains("git clone git://test module\ncd module\ncd directory\necho 'generating backend configuration'")); + assertTrue(script.contains("terraform version\nterraform init\nterraform apply")); + } + + @Test + void buildApplyScript_shouldGenerateAFullScript_forAModuleWithoutDirectory(){ + var stackCommandBuilder = new StackCommandBuilder(new Settings()); + + TerraformModule module = moduleWithoutDirectory(); + + var stack = new Stack(); + var script = stackCommandBuilder.buildApplyScript(stack, module); + + assertTrue(script.contains("git clone git://test module\ncd module\necho 'generating backend configuration'")); + assertTrue(script.contains("terraform version\nterraform init\nterraform apply")); + } + + @Test + void buildPlanScript_shouldGenerateAFullScript(){ + var stackCommandBuilder = new StackCommandBuilder(new Settings()); + + TerraformModule module = moduleWithDirectory(); + + var stack = new Stack(); + var script = stackCommandBuilder.buildPlanScript(stack, module); + + assertTrue(script.contains("git clone git://test module\ncd module\ncd directory\necho 'generating backend configuration'")); + assertTrue(script.contains("terraform version\nterraform init\nterraform plan")); + } + + @Test + void buildPlanScript_shouldGenerateAFullScript_forAModuleWithoutDirectory(){ + var stackCommandBuilder = new StackCommandBuilder(new Settings()); + + TerraformModule module = moduleWithoutDirectory(); + + var stack = new Stack(); + var script = stackCommandBuilder.buildPlanScript(stack, module); + + assertTrue(script.contains("git clone git://test module\ncd module\necho 'generating backend configuration'")); + assertTrue(script.contains("terraform version\nterraform init\nterraform plan")); + } + + @NotNull + private TerraformModule moduleWithDirectory() { + var module = new TerraformModule(); + module.setGitRepositoryUrl("git://test"); + module.setDirectory("directory"); + + var variable = new TerraformVariable(); + variable.setName("test"); + variable.setDefaultValue("defaultValue"); + module.setVariables(List.of(variable)); + return module; + } + + @NotNull + private TerraformModule moduleWithoutDirectory() { + var module = new TerraformModule(); + module.setGitRepositoryUrl("git://test"); + + var variable = new TerraformVariable(); + variable.setName("test"); + variable.setDefaultValue("defaultValue"); + module.setVariables(List.of(variable)); + return module; + } + } \ No newline at end of file