Skip to content

Commit

Permalink
Support multi-valued filters for services (relates #1)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmandalidis committed Oct 1, 2019
1 parent ab590a6 commit 3d7b19b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1988,11 +1988,11 @@ public List<Service> listServices(final Service.Criteria criteria)
assertApiVersionIsAbove("1.24");
final Map<String, List<String>> filters = new HashMap<>();

if (criteria.serviceId() != null) {
filters.put("id", Collections.singletonList(criteria.serviceId()));
if (criteria.serviceIds() != null) {
filters.put("id", criteria.serviceIds());
}
if (criteria.serviceName() != null) {
filters.put("name", Collections.singletonList(criteria.serviceName()));
if (criteria.serviceNames() != null) {
filters.put("name", criteria.serviceNames());
}

final List<String> labels = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableMap;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Nullable;
Expand Down Expand Up @@ -67,13 +69,13 @@ public abstract static class Criteria {
* Filter by service id.
*/
@Nullable
public abstract String serviceId();
public abstract List<String> serviceIds();

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

/**
* Filter by label.
Expand All @@ -88,25 +90,25 @@ public static Builder builder() {
@AutoValue.Builder
public abstract static class Builder {

public abstract Builder serviceId(final String serviceId);
public abstract Builder serviceIds(final List<String> serviceIds);

/**
* @deprecated As of release 7.0.0, replaced by {@link #serviceId(String)}.
*/
@Deprecated
public Builder withServiceId(final String serviceId) {
serviceId(serviceId);
serviceIds(Collections.singletonList(serviceId));
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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
import static java.lang.System.getenv;
import static java.util.Collections.singletonList;
import static java.util.Collections.singletonMap;
import static java.util.stream.Collectors.toList;
import static org.apache.commons.lang.StringUtils.containsIgnoreCase;
import static org.awaitility.Awaitility.await;
import static org.hamcrest.Matchers.allOf;
Expand Down Expand Up @@ -5378,28 +5379,38 @@ public void testListServices() throws Exception {
}

@Test
public void testListServicesFilterById() throws Exception {
public void testListServicesFilterByIds() throws Exception {
requireDockerApiVersionAtLeast("1.24", "swarm support");
final ServiceSpec spec = createServiceSpec(randomName());
final ServiceCreateResponse response = sut.createService(spec);
final ServiceSpec spec1 = createServiceSpec(randomName());
final ServiceSpec spec2 = createServiceSpec(randomName());
final ServiceCreateResponse response1 = sut.createService(spec1);
final ServiceCreateResponse response2 = sut.createService(spec2);

final List<Service> services = sut
.listServices(Service.find().serviceId(response.id()).build());
assertThat(services.size(), is(1));
assertThat(services.get(0).id(), is(response.id()));
.listServices(Service.find().serviceIds(Arrays.asList(
response1.id(), response2.id())).build());
assertThat(services.size(), is(2));
List<String> serviceIds = services.stream().map(Service::id).collect(toList());
assertThat(serviceIds, containsInAnyOrder(response1.id(), response2.id()));
}

@Test
public void testListServicesFilterByName() throws Exception {
public void testListServicesFilterByNames() throws Exception {
requireDockerApiVersionAtLeast("1.24", "swarm support");
final String serviceName = randomName();
final ServiceSpec spec = createServiceSpec(serviceName);
sut.createService(spec);
final String serviceName1 = randomName();
final ServiceSpec spec1 = createServiceSpec(serviceName1);
sut.createService(spec1);
final String serviceName2 = randomName();
final ServiceSpec spec2 = createServiceSpec(serviceName2);
sut.createService(spec2);

final List<Service> services =
sut.listServices(Service.find().serviceName(serviceName).build());
assertThat(services.size(), is(1));
assertThat(services.get(0).spec().name(), is(serviceName));
sut.listServices(Service.find().serviceNames(Arrays.asList(
serviceName1, serviceName2)).build());
assertThat(services.size(), is(2));
List<String> serviceNames = services.stream().map(service -> service.spec().name())
.collect(toList());
assertThat(serviceNames, containsInAnyOrder(serviceName1, serviceName2));
}

@Test
Expand Down

0 comments on commit 3d7b19b

Please sign in to comment.