本文整理匯總了Java中com.amazonaws.services.ec2.AmazonEC2Client類的典型用法代碼示例。如果您正苦於以下問題:Java AmazonEC2Client類的具體用法?Java AmazonEC2Client怎麽用?Java AmazonEC2Client使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
AmazonEC2Client類屬於com.amazonaws.services.ec2包,在下文中一共展示了AmazonEC2Client類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: createSnapshot
import com.amazonaws.services.ec2.AmazonEC2Client; //導入依賴的package包/類
/**
* Creates a snapshot and return the snapshot id.
*/
public static String createSnapshot(VerificationHost host, AmazonEC2Client client, String volumeId) {
CreateSnapshotRequest req = new CreateSnapshotRequest()
.withVolumeId(volumeId);
CreateSnapshotResult res = client.createSnapshot(req);
String snapshotId = res.getSnapshot().getSnapshotId();
Filter filter = new Filter().withName(SNAPSHOT_ID_ATTRIBUTE).withValues(snapshotId);
DescribeSnapshotsRequest snapshotsRequest = new DescribeSnapshotsRequest()
.withSnapshotIds(snapshotId)
.withFilters(filter);
host.waitFor("Timeout waiting for creating snapshot", () -> {
DescribeSnapshotsResult snapshotsResult = client.describeSnapshots(snapshotsRequest);
String state = snapshotsResult.getSnapshots().get(0).getState();
if (state.equalsIgnoreCase(SNAPSHOT_STATUS_COMPLETE)) {
return true;
}
return false;
});
return snapshotId;
}
示例2: getLatestSpotPrices
import com.amazonaws.services.ec2.AmazonEC2Client; //導入依賴的package包/類
/**
* This implementation uses DescribeSpotPriceHistory API which returns the latest spot price history for the specified AZ and instance types. This method
* then filters the returned list to only contain the latest spot price for each instance type.
*/
@Override
public List<SpotPrice> getLatestSpotPrices(String availabilityZone, Collection<String> instanceTypes, Collection<String> productDescriptions,
AwsParamsDto awsParamsDto)
{
AmazonEC2Client ec2Client = getEc2Client(awsParamsDto);
DescribeSpotPriceHistoryRequest describeSpotPriceHistoryRequest = new DescribeSpotPriceHistoryRequest();
describeSpotPriceHistoryRequest.setAvailabilityZone(availabilityZone);
describeSpotPriceHistoryRequest.setInstanceTypes(instanceTypes);
describeSpotPriceHistoryRequest.setProductDescriptions(productDescriptions);
DescribeSpotPriceHistoryResult describeSpotPriceHistoryResult = ec2Operations.describeSpotPriceHistory(ec2Client, describeSpotPriceHistoryRequest);
List<SpotPrice> spotPrices = new ArrayList<>();
Set<String> instanceTypesFound = new HashSet<>();
for (SpotPrice spotPriceHistoryEntry : describeSpotPriceHistoryResult.getSpotPriceHistory())
{
if (instanceTypesFound.add(spotPriceHistoryEntry.getInstanceType()))
{
spotPrices.add(spotPriceHistoryEntry);
}
}
return spotPrices;
}
示例3: getInstancesMapForZone
import com.amazonaws.services.ec2.AmazonEC2Client; //導入依賴的package包/類
@Override
public Map<AvailabilityZone, List<Instance>> getInstancesMapForZone(
AvailabilityZone zone, AmazonEC2Client client) throws Exception {
OperationStats op = new OperationStats("ec2InstanceStore", "getInstancesMapForZone");
try {
Map<AvailabilityZone, List<Instance>> ret = new HashMap<>();
ret.put(zone, getInstancesForZone(zone, client));
op.succeed();
return ret;
} catch (Exception e) {
op.failed();
logger.error(ExceptionUtils.getRootCauseMessage(e));
throw e;
}
}
示例4: getReservedInstancesForZone
import com.amazonaws.services.ec2.AmazonEC2Client; //導入依賴的package包/類
@Override
public Map<AvailabilityZone, List<ReservedInstances>> getReservedInstancesForZone(
AvailabilityZone zone, AmazonEC2Client client) throws Exception {
OperationStats op = new OperationStats("ec2InstanceStore", "getReservedInstancesForZone");
try {
Map<AvailabilityZone, List<ReservedInstances>> ret = new HashMap<>();
DescribeReservedInstancesRequest request = new DescribeReservedInstancesRequest()
.withFilters(new Filter("availability-zone", Arrays.asList(zone.getZoneName())))
.withSdkClientExecutionTimeout(
600 * 1000) //10 minutes time out for total execution including retries
.withSdkRequestTimeout(300 * 1000); //5 minutes time out for a single request
DescribeReservedInstancesResult result = client.describeReservedInstances(request);
ret.put(zone, result.getReservedInstances());
op.succeed();
return ret;
} catch (Exception e) {
op.failed();
logger.error(ExceptionUtils.getRootCauseMessage(e));
throw e;
}
}
示例5: createVolume
import com.amazonaws.services.ec2.AmazonEC2Client; //導入依賴的package包/類
/**
* Creates a volume and return the volume id.
*/
public static String createVolume(VerificationHost host, AmazonEC2Client client) {
CreateVolumeRequest req = new CreateVolumeRequest()
.withAvailabilityZone(zoneId + avalabilityZoneIdentifier)
.withSize(1);
CreateVolumeResult res = client.createVolume(req);
String volumeId = res.getVolume().getVolumeId();
Filter filter = new Filter().withName(VOLUME_ID_ATTRIBUTE).withValues(volumeId);
DescribeVolumesRequest volumesRequest = new DescribeVolumesRequest()
.withVolumeIds(volumeId)
.withFilters(filter);
host.waitFor("Timeout waiting for creating volume", () -> {
DescribeVolumesResult volumesResult = client.describeVolumes(volumesRequest);
String state = volumesResult.getVolumes().get(0).getState();
if (state.equalsIgnoreCase(VOLUME_STATUS_AVAILABLE)) {
return true;
}
return false;
});
return volumeId;
}
示例6: addNICDirectlyWithEC2Client
import com.amazonaws.services.ec2.AmazonEC2Client; //導入依賴的package包/類
/**
* Attach a provided AWS NIC to a given AWS VM with deviceIndex = number of NICs + 1
* returns the attachment ID of the newly created and attached NIC. This is necessary for
* removing it later for the goals of the test. The NIC is as well configured to be deleted on
* instance termination for sanity purposes.
*/
public static String addNICDirectlyWithEC2Client(ComputeState vm, AmazonEC2Client client,
VerificationHost host, String newNicId) {
// attach the new AWS NIC to the AWS VM
AttachNetworkInterfaceRequest attachNewNic = new AttachNetworkInterfaceRequest()
.withInstanceId(vm.id)
.withDeviceIndex(vm.networkInterfaceLinks.size())
.withNetworkInterfaceId(newNicId);
AttachNetworkInterfaceResult attachmetnResult = client.attachNetworkInterface(attachNewNic);
String attachmentId = attachmetnResult.getAttachmentId();
// ensure the new NIC is deleted when the VM is terminated
NetworkInterfaceAttachmentChanges attachTerm = new NetworkInterfaceAttachmentChanges()
.withAttachmentId(attachmentId)
.withDeleteOnTermination(true);
ModifyNetworkInterfaceAttributeRequest setDeleteOnTerm = new ModifyNetworkInterfaceAttributeRequest()
.withAttachment(attachTerm)
.withNetworkInterfaceId(newNicId);
client.modifyNetworkInterfaceAttribute(setDeleteOnTerm);
host.log("Created new NIC with id: %s to vm id: %s with attachment id: %s", newNicId,
vm.id, attachmentId);
return attachmentId;
}
示例7: provisionAWSVMWithEC2Client
import com.amazonaws.services.ec2.AmazonEC2Client; //導入依賴的package包/類
public static String provisionAWSVMWithEC2Client(VerificationHost host, AmazonEC2Client client,
String ami, String subnetId, String securityGroupId) {
RunInstancesRequest runInstancesRequest = new RunInstancesRequest()
.withSubnetId(subnetId)
.withImageId(ami)
.withInstanceType(instanceType)
.withMinCount(1).withMaxCount(1)
.withSecurityGroupIds(securityGroupId);
// handler invoked once the EC2 runInstancesAsync commands completes
RunInstancesResult result = null;
try {
result = client.runInstances(runInstancesRequest);
} catch (Exception e) {
host.log(Level.SEVERE, "Error encountered in provisioning machine on AWS",
Utils.toString(e));
}
assertNotNull(result);
assertNotNull(result.getReservation());
assertNotNull(result.getReservation().getInstances());
assertEquals(1, result.getReservation().getInstances().size());
return result.getReservation().getInstances().get(0).getInstanceId();
}
示例8: provisionAWSEBSVMWithEC2Client
import com.amazonaws.services.ec2.AmazonEC2Client; //導入依賴的package包/類
public static String provisionAWSEBSVMWithEC2Client(VerificationHost host, AmazonEC2Client client,
String ami, String subnetId, String securityGroupId, BlockDeviceMapping blockDeviceMapping) {
RunInstancesRequest runInstancesRequest = new RunInstancesRequest()
.withSubnetId(subnetId)
.withImageId(ami)
.withInstanceType(instanceType)
.withMinCount(1).withMaxCount(1)
.withSecurityGroupIds(securityGroupId)
.withBlockDeviceMappings(blockDeviceMapping);
// handler invoked once the EC2 runInstancesAsync commands completes
RunInstancesResult result = null;
try {
result = client.runInstances(runInstancesRequest);
} catch (Exception e) {
host.log(Level.SEVERE, "Error encountered in provisioning machine on AWS",
Utils.toString(e));
}
assertNotNull(result);
assertNotNull(result.getReservation());
assertNotNull(result.getReservation().getInstances());
assertEquals(1, result.getReservation().getInstances().size());
return result.getReservation().getInstances().get(0).getInstanceId();
}
示例9: startInstances
import com.amazonaws.services.ec2.AmazonEC2Client; //導入依賴的package包/類
private void startInstances(AmazonEC2Client ec2Client, Exchange exchange) {
Collection instanceIds;
StartInstancesRequest request = new StartInstancesRequest();
if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCES_IDS))) {
instanceIds = exchange.getIn().getHeader(EC2Constants.INSTANCES_IDS, Collection.class);
request.withInstanceIds(instanceIds);
} else {
throw new IllegalArgumentException("Instances Ids must be specified");
}
StartInstancesResult result;
try {
result = ec2Client.startInstances(request);
} catch (AmazonServiceException ase) {
LOG.trace("Start Instances command returned the error code {}", ase.getErrorCode());
throw ase;
}
LOG.trace("Starting instances with Ids [{}] ", Arrays.toString(instanceIds.toArray()));
Message message = getMessageForResponse(exchange);
message.setBody(result);
}
示例10: stopInstances
import com.amazonaws.services.ec2.AmazonEC2Client; //導入依賴的package包/類
private void stopInstances(AmazonEC2Client ec2Client, Exchange exchange) {
Collection instanceIds;
StopInstancesRequest request = new StopInstancesRequest();
if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCES_IDS))) {
instanceIds = exchange.getIn().getHeader(EC2Constants.INSTANCES_IDS, Collection.class);
request.withInstanceIds(instanceIds);
} else {
throw new IllegalArgumentException("Instances Ids must be specified");
}
StopInstancesResult result;
try {
result = ec2Client.stopInstances(request);
} catch (AmazonServiceException ase) {
LOG.trace("Stop Instances command returned the error code {}", ase.getErrorCode());
throw ase;
}
LOG.trace("Stopping instances with Ids [{}] ", Arrays.toString(instanceIds.toArray()));
Message message = getMessageForResponse(exchange);
message.setBody(result);
}
示例11: terminateInstances
import com.amazonaws.services.ec2.AmazonEC2Client; //導入依賴的package包/類
private void terminateInstances(AmazonEC2Client ec2Client, Exchange exchange) {
Collection instanceIds;
TerminateInstancesRequest request = new TerminateInstancesRequest();
if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCES_IDS))) {
instanceIds = exchange.getIn().getHeader(EC2Constants.INSTANCES_IDS, Collection.class);
request.withInstanceIds(instanceIds);
} else {
throw new IllegalArgumentException("Instances Ids must be specified");
}
TerminateInstancesResult result;
try {
result = ec2Client.terminateInstances(request);
} catch (AmazonServiceException ase) {
LOG.trace("Terminate Instances command returned the error code {}", ase.getErrorCode());
throw ase;
}
LOG.trace("Terminating instances with Ids [{}] ", Arrays.toString(instanceIds.toArray()));
Message message = getMessageForResponse(exchange);
message.setBody(result);
}
示例12: describeInstances
import com.amazonaws.services.ec2.AmazonEC2Client; //導入依賴的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: describeInstancesStatus
import com.amazonaws.services.ec2.AmazonEC2Client; //導入依賴的package包/類
private void describeInstancesStatus(AmazonEC2Client ec2Client, Exchange exchange) {
Collection instanceIds;
DescribeInstanceStatusRequest request = new DescribeInstanceStatusRequest();
if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCES_IDS))) {
instanceIds = exchange.getIn().getHeader(EC2Constants.INSTANCES_IDS, Collection.class);
request.withInstanceIds(instanceIds);
}
DescribeInstanceStatusResult result;
try {
result = ec2Client.describeInstanceStatus(request);
} catch (AmazonServiceException ase) {
LOG.trace("Describe Instances Status command returned the error code {}", ase.getErrorCode());
throw ase;
}
Message message = getMessageForResponse(exchange);
message.setBody(result);
}
示例14: rebootInstances
import com.amazonaws.services.ec2.AmazonEC2Client; //導入依賴的package包/類
private void rebootInstances(AmazonEC2Client ec2Client, Exchange exchange) {
Collection instanceIds;
RebootInstancesRequest request = new RebootInstancesRequest();
if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCES_IDS))) {
instanceIds = exchange.getIn().getHeader(EC2Constants.INSTANCES_IDS, Collection.class);
request.withInstanceIds(instanceIds);
} else {
throw new IllegalArgumentException("Instances Ids must be specified");
}
try {
LOG.trace("Rebooting instances with Ids [{}] ", Arrays.toString(instanceIds.toArray()));
ec2Client.rebootInstances(request);
} catch (AmazonServiceException ase) {
LOG.trace("Reboot Instances command returned the error code {}", ase.getErrorCode());
throw ase;
}
}
示例15: monitorInstances
import com.amazonaws.services.ec2.AmazonEC2Client; //導入依賴的package包/類
private void monitorInstances(AmazonEC2Client ec2Client, Exchange exchange) {
Collection instanceIds;
MonitorInstancesRequest request = new MonitorInstancesRequest();
if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCES_IDS))) {
instanceIds = exchange.getIn().getHeader(EC2Constants.INSTANCES_IDS, Collection.class);
request.withInstanceIds(instanceIds);
} else {
throw new IllegalArgumentException("Instances Ids must be specified");
}
MonitorInstancesResult result;
try {
result = ec2Client.monitorInstances(request);
} catch (AmazonServiceException ase) {
LOG.trace("Monitor Instances command returned the error code {}", ase.getErrorCode());
throw ase;
}
LOG.trace("Start Monitoring instances with Ids [{}] ", Arrays.toString(instanceIds.toArray()));
Message message = getMessageForResponse(exchange);
message.setBody(result);
}