Skip to content

Commit d700d72

Browse files
committed
add labels and attachable properties to network
1 parent ec30edc commit d700d72

File tree

5 files changed

+140
-0
lines changed

5 files changed

+140
-0
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ public interface CreateNetworkCmd extends SyncDockerCmd<CreateNetworkResponse> {
3636
@CheckForNull
3737
Boolean getEnableIPv6();
3838

39+
@CheckForNull
40+
Boolean getAttachable();
41+
42+
@CheckForNull
43+
Map<String, String> getLabels();
44+
3945
/** The new network's name. Required. */
4046
CreateNetworkCmd withName(@Nonnull String name);
4147

@@ -54,6 +60,20 @@ public interface CreateNetworkCmd extends SyncDockerCmd<CreateNetworkResponse> {
5460

5561
CreateNetworkCmd withEnableIpv6(boolean enableIpv6);
5662

63+
/**
64+
* If enabled, and the network is in the global scope, non-service containers on worker nodes will be able to connect to the network.
65+
*
66+
* @since {@link RemoteApiVersion#VERSION_1_21}
67+
*/
68+
CreateNetworkCmd withAttachable(Boolean attachable);
69+
70+
/**
71+
* Add label for network
72+
*
73+
* @since {@link RemoteApiVersion#VERSION_1_24}
74+
*/
75+
CreateNetworkCmd withLabels(Map<String, String> labels);
76+
5777
interface Exec extends DockerCmdSyncExec<CreateNetworkCmd, CreateNetworkResponse> {
5878
}
5979
}

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ public class Network implements Serializable {
3737
@JsonProperty("Options")
3838
private Map<String, String> options;
3939

40+
@JsonProperty("Attachable")
41+
private Boolean attachable;
42+
43+
@JsonProperty("Labels")
44+
public Map<String, String> labels;
45+
4046
public String getId() {
4147
return id;
4248
}
@@ -65,6 +71,14 @@ public Map<String, String> getOptions() {
6571
return options;
6672
}
6773

74+
public Boolean isAttachable() {
75+
return attachable;
76+
}
77+
78+
public Map<String, String> getLabels() {
79+
return labels;
80+
}
81+
6882
@Override
6983
public String toString() {
7084
return ToStringBuilder.reflectionToString(this);
@@ -158,6 +172,9 @@ public static class Config {
158172
@JsonProperty("Gateway")
159173
private String gateway;
160174

175+
@JsonProperty("NetworkID")
176+
private String networkID;
177+
161178
public String getSubnet() {
162179
return subnet;
163180
}
@@ -184,6 +201,14 @@ public Config withGateway(String gateway) {
184201
this.gateway = gateway;
185202
return this;
186203
}
204+
205+
public String getNetworkID() {
206+
return networkID;
207+
}
208+
209+
public void setNetworkID(String networkID) {
210+
this.networkID = networkID;
211+
}
187212
}
188213
}
189214
}

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import com.github.dockerjava.api.model.Network;
1111
import com.github.dockerjava.api.model.Network.Ipam;
1212

13+
import static com.google.common.base.Preconditions.checkNotNull;
14+
1315
public class CreateNetworkCmdImpl extends AbstrDockerCmd<CreateNetworkCmd, CreateNetworkResponse>
1416
implements CreateNetworkCmd {
1517

@@ -34,6 +36,12 @@ public class CreateNetworkCmdImpl extends AbstrDockerCmd<CreateNetworkCmd, Creat
3436
@JsonProperty("EnableIPv6")
3537
private Boolean enableIpv6;
3638

39+
@JsonProperty("Attachable")
40+
private Boolean attachable;
41+
42+
@JsonProperty("Labels")
43+
private Map<String, String> labels;
44+
3745
public CreateNetworkCmdImpl(DockerCmdSyncExec<CreateNetworkCmd, CreateNetworkResponse> execution) {
3846
super(execution);
3947
}
@@ -114,4 +122,33 @@ public CreateNetworkCmd withEnableIpv6(boolean enableIpv6) {
114122
this.enableIpv6 = enableIpv6;
115123
return this;
116124
}
125+
126+
@Override
127+
public Boolean getAttachable() {
128+
return this.attachable;
129+
}
130+
131+
/**
132+
* {@inheritDoc}
133+
*/
134+
@Override
135+
public CreateNetworkCmd withAttachable(Boolean attachable) {
136+
this.attachable = attachable;
137+
return this;
138+
}
139+
140+
@Override
141+
public Map<String, String> getLabels() {
142+
return labels;
143+
}
144+
145+
/**
146+
* {@inheritDoc}
147+
*/
148+
@Override
149+
public CreateNetworkCmd withLabels(Map<String, String> labels) {
150+
checkNotNull(labels, "labels was not specified");
151+
this.labels = labels;
152+
return this;
153+
}
117154
}

src/test/java/com/github/dockerjava/core/command/CreateContainerCmdImplTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,4 +777,28 @@ public void createContainerWithShmPidsLimit() throws DockerException {
777777

778778
assertThat(inspectContainerResponse.getHostConfig().getPidsLimit(), is(hostConfig.getPidsLimit()));
779779
}
780+
781+
@Test
782+
public void createContainerWithNetworkID() {
783+
final RemoteApiVersion apiVersion = getVersion(dockerClient);
784+
if (!apiVersion.isGreaterOrEqual(RemoteApiVersion.VERSION_1_24)) {
785+
throw new SkipException("API version should be >= 1.24");
786+
}
787+
String networkName = "net-" + UUID.randomUUID().toString();
788+
Map<String,String> labels=new HashMap<>();
789+
labels.put("com.example.label","test");
790+
CreateNetworkResponse createNetworkResponse = dockerClient.createNetworkCmd().withName(networkName)
791+
.withLabels(labels).withAttachable(true).exec();
792+
String networkId = createNetworkResponse.getId();
793+
CreateContainerResponse createContainerResponse = dockerClient.createContainerCmd(BUSYBOX_IMAGE).withLabels(labels).withCmd("true").exec();
794+
String containerId = createContainerResponse.getId();
795+
dockerClient.connectToNetworkCmd().withContainerId(containerId).withNetworkId(networkId).exec();
796+
InspectContainerResponse inspectContainerResponse = dockerClient.inspectContainerCmd(containerId).exec();
797+
ContainerNetwork containerNetwork = inspectContainerResponse.getNetworkSettings().getNetworks().get(networkName);
798+
if(containerNetwork==null){
799+
// swarm node used network id
800+
containerNetwork = inspectContainerResponse.getNetworkSettings().getNetworks().get(networkId);
801+
}
802+
assertThat(containerNetwork, notNullValue());
803+
}
780804
}

src/test/java/com/github/dockerjava/core/command/CreateNetworkCmdImplTest.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,20 @@
44
import com.github.dockerjava.api.exception.DockerException;
55
import com.github.dockerjava.api.model.Network;
66
import com.github.dockerjava.client.AbstractDockerClientTest;
7+
import com.github.dockerjava.core.RemoteApiVersion;
78
import org.testng.ITestResult;
9+
import org.testng.SkipException;
810
import org.testng.annotations.AfterMethod;
911
import org.testng.annotations.AfterTest;
1012
import org.testng.annotations.BeforeMethod;
1113
import org.testng.annotations.BeforeTest;
1214
import org.testng.annotations.Test;
1315

1416
import java.lang.reflect.Method;
17+
import java.util.HashMap;
18+
import java.util.Map;
19+
20+
import static com.github.dockerjava.utils.TestUtils.getVersion;
1521

1622
@Test(groups = "integration")
1723
public class CreateNetworkCmdImplTest extends AbstractDockerClientTest {
@@ -64,4 +70,32 @@ public void createNetworkWithIpamConfig() throws DockerException {
6470
assertEquals(network.getDriver(), "bridge");
6571
assertEquals("10.67.79.0/24", network.getIpam().getConfig().iterator().next().getSubnet());
6672
}
73+
74+
@Test
75+
public void createAttachableNetwork() throws DockerException {
76+
final RemoteApiVersion apiVersion = getVersion(dockerClient);
77+
if (!apiVersion.isGreaterOrEqual(RemoteApiVersion.VERSION_1_24)) {
78+
throw new SkipException("API version should be >= 1.24");
79+
}
80+
String networkName = "createAttachableNetwork";
81+
CreateNetworkResponse createNetworkResponse = dockerClient.createNetworkCmd().withName(networkName).withAttachable(true).exec();
82+
assertNotNull(createNetworkResponse.getId());
83+
Network network = dockerClient.inspectNetworkCmd().withNetworkId(createNetworkResponse.getId()).exec();
84+
assertTrue(network.isAttachable());
85+
}
86+
87+
@Test
88+
public void createNetworkWithLabel() throws DockerException {
89+
final RemoteApiVersion apiVersion = getVersion(dockerClient);
90+
if (!apiVersion.isGreaterOrEqual(RemoteApiVersion.VERSION_1_21)) {
91+
throw new SkipException("API version should be >= 1.21");
92+
}
93+
String networkName = "createNetworkWithLabel";
94+
Map<String,String> labels=new HashMap<>();
95+
labels.put("com.example.usage","test");
96+
CreateNetworkResponse createNetworkResponse = dockerClient.createNetworkCmd().withName(networkName).withLabels(labels).exec();
97+
assertNotNull(createNetworkResponse.getId());
98+
Network network = dockerClient.inspectNetworkCmd().withNetworkId(createNetworkResponse.getId()).exec();
99+
assertEquals(network.getLabels(), labels);
100+
}
67101
}

0 commit comments

Comments
 (0)