Skip to content

Commit

Permalink
🐛 : use module git directory
Browse files Browse the repository at this point in the history
  • Loading branch information
juwit committed Jun 17, 2019
1 parent f7c4fb3 commit 51815b4
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 17 deletions.
41 changes: 25 additions & 16 deletions src/main/java/io/codeka/gaia/runner/StackCommandBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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[]{}));
}

/**
Expand All @@ -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) {
Expand Down
81 changes: 80 additions & 1 deletion src/test/java/io/codeka/gaia/runner/StackCommandBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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;
}

}

0 comments on commit 51815b4

Please sign in to comment.