Skip to content

Commit

Permalink
Support listing services by label (fixes spotify#615)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmandalidis committed Feb 7, 2017
1 parent 2b9a2ca commit 5e519d2
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/main/java/com/spotify/docker/client/DefaultDockerClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,13 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -1675,7 +1677,6 @@ public List<Service> listServices() throws DockerException, InterruptedException
public List<Service> listServices(final Service.Criteria criteria)
throws DockerException, InterruptedException {
assertApiVersionIsAbove("1.24");
WebTarget resource = resource().path("services");
final Map<String, List<String>> filters = new HashMap<>();

if (criteria.serviceId() != null) {
Expand All @@ -1685,6 +1686,20 @@ public List<Service> listServices(final Service.Criteria criteria)
filters.put("name", Collections.singletonList(criteria.serviceName()));
}

final List<String> labels = new ArrayList<>();
for (Entry<String, String> input: criteria.labels().entrySet()) {
if ("".equals(input.getValue())) {
labels.add(input.getKey());
} else {
labels.add(String.format("%s=%s", input.getKey(), input.getValue()));
}
}

if (!labels.isEmpty()) {
filters.put("label", labels);
}

WebTarget resource = resource().path("services");
resource = resource.queryParam("filters", urlEncodeFilters(filters));
return request(GET, SERVICE_LIST, resource, resource.request(APPLICATION_JSON_TYPE));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableMap;

import java.util.Date;
import java.util.Map;

import javax.annotation.Nullable;

@AutoValue
Expand Down Expand Up @@ -71,6 +74,12 @@ public abstract static class Criteria {
@Nullable
public abstract String serviceName();

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

public static Builder builder() {
return new AutoValue_Service_Criteria.Builder();
}
Expand Down Expand Up @@ -100,6 +109,15 @@ public Builder withServiceName(final String serviceName) {
return this;
}

public abstract Builder labels(final Map<String, String> labels);

abstract ImmutableMap.Builder<String, String> labelsBuilder();

public Builder addLabel(final String label, final String value) {
labelsBuilder().put(label, value);
return this;
}

public abstract Criteria build();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4210,6 +4210,27 @@ public void testListServicesFilterByName() throws Exception {
assertThat(services.size(), is(1));
assertThat(services.get(0).spec().name(), is(serviceName));
}

@Test
public void testListServicesFilterByLabel() throws Exception {
requireDockerApiVersionAtLeast("1.24", "swarm support");
final String serviceName = randomName();

Map<String, String> labels = new HashMap<>();
labels.put("foo", "bar");

final ServiceSpec spec = createServiceSpec(serviceName, labels);
sut.createService(spec);

final List<Service> services = sut.listServices(Service.find().addLabel("foo", "bar").build());

assertThat(services.size(), is(1));
assertThat(services.get(0).spec().labels().get("foo"), is("bar"));

final List<Service> notFoundServices = sut.listServices(Service.find()
.addLabel("bar", "foo").build());
assertThat(notFoundServices.size(), is(0));
}

@Test
public void testRemoveService() throws Exception {
Expand Down Expand Up @@ -4264,6 +4285,12 @@ public void testListTaskWithCriteria() throws Exception {
}

private ServiceSpec createServiceSpec(final String serviceName) {
return this.createServiceSpec(serviceName, new HashMap<String, String>());
}

private ServiceSpec createServiceSpec(final String serviceName,
final Map<String, String> labels) {

final TaskSpec taskSpec = TaskSpec
.builder()
.containerSpec(ContainerSpec.builder().image("alpine")
Expand All @@ -4274,6 +4301,7 @@ private ServiceSpec createServiceSpec(final String serviceName) {

return ServiceSpec.builder().name(serviceName).taskTemplate(taskSpec)
.mode(serviceMode)
.labels(labels)
.build();
}

Expand Down

0 comments on commit 5e519d2

Please sign in to comment.