本文整理汇总了Java中com.amazonaws.services.ec2.model.RunInstancesRequest.withPlacement方法的典型用法代码示例。如果您正苦于以下问题:Java RunInstancesRequest.withPlacement方法的具体用法?Java RunInstancesRequest.withPlacement怎么用?Java RunInstancesRequest.withPlacement使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.amazonaws.services.ec2.model.RunInstancesRequest
的用法示例。
在下文中一共展示了RunInstancesRequest.withPlacement方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createAndRunInstance
import com.amazonaws.services.ec2.model.RunInstancesRequest; //导入方法依赖的package包/类
private void createAndRunInstance(AmazonEC2Client ec2Client, Exchange exchange) {
String ami;
InstanceType instanceType;
int minCount;
int maxCount;
boolean monitoring;
String kernelId;
boolean ebsOptimized;
Collection securityGroups;
String keyName;
String clientToken;
Placement placement;
RunInstancesRequest request = new RunInstancesRequest();
if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.IMAGE_ID))) {
ami = exchange.getIn().getHeader(EC2Constants.IMAGE_ID, String.class);
request.withImageId(ami);
} else {
throw new IllegalArgumentException("AMI must be specified");
}
if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCE_TYPE))) {
instanceType = exchange.getIn().getHeader(EC2Constants.INSTANCE_TYPE, InstanceType.class);
request.withInstanceType(instanceType.toString());
} else {
throw new IllegalArgumentException("Instance Type must be specified");
}
if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCE_MIN_COUNT))) {
minCount = exchange.getIn().getHeader(EC2Constants.INSTANCE_MIN_COUNT, Integer.class);
request.withMinCount(minCount);
} else {
throw new IllegalArgumentException("Min instances count must be specified");
}
if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCE_MAX_COUNT))) {
maxCount = exchange.getIn().getHeader(EC2Constants.INSTANCE_MAX_COUNT, Integer.class);
request.withMaxCount(maxCount);
} else {
throw new IllegalArgumentException("Max instances count must be specified");
}
if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCE_MONITORING))) {
monitoring = exchange.getIn().getHeader(EC2Constants.INSTANCE_MONITORING, Boolean.class);
request.withMonitoring(monitoring);
}
if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCE_KERNEL_ID))) {
kernelId = exchange.getIn().getHeader(EC2Constants.INSTANCE_KERNEL_ID, String.class);
request.withKernelId(kernelId);
}
if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCE_EBS_OPTIMIZED))) {
ebsOptimized = exchange.getIn().getHeader(EC2Constants.INSTANCE_EBS_OPTIMIZED, Boolean.class);
request.withEbsOptimized(ebsOptimized);
}
if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCE_SECURITY_GROUPS))) {
securityGroups = exchange.getIn().getHeader(EC2Constants.INSTANCE_SECURITY_GROUPS, Collection.class);
request.withSecurityGroups(securityGroups);
}
if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCES_KEY_PAIR))) {
keyName = exchange.getIn().getHeader(EC2Constants.INSTANCES_KEY_PAIR, String.class);
request.withKeyName(keyName);
}
if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCES_CLIENT_TOKEN))) {
clientToken = exchange.getIn().getHeader(EC2Constants.INSTANCES_CLIENT_TOKEN, String.class);
request.withClientToken(clientToken);
}
if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCES_PLACEMENT))) {
placement = exchange.getIn().getHeader(EC2Constants.INSTANCES_PLACEMENT, Placement.class);
request.withPlacement(placement);
}
RunInstancesResult result;
try {
result = ec2Client.runInstances(request);
} catch (AmazonServiceException ase) {
LOG.trace("Run Instances command returned the error code {}", ase.getErrorCode());
throw ase;
}
LOG.trace("Creating and running instances with ami [{}] and instance type {}", ami, instanceType.toString());
Message message = getMessageForResponse(exchange);
message.setBody(result);
}
示例2: newRunInstancesRequest
import com.amazonaws.services.ec2.model.RunInstancesRequest; //导入方法依赖的package包/类
/**
* Builds a {@code RunInstancesRequest} starting from a template and a virtual instance ID.
* Instances will be tagged as they're created.
*
* @param template the instance template
* @param virtualInstanceId the virtual instance IDs
* @param userDefinedTags user defined tags to attach to the instance
*/
@SuppressWarnings("ConstantConditions")
private RunInstancesRequest newRunInstancesRequest(EC2InstanceTemplate template,
String virtualInstanceId, List<Tag> userDefinedTags) {
String image = template.getImage();
String type = template.getType();
InstanceNetworkInterfaceSpecification network =
getInstanceNetworkInterfaceSpecification(template);
List<BlockDeviceMapping> deviceMappings = getBlockDeviceMappings(template);
LOG.info(">> Instance request type: {}, image: {}", type, image);
List<Tag> tags = ec2TagHelper.getInstanceTags(template, virtualInstanceId, userDefinedTags);
List<TagSpecification> tagSpecifications = Lists.newArrayList(
new TagSpecification().withTags(tags).withResourceType(ResourceType.Instance),
new TagSpecification().withTags(tags).withResourceType(ResourceType.Volume));
RunInstancesRequest request = new RunInstancesRequest()
.withImageId(image)
.withInstanceType(type)
.withMaxCount(1)
.withMinCount(1)
.withClientToken(UUID.randomUUID().toString())
.withNetworkInterfaces(network)
.withTagSpecifications(tagSpecifications)
.withBlockDeviceMappings(deviceMappings)
.withEbsOptimized(template.isEbsOptimized());
if (template.getIamProfileName().isPresent()) {
request.withIamInstanceProfile(new IamInstanceProfileSpecification()
.withName(template.getIamProfileName().get()));
}
if (template.getKeyName().isPresent()) {
request.withKeyName(template.getKeyName().get());
}
Placement placement = null;
if (template.getAvailabilityZone().isPresent()) {
placement = new Placement().withAvailabilityZone(template.getAvailabilityZone().get());
}
if (template.getPlacementGroup().isPresent()) {
placement = (placement == null) ?
new Placement().withGroupName(template.getPlacementGroup().get())
: placement.withGroupName(template.getPlacementGroup().get());
}
placement = (placement == null) ?
new Placement().withTenancy(template.getTenancy())
: placement.withTenancy(template.getTenancy());
request.withPlacement(placement);
Optional<String> userData = template.getUserData();
if (userData.isPresent()) {
request.withUserData(userData.get());
}
return request;
}