Example usage for com.amazonaws.services.ecs.model ListTaskDefinitionsRequest setNextToken

List of usage examples for com.amazonaws.services.ecs.model ListTaskDefinitionsRequest setNextToken

Introduction

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

Prototype


public void setNextToken(String nextToken) 

Source Link

Document

The nextToken value returned from a ListTaskDefinitions request indicating that more results are available to fulfill the request and further calls will be needed.

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 w w w .  j av a2s.  co 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;
}