Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ public interface CreateNetworkCmd extends SyncDockerCmd<CreateNetworkResponse> {
@CheckForNull
Boolean getEnableIPv6();

@CheckForNull
Boolean getAttachable();

@CheckForNull
Map<String, String> getLabels();

/** The new network's name. Required. */
CreateNetworkCmd withName(@Nonnull String name);

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

CreateNetworkCmd withEnableIpv6(boolean enableIpv6);

/**
* If enabled, and the network is in the global scope, non-service containers on worker nodes will be able to connect to the network.
*
* @since {@link RemoteApiVersion#VERSION_1_21}
*/
CreateNetworkCmd withAttachable(Boolean attachable);

/**
* Add label for network
*
* @since {@link RemoteApiVersion#VERSION_1_24}
*/
CreateNetworkCmd withLabels(Map<String, String> labels);

interface Exec extends DockerCmdSyncExec<CreateNetworkCmd, CreateNetworkResponse> {
}
}
25 changes: 25 additions & 0 deletions src/main/java/com/github/dockerjava/api/model/Network.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ public class Network implements Serializable {
@JsonProperty("Options")
private Map<String, String> options;

@JsonProperty("Attachable")
private Boolean attachable;

@JsonProperty("Labels")
public Map<String, String> labels;

public String getId() {
return id;
}
Expand Down Expand Up @@ -65,6 +71,14 @@ public Map<String, String> getOptions() {
return options;
}

public Boolean isAttachable() {
return attachable;
}

public Map<String, String> getLabels() {
return labels;
}

@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
Expand Down Expand Up @@ -158,6 +172,9 @@ public static class Config {
@JsonProperty("Gateway")
private String gateway;

@JsonProperty("NetworkID")
private String networkID;

public String getSubnet() {
return subnet;
}
Expand All @@ -184,6 +201,14 @@ public Config withGateway(String gateway) {
this.gateway = gateway;
return this;
}

public String getNetworkID() {
return networkID;
}

public void setNetworkID(String networkID) {
this.networkID = networkID;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import com.github.dockerjava.api.model.Network;
import com.github.dockerjava.api.model.Network.Ipam;

import static com.google.common.base.Preconditions.checkNotNull;

public class CreateNetworkCmdImpl extends AbstrDockerCmd<CreateNetworkCmd, CreateNetworkResponse>
implements CreateNetworkCmd {

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

@JsonProperty("Attachable")
private Boolean attachable;

@JsonProperty("Labels")
private Map<String, String> labels;

public CreateNetworkCmdImpl(DockerCmdSyncExec<CreateNetworkCmd, CreateNetworkResponse> execution) {
super(execution);
}
Expand Down Expand Up @@ -114,4 +122,33 @@ public CreateNetworkCmd withEnableIpv6(boolean enableIpv6) {
this.enableIpv6 = enableIpv6;
return this;
}

@Override
public Boolean getAttachable() {
return this.attachable;
}

/**
* {@inheritDoc}
*/
@Override
public CreateNetworkCmd withAttachable(Boolean attachable) {
this.attachable = attachable;
return this;
}

@Override
public Map<String, String> getLabels() {
return labels;
}

/**
* {@inheritDoc}
*/
@Override
public CreateNetworkCmd withLabels(Map<String, String> labels) {
checkNotNull(labels, "labels was not specified");
this.labels = labels;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -777,4 +777,28 @@ public void createContainerWithShmPidsLimit() throws DockerException {

assertThat(inspectContainerResponse.getHostConfig().getPidsLimit(), is(hostConfig.getPidsLimit()));
}

@Test
public void createContainerWithNetworkID() {
final RemoteApiVersion apiVersion = getVersion(dockerClient);
if (!apiVersion.isGreaterOrEqual(RemoteApiVersion.VERSION_1_24)) {
throw new SkipException("API version should be >= 1.24");
}
String networkName = "net-" + UUID.randomUUID().toString();
Map<String,String> labels=new HashMap<>();
labels.put("com.example.label","test");
CreateNetworkResponse createNetworkResponse = dockerClient.createNetworkCmd().withName(networkName)
.withLabels(labels).withAttachable(true).exec();
String networkId = createNetworkResponse.getId();
CreateContainerResponse createContainerResponse = dockerClient.createContainerCmd(BUSYBOX_IMAGE).withLabels(labels).withCmd("true").exec();
String containerId = createContainerResponse.getId();
dockerClient.connectToNetworkCmd().withContainerId(containerId).withNetworkId(networkId).exec();
InspectContainerResponse inspectContainerResponse = dockerClient.inspectContainerCmd(containerId).exec();
ContainerNetwork containerNetwork = inspectContainerResponse.getNetworkSettings().getNetworks().get(networkName);
if(containerNetwork==null){
// swarm node used network id
containerNetwork = inspectContainerResponse.getNetworkSettings().getNetworks().get(networkId);
}
assertThat(containerNetwork, notNullValue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,20 @@
import com.github.dockerjava.api.exception.DockerException;
import com.github.dockerjava.api.model.Network;
import com.github.dockerjava.client.AbstractDockerClientTest;
import com.github.dockerjava.core.RemoteApiVersion;
import org.testng.ITestResult;
import org.testng.SkipException;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import static com.github.dockerjava.utils.TestUtils.getVersion;

@Test(groups = "integration")
public class CreateNetworkCmdImplTest extends AbstractDockerClientTest {
Expand Down Expand Up @@ -64,4 +70,32 @@ public void createNetworkWithIpamConfig() throws DockerException {
assertEquals(network.getDriver(), "bridge");
assertEquals("10.67.79.0/24", network.getIpam().getConfig().iterator().next().getSubnet());
}

@Test
public void createAttachableNetwork() throws DockerException {
final RemoteApiVersion apiVersion = getVersion(dockerClient);
if (!apiVersion.isGreaterOrEqual(RemoteApiVersion.VERSION_1_24)) {
throw new SkipException("API version should be >= 1.24");
}
String networkName = "createAttachableNetwork";
CreateNetworkResponse createNetworkResponse = dockerClient.createNetworkCmd().withName(networkName).withAttachable(true).exec();
assertNotNull(createNetworkResponse.getId());
Network network = dockerClient.inspectNetworkCmd().withNetworkId(createNetworkResponse.getId()).exec();
assertTrue(network.isAttachable());
}

@Test
public void createNetworkWithLabel() throws DockerException {
final RemoteApiVersion apiVersion = getVersion(dockerClient);
if (!apiVersion.isGreaterOrEqual(RemoteApiVersion.VERSION_1_21)) {
throw new SkipException("API version should be >= 1.21");
}
String networkName = "createNetworkWithLabel";
Map<String,String> labels=new HashMap<>();
labels.put("com.example.usage","test");
CreateNetworkResponse createNetworkResponse = dockerClient.createNetworkCmd().withName(networkName).withLabels(labels).exec();
assertNotNull(createNetworkResponse.getId());
Network network = dockerClient.inspectNetworkCmd().withNetworkId(createNetworkResponse.getId()).exec();
assertEquals(network.getLabels(), labels);
}
}