diff --git a/build.gradle b/build.gradle index 90b58e4..d0b6de7 100644 --- a/build.gradle +++ b/build.gradle @@ -27,7 +27,7 @@ if (System.getenv('TRAVIS_BRANCH') && System.getenv('TRAVIS_PULL_REQUEST') == 'f } group = 'gyro' -version = '0.15-SNAPSHOT' +version = '0.99.0-SNAPSHOT' java { sourceCompatibility = JavaVersion.VERSION_1_8 @@ -48,13 +48,13 @@ repositories { } dependencies { - api 'gyro:gyro-core:0.15-SNAPSHOT' + api 'gyro:gyro-core:0.99.0-SNAPSHOT' implementation 'com.psddev:dari-util:3.3.607-xe0f27a' implementation 'org.yaml:snakeyaml:1.24' implementation 'org.eclipse.jgit:org.eclipse.jgit:5.4.0.201906121030-r' implementation 'us.monoid.web:resty:0.3.2' - implementation 'gyro:gyro-aws-provider:0.15-SNAPSHOT' + implementation 'gyro:gyro-aws-provider:0.99.0-SNAPSHOT' implementation enforcedPlatform('software.amazon.awssdk:bom:2.5.70') implementation 'software.amazon.awssdk:s3' implementation 'software.amazon.awssdk:apache-client' diff --git a/src/main/java/gyro/ubuntu/AmiLocatorResolver.java b/src/main/java/gyro/ubuntu/AmiLocatorResolver.java new file mode 100644 index 0000000..ea651d0 --- /dev/null +++ b/src/main/java/gyro/ubuntu/AmiLocatorResolver.java @@ -0,0 +1,61 @@ +package gyro.ubuntu; + +import gyro.core.GyroException; +import gyro.core.Type; +import gyro.core.reference.ReferenceResolver; +import gyro.core.scope.Scope; +import org.apache.commons.io.IOUtils; +import us.monoid.json.JSONArray; +import us.monoid.json.JSONException; +import us.monoid.json.JSONObject; + +import java.net.URL; +import java.nio.charset.StandardCharsets; +import java.util.HashSet; +import java.util.List; + +@Type("ubuntu-ami-locator") +public class AmiLocatorResolver extends ReferenceResolver { + + @Override + public Object resolve(Scope scope, List arguments) throws Exception { + + if (arguments.size() != 4) { + throw new GyroException("ubuntu-ami-locator needs 4 arguments - 'name', 'architecture', 'instance-type' and 'region'"); + } + + String name = (String) arguments.get(0); + String arch = (String) arguments.get(1); + String type = (String) arguments.get(2); + String zone = (String) arguments.get(3); + + JSONObject json = new JSONObject(IOUtils.toString(new URL("https://cloud-images.ubuntu.com/locator/ec2/releasesTable"), StandardCharsets.UTF_8)); + JSONArray array = json.getJSONArray("aaData"); + + HashSet amiMappers = new HashSet<>(); + + for (int i = 0; i< array.length(); i++) { + JSONArray jsonArray = array.getJSONArray(i); + UbuntuAmiMapper amiMapper = new UbuntuAmiMapper(); + setUbuntuMapper(amiMapper, jsonArray); + amiMappers.add(amiMapper); + } + + return amiMappers.stream() + .filter(o -> o.getZone().equals(zone) + && o.getName().equals(name) + && o.getArchitecture().equals(arch) + && o.getType().equals(type)) + .map(UbuntuAmiMapper::getAmi).findFirst().orElse(null); + } + + private void setUbuntuMapper(UbuntuAmiMapper amiMapper, JSONArray jsonArray) throws JSONException { + amiMapper.setZone(jsonArray.getString(0)); + amiMapper.setName(jsonArray.getString(1)); + amiMapper.setVersion(jsonArray.getString(2)); + amiMapper.setArchitecture(jsonArray.getString(3)); + amiMapper.setType(jsonArray.getString(4)); + amiMapper.setRelease(jsonArray.getString(5)); + amiMapper.setAmi("ami-" + jsonArray.getString(6).split(">ami-")[1].split("")[0]); + } +} diff --git a/src/main/java/gyro/ubuntu/UbuntuAmiMapper.java b/src/main/java/gyro/ubuntu/UbuntuAmiMapper.java new file mode 100644 index 0000000..748bfc6 --- /dev/null +++ b/src/main/java/gyro/ubuntu/UbuntuAmiMapper.java @@ -0,0 +1,67 @@ +package gyro.ubuntu; + +public class UbuntuAmiMapper { + private String zone; + private String name; + private String version; + private String architecture; + private String type; + private String release; + private String ami; + + public String getZone() { + return zone; + } + + public void setZone(String zone) { + this.zone = zone; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getVersion() { + return version; + } + + public void setVersion(String version) { + this.version = version; + } + + public String getArchitecture() { + return architecture; + } + + public void setArchitecture(String architecture) { + this.architecture = architecture; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public String getRelease() { + return release; + } + + public void setRelease(String release) { + this.release = release; + } + + public String getAmi() { + return ami; + } + + public void setAmi(String ami) { + this.ami = ami; + } +}