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
12 changes: 12 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 @@ -149,6 +149,18 @@ public String getIpRange() {
public String getGateway() {
return gateway;
}

public void setSubnet(String subnet) {
this.subnet = subnet;
}

public void setIpRange(String ipRange) {
this.ipRange = ipRange;
}

public void setGateway(String gateway) {
this.gateway = gateway;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ public CreateNetworkCmd withDriver(String driver) {

@Override
public CreateNetworkCmd withIpamConfig(Ipam.Config config) {
if (this.ipam == null) {
this.ipam = new Ipam();
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if it null, why do you need set it? Is this field required for API request?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's an optional config to create a network
https://docs.docker.com/engine/reference/api/docker_remote_api_v1.21/#create-a-network
If I don't set it then the next line will raise NPE, for me it's a bug that prevent user to set custom configuration for network creation.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NPE where? In docker daemon?

this.ipam.getConfig().add(config);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. In this line.

return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,20 @@ public void createNetwork() throws DockerException {
assertEquals(network.getName(), networkName);
assertEquals(network.getDriver(), "bridge");
}

@Test
public void createNetworkWithIpamConfig() throws DockerException {

String networkName = "testNetwork";
Network.Ipam.Config config = new Network.Ipam.Config();
config.setSubnet("10.67.79.0/24");
CreateNetworkResponse createNetworkResponse = dockerClient.createNetworkCmd().withName(networkName).withIpamConfig(config).exec();

assertNotNull(createNetworkResponse.getId());

Network network = dockerClient.inspectNetworkCmd().withNetworkId(createNetworkResponse.getId()).exec();
assertEquals(network.getName(), networkName);
assertEquals(network.getDriver(), "bridge");
assertEquals("10.67.79.0/24", network.getIpam().getConfig().iterator().next().getSubnet());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,20 @@ public void createNetwork() throws DockerException {
assertEquals(network.getName(), networkName);
assertEquals(network.getDriver(), "bridge");
}

@Test
public void createNetworkWithIpamConfig() throws DockerException {

String networkName = "testNetwork";
Network.Ipam.Config config = new Network.Ipam.Config();
config.setSubnet("10.67.79.0/24");
CreateNetworkResponse createNetworkResponse = dockerClient.createNetworkCmd().withName(networkName).withIpamConfig(config).exec();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please create separate test


assertNotNull(createNetworkResponse.getId());

Network network = dockerClient.inspectNetworkCmd().withNetworkId(createNetworkResponse.getId()).exec();
assertEquals(network.getName(), networkName);
assertEquals(network.getDriver(), "bridge");
assertEquals("10.67.79.0/24", network.getIpam().getConfig().iterator().next().getSubnet());
}
}