本文整理汇总了Java中com.amazonaws.services.ec2.model.DescribeInstancesResult类的典型用法代码示例。如果您正苦于以下问题:Java DescribeInstancesResult类的具体用法?Java DescribeInstancesResult怎么用?Java DescribeInstancesResult使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
DescribeInstancesResult类属于com.amazonaws.services.ec2.model包,在下文中一共展示了DescribeInstancesResult类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getInstanceState
import com.amazonaws.services.ec2.model.DescribeInstancesResult; //导入依赖的package包/类
public String getInstanceState(String instanceId) {
LOGGER.debug("getInstanceState('{}') entered", instanceId);
DescribeInstancesResult result = getEC2().describeInstances(
new DescribeInstancesRequest().withInstanceIds(instanceId));
List<Reservation> reservations = result.getReservations();
Set<Instance> instances = new HashSet<Instance>();
for (Reservation reservation : reservations) {
instances.addAll(reservation.getInstances());
if (instances.size() > 0) {
String state = instances.iterator().next().getState().getName();
LOGGER.debug(" InstanceState: {}", state);
return state;
}
}
LOGGER.debug("getInstanceState('{}') left", instanceId);
return null;
}
示例2: processResponse
import com.amazonaws.services.ec2.model.DescribeInstancesResult; //导入依赖的package包/类
private List<Instance> processResponse(DescribeInstancesResult response) {
List<Instance> returnedInstances = new ArrayList<>();
for (Reservation reservation : response.getReservations()) {
for (com.amazonaws.services.ec2.model.Instance instance : reservation.getInstances()) {
// Don't show dead servers or servers not tagged as belonging to Swordfish
if (instance.getState().getName().equals("terminated") || !instance.getTags().contains(new Tag().withKey("Swordfish").withValue("true"))) {
continue;
}
Instance instanceDetails = getInstanceDetails(instance);
returnedInstances.add(instanceDetails);
instanceRepository.save(instanceDetails);
refreshClient(instanceDetails);
}
}
return returnedInstances;
}
示例3: listsInstances
import com.amazonaws.services.ec2.model.DescribeInstancesResult; //导入依赖的package包/类
/**
* InstanceList can list instances.
*/
@Test
public void listsInstances() {
Mockito.when(
this.client.describeInstances(
Mockito.any(DescribeInstancesRequest.class)
)
).thenReturn(
new DescribeInstancesResult()
.withReservations(
Collections.singletonList(this.fakeReservation())
)
);
final List<Instance> instances = new InstanceList(
"blah",
this.fakeConfig(),
this.provider
).get();
Assertions.assertThat(instances).hasSize(1);
}
示例4: listsInstancesFromMultipleReservartions
import com.amazonaws.services.ec2.model.DescribeInstancesResult; //导入依赖的package包/类
/**
* InstanceList can list instances.
*/
@Test
public void listsInstancesFromMultipleReservartions() {
Mockito.when(
this.client.describeInstances(
Mockito.any(DescribeInstancesRequest.class)
)
).thenReturn(
new DescribeInstancesResult()
.withReservations(
Arrays.asList(
this.fakeReservation(),
this.fakeReservation(),
this.fakeReservation(),
this.fakeReservation()
)
)
);
final List<Instance> instances = new InstanceList(
"blah",
this.fakeConfig(),
this.provider
).get();
Assertions.assertThat(instances).hasSize(4);
}
示例5: getInstanceStateName
import com.amazonaws.services.ec2.model.DescribeInstancesResult; //导入依赖的package包/类
public String getInstanceStateName(InstanceRequest instanceRequest, Context context) {
AmazonEC2Async client = createEC2Client();
try {
DescribeInstancesResult result = client
.describeInstances(new DescribeInstancesRequest().withInstanceIds(instanceRequest.getInstanceId()));
List<Instance> instances = result.getReservations().get(0).getInstances();
if (instances.size() != 1) {
throw new RuntimeException("instance can not be found.");
}
return instances.get(0).getState().getName();
} finally {
client.shutdown();
}
}
示例6: getRemoteInstances
import com.amazonaws.services.ec2.model.DescribeInstancesResult; //导入依赖的package包/类
/**
* Get the instances from AWS filtered by the instances Ids known to the local system.
*/
public void getRemoteInstances(EnumerationDeletionContext aws,
AWSEnumerationDeletionSubStage next) {
if (aws.localInstanceIds == null || aws.localInstanceIds.size() == 0) {
logFine(() -> "No local records found. No states need to be fetched from the AWS"
+ " endpoint.");
aws.subStage = next;
deleteResourcesInLocalSystem(aws);
return;
}
DescribeInstancesRequest request = new DescribeInstancesRequest();
Filter runningInstanceFilter = getAWSNonTerminatedInstancesFilter();
request.getFilters().add(runningInstanceFilter);
// Get only the instances from the remote system for which a compute state exists in the
// local system.
logFine(() -> String.format("Fetching instance details for %d instances on the AWS"
+ " endpoint.", aws.localInstanceIds.keySet().size()));
request.getInstanceIds().addAll(new ArrayList<>(aws.localInstanceIds.keySet()));
AsyncHandler<DescribeInstancesRequest, DescribeInstancesResult> resultHandler =
new AWSEnumerationAsyncHandler(this, aws, next);
aws.amazonEC2Client.describeInstancesAsync(request,
resultHandler);
}
示例7: onSuccess
import com.amazonaws.services.ec2.model.DescribeInstancesResult; //导入依赖的package包/类
@Override
public void onSuccess(DescribeInstancesRequest request,
DescribeInstancesResult result) {
OperationContext.restoreOperationContext(this.opContext);
int totalNumberOfInstances = 0;
// Print the details of the instances discovered on the AWS endpoint
for (Reservation r : result.getReservations()) {
for (Instance i : r.getInstances()) {
++totalNumberOfInstances;
final int finalTotal1 = totalNumberOfInstances;
this.service.logFine(() -> String.format("%d=====Instance details %s =====",
finalTotal1, i.getInstanceId()));
this.aws.remoteInstanceIds.add(i.getInstanceId());
}
}
final int finalTotal2 = totalNumberOfInstances;
this.service.logFine(() -> String.format("Successfully enumerated %d instances on the"
+ " AWS host.", finalTotal2));
this.aws.subStage = this.next;
((AWSEnumerationAndDeletionAdapterService) this.service)
.deleteResourcesInLocalSystem(this.aws);
return;
}
示例8: getAwsInstancesByIds
import com.amazonaws.services.ec2.model.DescribeInstancesResult; //导入依赖的package包/类
/**
* Method to get Instance details directly from Amazon
*
* @throws Throwable
*/
public static List<Instance> getAwsInstancesByIds(AmazonEC2AsyncClient client,
VerificationHost host, List<String> instanceIds)
throws Throwable {
host.log("Getting instances with ids " + instanceIds
+ " from the AWS endpoint using the EC2 client.");
DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest()
.withInstanceIds(instanceIds);
DescribeInstancesResult describeInstancesResult = client
.describeInstances(describeInstancesRequest);
return describeInstancesResult.getReservations().stream()
.flatMap(r -> r.getInstances().stream()).collect(Collectors.toList());
}
示例9: getPublicDnsForAutoScalingGroup
import com.amazonaws.services.ec2.model.DescribeInstancesResult; //导入依赖的package包/类
/**
* For a given AutoScaling group logical id, get the public dns names associated with each instance.
*
* @param logicalId AutoScaling group logical id
* @return List of public dns names
*/
public List<String> getPublicDnsForAutoScalingGroup(final String logicalId) {
final List<String> instanceIds = Lists.newLinkedList();
final Optional<AutoScalingGroup> autoScalingGroup = describeAutoScalingGroup(logicalId);
final List<String> publicDnsNames = Lists.newLinkedList();
if (autoScalingGroup.isPresent()) {
autoScalingGroup.get()
.getInstances().stream().forEach(instance -> instanceIds.add(instance.getInstanceId()));
final DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest()
.withInstanceIds(instanceIds);
final DescribeInstancesResult describeInstancesResult =
ec2Client.describeInstances(describeInstancesRequest);
describeInstancesResult.getReservations().forEach(reservation ->
reservation.getInstances().stream().forEach(instance ->
publicDnsNames.add(instance.getPublicDnsName()))
);
}
return publicDnsNames;
}
示例10: getInstancesByTag
import com.amazonaws.services.ec2.model.DescribeInstancesResult; //导入依赖的package包/类
/**
* Gets all EC2 instances with the given tag key/value pair
* @param tagKey - Key of the tag
* @param tagValue - Value of the tag
* @param filters - Array of EC2 filters
* @return - List of instances with the given tag
*/
public List<Instance> getInstancesByTag(final String tagKey, final String tagValue, final Filter... filters) {
final String filterName = String.format(FILTER_NAME_TEMPL_FOR_EC2_TAGS, tagKey);
final Filter tagFilter = new Filter().withName(filterName).withValues(tagValue);
final Set<Filter> filterSet = Sets.newHashSet(filters);
filterSet.add(tagFilter);
final DescribeInstancesRequest request = new DescribeInstancesRequest().withFilters(filterSet);
DescribeInstancesResult result = ec2Client.describeInstances(request);
List<Instance> instances = Lists.newArrayList();
result.getReservations().forEach(reservation -> {
instances.addAll(reservation.getInstances());
});
return instances;
}
示例11: testGetInstancesByTagHappy
import com.amazonaws.services.ec2.model.DescribeInstancesResult; //导入依赖的package包/类
@Test
public void testGetInstancesByTagHappy() {
String tagKey = "tag key";
String tagValue = "tag value";
Filter filter = new Filter().withName(INSTANCE_STATE_FILTER_NAME).withValues(INSTANCE_STATE_RUNNING_FILTER_VALUE);
Instance instance = mock(Instance.class);
when(ec2Client.describeInstances(new DescribeInstancesRequest()
.withFilters(
filter,
new Filter()
.withName(String.format(FILTER_NAME_TEMPL_FOR_EC2_TAGS, tagKey))
.withValues(tagValue)
)
)).thenReturn(
new DescribeInstancesResult()
.withReservations(
new Reservation()
.withInstances(instance))
);
List<Instance> instances = ec2Service.getInstancesByTag(tagKey, tagValue, filter);
assertTrue(instances.contains(instance));
}
示例12: describeInstances
import com.amazonaws.services.ec2.model.DescribeInstancesResult; //导入依赖的package包/类
private void describeInstances(AmazonEC2Client ec2Client, Exchange exchange) {
Collection instanceIds;
DescribeInstancesRequest request = new DescribeInstancesRequest();
if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCES_IDS))) {
instanceIds = exchange.getIn().getHeader(EC2Constants.INSTANCES_IDS, Collection.class);
request.withInstanceIds(instanceIds);
}
DescribeInstancesResult result;
try {
result = ec2Client.describeInstances(request);
} catch (AmazonServiceException ase) {
LOG.trace("Describe Instances command returned the error code {}", ase.getErrorCode());
throw ase;
}
Message message = getMessageForResponse(exchange);
message.setBody(result);
}
示例13: forEachInstance
import com.amazonaws.services.ec2.model.DescribeInstancesResult; //导入依赖的package包/类
/**
* Iterates through the instances in the specified {@code DescribeInstancesResult}
* and calls the specified handler on each instance. This method will retrieve the
* follow-on {@code DescribeInstanceResult}s if the result holds a {@code nextToken}.
*
* @param result the {@code DescribeInstancesResult}
* @param instanceHandler the instance handler
*/
@VisibleForTesting
void forEachInstance(DescribeInstancesResult result, InstanceHandler instanceHandler) {
List<Reservation> reservations;
while (!(reservations = result.getReservations()).isEmpty()) {
for (Reservation reservation : reservations) {
for (Instance instance : reservation.getInstances()) {
LOG.debug("Calling instance handler with instance {}", instance);
instanceHandler.handle(instance);
}
}
if (result.getNextToken() != null) {
result = client.describeInstances(
new DescribeInstancesRequest().withNextToken(result.getNextToken()));
} else {
break;
}
}
}
示例14: describeInstances
import com.amazonaws.services.ec2.model.DescribeInstancesResult; //导入依赖的package包/类
/**
*
* @{inheritDoc
*/
@Override
public List<VMInformation> describeInstances(String... instanceIds) {
List<VMInformation> result = new ArrayList<VMInformation>();
try {
DescribeInstancesResult results = asynchEc2Client.describeInstances();
HashSet<String> ids = new HashSet<String>(Arrays.asList(instanceIds));
for (Reservation reservationDescription : results.getReservations()) {
for (com.amazonaws.services.ec2.model.Instance instance : reservationDescription.getInstances()) {
if (ids.contains(instance.getInstanceId())) {
result.add(new AmazonDataConverter().instanceToVmInformation(reservationDescription, instance, vmRegion));
}
}
// result.addAll(TypicaDataConverter.processReservationDescription(reservationDescription));
}
} catch (Exception e) {
LOG.error("Failed to retrieve instance from Amazon: " + e.getMessage());
throw new RuntimeException(e);
}
return result;
}
示例15: findInstancesOfType
import com.amazonaws.services.ec2.model.DescribeInstancesResult; //导入依赖的package包/类
/**
* @{inheritDoc
*/
@Override
public List<VMInformation> findInstancesOfType(VMRegion region, VMImageType type) {
List<VMInformation> ret = new ArrayList<VMInformation>();
try {
DescribeInstancesResult instances = asynchEc2Client.describeInstances();
InstanceDescription instanceForRegionAndType = new TankConfig().getVmManagerConfig().getInstanceForRegionAndType(region, type);
for (Reservation res : instances.getReservations()) {
if (res.getInstances() != null) {
for (com.amazonaws.services.ec2.model.Instance inst : res.getInstances()) {
if ((inst.getState().getName().equalsIgnoreCase("running") || inst.getState().getName().equalsIgnoreCase("pending"))
&& inst.getImageId().equals(instanceForRegionAndType.getAmi())) {
ret.add(new AmazonDataConverter().instanceToVmInformation(res, inst, region));
}
}
}
}
} catch (Exception e) {
LOG.error("Error getting instances: " + e.toString(), e);
throw new RuntimeException(e);
}
return ret;
}