Example usage for com.amazonaws.services.ecs.model ListTaskDefinitionsResult getNextToken

List of usage examples for com.amazonaws.services.ecs.model ListTaskDefinitionsResult getNextToken

Introduction

In this page you can find the example usage for com.amazonaws.services.ecs.model ListTaskDefinitionsResult getNextToken.

Prototype


public String getNextToken() 

Source Link

Document

The nextToken value to include in a future ListTaskDefinitions request.

Usage

From source file:com.netflix.spinnaker.clouddriver.ecs.provider.agent.TaskDefinitionCachingAgent.java

License:Apache License

@Override
protected List<TaskDefinition> getItems(AmazonECS ecs, ProviderCache providerCache) {
    List<TaskDefinition> taskDefinitionList = new LinkedList<>();
    Set<String> cachedArns = providerCache.getIdentifiers(TASK_DEFINITIONS.toString()).stream()
            .filter(id -> keyAccountRegionFilter(TASK_DEFINITIONS.toString(), id)).map(id -> {
                Map<String, String> keyParts = Keys.parse(id);
                return keyParts.get("taskDefinitionArn");
            }).collect(Collectors.toSet());

    String nextToken = null;//from www. j  a  v  a2  s. c  o  m
    do {
        ListTaskDefinitionsRequest listTasksRequest = new ListTaskDefinitionsRequest();
        if (nextToken != null) {
            listTasksRequest.setNextToken(nextToken);
        }
        ListTaskDefinitionsResult listTaskDefinitionsResult = ecs.listTaskDefinitions(listTasksRequest);
        List<String> taskDefinitionArns = listTaskDefinitionsResult.getTaskDefinitionArns();

        if (taskDefinitionArns.size() == 0) {
            continue;
        }

        Set<String> newTaskDefArns = new HashSet<>(taskDefinitionArns);
        newTaskDefArns.removeAll(cachedArns);

        Set<String> existingTaskDefArns = new HashSet<>(taskDefinitionArns);
        existingTaskDefArns.removeAll(newTaskDefArns);

        if (!existingTaskDefArns.isEmpty()) {
            // TaskDefinitions are immutable, there's no reason to make a describe call on existing ones.
            taskDefinitionList.addAll(retrieveFromCache(existingTaskDefArns, providerCache));
        }

        for (String taskDefinitionArn : newTaskDefArns) {
            TaskDefinition taskDefinition = ecs
                    .describeTaskDefinition(
                            new DescribeTaskDefinitionRequest().withTaskDefinition(taskDefinitionArn))
                    .getTaskDefinition();
            taskDefinitionList.add(taskDefinition);
        }

        nextToken = listTaskDefinitionsResult.getNextToken();
    } while (nextToken != null && nextToken.length() != 0);
    return taskDefinitionList;
}