Skip to content

Instantly share code, notes, and snippets.

@sairamkrish
Last active May 3, 2019 13:15
Show Gist options
  • Save sairamkrish/db35788cb0fad45fc0fe25e55907f16b to your computer and use it in GitHub Desktop.
Save sairamkrish/db35788cb0fad45fc0fe25e55907f16b to your computer and use it in GitHub Desktop.
This class helps to convert a property file entries into cloudformation script to add them as SSM::Parameter
package java_experiments;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;
/*
* This class helps to convert a property file entries into cloudformation script to add them as SSM::Parameter
*/
public class ConvertPropertiesToCfn {
public static void main(String[] args) throws IOException{
processProperties();
}
private static void processProperties() throws IOException {
String propFileName = "env.properties";
Map<String, String> prop = null;
try(var inStream = ConvertPropertiesToCfn.class.getClassLoader().getResourceAsStream(propFileName)){
prop = getOrderedProperties(inStream);
}catch(IOException ie) {
throw new RuntimeException("Could not read file");
}
StringBuffer strBuf = new StringBuffer();
for (Object k : prop.keySet()) {
String key = (String) k ;
String value = prop.get(key);
strBuf.append("\n");
strBuf.append(String.format(" %s: ",toCamelCase(key)));
strBuf.append("\n");
strBuf.append(" Type: \"AWS::SSM::Parameter\" ");
strBuf.append("\n");
strBuf.append(" Properties: ");
strBuf.append("\n");
strBuf.append(String.format(" Name: \"%s/service-web/%s\" ","stage",key));
strBuf.append("\n");
if (value.equals("REQUEST_VALUE")) {
strBuf.append(" Type: \"SecureString\" ");
}else {
strBuf.append(" Type: \"String\" ");
}
strBuf.append("\n");
strBuf.append(String.format(" Value: \"%s\" ",value));
strBuf.append("\n");
strBuf.append(" ");
strBuf.append("\n");
}
System.out.println(strBuf);
}
static String toCamelCase(String s){
String[] parts = s.split("_");
String camelCaseString = "";
for (String part : parts){
camelCaseString = camelCaseString + toProperCase(part);
}
return camelCaseString;
}
static String toProperCase(String s) {
return s.substring(0, 1).toUpperCase() +
s.substring(1).toLowerCase();
}
public static Map<String, String> getOrderedProperties(InputStream in) throws IOException{
Map<String, String> mp = new LinkedHashMap<>();
(new Properties(){
public synchronized Object put(Object key, Object value) {
return mp.put((String) key, (String) value);
}
}).load(in);
return mp;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment