/** * Indicates whether the Gateway is currently deployed in AWS environment * by returning true. Otherwise, it returns false. * * It could either be DevPay or non-DevPay license. * * @return boolean */ public static boolean isDeployedToAWS() { try { // Ping the AWS-specific meta-data URL to figure out whether the // Gateway is deployed in a AWS Cloud environment. invokeUrl("http://169.254.169.254/latest/meta-data"); } catch (IOException ex) { return false; } return true; }
/** * Returns the AccountId of the user who is running the instance. * * @return String representing the AccountId or the owner-id * @throws java.io.IOException if failed to retrieve the AccountId using the * Cloud infrastructure */ public static String getAccountId() throws IOException { // Get the MAC address of the machine. String macUrl = getMetadataUrl() + "/network/interfaces/macs/"; String mac = invokeUrl(macUrl).trim(); // Use the MAC address to obtain the owner-id or the // AWS AccountId. String idUrl = macUrl + mac + "owner-id"; String acctId = invokeUrl(idUrl).trim(); assert acctId != null; return acctId; }
/** * Returns the local address (IPv4) of the instance. The local address * is defined to be * Public IP address if launched with direct addressing; private IP * address if launched with public addressing. * * @return local IP address (IPv4) of the instance * @throws java.io.IOException */ public static String getLocalIPv4() throws IOException { String url = getMetadataUrl() + "/local-ipv4"; String localIPv4 = invokeUrl(url); if ((localIPv4 == null) || (localIPv4.trim().length() == 0)) { String msg = "No local IPv4 assigned to the instance"; throw new IllegalStateException(msg); } return localIPv4.trim(); }
/** * Returns the region in which the instance is running. * * @return String representing the region where the instance is * running * @throws java.io.IOException if failed to retrieve the region information * using the Cloud infrastructure */ public static String getRegion() throws IOException { String url = getMetadataUrl() + "/placement/availability-zone"; String zone = invokeUrl(url); zone = zone.trim(); // In case of AWS, the zone includes an extra character // at the end such as "us-east-1a", "us-east-1b", "eu-west-1a", // etc. We have to strip that last character to get the // correct region. String region = zone.substring(0, zone.length() - 1); assert region != null; return region; }
/** * Returns the name of the security group from the list that is * obtained from the resource vendor. An instance may belong to multiple * security groups. And, the list of security groups obtained from the * vendor may not be ordered. If the vendor supports the notion of * a default security group, then that should be returned. Otherwise, * the implementation will be vendor-specific. * * @return * @throws java.io.IOException */ public static String getSecurityGroupName() throws IOException { // For AWS, we are returning the first security group from the list // that is obtained by querying the meta-data. String url = getMetadataUrl() + "/security-groups"; String groups = invokeUrl(url); if ((groups == null) || (groups.trim().length() == 0)) { String msg = "No security-group assigned to the instance"; throw new IllegalStateException(msg); } StringTokenizer tokenizer = new StringTokenizer(groups, "\n"); return tokenizer.nextToken(); // We only need the first one. }