-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
extract the generation of the main terraform command extract the generation of the whole runner script
- Loading branch information
Showing
3 changed files
with
163 additions
and
44 deletions.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
src/main/java/io/codeka/gaia/runner/StackCommandBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package io.codeka.gaia.runner; | ||
|
||
import io.codeka.gaia.bo.Settings; | ||
import io.codeka.gaia.bo.Stack; | ||
import io.codeka.gaia.bo.TerraformModule; | ||
import io.codeka.gaia.bo.backend.Backend; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* A builder class to create stack commands | ||
*/ | ||
@Component | ||
public class StackCommandBuilder { | ||
|
||
private Settings settings; | ||
|
||
@Autowired | ||
StackCommandBuilder(Settings settings) { | ||
this.settings = settings; | ||
} | ||
|
||
/** | ||
* | ||
* @return | ||
*/ | ||
String buildApplyScript(Stack stack, TerraformModule module){ | ||
var backendGenCommand = String.format("echo \"terraform {\n" + | ||
" backend \\\"http\\\" {\n" + | ||
"\t\taddress=\\\""+settings.getExternalUrl()+"/api/state/%s\\\"\n" + | ||
"\t}\n" + | ||
"}\n\" > backend.tf", stack.getId()); | ||
|
||
String applyCommand = buildApplyCommand(stack, module); | ||
|
||
System.out.println(applyCommand); | ||
|
||
var commands = new String[]{ | ||
"set -ex", | ||
String.format("git clone %s module", module.getGitRepositoryUrl()), | ||
"cd module", | ||
"echo 'generating backend configuration'", | ||
backendGenCommand, | ||
"cat backend.tf", | ||
"terraform version", | ||
"terraform init", | ||
applyCommand | ||
}; | ||
|
||
// generate a backend.tf config ! | ||
|
||
return String.join("\n", commands); | ||
} | ||
|
||
String buildApplyCommand(Stack stack, TerraformModule module) { | ||
var varFormatString = "-var \"%s=%s\" "; | ||
var variablesBuilder = new StringBuilder(); | ||
|
||
module.getVariables().forEach(terraformVariable -> { | ||
|
||
var name = terraformVariable.getName(); | ||
String value = terraformVariable.getDefaultValue(); | ||
// try getting the value from the stack | ||
if(stack.getVariableValues().containsKey(name)){ | ||
value = stack.getVariableValues().get(name); | ||
} | ||
variablesBuilder.append(String.format(varFormatString, name, value)); | ||
}); | ||
|
||
return String.format("terraform apply --auto-approve %s", variablesBuilder.toString()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
src/test/java/io/codeka/gaia/runner/StackCommandBuilderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package io.codeka.gaia.runner; | ||
|
||
import io.codeka.gaia.bo.Settings; | ||
import io.codeka.gaia.bo.Stack; | ||
import io.codeka.gaia.bo.TerraformModule; | ||
import io.codeka.gaia.bo.TerraformVariable; | ||
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.*; | ||
|
||
class StackCommandBuilderTest { | ||
|
||
@Test | ||
void buildApplyCommand_shouldGenerateASimpleApplyCommand(){ | ||
var stackCommandBuilder = new StackCommandBuilder(new Settings()); | ||
|
||
var module = new TerraformModule(); | ||
module.setVariables(Collections.emptyList()); | ||
|
||
var stack = new Stack(); | ||
|
||
var applyCommand = stackCommandBuilder.buildApplyCommand(stack, module); | ||
|
||
assertEquals("terraform apply --auto-approve ", applyCommand); | ||
} | ||
|
||
@Test | ||
void buildApplyCommand_shouldGenerateASingleVariableApplyCommand(){ | ||
var stackCommandBuilder = new StackCommandBuilder(new Settings()); | ||
|
||
var module = new TerraformModule(); | ||
var variable = new TerraformVariable(); | ||
variable.setName("test"); | ||
module.setVariables(List.of(variable)); | ||
|
||
var stack = new Stack(); | ||
stack.setVariableValues(Map.of("test", "value")); | ||
|
||
var applyCommand = stackCommandBuilder.buildApplyCommand(stack, module); | ||
|
||
assertEquals("terraform apply --auto-approve -var \"test=value\" ", applyCommand); | ||
} | ||
|
||
@Test | ||
void buildApplyCommand_shouldGenerateAMultipleVariableApplyCommand(){ | ||
var stackCommandBuilder = new StackCommandBuilder(new Settings()); | ||
|
||
var module = new TerraformModule(); | ||
var variable = new TerraformVariable(); | ||
variable.setName("test"); | ||
var variable2 = new TerraformVariable(); | ||
variable2.setName("test2"); | ||
module.setVariables(List.of(variable, variable2)); | ||
|
||
var stack = new Stack(); | ||
stack.setVariableValues(Map.of("test", "value", "test2", "value2")); | ||
|
||
var applyCommand = stackCommandBuilder.buildApplyCommand(stack, module); | ||
|
||
assertEquals("terraform apply --auto-approve -var \"test=value\" -var \"test2=value2\" ", applyCommand); | ||
} | ||
|
||
@Test | ||
void buildApplyCommand_shouldUseDefaultVariableValues(){ | ||
var stackCommandBuilder = new StackCommandBuilder(new Settings()); | ||
|
||
var module = new TerraformModule(); | ||
var variable = new TerraformVariable(); | ||
variable.setName("test"); | ||
variable.setDefaultValue("defaultValue"); | ||
module.setVariables(List.of(variable)); | ||
|
||
var stack = new Stack(); | ||
stack.setVariableValues(Collections.emptyMap()); | ||
|
||
var applyCommand = stackCommandBuilder.buildApplyCommand(stack, module); | ||
|
||
assertEquals("terraform apply --auto-approve -var \"test=defaultValue\" ", applyCommand); | ||
} | ||
|
||
} |