From 191799ab976bd7dad7162d62bf86f2d3cc5d0dbd Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sat, 30 Jan 2021 14:04:37 +0100 Subject: Better Terraform resources. --- .../trygvis/rules/terraform/TerraformResource.java | 117 ++++++++++++++++++++- 1 file changed, 113 insertions(+), 4 deletions(-) (limited to 'module/ri-base/src/main/java/io/trygvis') diff --git a/module/ri-base/src/main/java/io/trygvis/rules/terraform/TerraformResource.java b/module/ri-base/src/main/java/io/trygvis/rules/terraform/TerraformResource.java index 41bd6ae..8ca6ae2 100644 --- a/module/ri-base/src/main/java/io/trygvis/rules/terraform/TerraformResource.java +++ b/module/ri-base/src/main/java/io/trygvis/rules/terraform/TerraformResource.java @@ -1,14 +1,16 @@ package io.trygvis.rules.terraform; -import java.util.HashMap; -import java.util.Map; +import com.fasterxml.jackson.annotation.JsonValue; +import java.util.*; + +@SuppressWarnings("unused") public class TerraformResource { private static final String EOL = System.getProperty("line.separator"); private final String output; private final String kind; private final String instance; - private final Map values = new HashMap<>(); + private final Map values = new LinkedHashMap<>(); public TerraformResource(String output, String kind, String instance) { this.output = output; @@ -20,10 +22,40 @@ public class TerraformResource { return output; } - public void putString(String key, String value) { + public String getKind() { + return kind; + } + + public String getInstance() { + return instance; + } + + public Map getValues() { + return values; + } + + public void set(String key, String value) { values.put(key, new StringTerraformValue(value)); } + public void set(String key, boolean value) { + values.put(key, new BooleanTerraformValue(value)); + } + + public void set(String key, int value) { + values.put(key, new IntegerTerraformValue(value)); + } + + public void setExpression(String key, String value) { + values.put(key, new ExpressionTerraformValue(value)); + } + + public ArrayTerraformValue array(String key) { + var array = new ArrayTerraformValue(); + values.put(key, array); + return array; + } + public String asString() { var buf = new StringBuilder(); @@ -58,6 +90,11 @@ public class TerraformResource { this.value = value; } + @JsonValue + public String getValue() { + return value; + } + @Override public String asString() { return quote(value); @@ -71,6 +108,29 @@ public class TerraformResource { this.value = value; } + @JsonValue + public boolean getValue() { + return value; + } + + @Override + public String asString() { + return String.valueOf(value); + } + } + + public static class IntegerTerraformValue implements TerraformValue { + private final int value; + + public IntegerTerraformValue(int value) { + this.value = value; + } + + @JsonValue + public int getValue() { + return value; + } + @Override public String asString() { return String.valueOf(value); @@ -84,12 +144,61 @@ public class TerraformResource { this.value = value; } + @JsonValue + public String getValue() { + return value; + } + @Override public String asString() { return value; } } + public static class ArrayTerraformValue implements TerraformValue { + private final List values = new ArrayList<>(); + + @JsonValue + public List getValues() { + return values; + } + + public ArrayTerraformValue add(String value) { + values.add(new StringTerraformValue(value)); + return this; + } + + public ArrayTerraformValue add(int value) { + values.add(new IntegerTerraformValue(value)); + return this; + } + + public ArrayTerraformValue add(boolean value) { + values.add(new BooleanTerraformValue(value)); + return this; + } + + public ArrayTerraformValue addExpression(String value) { + values.add(new ExpressionTerraformValue(value)); + return this; + } + + @Override + public String asString() { + if (values.isEmpty()) { + return "[]"; + } + + if (values.size() == 1) { + return "[" + values.get(0).asString() + "]"; + } + + var j = new StringJoiner(",\n ", "[\n", "\n ]"); + values.forEach(value -> j.add(value.asString())); + return j.toString(); + } + } + private static String quote(String value) { return "\"%s\"".formatted(value); } -- cgit v1.2.3