From b7d0da791505ec08bc5e87dc1f5245078c8b3d42 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sun, 3 Jan 2021 23:31:47 +0100 Subject: Better sorted output files. --- out/phase-1.yaml | 322 ++++++++++++------------ out/vs0.yaml | 54 ++-- src/main/java/io/trygvis/rules/acme/AcmeIo.java | 102 +++++++- 3 files changed, 277 insertions(+), 201 deletions(-) diff --git a/out/phase-1.yaml b/out/phase-1.yaml index c8dfa11..48a6365 100644 --- a/out/phase-1.yaml +++ b/out/phase-1.yaml @@ -1,113 +1,12 @@ --- -type: "io.trygvis.rules.acme.AcmeMyApp" -data: - environment: "production" - dockerTag: "master" ---- -type: "io.trygvis.rules.acme.AcmeMyApp" -data: - environment: "ci" - dockerTag: "development" ---- -type: "io.trygvis.rules.acme.AcmeServer" -data: - machine: - name: "acme-3" - fqdn: "acme-3.machine.acme.com" ---- -type: "io.trygvis.rules.acme.AcmeServer" -data: - machine: - name: "acme-2" - fqdn: "acme-2.machine.acme.com" ---- -type: "io.trygvis.rules.acme.AcmeServer" -data: - machine: - name: "acme-1" - fqdn: "acme-1.machine.acme.com" ---- -type: "io.trygvis.rules.acme.WgHost" -data: - name: "acme-3" - machine: - name: "acme-3" - fqdn: "acme-3.machine.acme.com" - net: "vs0" - publicName: null - netToNetIp: null - networkIp: null ---- -type: "io.trygvis.rules.acme.WgHost" -data: - name: "ws-2" - machine: null - net: "vs0" - publicName: null - netToNetIp: null - networkIp: null ---- -type: "io.trygvis.rules.acme.WgHost" -data: - name: "acme-1" - machine: - name: "acme-1" - fqdn: "acme-1.machine.acme.com" - net: "vs0" - publicName: null - netToNetIp: null - networkIp: null ---- -type: "io.trygvis.rules.acme.WgHost" -data: - name: "ws-1" - machine: null - net: "vs0" - publicName: null - netToNetIp: null - networkIp: null ---- -type: "io.trygvis.rules.acme.WgHost" -data: - name: "acme-2" - machine: - name: "acme-2" - fqdn: "acme-2.machine.acme.com" - net: "vs0" - publicName: null - netToNetIp: null - networkIp: null ---- -type: "io.trygvis.rules.acme.WgNet" -data: - name: "vs0" - domain: "vpn.acme.com" ---- -type: "io.trygvis.rules.dba.Cluster" -data: - name: "acme-production" ---- -type: "io.trygvis.rules.dba.Cluster" -data: - name: "acme-ci" ---- -type: "io.trygvis.rules.dba.Container" -data: - cluster: - name: "acme-production" - name: "db" - machineRole: "mdb" - image: "mongodb" - tag: "3.2" ---- type: "io.trygvis.rules.dba.Container" data: cluster: - name: "acme-production" - name: "db" - machineRole: "pdb" - image: "postgresql" - tag: "13" + name: "acme-ci" + name: "app" + machineRole: "statera-console" + image: "statera-console" + tag: "development" --- type: "io.trygvis.rules.dba.Container" data: @@ -140,27 +39,36 @@ type: "io.trygvis.rules.dba.Container" data: cluster: name: "acme-ci" - name: "db" - machineRole: "pdb" - image: "postgresql" - tag: "13" + name: "app" + machineRole: "statera" + image: "statera" + tag: "development" --- type: "io.trygvis.rules.dba.Container" data: cluster: name: "acme-production" name: "app" - machineRole: "4tune-web" - image: "4tune-web" + machineRole: "4tune-api" + image: "4tune-api" tag: "master" --- type: "io.trygvis.rules.dba.Container" data: cluster: - name: "acme-production" + name: "acme-ci" name: "app" machineRole: "4tune-api" image: "4tune-api" + tag: "development" +--- +type: "io.trygvis.rules.dba.Container" +data: + cluster: + name: "acme-production" + name: "app" + machineRole: "4tune-web" + image: "4tune-web" tag: "master" --- type: "io.trygvis.rules.dba.Container" @@ -168,40 +76,101 @@ data: cluster: name: "acme-ci" name: "db" - machineRole: "mdb" - image: "mongodb" - tag: "3.2" + machineRole: "pdb" + image: "postgresql" + tag: "13" --- type: "io.trygvis.rules.dba.Container" data: cluster: - name: "acme-ci" - name: "app" - machineRole: "statera-console" - image: "statera-console" - tag: "development" + name: "acme-production" + name: "db" + machineRole: "pdb" + image: "postgresql" + tag: "13" --- type: "io.trygvis.rules.dba.Container" data: cluster: name: "acme-ci" - name: "app" - machineRole: "statera" - image: "statera" - tag: "development" + name: "db" + machineRole: "mdb" + image: "mongodb" + tag: "3.2" --- type: "io.trygvis.rules.dba.Container" data: cluster: - name: "acme-ci" - name: "app" - machineRole: "4tune-api" - image: "4tune-api" - tag: "development" + name: "acme-production" + name: "db" + machineRole: "mdb" + image: "mongodb" + tag: "3.2" +--- +type: "io.trygvis.rules.acme.AcmeMyApp" +data: + environment: "ci" + dockerTag: "development" +--- +type: "io.trygvis.rules.acme.AcmeMyApp" +data: + environment: "production" + dockerTag: "master" +--- +type: "io.trygvis.rules.acme.WgHost" +data: + name: "acme-1" + machine: + name: "acme-1" + fqdn: "acme-1.machine.acme.com" + net: "vs0" + publicName: null + netToNetIp: null + networkIp: null +--- +type: "io.trygvis.rules.acme.WgHost" +data: + name: "acme-2" + machine: + name: "acme-2" + fqdn: "acme-2.machine.acme.com" + net: "vs0" + publicName: null + netToNetIp: null + networkIp: null +--- +type: "io.trygvis.rules.acme.WgHost" +data: + name: "acme-3" + machine: + name: "acme-3" + fqdn: "acme-3.machine.acme.com" + net: "vs0" + publicName: null + netToNetIp: null + networkIp: null +--- +type: "io.trygvis.rules.acme.WgHost" +data: + name: "ws-1" + machine: null + net: "vs0" + publicName: null + netToNetIp: null + networkIp: null +--- +type: "io.trygvis.rules.acme.WgHost" +data: + name: "ws-2" + machine: null + net: "vs0" + publicName: null + netToNetIp: null + networkIp: null --- type: "io.trygvis.rules.dns.DnsEntry" data: - fqdn: "acme-1.vpn.acme.com" + fqdn: "ws-2.vpn.acme.com" type: "A" --- type: "io.trygvis.rules.dns.DnsEntry" @@ -211,33 +180,61 @@ data: --- type: "io.trygvis.rules.dns.DnsEntry" data: - fqdn: "ws-2.vpn.acme.com" + fqdn: "acme-2.vpn.acme.com" type: "A" --- type: "io.trygvis.rules.dns.DnsEntry" data: - fqdn: "acme-2.vpn.acme.com" + fqdn: "acme-3.vpn.acme.com" type: "A" --- type: "io.trygvis.rules.dns.DnsEntry" data: - fqdn: "acme-3.vpn.acme.com" + fqdn: "acme-1.vpn.acme.com" type: "A" --- -type: "io.trygvis.rules.engine.KeyValue" +type: "io.trygvis.rules.dba.Cluster" data: - key: "rm-gen" - value: null + name: "acme-ci" --- -type: "io.trygvis.rules.machine.Machine" +type: "io.trygvis.rules.dba.Cluster" data: - name: "ws-2" - fqdn: null + name: "acme-production" --- -type: "io.trygvis.rules.machine.Machine" +type: "io.trygvis.rules.terraform.ScalewayMachine" data: - name: "acme-2" - fqdn: "acme-2.machine.acme.com" + machine: + name: "acme-1" + fqdn: "acme-1.machine.acme.com" + key: "acme-1" +--- +type: "io.trygvis.rules.terraform.ScalewayMachine" +data: + machine: + name: "acme-2" + fqdn: "acme-2.machine.acme.com" + key: "acme-2" +--- +type: "io.trygvis.rules.terraform.ScalewayMachine" +data: + machine: + name: "acme-3" + fqdn: "acme-3.machine.acme.com" + key: "acme-3" +--- +type: "io.trygvis.rules.terraform.ScalewayMachine" +data: + machine: + name: "ws-1" + fqdn: null + key: "ws-1" +--- +type: "io.trygvis.rules.terraform.ScalewayMachine" +data: + machine: + name: "ws-2" + fqdn: null + key: "ws-2" --- type: "io.trygvis.rules.machine.Machine" data: @@ -246,45 +243,48 @@ data: --- type: "io.trygvis.rules.machine.Machine" data: - name: "ws-1" - fqdn: null + name: "acme-2" + fqdn: "acme-2.machine.acme.com" --- type: "io.trygvis.rules.machine.Machine" data: name: "acme-3" fqdn: "acme-3.machine.acme.com" --- -type: "io.trygvis.rules.terraform.ScalewayMachine" +type: "io.trygvis.rules.machine.Machine" data: - machine: - name: "ws-2" - fqdn: null - key: "ws-2" + name: "ws-1" + fqdn: null --- -type: "io.trygvis.rules.terraform.ScalewayMachine" +type: "io.trygvis.rules.machine.Machine" +data: + name: "ws-2" + fqdn: null +--- +type: "io.trygvis.rules.acme.AcmeServer" data: machine: name: "acme-3" fqdn: "acme-3.machine.acme.com" - key: "acme-3" --- -type: "io.trygvis.rules.terraform.ScalewayMachine" +type: "io.trygvis.rules.acme.AcmeServer" data: machine: name: "acme-1" fqdn: "acme-1.machine.acme.com" - key: "acme-1" --- -type: "io.trygvis.rules.terraform.ScalewayMachine" +type: "io.trygvis.rules.acme.AcmeServer" data: machine: name: "acme-2" fqdn: "acme-2.machine.acme.com" - key: "acme-2" --- -type: "io.trygvis.rules.terraform.ScalewayMachine" +type: "io.trygvis.rules.engine.KeyValue" data: - machine: - name: "ws-1" - fqdn: null - key: "ws-1" + key: "rm-gen" + value: null +--- +type: "io.trygvis.rules.acme.WgNet" +data: + name: "vs0" + domain: "vpn.acme.com" diff --git a/out/vs0.yaml b/out/vs0.yaml index c400981..2ca168b 100644 --- a/out/vs0.yaml +++ b/out/vs0.yaml @@ -1,10 +1,10 @@ --- type: "io.trygvis.rules.acme.WgHost" data: - name: "acme-3" + name: "acme-1" machine: - name: "acme-3" - fqdn: "acme-3.machine.acme.com" + name: "acme-1" + fqdn: "acme-1.machine.acme.com" net: "vs0" publicName: null netToNetIp: null @@ -12,8 +12,10 @@ data: --- type: "io.trygvis.rules.acme.WgHost" data: - name: "ws-2" - machine: null + name: "acme-2" + machine: + name: "acme-2" + fqdn: "acme-2.machine.acme.com" net: "vs0" publicName: null netToNetIp: null @@ -21,10 +23,10 @@ data: --- type: "io.trygvis.rules.acme.WgHost" data: - name: "acme-1" + name: "acme-3" machine: - name: "acme-1" - fqdn: "acme-1.machine.acme.com" + name: "acme-3" + fqdn: "acme-3.machine.acme.com" net: "vs0" publicName: null netToNetIp: null @@ -41,23 +43,16 @@ data: --- type: "io.trygvis.rules.acme.WgHost" data: - name: "acme-2" - machine: - name: "acme-2" - fqdn: "acme-2.machine.acme.com" + name: "ws-2" + machine: null net: "vs0" publicName: null netToNetIp: null networkIp: null --- -type: "io.trygvis.rules.acme.WgNet" -data: - name: "vs0" - domain: "vpn.acme.com" ---- type: "io.trygvis.rules.dns.DnsEntry" data: - fqdn: "acme-1.vpn.acme.com" + fqdn: "ws-2.vpn.acme.com" type: "A" --- type: "io.trygvis.rules.dns.DnsEntry" @@ -67,23 +62,23 @@ data: --- type: "io.trygvis.rules.dns.DnsEntry" data: - fqdn: "ws-2.vpn.acme.com" + fqdn: "acme-2.vpn.acme.com" type: "A" --- type: "io.trygvis.rules.dns.DnsEntry" data: - fqdn: "acme-2.vpn.acme.com" + fqdn: "acme-3.vpn.acme.com" type: "A" --- type: "io.trygvis.rules.dns.DnsEntry" data: - fqdn: "acme-3.vpn.acme.com" + fqdn: "acme-1.vpn.acme.com" type: "A" --- type: "io.trygvis.rules.machine.Machine" data: - name: "ws-2" - fqdn: null + name: "acme-1" + fqdn: "acme-1.machine.acme.com" --- type: "io.trygvis.rules.machine.Machine" data: @@ -92,8 +87,8 @@ data: --- type: "io.trygvis.rules.machine.Machine" data: - name: "acme-1" - fqdn: "acme-1.machine.acme.com" + name: "acme-3" + fqdn: "acme-3.machine.acme.com" --- type: "io.trygvis.rules.machine.Machine" data: @@ -102,5 +97,10 @@ data: --- type: "io.trygvis.rules.machine.Machine" data: - name: "acme-3" - fqdn: "acme-3.machine.acme.com" + name: "ws-2" + fqdn: null +--- +type: "io.trygvis.rules.acme.WgNet" +data: + name: "vs0" + domain: "vpn.acme.com" diff --git a/src/main/java/io/trygvis/rules/acme/AcmeIo.java b/src/main/java/io/trygvis/rules/acme/AcmeIo.java index 9235992..488c93a 100644 --- a/src/main/java/io/trygvis/rules/acme/AcmeIo.java +++ b/src/main/java/io/trygvis/rules/acme/AcmeIo.java @@ -11,13 +11,15 @@ import org.kie.api.runtime.rule.FactHandle; import java.io.File; import java.io.FileWriter; import java.io.IOException; -import java.util.AbstractMap; +import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.Collection; +import java.util.Comparator; +import java.util.HashMap; import java.util.List; -import java.util.Map; import java.util.function.Function; +@SuppressWarnings("unchecked") public class AcmeIo { private final ObjectMapper mapper; @@ -55,6 +57,73 @@ public class AcmeIo { dump(s, factHandles, (o) -> true); } + static class FactCollection { + public final Class type; + public final List values; + + public FactCollection(Class type) { + this.type = type; + this.values = new ArrayList<>(); + } + + public void sort() { + var comparator = comparable(type, "key"); + + if (comparator == null) { + comparator = comparable(type, "name"); + } + + if (comparator == null) { + comparator = Comparator.comparingInt(System::identityHashCode); + } + + this.values.sort(comparator); + } + } + + private static > Comparator comparable(Class klass, String name) { + + if (klass.getName().contains("Wg")) { + System.out.println("AcmeIo.invoker"); + } + + try { + var method = klass.getMethod("get" + name.substring(0, 1).toUpperCase() + name.substring(1)); + if (!method.isAccessible()) { + if (!method.trySetAccessible()) + return null; + } + + return (a, b) -> { + try { + var x = (T) method.invoke(a); + var y = (T) method.invoke(b); + return x.compareTo(y); + } catch (IllegalAccessException | InvocationTargetException e) { + throw new RuntimeException(e); + } + }; + } catch (NoSuchMethodException ignored) { + } + + try { + var field = klass.getField(name); + + return (a, b) -> { + try { + var x = (T) field.get(a); + var y = (T) field.get(b); + return x.compareTo(y); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } + }; + } catch (NoSuchFieldException ignored) { + } + + return null; + } + public void dump(String s, Collection factHandles, Function filter) throws IOException { var out = new File("out"); @@ -64,7 +133,7 @@ public class AcmeIo { } } - List> facts = new ArrayList<>(factHandles.size()); + var facts = new HashMap, FactCollection>(factHandles.size()); for (var handle : factHandles) { if (handle instanceof DefaultFactHandle h) { var obj = h.getObject(); @@ -72,22 +141,29 @@ public class AcmeIo { continue; } - facts.add(new AbstractMap.SimpleImmutableEntry<>( - h.getObjectClassName(), - obj)); + Class type = obj.getClass(); + var collection = facts.get(type); + + if (collection == null) { + collection = new FactCollection(type); + facts.put(type, collection); + } + + collection.values.add(obj); } } - facts.sort(Map.Entry.comparingByKey()); - var factory = mapper.getFactory(); try (var writer = new FileWriter(new File(out, s + ".yaml")); var g = factory.createGenerator(writer)) { - for (Map.Entry fact : facts) { - g.writeObject(new AcmeObject( - fact.getKey(), - mapper.valueToTree(fact.getValue()) - )); + for (var e : facts.entrySet()) { + var name = e.getKey().getName(); + + var collection = e.getValue(); + collection.sort(); + for (var fact : collection.values) { + g.writeObject(new AcmeObject(name, mapper.valueToTree(fact))); + } } } } -- cgit v1.2.3