Skip to content

Commit

Permalink
♻️ : convert TerraformVariable.java to Variable.kt
Browse files Browse the repository at this point in the history
  • Loading branch information
juwit committed Nov 12, 2019
1 parent f077694 commit 27a83c8
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 149 deletions.
8 changes: 4 additions & 4 deletions src/main/java/io/codeka/gaia/hcl/HclTreeListener.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ package io.codeka.gaia.hcl

import io.codeka.gaia.hcl.antlr.hclBaseListener
import io.codeka.gaia.hcl.antlr.hclParser
import io.codeka.gaia.modules.bo.Variable
import java.util.*

class HclTreeListener : hclBaseListener() {

var variables: MutableList<Variable> = LinkedList()
var outputs: MutableList<Output> = LinkedList()

private var currentVariable: Variable = Variable()
private var currentVariable: Variable = Variable("")
private var currentOutput:Output = Output()

override fun enterVariableDirective(ctx: hclParser.VariableDirectiveContext) {
this.currentVariable = Variable()
this.currentVariable.name = ctx.identifier().text
this.currentVariable = Variable(name = ctx.identifier().text)
}

override fun exitVariableDirective(ctx: hclParser.VariableDirectiveContext) {
Expand All @@ -30,7 +30,7 @@ class HclTreeListener : hclBaseListener() {
}

override fun enterVariableDefault(ctx: hclParser.VariableDefaultContext) {
this.currentVariable.default = ctx.expression().text
this.currentVariable.defaultValue = ctx.expression().text
}

override fun enterOutputDirective(ctx: hclParser.OutputDirectiveContext) {
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/io/codeka/gaia/hcl/HclTypes.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
package io.codeka.gaia.hcl

data class Variable(var name: String = "", var type: String = "", var description: String = "", var default: String = "");

data class Output(var name: String = "", var value: String = "", var description: String = "", var sensitive: String = "false");

6 changes: 3 additions & 3 deletions src/main/java/io/codeka/gaia/modules/bo/TerraformModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class TerraformModule {
private String directory;

@Valid
private List<TerraformVariable> variables = new ArrayList<>();
private List<Variable> variables = new ArrayList<>();

@NotBlank
private String name;
Expand Down Expand Up @@ -93,11 +93,11 @@ public String getDescription() {
return description;
}

public List<TerraformVariable> getVariables() {
public List<Variable> getVariables() {
return variables;
}

public void setVariables(List<TerraformVariable> variables) {
public void setVariables(List<Variable> variables) {
this.variables = variables;
}

Expand Down
71 changes: 0 additions & 71 deletions src/main/java/io/codeka/gaia/modules/bo/TerraformVariable.java

This file was deleted.

19 changes: 19 additions & 0 deletions src/main/java/io/codeka/gaia/modules/bo/Variable.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.codeka.gaia.modules.bo

import javax.validation.constraints.NotBlank

/**
* Represents a module variable.
* We use `@JvmOverloads` annotation to tell the Kotlin compiler to generate constructors that use default values.
* Also, the `@NotBlank` annotation is targeted to the field, and not the constructor param.
*
* @see https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.jvm/-jvm-overloads/index.html
* @see https://kotlinlang.org/docs/reference/annotations.html#annotation-use-site-targets
*/
data class Variable @JvmOverloads constructor(@field:NotBlank val name: String,
var type: String? = null,
var description: String? = null,
var defaultValue: String? = null,
var editable: Boolean = false,
var mandatory: Boolean = false,
var validationRegex: String? = null)
2 changes: 0 additions & 2 deletions src/main/java/io/codeka/gaia/modules/bo/bo.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class MandatoryStackVariablesValidator(@Autowired val moduleRepository: Terrafor
// this should (let's hope so) never happen ! another validator should handle this case
val module = this.moduleRepository.findByIdOrNull(stack.moduleId) ?: return true

return module.variables.filter { it.isMandatory }
return module.variables.filter { it.mandatory }
.filter { it.defaultValue.isNullOrBlank() }
.all { ! stack.variableValues[it.name].isNullOrBlank() }
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.codeka.gaia.stacks.service

import io.codeka.gaia.modules.bo.TerraformVariable
import io.codeka.gaia.modules.bo.Variable
import io.codeka.gaia.modules.repository.TerraformModuleRepository
import io.codeka.gaia.stacks.bo.RegexStackVariablesValidation
import io.codeka.gaia.stacks.bo.Stack
Expand Down Expand Up @@ -34,7 +34,7 @@ class RegexStackVariablesValidator(@Autowired val moduleRepository: TerraformMod
}
}

private fun TerraformVariable.validateWithPattern(value: String): Boolean {
private fun Variable.validateWithPattern(value: String): Boolean {
val pattern = Pattern.compile(this.validationRegex)
return pattern.matcher(value).matches()
}
16 changes: 13 additions & 3 deletions src/test/java/io/codeka/gaia/hcl/HCLParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.codeka.gaia.hcl.antlr.hclLexer;
import io.codeka.gaia.hcl.antlr.hclParser;
import io.codeka.gaia.modules.bo.Variable;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
Expand Down Expand Up @@ -37,9 +38,18 @@ void parsing_variables_shouldWork() throws IOException {
// then
assertThat(hclTreeListener.getVariables()).hasSize(3);

Variable stringVar = new Variable("\"string_var\"", "\"string\"", "\"a test string var\"", "\"foo\"");
Variable numberVar = new Variable("\"number_var\"", "number", "\"a test number var\"", "42");
Variable boolVar = new Variable("\"bool_var\"", "", "", "false");
var stringVar = new Variable("\"string_var\"");
stringVar.setType("\"string\"");
stringVar.setDescription("\"a test string var\"");
stringVar.setDefaultValue("\"foo\"");

var numberVar = new Variable("\"number_var\"");
numberVar.setType("number");
numberVar.setDescription("\"a test number var\"");
numberVar.setDefaultValue("42");

var boolVar = new Variable("\"bool_var\"");
boolVar.setDefaultValue("false");

assertThat(hclTreeListener.getVariables()).contains(stringVar, numberVar, boolVar);
}
Expand Down
59 changes: 20 additions & 39 deletions src/test/java/io/codeka/gaia/runner/StackCommandBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.github.mustachejava.DefaultMustacheFactory;
import io.codeka.gaia.modules.bo.TerraformModule;
import io.codeka.gaia.modules.bo.TerraformVariable;
import io.codeka.gaia.modules.bo.Variable;
import io.codeka.gaia.registries.RegistryOAuth2Provider;
import io.codeka.gaia.settings.bo.Settings;
import io.codeka.gaia.stacks.bo.Stack;
Expand Down Expand Up @@ -52,8 +52,7 @@ void buildPlanCommand_shouldGenerateASimpleApplyCommand() {
@Test
void buildPlanCommand_shouldGenerateASingleVariableApplyCommand() {
var module = new TerraformModule();
var variable = new TerraformVariable();
variable.setName("test");
var variable = new Variable("test");
module.setVariables(List.of(variable));

var stack = new Stack();
Expand All @@ -67,10 +66,8 @@ void buildPlanCommand_shouldGenerateASingleVariableApplyCommand() {
@Test
void buildPlanCommand_shouldGenerateAMultipleVariableApplyCommand() {
var module = new TerraformModule();
var variable = new TerraformVariable();
variable.setName("test");
var variable2 = new TerraformVariable();
variable2.setName("test2");
var variable = new Variable("test");
var variable2 = new Variable("test2");
module.setVariables(List.of(variable, variable2));

var stack = new Stack();
Expand All @@ -84,8 +81,7 @@ void buildPlanCommand_shouldGenerateAMultipleVariableApplyCommand() {
@Test
void buildPlanCommand_shouldUseDefaultVariableValues() {
var module = new TerraformModule();
var variable = new TerraformVariable();
variable.setName("test");
var variable = new Variable("test");
variable.setDefaultValue("defaultValue");
module.setVariables(List.of(variable));

Expand Down Expand Up @@ -165,8 +161,7 @@ void buildApplyCommand_shouldGenerateASimpleApplyCommand() {
@Test
void buildApplyCommand_shouldGenerateASingleVariableApplyCommand() {
var module = new TerraformModule();
var variable = new TerraformVariable();
variable.setName("test");
var variable = new Variable("test");
module.setVariables(List.of(variable));

var stack = new Stack();
Expand All @@ -180,10 +175,8 @@ void buildApplyCommand_shouldGenerateASingleVariableApplyCommand() {
@Test
void buildApplyCommand_shouldGenerateAMultipleVariableApplyCommand() {
var module = new TerraformModule();
var variable = new TerraformVariable();
variable.setName("test");
var variable2 = new TerraformVariable();
variable2.setName("test2");
var variable = new Variable("test");
var variable2 = new Variable("test2");
module.setVariables(List.of(variable, variable2));

var stack = new Stack();
Expand All @@ -197,8 +190,7 @@ void buildApplyCommand_shouldGenerateAMultipleVariableApplyCommand() {
@Test
void buildApplyCommand_shouldUseDefaultVariableValues() {
var module = new TerraformModule();
var variable = new TerraformVariable();
variable.setName("test");
var variable = new Variable("test");
variable.setDefaultValue("defaultValue");
module.setVariables(List.of(variable));

Expand Down Expand Up @@ -278,8 +270,7 @@ void buildPlanDestroyCommand_shouldGenerateASimpleApplyCommand() {
@Test
void buildPlanDestroyCommand_shouldGenerateASingleVariableApplyCommand() {
var module = new TerraformModule();
var variable = new TerraformVariable();
variable.setName("test");
var variable = new Variable("test");
module.setVariables(List.of(variable));

var stack = new Stack();
Expand All @@ -293,10 +284,8 @@ void buildPlanDestroyCommand_shouldGenerateASingleVariableApplyCommand() {
@Test
void buildPlanDestroyCommand_shouldGenerateAMultipleVariableApplyCommand() {
var module = new TerraformModule();
var variable = new TerraformVariable();
variable.setName("test");
var variable2 = new TerraformVariable();
variable2.setName("test2");
var variable = new Variable("test");
var variable2 = new Variable("test2");
module.setVariables(List.of(variable, variable2));

var stack = new Stack();
Expand All @@ -310,8 +299,7 @@ void buildPlanDestroyCommand_shouldGenerateAMultipleVariableApplyCommand() {
@Test
void buildPlanDestroyCommand_shouldUseDefaultVariableValues() {
var module = new TerraformModule();
var variable = new TerraformVariable();
variable.setName("test");
var variable = new Variable("test");
variable.setDefaultValue("defaultValue");
module.setVariables(List.of(variable));

Expand Down Expand Up @@ -391,8 +379,7 @@ void buildDestroyCommand_shouldGenerateASimpleApplyCommand() {
@Test
void buildDestroyCommand_shouldGenerateASingleVariableApplyCommand() {
var module = new TerraformModule();
var variable = new TerraformVariable();
variable.setName("test");
var variable = new Variable("test");
module.setVariables(List.of(variable));

var stack = new Stack();
Expand All @@ -406,10 +393,8 @@ void buildDestroyCommand_shouldGenerateASingleVariableApplyCommand() {
@Test
void buildDestroyCommand_shouldGenerateAMultipleVariableApplyCommand() {
var module = new TerraformModule();
var variable = new TerraformVariable();
variable.setName("test");
var variable2 = new TerraformVariable();
variable2.setName("test2");
var variable = new Variable("test");
var variable2 = new Variable("test2");
module.setVariables(List.of(variable, variable2));

var stack = new Stack();
Expand All @@ -423,8 +408,7 @@ void buildDestroyCommand_shouldGenerateAMultipleVariableApplyCommand() {
@Test
void buildDestroyCommand_shouldUseDefaultVariableValues() {
var module = new TerraformModule();
var variable = new TerraformVariable();
variable.setName("test");
var variable = new Variable("test");
variable.setDefaultValue("defaultValue");
module.setVariables(List.of(variable));

Expand Down Expand Up @@ -495,8 +479,7 @@ private TerraformModule moduleWithDirectory() {
module.setGitRepositoryUrl("git://test");
module.setDirectory("directory");

var variable = new TerraformVariable();
variable.setName("test");
var variable = new Variable("test");
variable.setDefaultValue("defaultValue");
module.setVariables(List.of(variable));

Expand All @@ -510,8 +493,7 @@ private TerraformModule moduleWithoutDirectory() {
var module = new TerraformModule();
module.setGitRepositoryUrl("git://test");

var variable = new TerraformVariable();
variable.setName("test");
var variable = new Variable("test");
variable.setDefaultValue("defaultValue");
module.setVariables(List.of(variable));

Expand All @@ -525,8 +507,7 @@ private TerraformModule moduleWithAccessToken() {
var module = new TerraformModule();
module.setGitRepositoryUrl("git://test");

var variable = new TerraformVariable();
variable.setName("test");
var variable = new Variable("test");
variable.setDefaultValue("defaultValue");
module.setVariables(List.of(variable));

Expand Down

0 comments on commit 27a83c8

Please sign in to comment.