1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.common.aws.status;
17
18 import static com.google.common.base.Preconditions.checkArgument;
19
20 import java.io.File;
21 import java.io.IOException;
22 import java.util.List;
23
24 import org.apache.commons.io.FileUtils;
25 import org.junit.Test;
26 import org.kuali.common.aws.status.model.AwsRecord;
27 import org.kuali.common.aws.status.model.DnsmeRecord;
28 import org.kuali.common.aws.status.model.Environment;
29 import org.kuali.common.util.file.CanonicalFile;
30 import org.kuali.common.util.log.LoggerUtils;
31 import org.slf4j.Logger;
32
33 import com.google.common.base.Joiner;
34 import com.google.common.base.Splitter;
35 import com.google.common.collect.Lists;
36
37 public class MergeTest {
38
39 private static final Splitter SPLITTER = Splitter.on(',');
40 private static final Joiner JOINER = Joiner.on(',');
41
42 private static final String DNSME = "./target/dnsme/records.csv";
43 private static final String AWS = GetStatusTest.OUTPUT_PATH;
44 private static final String KUALI = "./target/kuali/envs.csv";
45
46 private static final Logger logger = LoggerUtils.make();
47
48 @Test
49 public void test() {
50 try {
51
52 File dnsme = new CanonicalFile(System.getProperty("dnsme.in", DNSME));
53 File aws = new CanonicalFile(System.getProperty("aws.in", AWS));
54 File kuali = new CanonicalFile(System.getProperty("kuali.out", KUALI));
55 checkArgument(dnsme.exists(), "[%s] does not exist", dnsme);
56 checkArgument(aws.exists(), "[%s] does not exist", aws);
57 List<DnsmeRecord> drecs = getDnsmeRecords(dnsme);
58 List<AwsRecord> arecs = getAwsRecords(aws);
59 List<Environment> envs = merge(drecs, arecs);
60 logger.info(String.format("Merged %s records", envs.size()));
61 List<String> lines = getLines(envs);
62 FileUtils.writeLines(kuali, lines);
63 logger.info(String.format("created -> [%s]", kuali));
64 } catch (Exception e) {
65 e.printStackTrace();
66 }
67
68 }
69
70 protected List<String> getLines(List<Environment> envs) {
71 List<String> lines = Lists.newArrayList();
72 lines.add("project,env,fqdn,type");
73 for (Environment env : envs) {
74 lines.add(getLine(env));
75 }
76 return lines;
77 }
78
79 protected String getLine(Environment env) {
80 List<String> tokens = Lists.newArrayList();
81 tokens.add(env.getProject());
82 tokens.add(env.getId());
83 tokens.add(env.getDns());
84 tokens.add(env.getType());
85 return JOINER.join(tokens);
86 }
87
88 protected List<Environment> merge(List<DnsmeRecord> drecs, List<AwsRecord> arecs) {
89 List<Environment> envs = Lists.newArrayList();
90 for (AwsRecord arec : arecs) {
91 DnsmeRecord drec = findRecord(arec, drecs);
92 Environment env = new Environment();
93 env.setProject(arec.getProject());
94 env.setId(arec.getEnv());
95 env.setDns(drec.getDns() + ".kuali.org");
96 env.setType(arec.getType());
97 envs.add(env);
98 }
99 return envs;
100 }
101
102 protected DnsmeRecord findRecord(AwsRecord arec, List<DnsmeRecord> drecs) {
103 for (DnsmeRecord drec : drecs) {
104 if (drec.getAlias().equals(arec.getDns())) {
105 return drec;
106 }
107 }
108 throw new IllegalStateException(String.format("No corresponding DNSME entry for %s - [%s=%s]", arec.getProject(), arec.getEnv(), arec.getDns()));
109 }
110
111 protected List<AwsRecord> getAwsRecords(File file) throws IOException {
112 List<String> lines = FileUtils.readLines(file);
113 List<AwsRecord> records = Lists.newArrayList();
114 for (int i = 1; i < lines.size(); i++) {
115 String line = lines.get(i);
116 List<String> tokens = Lists.newArrayList(SPLITTER.split(line));
117 AwsRecord record = new AwsRecord();
118 record.setProject(tokens.get(0));
119 record.setEnv(tokens.get(1));
120 record.setDns(tokens.get(2));
121 record.setType(tokens.get(3));
122 records.add(record);
123 }
124 return records;
125
126 }
127
128 protected List<DnsmeRecord> getDnsmeRecords(File file) throws IOException {
129 List<String> lines = FileUtils.readLines(file);
130 List<DnsmeRecord> records = Lists.newArrayList();
131 for (int i = 1; i < lines.size(); i++) {
132 String line = lines.get(i);
133 List<String> tokens = Lists.newArrayList(SPLITTER.split(line));
134 DnsmeRecord record = new DnsmeRecord();
135 record.setDns(tokens.get(0));
136 record.setAlias(tokens.get(1));
137 record.setType(tokens.get(2));
138 record.setTtl(tokens.get(3));
139 records.add(record);
140 }
141 return records;
142
143 }
144
145 }