com.amazonaws.services.ec2.model.CreateTagsRequest Java Examples

The following examples show how to use com.amazonaws.services.ec2.model.CreateTagsRequest. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ResourceTaggingManager.java    From pacbot with Apache License 2.0 6 votes vote down vote up
/**
 *
 * @param resourceId
 * @param clientMap
 * @param pacTag
 * @return
 */
private Boolean setEFSTag(final String resourceId,
        final Map<String, Object> clientMap, Map<String, String> pacTag) {
    com.amazonaws.services.elasticfilesystem.model.Tag tag = new com.amazonaws.services.elasticfilesystem.model.Tag();
    for (Map.Entry<String, String> tags : pacTag.entrySet()) {
        tag.setKey(tags.getKey());
        tag.setValue(tags.getValue());
    }
    AmazonElasticFileSystem fileSystem = (AmazonElasticFileSystem) clientMap
            .get("client");
    com.amazonaws.services.elasticfilesystem.model.CreateTagsRequest createTagsRequest = new com.amazonaws.services.elasticfilesystem.model.CreateTagsRequest();
    createTagsRequest.setFileSystemId(resourceId);
    createTagsRequest.setTags(Arrays.asList(tag));
    try {
        fileSystem.createTags(createTagsRequest);
        return Boolean.TRUE;
    } catch (AmazonServiceException ase) {
        logger.error("error tagging efs - > " + resourceId, ase);
        throw ase;
    }
}
 
Example #2
Source File: ResourceTaggingManager.java    From pacbot with Apache License 2.0 6 votes vote down vote up
/**
 *
 * @param resourceId
 * @param clientMap
 * @param pacTag
 * @return
 */
private Boolean setRedshiftTag(final String resourceId,
        final Map<String, Object> clientMap, Map<String, String> pacTag) {
   com.amazonaws.services.redshift.model.Tag tag = new com.amazonaws.services.redshift.model.Tag();
    for (Map.Entry<String, String> tags : pacTag.entrySet()) {
        tag.setKey(tags.getKey());
        tag.setValue(tags.getValue());
    }
    AmazonRedshift amazonRedshift = (AmazonRedshift) clientMap
            .get("client");
    com.amazonaws.services.redshift.model.CreateTagsRequest createTagsRequest = new com.amazonaws.services.redshift.model.CreateTagsRequest();
    createTagsRequest.setResourceName(resourceId);
    createTagsRequest.setTags(Arrays.asList(tag));
    try {
        amazonRedshift.createTags(createTagsRequest);
        return Boolean.TRUE;
    } catch (AmazonServiceException ase) {
        logger.error("error tagging redshift - > " + resourceId, ase);
        throw ase;
    }
}
 
Example #3
Source File: Ec2InstanceStore.java    From soundwave with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
  }
}
 
Example #4
Source File: AwsTagReporter.java    From SeleniumGridScaler with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example #5
Source File: EC2Connector.java    From jenkins-deployment-dashboard-plugin with MIT License 6 votes vote down vote up
@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;
}
 
Example #6
Source File: VpcImpl.java    From aws-sdk-java-resources with Apache License 2.0 5 votes vote down vote up
@Override
public List<com.amazonaws.resources.ec2.Tag> createTags(CreateTagsRequest
        request, ResultCapture<Void> extractor) {

    ActionResult result = resource.performAction("CreateTags", request,
            extractor);

    if (result == null) return null;
    return CodecUtils.transform(result.getResources(), TagImpl.CODEC);
}
 
Example #7
Source File: VpcImpl.java    From aws-sdk-java-resources with Apache License 2.0 5 votes vote down vote up
@Override
public List<com.amazonaws.resources.ec2.Tag> createTags(List<Tag> tags,
        ResultCapture<Void> extractor) {

    CreateTagsRequest request = new CreateTagsRequest()
        .withTags(tags);
    return createTags(request, extractor);
}
 
Example #8
Source File: AwsTaggingServiceTest.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
@Test
public void tesTagRootVolumesForSingleInstance() {
    CloudResource instance = CloudResource.builder()
            .type(ResourceType.AWS_INSTANCE).instanceId(INSTANCE_ID).name(INSTANCE_ID).status(CommonStatus.CREATED).build();

    DescribeInstancesResult describeResult = new DescribeInstancesResult()
            .withReservations(new Reservation()
                    .withInstances(new Instance()
                            .withInstanceId(INSTANCE_ID)
                            .withBlockDeviceMappings(new InstanceBlockDeviceMapping()
                                    .withDeviceName("/dev/sda1")
                                    .withEbs(new EbsInstanceBlockDevice().withVolumeId(VOLUME_ID)))
                            .withRootDeviceName("/dev/sda1"))
            );

    AmazonEC2Client ec2Client = mock(AmazonEC2Client.class);
    when(ec2Client.describeInstances(any())).thenReturn(describeResult);
    Map<String, String> userTags = Map.of("key1", "val1", "key2", "val2");

    awsTaggingService.tagRootVolumes(authenticatedContext(), ec2Client, List.of(instance), userTags);

    verify(ec2Client, times(1)).createTags(tagRequestCaptor.capture());
    CreateTagsRequest request = tagRequestCaptor.getValue();
    assertEquals(1, request.getResources().size());
    assertEquals(VOLUME_ID, request.getResources().get(0));
    List<com.amazonaws.services.ec2.model.Tag> tags = request.getTags();
    assertThat(tags, containsInAnyOrder(
            hasProperty("key", Matchers.is("key1")),
            hasProperty("key", Matchers.is("key2"))
    ));
    assertThat(tags, containsInAnyOrder(
            hasProperty("value", Matchers.is("val1")),
            hasProperty("value", Matchers.is("val2"))
    ));
}
 
Example #9
Source File: InternetGatewayImpl.java    From aws-sdk-java-resources with Apache License 2.0 5 votes vote down vote up
@Override
public List<com.amazonaws.resources.ec2.Tag> createTags(List<Tag> tags,
        ResultCapture<Void> extractor) {

    CreateTagsRequest request = new CreateTagsRequest()
        .withTags(tags);
    return createTags(request, extractor);
}
 
Example #10
Source File: InternetGatewayImpl.java    From aws-sdk-java-resources with Apache License 2.0 5 votes vote down vote up
@Override
public List<com.amazonaws.resources.ec2.Tag> createTags(CreateTagsRequest
        request, ResultCapture<Void> extractor) {

    ActionResult result = resource.performAction("CreateTags", request,
            extractor);

    if (result == null) return null;
    return CodecUtils.transform(result.getResources(), TagImpl.CODEC);
}
 
Example #11
Source File: DhcpOptionsImpl.java    From aws-sdk-java-resources with Apache License 2.0 5 votes vote down vote up
@Override
public List<com.amazonaws.resources.ec2.Tag> createTags(List<Tag> tags,
        ResultCapture<Void> extractor) {

    CreateTagsRequest request = new CreateTagsRequest()
        .withTags(tags);
    return createTags(request, extractor);
}
 
Example #12
Source File: DhcpOptionsImpl.java    From aws-sdk-java-resources with Apache License 2.0 5 votes vote down vote up
@Override
public List<com.amazonaws.resources.ec2.Tag> createTags(CreateTagsRequest
        request, ResultCapture<Void> extractor) {

    ActionResult result = resource.performAction("CreateTags", request,
            extractor);

    if (result == null) return null;
    return CodecUtils.transform(result.getResources(), TagImpl.CODEC);
}
 
Example #13
Source File: SnapshotImpl.java    From aws-sdk-java-resources with Apache License 2.0 5 votes vote down vote up
@Override
public List<com.amazonaws.resources.ec2.Tag> createTags(List<Tag> tags,
        ResultCapture<Void> extractor) {

    CreateTagsRequest request = new CreateTagsRequest()
        .withTags(tags);
    return createTags(request, extractor);
}
 
Example #14
Source File: SnapshotImpl.java    From aws-sdk-java-resources with Apache License 2.0 5 votes vote down vote up
@Override
public List<com.amazonaws.resources.ec2.Tag> createTags(CreateTagsRequest
        request, ResultCapture<Void> extractor) {

    ActionResult result = resource.performAction("CreateTags", request,
            extractor);

    if (result == null) return null;
    return CodecUtils.transform(result.getResources(), TagImpl.CODEC);
}
 
Example #15
Source File: InstanceImpl.java    From aws-sdk-java-resources with Apache License 2.0 5 votes vote down vote up
@Override
public List<com.amazonaws.resources.ec2.Tag> createTags(CreateTagsRequest
        request, ResultCapture<Void> extractor) {

    ActionResult result = resource.performAction("CreateTags", request,
            extractor);

    if (result == null) return null;
    return CodecUtils.transform(result.getResources(), TagImpl.CODEC);
}
 
Example #16
Source File: NetworkInterfaceImpl.java    From aws-sdk-java-resources with Apache License 2.0 5 votes vote down vote up
@Override
public List<com.amazonaws.resources.ec2.Tag> createTags(CreateTagsRequest
        request, ResultCapture<Void> extractor) {

    ActionResult result = resource.performAction("CreateTags", request,
            extractor);

    if (result == null) return null;
    return CodecUtils.transform(result.getResources(), TagImpl.CODEC);
}
 
Example #17
Source File: NetworkInterfaceImpl.java    From aws-sdk-java-resources with Apache License 2.0 5 votes vote down vote up
@Override
public List<com.amazonaws.resources.ec2.Tag> createTags(List<Tag> tags,
        ResultCapture<Void> extractor) {

    CreateTagsRequest request = new CreateTagsRequest()
        .withTags(tags);
    return createTags(request, extractor);
}
 
Example #18
Source File: NetworkAclImpl.java    From aws-sdk-java-resources with Apache License 2.0 5 votes vote down vote up
@Override
public List<com.amazonaws.resources.ec2.Tag> createTags(CreateTagsRequest
        request, ResultCapture<Void> extractor) {

    ActionResult result = resource.performAction("CreateTags", request,
            extractor);

    if (result == null) return null;
    return CodecUtils.transform(result.getResources(), TagImpl.CODEC);
}
 
Example #19
Source File: AwsTaggingService.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
public void tagRootVolumes(AuthenticatedContext ac, AmazonEC2Client ec2Client, List<CloudResource> instanceResources, Map<String, String> userDefinedTags) {
    String stackName = ac.getCloudContext().getName();
    LOGGER.debug("Fetch AWS instances to collect all root volume ids for stack: {}", stackName);
    List<String> instanceIds = instanceResources.stream().map(CloudResource::getInstanceId).collect(Collectors.toList());
    DescribeInstancesResult describeInstancesResult = ec2Client.describeInstances(new DescribeInstancesRequest().withInstanceIds(instanceIds));

    List<Instance> instances = describeInstancesResult.getReservations().stream().flatMap(res -> res.getInstances().stream()).collect(Collectors.toList());
    List<String> rootVolumeIds = instances.stream()
            .map(this::getRootVolumeId)
            .filter(Optional::isPresent)
            .map(blockDeviceMapping -> blockDeviceMapping.get().getEbs().getVolumeId())
            .collect(Collectors.toList());

    int instanceCount = instances.size();
    int volumeCount = rootVolumeIds.size();
    if (instanceCount != volumeCount) {
        LOGGER.debug("Did not find all root volumes, instanceResources: {}, found root volumes: {} for stack: {}", instanceCount, volumeCount, stackName);
    } else {
        LOGGER.debug("Found all ({}) root volumes for stack: {}", volumeCount, stackName);
    }

    AtomicInteger counter = new AtomicInteger();
    Collection<List<String>> volumeIdChunks = rootVolumeIds.stream()
            .collect(Collectors.groupingBy(it -> counter.getAndIncrement() / MAX_RESOURCE_PER_REQUEST)).values();

    Collection<Tag> tags = prepareEc2Tags(ac, userDefinedTags);
    for (List<String> volumeIds : volumeIdChunks) {
        LOGGER.debug("Tag {} root volumes for stack: {}", volumeIds.size(), stackName);
        ec2Client.createTags(new CreateTagsRequest().withResources(volumeIds).withTags(tags));
    }
}
 
Example #20
Source File: VolumeImpl.java    From aws-sdk-java-resources with Apache License 2.0 5 votes vote down vote up
@Override
public List<com.amazonaws.resources.ec2.Tag> createTags(CreateTagsRequest
        request, ResultCapture<Void> extractor) {

    ActionResult result = resource.performAction("CreateTags", request,
            extractor);

    if (result == null) return null;
    return CodecUtils.transform(result.getResources(), TagImpl.CODEC);
}
 
Example #21
Source File: SubnetImpl.java    From aws-sdk-java-resources with Apache License 2.0 5 votes vote down vote up
@Override
public List<com.amazonaws.resources.ec2.Tag> createTags(List<Tag> tags,
        ResultCapture<Void> extractor) {

    CreateTagsRequest request = new CreateTagsRequest()
        .withTags(tags);
    return createTags(request, extractor);
}
 
Example #22
Source File: SubnetImpl.java    From aws-sdk-java-resources with Apache License 2.0 5 votes vote down vote up
@Override
public List<com.amazonaws.resources.ec2.Tag> createTags(CreateTagsRequest
        request, ResultCapture<Void> extractor) {

    ActionResult result = resource.performAction("CreateTags", request,
            extractor);

    if (result == null) return null;
    return CodecUtils.transform(result.getResources(), TagImpl.CODEC);
}
 
Example #23
Source File: RouteTableImpl.java    From aws-sdk-java-resources with Apache License 2.0 5 votes vote down vote up
@Override
public List<com.amazonaws.resources.ec2.Tag> createTags(List<Tag> tags,
        ResultCapture<Void> extractor) {

    CreateTagsRequest request = new CreateTagsRequest()
        .withTags(tags);
    return createTags(request, extractor);
}
 
Example #24
Source File: RouteTableImpl.java    From aws-sdk-java-resources with Apache License 2.0 5 votes vote down vote up
@Override
public List<com.amazonaws.resources.ec2.Tag> createTags(CreateTagsRequest
        request, ResultCapture<Void> extractor) {

    ActionResult result = resource.performAction("CreateTags", request,
            extractor);

    if (result == null) return null;
    return CodecUtils.transform(result.getResources(), TagImpl.CODEC);
}
 
Example #25
Source File: SecurityGroupImpl.java    From aws-sdk-java-resources with Apache License 2.0 5 votes vote down vote up
@Override
public List<com.amazonaws.resources.ec2.Tag> createTags(List<Tag> tags,
        ResultCapture<Void> extractor) {

    CreateTagsRequest request = new CreateTagsRequest()
        .withTags(tags);
    return createTags(request, extractor);
}
 
Example #26
Source File: SecurityGroupImpl.java    From aws-sdk-java-resources with Apache License 2.0 5 votes vote down vote up
@Override
public List<com.amazonaws.resources.ec2.Tag> createTags(CreateTagsRequest
        request, ResultCapture<Void> extractor) {

    ActionResult result = resource.performAction("CreateTags", request,
            extractor);

    if (result == null) return null;
    return CodecUtils.transform(result.getResources(), TagImpl.CODEC);
}
 
Example #27
Source File: ImageImpl.java    From aws-sdk-java-resources with Apache License 2.0 5 votes vote down vote up
@Override
public List<com.amazonaws.resources.ec2.Tag> createTags(List<Tag> tags,
        ResultCapture<Void> extractor) {

    CreateTagsRequest request = new CreateTagsRequest()
        .withTags(tags);
    return createTags(request, extractor);
}
 
Example #28
Source File: ImageImpl.java    From aws-sdk-java-resources with Apache License 2.0 5 votes vote down vote up
@Override
public List<com.amazonaws.resources.ec2.Tag> createTags(CreateTagsRequest
        request, ResultCapture<Void> extractor) {

    ActionResult result = resource.performAction("CreateTags", request,
            extractor);

    if (result == null) return null;
    return CodecUtils.transform(result.getResources(), TagImpl.CODEC);
}
 
Example #29
Source File: NetworkAclImpl.java    From aws-sdk-java-resources with Apache License 2.0 5 votes vote down vote up
@Override
public List<com.amazonaws.resources.ec2.Tag> createTags(List<Tag> tags,
        ResultCapture<Void> extractor) {

    CreateTagsRequest request = new CreateTagsRequest()
        .withTags(tags);
    return createTags(request, extractor);
}
 
Example #30
Source File: ResourceTaggingManager.java    From pacbot with Apache License 2.0 5 votes vote down vote up
/**
*
* @param resourceId
* @param clientMap
* @param pacTag
* @return
*/
   private Boolean setEC2VolumeTag(final String resourceId, final Map<String, Object> clientMap,
           Map<String, String> pacTag) {
       AmazonEC2 ec2Client = (AmazonEC2) clientMap.get("client");
       CreateTagsRequest createTagsRequest = new CreateTagsRequest(Arrays.asList(resourceId), new ArrayList<>());
       createTagsRequest.setTags(pacTag.entrySet().stream().map(t -> new Tag(t.getKey(), t.getValue()))
               .collect(Collectors.toList()));
       try {
           ec2Client.createTags(createTagsRequest);
           return Boolean.TRUE;
       } catch (AmazonServiceException ase) {
           logger.error("error tagging ec2 - > " + resourceId, ase);
           throw ase;
       }
   }