X Tutup
Skip to content

Commit e3c4675

Browse files
committed
implement links and aliases for user defined networks
add missing network options
1 parent d084518 commit e3c4675

13 files changed

+360
-28
lines changed

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

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

3+
import com.github.dockerjava.api.model.ContainerNetwork;
34
import com.github.dockerjava.core.RemoteApiVersion;
45

56
import javax.annotation.CheckForNull;
@@ -18,10 +19,15 @@ public interface ConnectToNetworkCmd extends SyncDockerCmd<Void> {
1819
@CheckForNull
1920
public String getContainerId();
2021

22+
@CheckForNull
23+
ContainerNetwork getEndpointConfig();
24+
2125
public ConnectToNetworkCmd withNetworkId(@Nonnull String networkId);
2226

2327
public ConnectToNetworkCmd withContainerId(@Nonnull String containerId);
2428

29+
public ConnectToNetworkCmd withEndpointConfig(@Nonnull ContainerNetwork endpointConfig);
30+
2531
public static interface Exec extends DockerCmdSyncExec<ConnectToNetworkCmd, Void> {
2632
}
2733
}

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

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

3-
import java.util.List;
4-
import java.util.Map;
5-
6-
import javax.annotation.CheckForNull;
7-
83
import com.github.dockerjava.api.exception.ConflictException;
94
import com.github.dockerjava.api.exception.NotFoundException;
105
import com.github.dockerjava.api.model.Bind;
@@ -21,8 +16,15 @@
2116
import com.github.dockerjava.api.model.Volume;
2217
import com.github.dockerjava.api.model.VolumesFrom;
2318

19+
import javax.annotation.CheckForNull;
20+
import java.util.List;
21+
import java.util.Map;
22+
2423
public interface CreateContainerCmd extends SyncDockerCmd<CreateContainerResponse> {
2524

25+
@CheckForNull
26+
List<String> getAliases();
27+
2628
@CheckForNull
2729
public Bind[] getBinds();
2830

@@ -92,6 +94,12 @@ public interface CreateContainerCmd extends SyncDockerCmd<CreateContainerRespons
9294
@CheckForNull
9395
public String getImage();
9496

97+
@CheckForNull
98+
String getIpv4Address();
99+
100+
@CheckForNull
101+
String getIpv6Address();
102+
95103
@CheckForNull
96104
Map<String, String> getLabels();
97105

@@ -182,6 +190,18 @@ public interface CreateContainerCmd extends SyncDockerCmd<CreateContainerRespons
182190
@CheckForNull
183191
public Boolean isTty();
184192

193+
/**
194+
* Add network-scoped alias for the container
195+
* @param aliases on ore more aliases
196+
*/
197+
CreateContainerCmd withAliases(String... aliases);
198+
199+
/**
200+
* Add network-scoped alias for the container
201+
* @param aliases on ore more aliases
202+
*/
203+
CreateContainerCmd withAliases(List<String> aliases);
204+
185205
public CreateContainerCmd withAttachStderr(Boolean attachStderr);
186206

187207
public CreateContainerCmd withAttachStdin(Boolean attachStdin);
@@ -301,6 +321,10 @@ public interface CreateContainerCmd extends SyncDockerCmd<CreateContainerRespons
301321

302322
public CreateContainerCmd withImage(String image);
303323

324+
CreateContainerCmd withIpv4Address(String ipv4Address);
325+
326+
CreateContainerCmd withIpv6Address(String ipv6Address);
327+
304328
public CreateContainerCmd withLabels(Map<String, String> labels);
305329

306330
/**

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

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,36 @@ public interface CreateNetworkCmd extends SyncDockerCmd<CreateNetworkResponse> {
2525
@CheckForNull
2626
public Network.Ipam getIpam();
2727

28+
@CheckForNull
29+
public Map<String, String> getOptions();
30+
31+
@CheckForNull
32+
public Boolean getCheckDuplicate();
33+
34+
@CheckForNull
35+
public Boolean getInternal();
36+
37+
@CheckForNull
38+
public Boolean getEnableIPv6();
39+
2840
/** The new network's name. Required. */
2941
public CreateNetworkCmd withName(@Nonnull String name);
3042

31-
/** Optional custom IP scheme for the network. */
32-
public CreateNetworkCmd withIpamConfig(Ipam.Config config);
33-
3443
/** Name of the network driver to use. Defaults to <code>bridge</code>. */
3544
public CreateNetworkCmd withDriver(String driver);
3645

46+
/** Ipam config, such es subnet, gateway and ip range of the network */
47+
CreateNetworkCmd withIpam(Ipam ipam);
48+
3749
/** Driver specific options */
3850
public CreateNetworkCmd withOptions(Map<String, String> options);
3951

52+
public CreateNetworkCmd withCheckDuplicate(boolean checkForDuplicate);
53+
54+
public CreateNetworkCmd withInternal(boolean internal);
55+
56+
public CreateNetworkCmd withEnableIpv6(boolean enableIpv6);
57+
4058
public static interface Exec extends DockerCmdSyncExec<CreateNetworkCmd, CreateNetworkResponse> {
4159
}
4260
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,14 @@ public interface DisconnectFromNetworkCmd extends SyncDockerCmd<Void> {
1818
@CheckForNull
1919
public String getContainerId();
2020

21+
Boolean getForce();
22+
2123
public DisconnectFromNetworkCmd withNetworkId(@Nonnull String networkId);
2224

2325
public DisconnectFromNetworkCmd withContainerId(@Nonnull String containerId);
2426

27+
DisconnectFromNetworkCmd withContainerId(boolean force);
28+
2529
public static interface Exec extends DockerCmdSyncExec<DisconnectFromNetworkCmd, Void> {
2630
}
2731
}

src/main/java/com/github/dockerjava/api/model/ContainerNetwork.java

Lines changed: 63 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.dockerjava.api.model;
22

3+
import com.fasterxml.jackson.annotation.JsonIgnore;
34
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
45
import com.fasterxml.jackson.annotation.JsonProperty;
56
import com.github.dockerjava.core.RemoteApiVersion;
@@ -8,9 +9,14 @@
89
import org.apache.commons.lang.builder.ToStringBuilder;
910

1011
import javax.annotation.CheckForNull;
12+
import java.util.Arrays;
1113
import java.util.List;
1214

1315
/**
16+
* Types taken form
17+
* {@see https://github.com/docker/engine-api/blob/release/1.10/types/network/network.go}
18+
* Docker named it EndpointSettings
19+
*
1420
* @see ContainerNetworkSettings
1521
* @author Kanstantsin Shautsou
1622
*/
@@ -20,16 +26,16 @@ public class ContainerNetwork {
2026
* FIXME verify
2127
*/
2228
@JsonProperty("IPAMConfig")
23-
private Network.Ipam.Config ipamConfig;
29+
private Ipam ipamConfig;
2430

2531
/**
2632
* FIXME verify
2733
*/
2834
@JsonProperty("Links")
29-
private List<String> links;
35+
private Links links;
3036

3137
/**
32-
* FIXME no docs, unknown field.
38+
* Add network-scoped alias for the container
3339
* Type picked from `docker/vendor/src/github.com/docker/engine-api/types/network/network.go`
3440
*
3541
* @since {@link RemoteApiVersion#VERSION_1_22}
@@ -80,6 +86,14 @@ public ContainerNetwork withAliases(List<String> aliases) {
8086
return this;
8187
}
8288

89+
/**
90+
* @see #aliases
91+
*/
92+
public ContainerNetwork withAliases(String... aliases) {
93+
this.aliases = Arrays.asList(aliases);
94+
return this;
95+
}
96+
8397
/**
8498
* @see #endpointId
8599
*/
@@ -155,7 +169,7 @@ public String getIpAddress() {
155169
/**
156170
* @see #ipAddress
157171
*/
158-
public ContainerNetwork withIpAddress(String ipAddress) {
172+
public ContainerNetwork withIpv4Address(String ipAddress) {
159173
this.ipAddress = ipAddress;
160174
return this;
161175
}
@@ -164,14 +178,14 @@ public ContainerNetwork withIpAddress(String ipAddress) {
164178
* @see #ipamConfig
165179
*/
166180
@CheckForNull
167-
public Network.Ipam.Config getIpamConfig() {
181+
public Ipam getIpamConfig() {
168182
return ipamConfig;
169183
}
170184

171185
/**
172186
* @see #ipamConfig
173187
*/
174-
public ContainerNetwork withIpamConfig(Network.Ipam.Config ipamConfig) {
188+
public ContainerNetwork withIpamConfig(Ipam ipamConfig) {
175189
this.ipamConfig = ipamConfig;
176190
return this;
177191
}
@@ -212,15 +226,24 @@ public ContainerNetwork withIpV6Gateway(String ipV6Gateway) {
212226
* @see #links
213227
*/
214228
@CheckForNull
215-
public List<String> getLinks() {
216-
return links;
229+
@JsonIgnore
230+
public Link[] getLinks() {
231+
return links == null ? new Link[0] : links.getLinks();
232+
}
233+
234+
/**
235+
* @see #links
236+
*/
237+
public ContainerNetwork withLinks(List<Link> links) {
238+
this.links = new Links(links);
239+
return this;
217240
}
218241

219242
/**
220243
* @see #links
221244
*/
222-
public ContainerNetwork withLinks(List<String> links) {
223-
this.links = links;
245+
public ContainerNetwork withLinks(Link... links) {
246+
this.links = new Links(links);
224247
return this;
225248
}
226249

@@ -270,4 +293,34 @@ public boolean equals(Object o) {
270293
public int hashCode() {
271294
return HashCodeBuilder.reflectionHashCode(this);
272295
}
296+
297+
/**
298+
* Docker named it EndpointIPAMConfig
299+
*/
300+
public static class Ipam {
301+
302+
@JsonProperty("IPv4Address")
303+
private String ipv4Address;
304+
305+
@JsonProperty("IPv6Address")
306+
private String ipv6Address;
307+
308+
public String getIpv4Address() {
309+
return ipv4Address;
310+
}
311+
312+
public String getIpv6Address() {
313+
return ipv6Address;
314+
}
315+
316+
public Ipam withIpv4Address(String ipv4Address) {
317+
this.ipv4Address = ipv4Address;
318+
return this;
319+
}
320+
321+
public Ipam withIpv6Address(String ipv6Address) {
322+
this.ipv6Address = ipv6Address;
323+
return this;
324+
}
325+
}
273326
}

src/main/java/com/github/dockerjava/api/model/ContainerNetworkSettings.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.dockerjava.api.model;
22

3+
import com.fasterxml.jackson.annotation.JsonIgnore;
34
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
45
import com.fasterxml.jackson.annotation.JsonProperty;
56
import com.github.dockerjava.core.RemoteApiVersion;
@@ -30,6 +31,16 @@ public Map<String, ContainerNetwork> getNetworks() {
3031
return networks;
3132
}
3233

34+
@JsonIgnore
35+
public ContainerNetwork getOrCreateNetwork(String name) {
36+
ContainerNetwork containerNetwork = networks.get(name);
37+
if (containerNetwork == null) {
38+
containerNetwork = new ContainerNetwork();
39+
networks.put(name, containerNetwork);
40+
}
41+
return containerNetwork;
42+
}
43+
3344
/**
3445
* @see #networks
3546
*/

src/main/java/com/github/dockerjava/api/model/HostConfig.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.apache.commons.lang.builder.ToStringBuilder;
1212

1313
import javax.annotation.CheckForNull;
14+
import java.util.Arrays;
1415
import java.util.List;
1516

1617
/**
@@ -21,6 +22,8 @@
2122
@JsonInclude(Include.NON_NULL)
2223
public class HostConfig {
2324

25+
private static final List<String> PREDEFINED_NETWORKS = Arrays.asList("bridge", "host", "none");
26+
2427
@JsonProperty("Binds")
2528
private Binds binds;
2629

@@ -426,6 +429,15 @@ public String getVolumeDriver() {
426429
return volumeDriver;
427430
}
428431

432+
/**
433+
* Parse the network mode as specified at
434+
* {@see https://github.com/docker/engine-api/blob/master/types/container/hostconfig_unix.go}
435+
*/
436+
@JsonIgnore
437+
public boolean isUserDefinedNetwork() {
438+
return networkMode != null && !PREDEFINED_NETWORKS.contains(networkMode) && !networkMode.startsWith("container:");
439+
}
440+
429441
@JsonIgnore
430442
public void setBinds(Bind... binds) {
431443
this.binds = new Binds(binds);

src/main/java/com/github/dockerjava/api/model/Network.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.apache.commons.lang.builder.ToStringBuilder;
77

88
import java.util.ArrayList;
9+
import java.util.Arrays;
910
import java.util.List;
1011
import java.util.Map;
1112

@@ -121,6 +122,16 @@ public List<Config> getConfig() {
121122
return config;
122123
}
123124

125+
public Ipam withConfig(List<Config> ipamConfigs) {
126+
config = ipamConfigs;
127+
return this;
128+
}
129+
130+
public Ipam withConfig(Config... ipamConfigs) {
131+
config = Arrays.asList(ipamConfigs);
132+
return this;
133+
}
134+
124135
@Override
125136
public String toString() {
126137
return ToStringBuilder.reflectionToString(this);
@@ -149,6 +160,21 @@ public String getIpRange() {
149160
public String getGateway() {
150161
return gateway;
151162
}
163+
164+
public Config withSubnet(String subnet) {
165+
this.subnet = subnet;
166+
return this;
167+
}
168+
169+
public Config withIpRange(String ipRange) {
170+
this.ipRange = ipRange;
171+
return this;
172+
}
173+
174+
public Config withGateway(String gateway) {
175+
this.gateway = gateway;
176+
return this;
177+
}
152178
}
153179
}
154180
}

0 commit comments

Comments
 (0)
X Tutup