View Javadoc
1   package com.meltmedia.jgroups.aws;
2   
3   import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4   import com.fasterxml.jackson.annotation.JsonProperty;
5   import com.fasterxml.jackson.databind.ObjectMapper;
6   import org.apache.http.HttpResponse;
7   import org.apache.http.HttpStatus;
8   import org.apache.http.client.HttpClient;
9   import org.apache.http.client.methods.HttpGet;
10  import org.apache.http.util.EntityUtils;
11  
12  import java.io.IOException;
13  import java.net.URI;
14  import java.net.URISyntaxException;
15  import java.util.Objects;
16  
17  @JsonIgnoreProperties(ignoreUnknown = true)
18  public class InstanceIdentity {
19    private static String INSTANCE_IDENTITY_URL = "http://instance-data/latest/dynamic/instance-identity/document";
20    private static URI INSTANCE_IDENTITY_URI;
21  
22    static {
23      try {
24        INSTANCE_IDENTITY_URI = new URI(INSTANCE_IDENTITY_URL);
25      } catch (URISyntaxException e) {
26        throw new RuntimeException("this should never happen");
27      }
28    }
29  
30  
31    public final String availabilityZone;
32    public final String privateIp;
33    public final String instanceId;
34    public final String instanceType;
35    public final String imageId;
36    public final String architecture;
37    public final String region;
38  
39    public InstanceIdentity(
40        @JsonProperty("availabilityZone") final String availabilityZone,
41        @JsonProperty("privateIp") final String privateIp,
42        @JsonProperty("instanceId") final String instanceId,
43        @JsonProperty("instanceType") final String instanceType,
44        @JsonProperty("imageId") final String imageId,
45        @JsonProperty("architecture") final String architecture,
46        @JsonProperty("region") final String region) {
47      this.availabilityZone = Objects.requireNonNull(availabilityZone, "availabilityZone cannot be null");
48      this.privateIp = Objects.requireNonNull(privateIp, "privateIp cannot be null");
49      this.instanceId = Objects.requireNonNull(instanceId, "instanceId cannot be null");
50      this.instanceType = Objects.requireNonNull(instanceType, "instanceType cannot be null");
51      this.imageId = Objects.requireNonNull(imageId, "imageId cannot be null");
52      this.architecture = Objects.requireNonNull(architecture, "architecture cannot be null");
53      this.region = Objects.requireNonNull(region, "region cannot be null");
54    }
55  
56    public static InstanceIdentity getIdentity(final HttpClient client) throws IOException {
57      return new ObjectMapper().readValue(getIdentityDocument(client), InstanceIdentity.class);
58    }
59  
60    /**
61     * Gets the body of the content returned from a GET request to uri.
62     *
63     * @param client
64     * @return the body of the message returned from the GET request.
65     * @throws IOException if there is an error encountered while getting the content.
66     */
67    private static String getIdentityDocument(final HttpClient client) throws IOException {
68      try {
69        final HttpGet getInstance = new HttpGet();
70        getInstance.setURI(INSTANCE_IDENTITY_URI);
71        final HttpResponse response = client.execute(getInstance);
72        if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
73          throw new IOException("failed to get instance identity, tried: " + INSTANCE_IDENTITY_URL + ", response: " + response.getStatusLine().getReasonPhrase());
74        }
75        return EntityUtils.toString(response.getEntity());
76      } catch (Exception e) {
77        throw new IOException("failed to get instance identity", e);
78      }
79    }
80  }