Skip to content

Commit

Permalink
Support multi-valued filters for tasks (fixes #1)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmandalidis committed Oct 1, 2019
1 parent 3d7b19b commit 23208a4
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 32 deletions.
24 changes: 12 additions & 12 deletions src/main/java/com/spotify/docker/client/DefaultDockerClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -2073,23 +2073,23 @@ public List<Task> listTasks(final Task.Criteria criteria)
assertApiVersionIsAbove("1.24");
final Map<String, List<String>> filters = new HashMap<>();

if (criteria.taskId() != null) {
filters.put("id", Collections.singletonList(criteria.taskId()));
if (criteria.taskIds() != null) {
filters.put("id", criteria.taskIds());
}
if (criteria.taskName() != null) {
filters.put("name", Collections.singletonList(criteria.taskName()));
if (criteria.taskNames() != null) {
filters.put("name", criteria.taskNames());
}
if (criteria.serviceName() != null) {
filters.put("service", Collections.singletonList(criteria.serviceName()));
if (criteria.serviceNames() != null) {
filters.put("service", criteria.serviceNames());
}
if (criteria.nodeId() != null) {
filters.put("node", Collections.singletonList(criteria.nodeId()));
if (criteria.nodeIds() != null) {
filters.put("node", criteria.nodeIds());
}
if (criteria.label() != null) {
filters.put("label", Collections.singletonList(criteria.label()));
if (criteria.labels() != null) {
filters.put("label", criteria.labels());
}
if (criteria.desiredState() != null) {
filters.put("desired-state", Collections.singletonList(criteria.desiredState()));
if (criteria.desiredStates() != null) {
filters.put("desired-state", criteria.desiredStates());
}

WebTarget resource = resource().path("tasks");
Expand Down
38 changes: 20 additions & 18 deletions src/main/java/com/spotify/docker/client/messages/swarm/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import com.google.common.collect.ImmutableList;

import com.google.common.collect.ImmutableMap;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -113,37 +115,37 @@ public abstract static class Criteria {
* Filter by task id.
*/
@Nullable
public abstract String taskId();
public abstract List<String> taskIds();

/**
* Filter by task name.
*/
@Nullable
public abstract String taskName();
public abstract List<String> taskNames();

/**
* Filter by service name.
*/
@Nullable
public abstract String serviceName();
public abstract List<String> serviceNames();

/**
* Filter by node id.
*/
@Nullable
public abstract String nodeId();
public abstract List<String> nodeIds();

/**
* Filter by label.
*/
@Nullable
public abstract String label();
public abstract List<String> labels();

/**
* Filter by desired state.
*/
@Nullable
public abstract String desiredState();
public abstract List<String> desiredStates();

public static Builder builder() {
return new AutoValue_Task_Criteria.Builder();
Expand All @@ -152,69 +154,69 @@ public static Builder builder() {
@AutoValue.Builder
public abstract static class Builder {

public abstract Builder taskId(final String taskId);
public abstract Builder taskIds(final List<String> taskIds);

/**
* @deprecated As of release 7.0.0, replaced by {@link #taskId(String)}.
*/
@Deprecated
public Builder withTaskId(final String taskId) {
taskId(taskId);
taskIds(Collections.singletonList(taskId));
return this;
}

public abstract Builder taskName(final String taskName);
public abstract Builder taskNames(final List<String> taskNames);

/**
* @deprecated As of release 7.0.0, replaced by {@link #taskName(String)}.
*/
@Deprecated
public Builder withTaskName(final String taskName) {
taskName(taskName);
taskNames(Collections.singletonList(taskName));
return this;
}

public abstract Builder serviceName(final String serviceName);
public abstract Builder serviceNames(final List<String> serviceNames);

/**
* @deprecated As of release 7.0.0, replaced by {@link #serviceName(String)}.
*/
@Deprecated
public Builder withServiceName(final String serviceName) {
serviceName(serviceName);
serviceNames(Collections.singletonList(serviceName));
return this;
}

public abstract Builder nodeId(final String nodeId);
public abstract Builder nodeIds(final List<String> nodeIds);

/**
* @deprecated As of release 7.0.0, replaced by {@link #nodeId(String)}.
*/
@Deprecated
public Builder withNodeId(final String nodeId) {
nodeId(nodeId);
nodeIds(Collections.singletonList(nodeId));
return this;
}

public abstract Builder label(final String label);
public abstract Builder labels(final List<String> labels);

/**
* @deprecated As of release 7.0.0, replaced by {@link #label(String)}.
*/
@Deprecated
public Builder withLabel(final String label) {
label(label);
labels(Collections.singletonList(label));
return this;
}

public abstract Builder desiredState(final String desiredState);
public abstract Builder desiredStates(final List<String> desiredStates);

/**
* @deprecated As of release 7.0.0, replaced by {@link #desiredState(String)}.
*/
@Deprecated
public Builder withDesiredState(final String desiredState) {
desiredState(desiredState);
desiredStates(Collections.singletonList(desiredState));
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
import java.sql.Date;
import java.time.Instant;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -1319,7 +1320,7 @@ public void testListTaskWithCriteria() throws Exception {
enqueueServerApiVersion("1.24");
enqueueServerApiResponse(200, "fixtures/1.24/tasks.json");
final String taskId = "task-1";
dockerClient.listTasks(Task.find().taskId(taskId).build());
dockerClient.listTasks(Task.find().taskIds(Collections.singletonList(taskId)).build());
takeRequestImmediately();
final RecordedRequest recordedRequest2 = takeRequestImmediately();
final HttpUrl requestUrl2 = recordedRequest2.getRequestUrl();
Expand All @@ -1333,7 +1334,8 @@ public void testListTaskWithCriteria() throws Exception {
enqueueServerApiVersion("1.24");
enqueueServerApiResponse(200, "fixtures/1.24/tasks.json");
final String serviceName = "service-1";
dockerClient.listTasks(Task.find().serviceName(serviceName).build());
dockerClient.listTasks(Task.find().serviceNames(Collections.singletonList(serviceName))
.build());
takeRequestImmediately();
final RecordedRequest recordedRequest3 = takeRequestImmediately();
final HttpUrl requestUrl3 = recordedRequest3.getRequestUrl();
Expand Down

0 comments on commit 23208a4

Please sign in to comment.