Skip to content

Commit 2582dce

Browse files
fbuecklersKostyaSha
authored andcommitted
add missing ListContainer filters (docker-java#744)
* add missing ListContainer filters * Reimplement the list filters with collection as parameters * Remove deprecated status method
1 parent 65a527d commit 2582dce

File tree

4 files changed

+374
-89
lines changed

4 files changed

+374
-89
lines changed

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

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package com.github.dockerjava.api.command;
22

3-
import java.util.List;
4-
import java.util.Map;
3+
import com.github.dockerjava.api.model.Container;
54

65
import javax.annotation.CheckForNull;
7-
8-
import com.github.dockerjava.api.model.Container;
6+
import java.util.Collection;
7+
import java.util.List;
8+
import java.util.Map;
99

1010
/**
1111
* List containers
@@ -38,22 +38,52 @@ public interface ListContainersCmd extends SyncDockerCmd<List<Container>> {
3838
ListContainersCmd withBefore(String before);
3939

4040
/**
41-
* @param exitcode
41+
* @param name
42+
* - Show only containers that has the container's name
43+
*/
44+
ListContainersCmd withNameFilter(Collection<String> name);
45+
46+
/**
47+
* @param id
48+
* - Show only containers that has the container's id
49+
*/
50+
ListContainersCmd withIdFilter(Collection<String> id);
51+
52+
/**
53+
* @param ancestor
54+
* - Show only containers created from an image or a descendant.
55+
*/
56+
ListContainersCmd withAncestorFilter(Collection<String> ancestor);
57+
58+
/**
59+
* @param volume
60+
* - Show only containers with volume name or mount point destination
61+
*/
62+
ListContainersCmd withVolumeFilter(Collection<String> volume);
63+
64+
/**
65+
* @param network
66+
* - Show only containers with network id or network name
67+
*/
68+
ListContainersCmd withNetworkFilter(Collection<String> network);
69+
70+
/**
71+
* @param exited
4272
* - Show only containers that exited with the passed exitcode.
4373
*/
44-
ListContainersCmd withExitcodeFilter(Integer exitcode);
74+
ListContainersCmd withExitedFilter(Integer exited);
4575

4676
/**
4777
* @param status
4878
* - Show only containers with the passed status (created|restarting|running|paused|exited).
4979
*/
50-
ListContainersCmd withStatusFilter(String status);
80+
ListContainersCmd withStatusFilter(Collection<String> status);
5181

5282
/**
5383
* @param labels
5484
* - Show only containers with the passed labels.
5585
*/
56-
ListContainersCmd withLabelFilter(String... labels);
86+
ListContainersCmd withLabelFilter(Collection<String> labels);
5787

5888
/**
5989
* @param labels
@@ -85,6 +115,13 @@ public interface ListContainersCmd extends SyncDockerCmd<List<Container>> {
85115
*/
86116
ListContainersCmd withSince(String since);
87117

118+
/**
119+
* @param filterName
120+
* @param filterValues
121+
* - Show only containers where the filter matches the given values
122+
*/
123+
ListContainersCmd withFilter(String filterName, Collection<String> filterValues);
124+
88125
interface Exec extends DockerCmdSyncExec<ListContainersCmd, List<Container>> {
89126
}
90127

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

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package com.github.dockerjava.core.command;
22

3-
import static com.google.common.base.Preconditions.checkArgument;
4-
import static com.google.common.base.Preconditions.checkNotNull;
3+
import com.github.dockerjava.api.command.ListContainersCmd;
4+
import com.github.dockerjava.api.model.Container;
5+
import com.github.dockerjava.core.util.FiltersBuilder;
56

7+
import java.util.Collection;
68
import java.util.List;
79
import java.util.Map;
810

9-
import com.github.dockerjava.api.command.ListContainersCmd;
10-
import com.github.dockerjava.api.model.Container;
11-
import com.github.dockerjava.core.util.FiltersBuilder;
11+
import static com.google.common.base.Preconditions.checkArgument;
12+
import static com.google.common.base.Preconditions.checkNotNull;
1213

1314
/**
1415
* List containers.
@@ -93,10 +94,33 @@ public ListContainersCmd withBefore(String before) {
9394
}
9495

9596
@Override
96-
public ListContainersCmd withLabelFilter(String... labels) {
97-
checkNotNull(labels, "labels was not specified");
98-
this.filters.withLabels(labels);
99-
return this;
97+
public ListContainersCmd withNameFilter(Collection<String> name) {
98+
return withFilter("name", name);
99+
}
100+
101+
@Override
102+
public ListContainersCmd withIdFilter(Collection<String> id) {
103+
return withFilter("id", id);
104+
}
105+
106+
@Override
107+
public ListContainersCmd withAncestorFilter(Collection<String> ancestor) {
108+
return withFilter("ancestor", ancestor);
109+
}
110+
111+
@Override
112+
public ListContainersCmd withVolumeFilter(Collection<String> volume) {
113+
return withFilter("volume", volume);
114+
}
115+
116+
@Override
117+
public ListContainersCmd withNetworkFilter(Collection<String> network) {
118+
return withFilter("network", network);
119+
}
120+
121+
@Override
122+
public ListContainersCmd withLabelFilter(Collection<String> labels) {
123+
return withFilter("label", labels);
100124
}
101125

102126
@Override
@@ -107,14 +131,21 @@ public ListContainersCmd withLabelFilter(Map<String, String> labels) {
107131
}
108132

109133
@Override
110-
public ListContainersCmd withExitcodeFilter(Integer exitcode) {
111-
checkNotNull(exitcode, "exitcode was not specified");
112-
this.filters.withFilter("exitcode", exitcode.toString());
134+
public ListContainersCmd withExitedFilter(Integer exited) {
135+
checkNotNull(exited, "exited was not specified");
136+
this.filters.withFilter("exited", exited.toString());
137+
return this;
138+
}
139+
140+
@Override
141+
public ListContainersCmd withFilter(String filterName, Collection<String> filterValues) {
142+
checkNotNull(filterValues, filterName + " was not specified");
143+
this.filters.withFilter(filterName, filterValues);
113144
return this;
114145
}
115146

116147
@Override
117-
public ListContainersCmd withStatusFilter(String status) {
148+
public ListContainersCmd withStatusFilter(Collection<String> status) {
118149
checkNotNull(status, "status was not specified");
119150
this.filters.withFilter("status", status);
120151
return this;

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

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

33
import java.util.ArrayList;
44
import java.util.Arrays;
5+
import java.util.Collection;
56
import java.util.HashMap;
67
import java.util.List;
78
import java.util.Map;
@@ -25,6 +26,11 @@ public FiltersBuilder withFilter(String key, String... value) {
2526
return this;
2627
}
2728

29+
public FiltersBuilder withFilter(String key, Collection<String> value) {
30+
filters.put(key, value instanceof List ? (List<String>) value : new ArrayList<>(value));
31+
return this;
32+
}
33+
2834
public List<String> getFilter(String key) {
2935
return filters.get(key);
3036
}

0 commit comments

Comments
 (0)