summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2021-01-31 12:05:03 +0100
committerTrygve Laugstøl <trygvis@inamo.no>2021-01-31 12:05:03 +0100
commit59ef07a9f583f0f0f64055a47938d57516b43541 (patch)
treedccbf769d14a101d281359ebc9208fe774e74ed3
parent191799ab976bd7dad7162d62bf86f2d3cc5d0dbd (diff)
downloadrules-sandbox-59ef07a9f583f0f0f64055a47938d57516b43541.tar.gz
rules-sandbox-59ef07a9f583f0f0f64055a47938d57516b43541.tar.bz2
rules-sandbox-59ef07a9f583f0f0f64055a47938d57516b43541.tar.xz
rules-sandbox-59ef07a9f583f0f0f64055a47938d57516b43541.zip
Better Terraform support.
-rw-r--r--module/ri-base/src/main/java/io/trygvis/rules/terraform/TerraformInputVariable.java41
-rw-r--r--module/ri-base/src/main/java/io/trygvis/rules/terraform/TerraformMain.java21
-rw-r--r--module/ri-base/src/main/java/io/trygvis/rules/terraform/TerraformProvider.java22
-rw-r--r--module/ri-base/src/main/java/io/trygvis/rules/terraform/TerraformResource.java1
-rw-r--r--module/ri-base/src/main/resources/io/trygvis/rules/scaleway/terraform.drl2
-rw-r--r--module/ri-base/src/main/resources/io/trygvis/rules/terraform/terraform-resources.drl14
-rw-r--r--module/ri-base/src/main/resources/templates/terraform/main.j210
-rw-r--r--module/ri-base/src/main/resources/templates/terraform/variables.j210
8 files changed, 120 insertions, 1 deletions
diff --git a/module/ri-base/src/main/java/io/trygvis/rules/terraform/TerraformInputVariable.java b/module/ri-base/src/main/java/io/trygvis/rules/terraform/TerraformInputVariable.java
new file mode 100644
index 0000000..1415767
--- /dev/null
+++ b/module/ri-base/src/main/java/io/trygvis/rules/terraform/TerraformInputVariable.java
@@ -0,0 +1,41 @@
+package io.trygvis.rules.terraform;
+
+public class TerraformInputVariable {
+ private String module;
+
+ private String name;
+ private String type;
+ private String default_;
+
+ public TerraformInputVariable(String module) {
+ this.module = module;
+ }
+
+ public String getModule() {
+ return module;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public String getType() {
+ return type;
+ }
+
+ public void setType(String type) {
+ this.type = type;
+ }
+
+ public String getDefault() {
+ return default_;
+ }
+
+ public void setDefault(String default_) {
+ this.default_ = default_;
+ }
+}
diff --git a/module/ri-base/src/main/java/io/trygvis/rules/terraform/TerraformMain.java b/module/ri-base/src/main/java/io/trygvis/rules/terraform/TerraformMain.java
new file mode 100644
index 0000000..82626d2
--- /dev/null
+++ b/module/ri-base/src/main/java/io/trygvis/rules/terraform/TerraformMain.java
@@ -0,0 +1,21 @@
+package io.trygvis.rules.terraform;
+
+import java.util.Map;
+import java.util.TreeMap;
+
+public class TerraformMain {
+ private String module;
+ private Map<String, TerraformProvider> providers = new TreeMap<String, TerraformProvider>();
+
+ public TerraformMain(String module) {
+ this.module = module;
+ }
+
+ public String getModule() {
+ return module;
+ }
+
+ public Map<String, TerraformProvider> getProviders() {
+ return providers;
+ }
+}
diff --git a/module/ri-base/src/main/java/io/trygvis/rules/terraform/TerraformProvider.java b/module/ri-base/src/main/java/io/trygvis/rules/terraform/TerraformProvider.java
new file mode 100644
index 0000000..613d953
--- /dev/null
+++ b/module/ri-base/src/main/java/io/trygvis/rules/terraform/TerraformProvider.java
@@ -0,0 +1,22 @@
+package io.trygvis.rules.terraform;
+
+public class TerraformProvider {
+ private String source;
+ private String version;
+
+ protected TerraformProvider() {
+ }
+
+ public TerraformProvider(String source, String version) {
+ this.source = source;
+ this.version = version;
+ }
+
+ public String getSource() {
+ return source;
+ }
+
+ public String getVersion() {
+ return version;
+ }
+}
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 8ca6ae2..2615dde 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
@@ -59,6 +59,7 @@ public class TerraformResource {
public String asString() {
var buf = new StringBuilder();
+ buf.append("resource ");
buf.append(quote(kind));
buf.append(" ");
buf.append(quote(instance));
diff --git a/module/ri-base/src/main/resources/io/trygvis/rules/scaleway/terraform.drl b/module/ri-base/src/main/resources/io/trygvis/rules/scaleway/terraform.drl
index 06bfc68..7e0ff03 100644
--- a/module/ri-base/src/main/resources/io/trygvis/rules/scaleway/terraform.drl
+++ b/module/ri-base/src/main/resources/io/trygvis/rules/scaleway/terraform.drl
@@ -1,4 +1,4 @@
-package io.trygvis.rules.terraform.scaleway
+package io.trygvis.rules.scaleway
import io.trygvis.rules.dba.Cluster
import io.trygvis.rules.dba.Container
diff --git a/module/ri-base/src/main/resources/io/trygvis/rules/terraform/terraform-resources.drl b/module/ri-base/src/main/resources/io/trygvis/rules/terraform/terraform-resources.drl
index 34af8fe..40a81f0 100644
--- a/module/ri-base/src/main/resources/io/trygvis/rules/terraform/terraform-resources.drl
+++ b/module/ri-base/src/main/resources/io/trygvis/rules/terraform/terraform-resources.drl
@@ -1,5 +1,9 @@
package io.trygvis.rules.terraform;
+
+import java.util.ArrayList
+import java.util.HashSet
import java.util.Map
+import java.util.Set
global io.trygvis.rules.engine.TemplateEngine te;
@@ -12,3 +16,13 @@ when
then
te.template("terraform/resource", $r.output, Map.of("resource", $r));
end
+
+rule "main.tf"
+ agenda-group "generate"
+when
+ $main : TerraformMain()
+ $variables : ArrayList() from collect(TerraformInputVariable(module == $main.module))
+then
+ te.template("terraform/main", $main.module + "/main.tf", Map.of("main", $main));
+ te.template("terraform/variables", $main.module + "/vars.tf", Map.of("variables", $variables));
+end
diff --git a/module/ri-base/src/main/resources/templates/terraform/main.j2 b/module/ri-base/src/main/resources/templates/terraform/main.j2
new file mode 100644
index 0000000..009f4ed
--- /dev/null
+++ b/module/ri-base/src/main/resources/templates/terraform/main.j2
@@ -0,0 +1,10 @@
+terraform {
+ required_providers {
+{% for alias, provider in main.providers.entrySet() %}
+ {{ alias }} = {
+ version = "{{ provider.version }}"
+ source = "{{ provider.source }}"
+ }
+{%- endfor %}
+ }
+}
diff --git a/module/ri-base/src/main/resources/templates/terraform/variables.j2 b/module/ri-base/src/main/resources/templates/terraform/variables.j2
new file mode 100644
index 0000000..6c37d99
--- /dev/null
+++ b/module/ri-base/src/main/resources/templates/terraform/variables.j2
@@ -0,0 +1,10 @@
+{%- for var in variables %}
+variable "{{ var.name }}" {
+{%- if var.type %}
+ type = {{ var.type }}
+{% endif %}
+{%- if var.default %}
+ default = {{ var.default }}
+{% endif %}
+}
+{% endfor %}