本文整理汇总了Java中com.amazonaws.services.ec2.model.CreateTagsRequest类的典型用法代码示例。如果您正苦于以下问题:Java CreateTagsRequest类的具体用法?Java CreateTagsRequest怎么用?Java CreateTagsRequest使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
CreateTagsRequest类属于com.amazonaws.services.ec2.model包,在下文中一共展示了CreateTagsRequest类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: setTagsForInstances
import com.amazonaws.services.ec2.model.CreateTagsRequest; //导入依赖的package包/类
/**
* Update tags for one all more instance
* @param instanceIds
* @param tags
* @throws Exception
*/
@Override
public void setTagsForInstances(List<String> instanceIds, List<Tag> tags) throws Exception {
Preconditions.checkNotNull(instanceIds);
Preconditions.checkNotNull(tags);
awsRateLimiter.acquire();
OperationStats op = new OperationStats("ec2InstanceStore", "setTagsForInstances");
try {
if (tags.size() > 0) {
CreateTagsRequest req = new CreateTagsRequest(instanceIds, tags);
defaultClient.createTags(req);
}
op.succeed();
} catch (Exception ex) {
op.failed();
throw ex;
}
}
示例2: attachSnapshotTags
import com.amazonaws.services.ec2.model.CreateTagsRequest; //导入依赖的package包/类
void attachSnapshotTags(AmazonEC2Async client, String sourceSnapshotId, String snapshotId) {
DescribeSnapshotsResult result = client
.describeSnapshots(new DescribeSnapshotsRequest().withSnapshotIds(sourceSnapshotId));
List<Snapshot> snapshots = result.getSnapshots();
if (snapshots.size() != 1) {
throw new RuntimeException("snapshot can not found. sourceSnapshotId[" + snapshotId + "]");
}
List<Tag> sourceSnapshotTags = snapshots.get(0).getTags();
List<Tag> tags = new ArrayList<Tag>();
tags.addAll(sourceSnapshotTags);
tags.add(new Tag("SourceSnapshotId", sourceSnapshotId));
tags.add(new Tag("BackupType", "copy-snapshot")); // overwrite
CreateTagsRequest snapshotTagsRequest = new CreateTagsRequest().withResources(snapshotId);
snapshotTagsRequest.setTags(tags);
client.createTags(snapshotTagsRequest);
}
示例3: createNameTagAsync
import com.amazonaws.services.ec2.model.CreateTagsRequest; //导入依赖的package包/类
public DeferredResult<Void> createNameTagAsync(String resourceId, String name) {
Tag nameTag = new Tag().withKey(AWS_TAG_NAME).withValue(name);
CreateTagsRequest request = new CreateTagsRequest()
.withResources(resourceId)
.withTags(nameTag);
String message = "Name tag AWS resource with id [" + resourceId + "] with name ["
+ name + "].";
AWSDeferredResultAsyncHandler<CreateTagsRequest, CreateTagsResult> handler =
new AWSDeferredResultAsyncHandler<>(this.service, message);
this.client.createTagsAsync(request, handler);
return handler.toDeferredResult()
.thenApply(result -> (Void) null);
}
示例4: tagSpotInstance
import com.amazonaws.services.ec2.model.CreateTagsRequest; //导入依赖的package包/类
/**
* Tags an EC2 instance. Expects that the instance already exists or is in the process of
* being created. This may also tag EBS volumes depending on template configurations.
*
* @param template the instance template
* @param userDefinedTags the user-defined tags
* @param virtualInstanceId the virtual instance id
* @param ec2InstanceId the EC2 instance id
* @return true if the instance was successfully tagged, false otherwise
* @throws InterruptedException if the operation is interrupted
*/
private boolean tagSpotInstance(EC2InstanceTemplate template, List<Tag> userDefinedTags,
String virtualInstanceId, String ec2InstanceId) throws InterruptedException {
LOG.info(">> Tagging instance {} / {}", ec2InstanceId, virtualInstanceId);
// We have to individually tag the spot instance and it's associated volumes
// since AWS doesn't allow specifying tags as part of the launch request for
// spot instances.
// Wait for the instance to be started. If it is terminating, skip tagging.
if (!waitUntilInstanceHasStarted(ec2InstanceId)) {
return false;
}
List<Tag> tags = ec2TagHelper.getInstanceTags(template, virtualInstanceId, userDefinedTags);
client.createTags(new CreateTagsRequest().withTags(tags).withResources(ec2InstanceId));
// Tag EBS volumes if they were part of instance launch request
if (EBSAllocationStrategy.get(template) == EBSAllocationStrategy.AS_INSTANCE_REQUEST) {
tagSpotEbsVolumes(ec2InstanceId, virtualInstanceId, tags);
}
return true;
}
示例5: runInstances
import com.amazonaws.services.ec2.model.CreateTagsRequest; //导入依赖的package包/类
public List<Instance> runInstances(RunInstancesRequest request, Tag... tags) throws Exception {
logger.info("create ec2 instance, request={}", request);
RunInstancesResult result = new Runner<RunInstancesResult>()
.maxAttempts(3)
.retryInterval(Duration.ofSeconds(20))
.retryOn(this::retryOnRunInstance)
.run(() -> ec2.runInstances(request));
Threads.sleepRoughly(Duration.ofSeconds(5)); // wait little bit to make sure instance is visible to tag service
List<String> instanceIds = result.getReservation().getInstances().stream().map(Instance::getInstanceId).collect(Collectors.toList());
CreateTagsRequest tagsRequest = new CreateTagsRequest()
.withResources(instanceIds)
.withTags(tags);
createTags(tagsRequest);
waitUntilRunning(instanceIds);
return describeInstances(instanceIds);
}
示例6: createAMI
import com.amazonaws.services.ec2.model.CreateTagsRequest; //导入依赖的package包/类
private String createAMI(Context context, String instanceId) throws Exception {
AWS.ec2.stopInstances(Lists.newArrayList(instanceId));
logger.info("create AMI, instanceId={}, imageName={}", instanceId, resource.name());
CreateImageResult result = AWS.ec2.ec2.createImage(new CreateImageRequest(instanceId, resource.name()));
String imageId = result.getImageId();
AWS.ec2.createTags(new CreateTagsRequest()
.withResources(imageId)
.withTags(tagHelper.env(),
tagHelper.resourceId(resource.id),
tagHelper.version(resource.nextVersion()),
tagHelper.name(resource.id + ":" + resource.nextVersion())));
String key = "ami/" + resource.id;
context.output(key, String.format("imageId=%s", imageId));
logger.info("result imageId => {}", imageId);
return imageId;
}
示例7: createSG
import com.amazonaws.services.ec2.model.CreateTagsRequest; //导入依赖的package包/类
private String createSG(Environment env) throws Exception {
String sgName = env.name + ":" + resourceId;
CreateSecurityGroupRequest request = new CreateSecurityGroupRequest(sgName, sgName);
if (bakeSubnet != null) request.setVpcId(bakeSubnet.getVpcId());
String sgId = AWS.ec2.createSecurityGroup(request).getGroupId();
AWS.ec2.createSGIngressRules(sgId, Lists.newArrayList(new IpPermission()
.withIpv4Ranges(new IpRange().withCidrIp("0.0.0.0/0"))
.withFromPort(22)
.withToPort(22)
.withIpProtocol("tcp")));
AWS.ec2.createTags(new CreateTagsRequest()
.withResources(sgId)
.withTags(tagHelper.name(resourceId), tagHelper.env(), tagHelper.resourceId(resourceId)));
return sgId;
}
示例8: execute
import com.amazonaws.services.ec2.model.CreateTagsRequest; //导入依赖的package包/类
@Override
public void execute(Context context) throws Exception {
logger.info("create route table, routeTable={}", resource.id);
resource.remoteRouteTable = AWS.vpc.ec2.createRouteTable(new CreateRouteTableRequest().withVpcId(resource.vpc.remoteVPC.getVpcId())).getRouteTable();
if (resource.internetGateway != null) {
AWS.vpc.ec2.createRoute(new CreateRouteRequest()
.withRouteTableId(resource.remoteRouteTable.getRouteTableId())
.withGatewayId(resource.internetGateway.remoteInternetGatewayId)
.withDestinationCidrBlock("0.0.0.0/0"));
} else {
AWS.vpc.ec2.createRoute(new CreateRouteRequest()
.withRouteTableId(resource.remoteRouteTable.getRouteTableId())
.withNatGatewayId(resource.nat.remoteNATGateway.getNatGatewayId())
.withDestinationCidrBlock("0.0.0.0/0"));
}
EC2TagHelper tagHelper = new EC2TagHelper(context.env);
AWS.ec2.createTags(new CreateTagsRequest()
.withResources(resource.remoteRouteTable.getRouteTableId())
.withTags(tagHelper.env(), tagHelper.name(resource.id), tagHelper.resourceId(resource.id)));
}
示例9: tagEnvironmentWithVersion
import com.amazonaws.services.ec2.model.CreateTagsRequest; //导入依赖的package包/类
@Override
public boolean tagEnvironmentWithVersion(Region region, DeployJobVariables jobVariables) {
String searchTag = jobVariables.getEnvironment();
String version = jobVariables.getVersion();
LOGGER.info("tagEnvironmentWithVersion " + region + " Tag " + searchTag + " version " + version);
boolean environmentSuccessfulTagged = false;
ec2.setRegion(region);
DescribeInstancesResult instances = ec2.describeInstances();
for (Reservation reservation : instances.getReservations()) {
for (Instance instance : reservation.getInstances()) {
for (Tag tag : instance.getTags()) {
if (tag.getValue().equalsIgnoreCase(searchTag)) {
CreateTagsRequest createTagsRequest = new CreateTagsRequest();
createTagsRequest.withResources(instance.getInstanceId()).withTags(new Tag(VERSION_TAG, version));
LOGGER.info("Create Tag " + version + " for instance " + instance.getInstanceId());
ec2.createTags(createTagsRequest);
environmentSuccessfulTagged = true;
}
}
}
}
return environmentSuccessfulTagged;
}
示例10: setTagsForInstance
import com.amazonaws.services.ec2.model.CreateTagsRequest; //导入依赖的package包/类
/**
* Sets tags for the specified instance
* @param instanceId
* @return
*/
private void setTagsForInstance(String instanceId) {
Set<Object> keys = awsProperties.keySet();
List<Tag> tags = new ArrayList<>();
for(Object o : keys) {
if(o instanceof String && ((String)o).startsWith("tag")) {
String values = (String)awsProperties.get(o);
String[] splitValues = values.split(",");
String key = splitValues[0];
String value = splitValues[1];
Tag tagToAdd = new Tag(key,value);
log.info("Adding tag: " + tagToAdd);
tags.add(tagToAdd);
}
}
// Including a hard coded tag here so we can track which resources originate from this plugin
Tag nodeTag = new Tag("LaunchSource","SeleniumGridScalerPlugin");
log.info("Adding hard-coded tag: " + nodeTag);
tags.add(nodeTag);
CreateTagsRequest ctr = new CreateTagsRequest(Arrays.asList(instanceId),tags);
ec2Client.createTags(ctr);
}
示例11: tagInstance
import com.amazonaws.services.ec2.model.CreateTagsRequest; //导入依赖的package包/类
public void tagInstance(String instanceId, String tag, String value, AmazonEC2 ec2Client) {
System.out.println(instanceId);
//quick fix
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// swallow
}
CreateTagsRequest request = new CreateTagsRequest();
request = request.withResources(instanceId)
.withTags(new Tag(tag, value));
ec2Client.createTags(request);
}
示例12: createTags
import com.amazonaws.services.ec2.model.CreateTagsRequest; //导入依赖的package包/类
private void createTags(PropertyHandler ph) throws APPlatformException {
List<Tag> tags = new ArrayList<Tag>();
tags.add(new Tag(PropertyHandler.TAG_NAME, ph.getInstanceName()));
tags.add(new Tag(PropertyHandler.TAG_SUBSCRIPTION_ID, ph.getSettings()
.getSubscriptionId()));
tags.add(new Tag(PropertyHandler.TAG_ORGANIZATION_ID, ph.getSettings()
.getOrganizationId()));
CreateTagsRequest ctr = new CreateTagsRequest();
LOGGER.debug("attach tags to resource " + ph.getAWSInstanceId());
ctr.withResources(ph.getAWSInstanceId()).setTags(tags);
getEC2().createTags(ctr);
}
示例13: testCreateInstance
import com.amazonaws.services.ec2.model.CreateTagsRequest; //导入依赖的package包/类
@Test
public void testCreateInstance() throws Exception {
ec2mock.createRunInstancesResult("instance1");
ec2mock.createDescribeImagesResult("image1");
ec2mock.createDescribeSubnetsResult("subnet-a77430d0");
ec2mock.createDescribeSecurityGroupResult("subnet-a77430d0",
"security_group1,security_group2");
ec2mock.createDescribeInstancesResult("instance1", "ok", "1.2.3.4");
Image image = ec2comm.resolveAMI("image1");
ec2comm.createInstance(image);
String result = ph.getAWSInstanceId();
assertEquals("instance1", result);
ArgumentCaptor<RunInstancesRequest> arg1 = ArgumentCaptor
.forClass(RunInstancesRequest.class);
verify(ec2).runInstances(arg1.capture());
RunInstancesRequest rir = arg1.getValue();
assertEquals("image1", rir.getImageId());
assertEquals("type1", rir.getInstanceType());
assertEquals("key_pair", rir.getKeyName());
assertEquals(1, rir.getMinCount().intValue());
assertEquals(1, rir.getMaxCount().intValue());
ArgumentCaptor<CreateTagsRequest> arg2 = ArgumentCaptor
.forClass(CreateTagsRequest.class);
verify(ec2).createTags(arg2.capture());
CreateTagsRequest ctr = arg2.getValue();
for (Tag t : ctr.getTags()) {
if (t.getKey().equalsIgnoreCase("Name")) {
assertEquals("name1", t.getValue());
} else if (t.getKey().equalsIgnoreCase("SubscriptionId")) {
assertEquals("subId", t.getValue());
} else if (t.getKey().equalsIgnoreCase("OrganizationId")) {
assertEquals("orgId", t.getValue());
}
}
parameters.put("USERDATA_URL", new Setting("USERDATA_URL", "userdata"));
}
示例14: attachSnapshotTags
import com.amazonaws.services.ec2.model.CreateTagsRequest; //导入依赖的package包/类
void attachSnapshotTags(AmazonEC2Async client, String volumeId, String snapshotId) {
List<Tag> tags = new ArrayList<Tag>();
tags.add(new Tag("VolumeId", volumeId));
tags.add(new Tag("BackupType", "snapshot"));
CreateTagsRequest snapshotTagsRequest = new CreateTagsRequest().withResources(snapshotId);
snapshotTagsRequest.setTags(tags);
client.createTags(snapshotTagsRequest);
}
示例15: createImageTags
import com.amazonaws.services.ec2.model.CreateTagsRequest; //导入依赖的package包/类
void createImageTags(AmazonEC2Async client, ImageCreateRequest imageCreateRequest, Context context) {
try {
// LambdaLogger logger = context.getLogger();
String instanceId = imageCreateRequest.getInstanceId();
String imageId = imageCreateRequest.getImageId();
List<Tag> tags = new ArrayList<Tag>();
tags.add(new Tag("InstanceId", instanceId));
tags.add(new Tag("BackupType", "image"));
String requestDate = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmm"));
tags.add(new Tag("RequestDate", requestDate));
// Tag to AMI
CreateTagsRequest createTagsRequest = new CreateTagsRequest().withResources(imageId);
createTagsRequest.setTags(tags);
Future<CreateTagsResult> amiTagsResult = client.createTagsAsync(createTagsRequest);
while (!amiTagsResult.isDone()) {
Thread.sleep(100);
}
// Tag to EBS Snapshot
tags.add(new Tag("ImageId", imageId)); // snapshotにはimageIdを付けておく。
List<String> snapshotIds = getSnapshotIdsFromImageId(client, imageCreateRequest, context);
CreateTagsRequest snapshotTagsRequest = new CreateTagsRequest().withResources(snapshotIds);
snapshotTagsRequest.setTags(tags);
Future<CreateTagsResult> snapshotTagsResult = client.createTagsAsync(snapshotTagsRequest);
while (!snapshotTagsResult.isDone()) {
Thread.sleep(100);
}
} catch (Exception e) {
context.getLogger().log("[ERROR][ImageStateCheckAndParge] message[" + e.getMessage() + "] stackTrace["
+ getStackTrace(e) + "] [" + imageCreateRequest + "]");
}
}
开发者ID:uzresk,项目名称:aws-auto-operations-using-lambda,代码行数:39,代码来源:ImageStateCheckAndPargeFunction.java