Search in sources :

Example 36 with Tag

use of software.amazon.awssdk.services.ec2.model.Tag in project SimianArmy by Netflix.

the class VolumeTaggingMonkey method tagVolumesWithLatestAttachment.

private void tagVolumesWithLatestAttachment(AWSClient awsClient) {
    List<Volume> volumes = awsClient.describeVolumes();
    LOGGER.info(String.format("Trying to tag %d volumes for Janitor Monkey meta data.", volumes.size()));
    Date now = calendar.now().getTime();
    for (Volume volume : volumes) {
        String owner = null, instanceId = null;
        Date lastDetachTime = null;
        List<VolumeAttachment> attachments = volume.getAttachments();
        List<Tag> tags = volume.getTags();
        // by Janitor monkey.
        if ("donotmark".equals(getTagValue(JanitorMonkey.JANITOR_TAG, tags))) {
            LOGGER.info(String.format("The volume %s is tagged as not handled by Janitor", volume.getVolumeId()));
            continue;
        }
        Map<String, String> janitorMetadata = parseJanitorTag(tags);
        // finding the instance attached most recently.
        VolumeAttachment latest = null;
        for (VolumeAttachment attachment : attachments) {
            if (latest == null || latest.getAttachTime().before(attachment.getAttachTime())) {
                latest = attachment;
            }
        }
        if (latest != null) {
            instanceId = latest.getInstanceId();
            owner = getOwnerEmail(instanceId, janitorMetadata, tags, awsClient);
        }
        if (latest == null || "detached".equals(latest.getState())) {
            if (janitorMetadata.get(JanitorMonkey.DETACH_TIME_TAG_KEY) == null) {
                // There is no attached instance and the last detached time is not set.
                // Use the current time as the last detached time.
                LOGGER.info(String.format("Setting the last detached time to %s for volume %s", now, volume.getVolumeId()));
                lastDetachTime = now;
            } else {
                LOGGER.debug(String.format("The volume %s was already marked as detached at time %s", volume.getVolumeId(), janitorMetadata.get(JanitorMonkey.DETACH_TIME_TAG_KEY)));
            }
        } else {
            // The volume is currently attached to an instance
            lastDetachTime = null;
        }
        String existingOwner = janitorMetadata.get(BasicSimianArmyContext.GLOBAL_OWNER_TAGKEY);
        if (owner == null && existingOwner != null) {
            // Save the current owner in the tag when we are not able to find a owner.
            owner = existingOwner;
        }
        if (needsUpdate(janitorMetadata, owner, instanceId, lastDetachTime)) {
            Event evt = updateJanitorMetaTag(volume, instanceId, owner, lastDetachTime, awsClient);
            if (evt != null) {
                context().recorder().recordEvent(evt);
            }
        }
    }
}
Also used : VolumeAttachment(com.amazonaws.services.ec2.model.VolumeAttachment) Volume(com.amazonaws.services.ec2.model.Volume) Event(com.netflix.simianarmy.MonkeyRecorder.Event) Tag(com.amazonaws.services.ec2.model.Tag) Date(java.util.Date)

Example 37 with Tag

use of software.amazon.awssdk.services.ec2.model.Tag in project crate by crate.

the class AwsEc2SeedHostsProvider method fetchDynamicNodes.

private List<TransportAddress> fetchDynamicNodes() {
    final List<TransportAddress> dynamicHosts = new ArrayList<>();
    final DescribeInstancesResult descInstances;
    try (AmazonEc2Reference clientReference = awsEc2Service.client()) {
        // Query EC2 API based on AZ, instance state, and tag.
        // NOTE: we don't filter by security group during the describe instances request for two reasons:
        // 1. differences in VPCs require different parameters during query (ID vs Name)
        // 2. We want to use two different strategies: (all security groups vs. any security groups)
        descInstances = clientReference.client().describeInstances(buildDescribeInstancesRequest());
    } catch (final AmazonClientException e) {
        LOGGER.info("Exception while retrieving instance list from AWS API: {}", e.getMessage());
        LOGGER.debug("Full exception:", e);
        return dynamicHosts;
    }
    LOGGER.trace("finding seed nodes...");
    for (final Reservation reservation : descInstances.getReservations()) {
        for (final Instance instance : reservation.getInstances()) {
            // lets see if we can filter based on groups
            if (!groups.isEmpty()) {
                final List<GroupIdentifier> instanceSecurityGroups = instance.getSecurityGroups();
                final List<String> securityGroupNames = new ArrayList<>(instanceSecurityGroups.size());
                final List<String> securityGroupIds = new ArrayList<>(instanceSecurityGroups.size());
                for (final GroupIdentifier sg : instanceSecurityGroups) {
                    securityGroupNames.add(sg.getGroupName());
                    securityGroupIds.add(sg.getGroupId());
                }
                if (bindAnyGroup) {
                    // We check if we can find at least one group name or one group id in groups.
                    if (disjoint(securityGroupNames, groups) && disjoint(securityGroupIds, groups)) {
                        LOGGER.trace("filtering out instance {} based on groups {}, not part of {}", instance.getInstanceId(), instanceSecurityGroups, groups);
                        // continue to the next instance
                        continue;
                    }
                } else {
                    // We need tp match all group names or group ids, otherwise we ignore this instance
                    if (!(securityGroupNames.containsAll(groups) || securityGroupIds.containsAll(groups))) {
                        LOGGER.trace("filtering out instance {} based on groups {}, does not include all of {}", instance.getInstanceId(), instanceSecurityGroups, groups);
                        // continue to the next instance
                        continue;
                    }
                }
            }
            String address = null;
            if (hostType.equals(PRIVATE_DNS)) {
                address = instance.getPrivateDnsName();
            } else if (hostType.equals(PRIVATE_IP)) {
                address = instance.getPrivateIpAddress();
            } else if (hostType.equals(PUBLIC_DNS)) {
                address = instance.getPublicDnsName();
            } else if (hostType.equals(PUBLIC_IP)) {
                address = instance.getPublicIpAddress();
            } else if (hostType.startsWith(TAG_PREFIX)) {
                // Reading the node host from its metadata
                final String tagName = hostType.substring(TAG_PREFIX.length());
                LOGGER.debug("reading hostname from [{}] instance tag", tagName);
                final List<Tag> tags = instance.getTags();
                for (final Tag tag : tags) {
                    if (tag.getKey().equals(tagName)) {
                        address = tag.getValue();
                        LOGGER.debug("using [{}] as the instance address", address);
                    }
                }
            } else {
                throw new IllegalArgumentException(hostType + " is unknown for discovery.ec2.host_type");
            }
            if (address != null) {
                try {
                    final TransportAddress[] addresses = transportService.addressesFromString(address);
                    for (int i = 0; i < addresses.length; i++) {
                        LOGGER.trace("adding {}, address {}, transport_address {}", instance.getInstanceId(), address, addresses[i]);
                        dynamicHosts.add(addresses[i]);
                    }
                } catch (final Exception e) {
                    final String finalAddress = address;
                    LOGGER.warn((Supplier<?>) () -> new ParameterizedMessage("failed to add {}, address {}", instance.getInstanceId(), finalAddress), e);
                }
            } else {
                LOGGER.trace("not adding {}, address is null, host_type {}", instance.getInstanceId(), hostType);
            }
        }
    }
    LOGGER.debug("using dynamic transport addresses {}", dynamicHosts);
    return dynamicHosts;
}
Also used : Instance(com.amazonaws.services.ec2.model.Instance) TransportAddress(org.elasticsearch.common.transport.TransportAddress) AmazonClientException(com.amazonaws.AmazonClientException) ArrayList(java.util.ArrayList) Collections.disjoint(java.util.Collections.disjoint) AmazonClientException(com.amazonaws.AmazonClientException) GroupIdentifier(com.amazonaws.services.ec2.model.GroupIdentifier) DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) Reservation(com.amazonaws.services.ec2.model.Reservation) Supplier(org.apache.logging.log4j.util.Supplier) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) Tag(com.amazonaws.services.ec2.model.Tag)

Example 38 with Tag

use of software.amazon.awssdk.services.ec2.model.Tag in project elasticsearch by elastic.

the class AmazonEC2Mock method describeInstances.

@Override
public DescribeInstancesResult describeInstances(DescribeInstancesRequest describeInstancesRequest) throws AmazonServiceException, AmazonClientException {
    Collection<Instance> filteredInstances = new ArrayList<>();
    logger.debug("--> mocking describeInstances");
    for (Instance instance : instances) {
        boolean tagFiltered = false;
        boolean instanceFound = false;
        Map<String, List<String>> expectedTags = new HashMap<>();
        Map<String, List<String>> instanceTags = new HashMap<>();
        for (Tag tag : instance.getTags()) {
            List<String> tags = instanceTags.get(tag.getKey());
            if (tags == null) {
                tags = new ArrayList<>();
                instanceTags.put(tag.getKey(), tags);
            }
            tags.add(tag.getValue());
        }
        for (Filter filter : describeInstancesRequest.getFilters()) {
            // If we have the same tag name and one of the values, we add the instance
            if (filter.getName().startsWith("tag:")) {
                tagFiltered = true;
                String tagName = filter.getName().substring(4);
                // if we have more than one value for the same key, then the key is appended with .x
                Pattern p = Pattern.compile("\\.\\d+", Pattern.DOTALL);
                Matcher m = p.matcher(tagName);
                if (m.find()) {
                    int i = tagName.lastIndexOf(".");
                    tagName = tagName.substring(0, i);
                }
                List<String> tags = expectedTags.get(tagName);
                if (tags == null) {
                    tags = new ArrayList<>();
                    expectedTags.put(tagName, tags);
                }
                tags.addAll(filter.getValues());
            }
        }
        if (tagFiltered) {
            logger.debug("--> expected tags: [{}]", expectedTags);
            logger.debug("--> instance tags: [{}]", instanceTags);
            instanceFound = true;
            for (Map.Entry<String, List<String>> expectedTagsEntry : expectedTags.entrySet()) {
                List<String> instanceTagValues = instanceTags.get(expectedTagsEntry.getKey());
                if (instanceTagValues == null) {
                    instanceFound = false;
                    break;
                }
                for (String expectedValue : expectedTagsEntry.getValue()) {
                    boolean valueFound = false;
                    for (String instanceTagValue : instanceTagValues) {
                        if (instanceTagValue.equals(expectedValue)) {
                            valueFound = true;
                        }
                    }
                    if (valueFound == false) {
                        instanceFound = false;
                    }
                }
            }
        }
        if (tagFiltered == false || instanceFound) {
            logger.debug("--> instance added");
            filteredInstances.add(instance);
        } else {
            logger.debug("--> instance filtered");
        }
    }
    return new DescribeInstancesResult().withReservations(new Reservation().withInstances(filteredInstances));
}
Also used : Pattern(java.util.regex.Pattern) Instance(com.amazonaws.services.ec2.model.Instance) HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) Reservation(com.amazonaws.services.ec2.model.Reservation) Filter(com.amazonaws.services.ec2.model.Filter) ArrayList(java.util.ArrayList) List(java.util.List) Tag(com.amazonaws.services.ec2.model.Tag) Map(java.util.Map) HashMap(java.util.HashMap)

Example 39 with Tag

use of software.amazon.awssdk.services.ec2.model.Tag in project GNS by MobilityFirst.

the class AWSEC2 method addInstanceTag.

/**
   * Adds the key and value as a 'tag' for the instance.
   *
   * @param ec2
   * @param createdInstanceId
   * @param key
   * @param value
   */
public static void addInstanceTag(AmazonEC2 ec2, String createdInstanceId, String key, String value) {
    List<String> resources = new LinkedList<>();
    resources.add(createdInstanceId);
    List<Tag> tags = new LinkedList<>();
    Tag nameTag = new Tag(key, value);
    tags.add(nameTag);
    CreateTagsRequest ctr = new CreateTagsRequest(resources, tags);
    ec2.createTags(ctr);
}
Also used : CreateTagsRequest(com.amazonaws.services.ec2.model.CreateTagsRequest) Tag(com.amazonaws.services.ec2.model.Tag) LinkedList(java.util.LinkedList)

Example 40 with Tag

use of software.amazon.awssdk.services.ec2.model.Tag in project herd by FINRAOS.

the class S3DaoImplTest method testTagObjectsS3FilesListEmpty.

@Test
public void testTagObjectsS3FilesListEmpty() {
    // Create an S3 file transfer request parameters DTO to access S3 objects without specifying S3 files.
    S3FileTransferRequestParamsDto s3FileTransferRequestParamsDto = new S3FileTransferRequestParamsDto();
    s3FileTransferRequestParamsDto.setS3BucketName(S3_BUCKET_NAME);
    s3FileTransferRequestParamsDto.setFiles(new ArrayList<>());
    // Create an S3 file transfer request parameters DTO to tag S3 objects.
    S3FileTransferRequestParamsDto s3ObjectTaggerParamsDto = new S3FileTransferRequestParamsDto();
    s3ObjectTaggerParamsDto.setAwsAccessKeyId(AWS_ASSUMED_ROLE_ACCESS_KEY);
    s3ObjectTaggerParamsDto.setAwsSecretKey(AWS_ASSUMED_ROLE_SECRET_KEY);
    s3ObjectTaggerParamsDto.setSessionToken(AWS_ASSUMED_ROLE_SESSION_TOKEN);
    // Create an S3 object tag.
    Tag tag = new Tag(S3_OBJECT_TAG_KEY, S3_OBJECT_TAG_VALUE);
    // Call the method under test.
    s3DaoImpl.tagObjects(s3FileTransferRequestParamsDto, s3ObjectTaggerParamsDto, tag);
    // Verify the external calls.
    verifyNoMoreInteractionsHelper();
}
Also used : S3FileTransferRequestParamsDto(org.finra.herd.model.dto.S3FileTransferRequestParamsDto) Tag(com.amazonaws.services.s3.model.Tag) Test(org.junit.Test) AbstractDaoTest(org.finra.herd.dao.AbstractDaoTest)

Aggregations

Tag (com.amazonaws.services.ec2.model.Tag)38 ArrayList (java.util.ArrayList)26 Tag (com.amazonaws.services.s3.model.Tag)19 HashMap (java.util.HashMap)17 Test (org.junit.Test)16 Instance (com.amazonaws.services.ec2.model.Instance)15 List (java.util.List)15 S3FileTransferRequestParamsDto (org.finra.herd.model.dto.S3FileTransferRequestParamsDto)14 File (java.io.File)11 Map (java.util.Map)11 GetObjectTaggingRequest (com.amazonaws.services.s3.model.GetObjectTaggingRequest)9 GetObjectTaggingResult (com.amazonaws.services.s3.model.GetObjectTaggingResult)9 HashSet (java.util.HashSet)9 Utils (com.vmware.xenon.common.Utils)8 CreateTagsRequest (com.amazonaws.services.ec2.model.CreateTagsRequest)7 Reservation (com.amazonaws.services.ec2.model.Reservation)7 S3ObjectSummary (com.amazonaws.services.s3.model.S3ObjectSummary)5 TagsUtil.newTagState (com.vmware.photon.controller.model.adapters.util.TagsUtil.newTagState)5 TagState (com.vmware.photon.controller.model.resources.TagService.TagState)5 DeferredResult (com.vmware.xenon.common.DeferredResult)5