use of com.amazonaws.services.ec2.AmazonEC2 in project eureka by Netflix.
the class ElasticNetworkInterfaceBinder method alreadyBound.
public boolean alreadyBound() throws MalformedURLException {
InstanceInfo myInfo = applicationInfoManager.getInfo();
String myInstanceId = ((AmazonInfo) myInfo.getDataCenterInfo()).get(AmazonInfo.MetaDataKey.instanceId);
AmazonEC2 ec2Service = getEC2Service();
List<InstanceNetworkInterface> instanceNetworkInterfaces = instanceData(myInstanceId, ec2Service).getNetworkInterfaces();
List<String> candidateIPs = getCandidateIps();
for (String ip : candidateIPs) {
for (InstanceNetworkInterface ini : instanceNetworkInterfaces) {
if (ip.equals(ini.getPrivateIpAddress())) {
logger.info("My instance {} seems to be already associated with the ip {}", myInstanceId, ip);
return true;
}
}
}
return false;
}
use of com.amazonaws.services.ec2.AmazonEC2 in project eureka by Netflix.
the class ElasticNetworkInterfaceBinder method bind.
/**
* Binds an ENI to the instance.
*
* The candidate ENI's are deduced in the same wa the EIP binder works: Via dns records or via service urls,
* depending on configuration.
*
* It will try to attach the first ENI that is:
* Available
* For this subnet
* In the list of candidate ENI's
*
* @throws MalformedURLException
*/
public void bind() throws MalformedURLException {
InstanceInfo myInfo = ApplicationInfoManager.getInstance().getInfo();
String myInstanceId = ((AmazonInfo) myInfo.getDataCenterInfo()).get(AmazonInfo.MetaDataKey.instanceId);
String myZone = ((AmazonInfo) myInfo.getDataCenterInfo()).get(AmazonInfo.MetaDataKey.availabilityZone);
final List<String> ips = getCandidateIps();
Ordering<NetworkInterface> ipsOrder = Ordering.natural().onResultOf(new Function<NetworkInterface, Integer>() {
public Integer apply(NetworkInterface networkInterface) {
return ips.indexOf(networkInterface.getPrivateIpAddress());
}
});
AmazonEC2 ec2Service = getEC2Service();
String subnetId = instanceData(myInstanceId, ec2Service).getSubnetId();
DescribeNetworkInterfacesResult result = ec2Service.describeNetworkInterfaces(new DescribeNetworkInterfacesRequest().withFilters(new Filter("private-ip-address", ips)).withFilters(new Filter("status", Lists.newArrayList("available"))).withFilters(new Filter("subnet-id", Lists.newArrayList(subnetId))));
if (result.getNetworkInterfaces().isEmpty()) {
logger.info("No ip is free to be associated with this instance. Candidate ips are: {} for zone: {}", ips, myZone);
} else {
NetworkInterface selected = ipsOrder.min(result.getNetworkInterfaces());
ec2Service.attachNetworkInterface(new AttachNetworkInterfaceRequest().withNetworkInterfaceId(selected.getNetworkInterfaceId()).withDeviceIndex(1).withInstanceId(myInstanceId));
}
}
use of com.amazonaws.services.ec2.AmazonEC2 in project chassis by Kixeye.
the class ServerInstanceContextTest method testValidContext.
@Test
public void testValidContext() {
final String region = Regions.US_WEST_2.getName();
final String instanceId = RandomStringUtils.random(20, "abcdefghi");
final String availabilityZone = region + RandomStringUtils.random(20, "abcdefghi");
final String environment = RandomStringUtils.random(20, "abcdefghi");
final String userData = new UserData(environment).toString();
final String elbName = environment + "-Zookeeper";
final String exhibitorUrl = "http://" + elbName + "-" + region + ".aws.amazon.com";
final int exhibitorPort = 80;
final String version = "1.0.0";
Ec2MetadataClient ec2MetadataClient = EasyMock.createMock(Ec2MetadataClient.class);
EasyMock.expect(ec2MetadataClient.getAvailabilityZone()).andAnswer(new IAnswer<String>() {
@Override
public String answer() throws Throwable {
return availabilityZone;
}
});
EasyMock.expect(ec2MetadataClient.getInstanceId()).andAnswer(new IAnswer<String>() {
@Override
public String answer() throws Throwable {
return instanceId;
}
});
EasyMock.expect(ec2MetadataClient.getUserData()).andAnswer(new IAnswer<String>() {
@Override
public String answer() throws Throwable {
return userData;
}
});
EasyMock.expect(ec2MetadataClient.getPrivateIpAddress()).andAnswer(new IAnswer<String>() {
@Override
public String answer() throws Throwable {
return "127.0.0.1";
}
});
EasyMock.expect(ec2MetadataClient.getPublicIpAddress()).andAnswer(new IAnswer<String>() {
@Override
public String answer() throws Throwable {
return "127.0.0.1";
}
});
AmazonEC2 amazonEC2 = EasyMock.createMock(AmazonEC2.class);
EasyMock.expect(amazonEC2.describeRegions()).andAnswer(new IAnswer<DescribeRegionsResult>() {
@Override
public DescribeRegionsResult answer() throws Throwable {
DescribeRegionsResult result = new DescribeRegionsResult();
List<Region> regions = new ArrayList<>();
for (Regions region : Regions.values()) {
Region r = new Region();
r.setRegionName(region.getName());
regions.add(r);
}
result.setRegions(regions);
return result;
}
});
EasyMock.expect(amazonEC2.describeTags(EasyMock.anyObject(DescribeTagsRequest.class))).andAnswer(new IAnswer<DescribeTagsResult>() {
@Override
public DescribeTagsResult answer() throws Throwable {
return new DescribeTagsResult();
}
});
com.amazonaws.regions.Region r = com.amazonaws.regions.Region.getRegion(Regions.fromName(region));
amazonEC2.setRegion(eqRegion(r));
EasyMock.expectLastCall();
AmazonElasticLoadBalancing amazonElasticLoadBalancing = EasyMock.createMock(AmazonElasticLoadBalancing.class);
EasyMock.expect(amazonElasticLoadBalancing.describeLoadBalancers()).andAnswer(new IAnswer<DescribeLoadBalancersResult>() {
@Override
public DescribeLoadBalancersResult answer() throws Throwable {
DescribeLoadBalancersResult result = new DescribeLoadBalancersResult();
List<LoadBalancerDescription> loadBalancers = new ArrayList<>();
LoadBalancerDescription lb1 = new LoadBalancerDescription();
lb1.setDNSName(exhibitorUrl);
lb1.setLoadBalancerName(elbName);
List<ListenerDescription> listenerDescriptions = new ArrayList<>();
ListenerDescription httpListenerDescription = new ListenerDescription();
Listener httpListener = new Listener();
httpListener.setProtocol("HTTP");
httpListener.setLoadBalancerPort(exhibitorPort);
httpListener.setInstancePort(8080);
httpListenerDescription.setListener(httpListener);
ListenerDescription httpsListenerDescription = new ListenerDescription();
Listener httpsListener = new Listener();
httpsListener.setProtocol("HTTPS");
httpsListener.setLoadBalancerPort(443);
httpsListener.setInstancePort(8080);
httpsListenerDescription.setListener(httpListener);
listenerDescriptions.add(httpListenerDescription);
listenerDescriptions.add(httpsListenerDescription);
lb1.setListenerDescriptions(listenerDescriptions);
loadBalancers.add(lb1);
result.setLoadBalancerDescriptions(loadBalancers);
return result;
}
});
amazonElasticLoadBalancing.setRegion(eqRegion(r));
EasyMock.expectLastCall();
EasyMock.replay(ec2MetadataClient, amazonEC2, amazonElasticLoadBalancing);
ServerInstanceContext context = new ServerInstanceContext(ec2MetadataClient, amazonEC2, amazonElasticLoadBalancing);
Assert.assertEquals(region, context.getRegion());
Assert.assertEquals(instanceId, context.getInstanceId());
Assert.assertEquals(availabilityZone, context.getAvailabilityZone());
Assert.assertEquals(environment, context.getEnvironment());
Assert.assertEquals(exhibitorUrl, context.getExhibitorHost());
Assert.assertEquals(exhibitorPort, context.getExhibitorPort());
Assert.assertNull(context.getAppName());
EasyMock.verify(ec2MetadataClient, amazonEC2, amazonElasticLoadBalancing);
EasyMock.reset(amazonEC2, amazonElasticLoadBalancing);
amazonEC2.shutdown();
EasyMock.expectLastCall();
amazonElasticLoadBalancing.shutdown();
EasyMock.expectLastCall();
String name = RandomStringUtils.random(20, "abcdefghi");
EasyMock.replay(amazonEC2, amazonElasticLoadBalancing);
context.setAppName(name);
context.setVersion(version);
Assert.assertEquals(name, context.getAppName());
context.shutdown();
EasyMock.verify(ec2MetadataClient, amazonEC2, amazonElasticLoadBalancing);
}
use of com.amazonaws.services.ec2.AmazonEC2 in project chassis by Kixeye.
the class AwsUtils method getInstanceName.
/**
* Fetches and instance's name Tag or null if it does not have one
* @param instanceId
* @param amazonEC2
* @return
*/
public static String getInstanceName(String instanceId, AmazonEC2 amazonEC2) {
DescribeTagsResult result = amazonEC2.describeTags(new DescribeTagsRequest().withFilters(new Filter().withName("resource-id").withValues(instanceId), new Filter().withName("resource-type").withValues("instance"), new Filter().withName("key").withValues(TAG_KEY_NAME)));
if (result.getTags().isEmpty()) {
return null;
}
String name = result.getTags().get(0).getValue();
return name == null || name.trim().equals("") ? null : name;
}
use of com.amazonaws.services.ec2.AmazonEC2 in project aws-doc-sdk-examples by awsdocs.
the class DescribeAccount method main.
public static void main(String[] args) {
// snippet-start:[ec2.java1.describe_account.main]
AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient();
try {
DescribeAccountAttributesResult accountResults = ec2.describeAccountAttributes();
List<AccountAttribute> accountList = accountResults.getAccountAttributes();
for (ListIterator iter = accountList.listIterator(); iter.hasNext(); ) {
AccountAttribute attribute = (AccountAttribute) iter.next();
System.out.print("\n The name of the attribute is " + attribute.getAttributeName());
List<AccountAttributeValue> values = attribute.getAttributeValues();
// iterate through the attribute values
for (ListIterator iterVals = values.listIterator(); iterVals.hasNext(); ) {
AccountAttributeValue myValue = (AccountAttributeValue) iterVals.next();
System.out.print("\n The value of the attribute is " + myValue.getAttributeValue());
}
}
System.out.print("Done");
} catch (Exception e) {
e.getStackTrace();
}
// snippet-end:[ec2.java1.describe_account.main]
}
Aggregations