package io.trygvis.rules.terraform import io.trygvis.rules.dba.Cluster import io.trygvis.rules.dba.Container import io.trygvis.rules.machine.Machine import io.trygvis.rules.dns.DnsEntry import io.trygvis.rules.dns.DnsEntryTerraformExpression import java.util.Map; global io.trygvis.rules.engine.TemplateEngine te; declare ScalewayMachine machine : Machine key : String end rule "Terraform for Machine" when $machine: Machine() then ScalewayMachine scw = new ScalewayMachine(); scw.setKey($machine.name); scw.setMachine($machine); insert(scw); end rule "Create DNS entry for Terraform Machine" when $machine : Machine(fqdn != null) not(DnsEntry(fqdn == $machine.fqdn)) then var a = DnsEntry.a($machine.fqdn); insert(a); var ipv4 = "scaleway_instance_ip.%s.address".formatted($machine.name); insert(new DnsEntryTerraformExpression(a, $machine.name, ipv4)); end rule "TF for TerraformMachine" when $m: Machine() $scw: ScalewayMachine(machine == $m) then var path = "platform/terraform/%s.tf".formatted($scw.getKey()); te.template("terraform-machine", path, Map.of("m", $m, "scw", $scw)); end rule "Terraform for DNS" when $entry: DnsEntry() $tf : DnsEntryTerraformExpression(entry == $entry) then var path = "dns/%s.tf".formatted($tf.key); te.template("terraform-record-set", path, Map.of( "entry", $entry, "tf", $tf) ); end