From 23208a4c5e35eccf06f3838d0de2d72a1135f610 Mon Sep 17 00:00:00 2001 From: Dimitris Mandalidis Date: Sun, 2 Dec 2018 15:58:59 +0200 Subject: [PATCH] Support multi-valued filters for tasks (fixes #1) --- .../docker/client/DefaultDockerClient.java | 24 ++++++------ .../docker/client/messages/swarm/Task.java | 38 ++++++++++--------- .../client/DefaultDockerClientUnitTest.java | 6 ++- 3 files changed, 36 insertions(+), 32 deletions(-) diff --git a/src/main/java/com/spotify/docker/client/DefaultDockerClient.java b/src/main/java/com/spotify/docker/client/DefaultDockerClient.java index a5c703a27..06dd7ab43 100644 --- a/src/main/java/com/spotify/docker/client/DefaultDockerClient.java +++ b/src/main/java/com/spotify/docker/client/DefaultDockerClient.java @@ -2073,23 +2073,23 @@ public List listTasks(final Task.Criteria criteria) assertApiVersionIsAbove("1.24"); final Map> 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"); diff --git a/src/main/java/com/spotify/docker/client/messages/swarm/Task.java b/src/main/java/com/spotify/docker/client/messages/swarm/Task.java index 1d6f2a47c..1fb30e9ee 100644 --- a/src/main/java/com/spotify/docker/client/messages/swarm/Task.java +++ b/src/main/java/com/spotify/docker/client/messages/swarm/Task.java @@ -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; @@ -113,37 +115,37 @@ public abstract static class Criteria { * Filter by task id. */ @Nullable - public abstract String taskId(); + public abstract List taskIds(); /** * Filter by task name. */ @Nullable - public abstract String taskName(); + public abstract List taskNames(); /** * Filter by service name. */ @Nullable - public abstract String serviceName(); + public abstract List serviceNames(); /** * Filter by node id. */ @Nullable - public abstract String nodeId(); + public abstract List nodeIds(); /** * Filter by label. */ @Nullable - public abstract String label(); + public abstract List labels(); /** * Filter by desired state. */ @Nullable - public abstract String desiredState(); + public abstract List desiredStates(); public static Builder builder() { return new AutoValue_Task_Criteria.Builder(); @@ -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 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 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 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 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 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 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; } diff --git a/src/test/java/com/spotify/docker/client/DefaultDockerClientUnitTest.java b/src/test/java/com/spotify/docker/client/DefaultDockerClientUnitTest.java index 43e2daff0..5af8b8eb2 100644 --- a/src/test/java/com/spotify/docker/client/DefaultDockerClientUnitTest.java +++ b/src/test/java/com/spotify/docker/client/DefaultDockerClientUnitTest.java @@ -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; @@ -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(); @@ -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();