diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2021-01-30 14:04:37 +0100 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2021-01-30 14:04:37 +0100 |
commit | 191799ab976bd7dad7162d62bf86f2d3cc5d0dbd (patch) | |
tree | 1cb264828b8d8f05ee67c57650bf05aff381d2fb /module/ri-base/src/main/java | |
parent | bb92e6eaa9bca36d9f1e356a2f578d22c98af22f (diff) | |
download | rules-sandbox-191799ab976bd7dad7162d62bf86f2d3cc5d0dbd.tar.gz rules-sandbox-191799ab976bd7dad7162d62bf86f2d3cc5d0dbd.tar.bz2 rules-sandbox-191799ab976bd7dad7162d62bf86f2d3cc5d0dbd.tar.xz rules-sandbox-191799ab976bd7dad7162d62bf86f2d3cc5d0dbd.zip |
Better Terraform resources.
Diffstat (limited to 'module/ri-base/src/main/java')
-rw-r--r-- | module/ri-base/src/main/java/io/trygvis/rules/terraform/TerraformResource.java | 117 |
1 files changed, 113 insertions, 4 deletions
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<String, TerraformValue> values = new HashMap<>(); + private final Map<String, TerraformValue> 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<String, TerraformValue> 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<TerraformValue> values = new ArrayList<>(); + + @JsonValue + public List<TerraformValue> 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); } |