Skip to content

Commit 0918e79

Browse files
committed
add missing label filter in v1.24 for ListServiceCmd
1 parent 276c495 commit 0918e79

File tree

5 files changed

+90
-11
lines changed

5 files changed

+90
-11
lines changed

src/main/java/com/github/dockerjava/api/command/ListServicesCmd.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,20 @@ public interface ListServicesCmd extends SyncDockerCmd<List<Service>> {
1818
Map<String, List<String>> getFilters();
1919

2020
/**
21-
* @param ids
22-
* - Show only services with the given ids
21+
* @param ids - Show only services with the given ids
2322
*/
2423
ListServicesCmd withIdFilter(List<String> ids);
24+
2525
/**
26-
*
27-
* @param names
28-
* - Show only services with the given names
26+
* @param names - Show only services with the given names
2927
*/
3028
ListServicesCmd withNameFilter(List<String> names);
3129

30+
/**
31+
* @param labels - Show only services with the passed labels. Labels is a {@link Map} that contains label keys and values
32+
*/
33+
ListServicesCmd withLabelFilter(Map<String, String> labels);
34+
3235
interface Exec extends DockerCmdSyncExec<ListServicesCmd, List<Service>> {
3336
}
3437

src/main/java/com/github/dockerjava/core/command/ListServicesCmdImpl.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import com.github.dockerjava.api.command.ListServicesCmd;
44
import com.github.dockerjava.api.model.Service;
5-
import com.github.dockerjava.core.util.ServiceFiltersBuilder;
5+
import com.github.dockerjava.core.util.FiltersBuilder;
66

77
import java.util.List;
88
import java.util.Map;
@@ -15,7 +15,7 @@
1515
public class ListServicesCmdImpl extends AbstrDockerCmd<ListServicesCmd, List<Service>> implements
1616
ListServicesCmd {
1717

18-
private ServiceFiltersBuilder filters = new ServiceFiltersBuilder();
18+
private FiltersBuilder filters = new FiltersBuilder();
1919

2020
public ListServicesCmdImpl(Exec exec) {
2121
super(exec);
@@ -29,15 +29,21 @@ public Map<String, List<String>> getFilters() {
2929
@Override
3030
public ListServicesCmd withIdFilter(List<String> ids) {
3131
checkNotNull(ids, "ids was not specified");
32-
this.filters.withIds(ids);
32+
this.filters.withFilter("id", ids);
3333
return this;
3434
}
3535

3636
@Override
3737
public ListServicesCmd withNameFilter(List<String> names) {
3838
checkNotNull(names, "names was not specified");
39-
this.filters.withNames(names);
39+
this.filters.withFilter("name", names);
4040
return this;
4141
}
4242

43+
@Override
44+
public ListServicesCmd withLabelFilter(Map<String, String> labels) {
45+
checkNotNull(labels, "labels was not specified");
46+
this.filters.withLabels(labels);
47+
return this;
48+
}
4349
}

src/main/java/com/github/dockerjava/core/util/ServiceFiltersBuilder.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
/**
1111
* Representation of filters to service lists.
1212
*/
13+
@Deprecated
1314
public class ServiceFiltersBuilder {
1415

1516
private Map<String, List<String>> filters = new HashMap<>();
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.github.dockerjava.cmd.swarm;
2+
3+
import com.github.dockerjava.api.DockerClient;
4+
import com.github.dockerjava.api.command.CreateServiceResponse;
5+
import com.github.dockerjava.api.exception.DockerException;
6+
import com.github.dockerjava.api.model.ContainerSpec;
7+
import com.github.dockerjava.api.model.Service;
8+
import com.github.dockerjava.api.model.ServiceModeConfig;
9+
import com.github.dockerjava.api.model.ServiceReplicatedModeOptions;
10+
import com.github.dockerjava.api.model.ServiceSpec;
11+
import com.github.dockerjava.api.model.SwarmSpec;
12+
import com.github.dockerjava.api.model.TaskSpec;
13+
import org.junit.Test;
14+
import org.slf4j.Logger;
15+
import org.slf4j.LoggerFactory;
16+
17+
import java.util.Collections;
18+
import java.util.List;
19+
import java.util.Map;
20+
21+
import static com.github.dockerjava.junit.DockerRule.DEFAULT_IMAGE;
22+
import static org.hamcrest.MatcherAssert.assertThat;
23+
import static org.hamcrest.Matchers.hasSize;
24+
import static org.hamcrest.Matchers.is;
25+
26+
public class ListServicesCmdExecIT extends SwarmCmdIT {
27+
public static final Logger LOG = LoggerFactory.getLogger(CreateServiceCmdExecIT.class);
28+
private static final String SERVICE_NAME = "inspect_service";
29+
private static final String LABEL_KEY = "com.github.dockerjava.usage";
30+
private static final String LABEL_VALUE = "test";
31+
32+
@Test
33+
public void testListServices() throws DockerException {
34+
DockerClient docker1 = startDockerInDocker();
35+
docker1.initializeSwarmCmd(new SwarmSpec()).exec();
36+
Map<String, String> serviceLabels = Collections.singletonMap(LABEL_KEY, LABEL_VALUE);
37+
CreateServiceResponse response = docker1.createServiceCmd(new ServiceSpec()
38+
.withLabels(serviceLabels)
39+
.withName(SERVICE_NAME)
40+
.withMode(new ServiceModeConfig().withReplicated(
41+
new ServiceReplicatedModeOptions()
42+
.withReplicas(1)
43+
))
44+
.withTaskTemplate(new TaskSpec()
45+
.withContainerSpec(new ContainerSpec()
46+
.withImage(DEFAULT_IMAGE))))
47+
.exec();
48+
String serviceId = response.getId();
49+
//filtering with service id
50+
List<Service> services = docker1.listServicesCmd().withIdFilter(Collections.singletonList(serviceId)).exec();
51+
assertThat(services, hasSize(1));
52+
//filtering with service name
53+
services = docker1.listServicesCmd().withNameFilter(Collections.singletonList(SERVICE_NAME)).exec();
54+
assertThat(services, hasSize(1));
55+
//filter labels
56+
services = docker1.listServicesCmd().withLabelFilter(serviceLabels).exec();
57+
assertThat(services, hasSize(1));
58+
docker1.removeServiceCmd(SERVICE_NAME).exec();
59+
}
60+
}

src/test/java/com/github/dockerjava/cmd/swarm/ListTasksCmdExecIT.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
import org.slf4j.Logger;
1515
import org.slf4j.LoggerFactory;
1616

17+
import java.util.Collections;
1718
import java.util.List;
19+
import java.util.Map;
1820

1921
import static com.github.dockerjava.junit.DockerRule.DEFAULT_IMAGE;
2022
import static org.hamcrest.MatcherAssert.assertThat;
@@ -24,14 +26,16 @@
2426
public class ListTasksCmdExecIT extends SwarmCmdIT {
2527
public static final Logger LOG = LoggerFactory.getLogger(CreateServiceCmdExecIT.class);
2628
private static final String SERVICE_NAME = "inspect_task";
29+
private static final String TASK_LABEL_KEY = "com.github.dockerjava.usage";
30+
private static final String TASK_LABEL_VALUE = "test";
2731

2832
@Test
2933
public void testListTasks() throws DockerException {
3034
dockerRule.getClient().initializeSwarmCmd(new SwarmSpec())
3135
.withListenAddr("127.0.0.1")
3236
.withAdvertiseAddr("127.0.0.1")
3337
.exec();
34-
38+
Map<String, String> taskLabels = Collections.singletonMap(TASK_LABEL_KEY, TASK_LABEL_VALUE);
3539
CreateServiceResponse response = dockerRule.getClient().createServiceCmd(new ServiceSpec()
3640
.withName(SERVICE_NAME)
3741
.withMode(new ServiceModeConfig().withReplicated(
@@ -40,7 +44,7 @@ public void testListTasks() throws DockerException {
4044
))
4145
.withTaskTemplate(new TaskSpec()
4246
.withContainerSpec(new ContainerSpec()
43-
.withImage(DEFAULT_IMAGE))))
47+
.withImage(DEFAULT_IMAGE))).withLabels(taskLabels))
4448
.exec();
4549
String serviceId = response.getId();
4650
//filtering with service id
@@ -60,6 +64,11 @@ public void testListTasks() throws DockerException {
6064
//filtering with state
6165
tasks = dockerRule.getClient().listTasksCmd().withStateFilter(TaskState.RUNNING).exec();
6266
assertThat(tasks, hasSize(2));
67+
//filter labels
68+
tasks = dockerRule.getClient().listTasksCmd().withLabelFilter(taskLabels).exec();
69+
assertThat(tasks, hasSize(2));
70+
tasks = dockerRule.getClient().listTasksCmd().withLabelFilter(TASK_LABEL_KEY + "=" + TASK_LABEL_VALUE).exec();
71+
assertThat(tasks, hasSize(2));
6372
dockerRule.getClient().removeServiceCmd(SERVICE_NAME).exec();
6473
}
6574
}

0 commit comments

Comments
 (0)