Skip to content

Commit

Permalink
fully implemented creation API
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco Gulino committed Dec 9, 2015
1 parent 05cc2b2 commit 81ac682
Show file tree
Hide file tree
Showing 7 changed files with 459 additions and 388 deletions.
23 changes: 3 additions & 20 deletions src/main/java/com/spotify/docker/client/DefaultDockerClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,7 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.spotify.docker.client.messages.AuthConfig;
import com.spotify.docker.client.messages.AuthRegistryConfig;
import com.spotify.docker.client.messages.Container;
import com.spotify.docker.client.messages.ContainerConfig;
import com.spotify.docker.client.messages.ContainerCreation;
import com.spotify.docker.client.messages.ContainerExit;
import com.spotify.docker.client.messages.ContainerInfo;
import com.spotify.docker.client.messages.ContainerStats;
import com.spotify.docker.client.messages.ExecState;
import com.spotify.docker.client.messages.Image;
import com.spotify.docker.client.messages.ImageInfo;
import com.spotify.docker.client.messages.ImageSearchResult;
import com.spotify.docker.client.messages.Info;
import com.spotify.docker.client.messages.Network;
import com.spotify.docker.client.messages.NetworkCreation;
import com.spotify.docker.client.messages.ProgressMessage;
import com.spotify.docker.client.messages.RemovedImage;
import com.spotify.docker.client.messages.Version;
import com.spotify.docker.client.messages.*;

import org.apache.http.client.config.RequestConfig;
import org.apache.http.config.Registry;
Expand Down Expand Up @@ -1053,12 +1036,12 @@ public Network inspectNetwork(String networkId) throws DockerException, Interrup
}

@Override
public NetworkCreation createNetwork(Network network)
public NetworkCreation createNetwork(NetworkConfig networkConfig)
throws DockerException, InterruptedException {
final WebTarget resource = resource().path("networks").path("create");

return request(POST, NetworkCreation.class, resource,
resource.request(APPLICATION_JSON_TYPE), Entity.json(network));
resource.request(APPLICATION_JSON_TYPE), Entity.json(networkConfig));
}

@Override
Expand Down
21 changes: 3 additions & 18 deletions src/main/java/com/spotify/docker/client/DockerClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,7 @@

import com.google.common.base.Throwables;

import com.spotify.docker.client.messages.AuthConfig;
import com.spotify.docker.client.messages.Container;
import com.spotify.docker.client.messages.ContainerConfig;
import com.spotify.docker.client.messages.ContainerCreation;
import com.spotify.docker.client.messages.ContainerExit;
import com.spotify.docker.client.messages.ContainerInfo;
import com.spotify.docker.client.messages.ContainerStats;
import com.spotify.docker.client.messages.ExecState;
import com.spotify.docker.client.messages.Image;
import com.spotify.docker.client.messages.ImageInfo;
import com.spotify.docker.client.messages.ImageSearchResult;
import com.spotify.docker.client.messages.Info;
import com.spotify.docker.client.messages.Network;
import com.spotify.docker.client.messages.NetworkCreation;
import com.spotify.docker.client.messages.RemovedImage;
import com.spotify.docker.client.messages.Version;
import com.spotify.docker.client.messages.*;

import java.io.Closeable;
import java.io.IOException;
Expand Down Expand Up @@ -653,12 +638,12 @@ public String getName() {
/**
* Create a new network
*
* @param network
* @param networkConfig The network creation parameters
* @return
* @throws DockerException if a server error occurred (500)
* @throws InterruptedException If the thread is interrupted
*/
NetworkCreation createNetwork(final Network network) throws DockerException, InterruptedException;
NetworkCreation createNetwork(final NetworkConfig networkConfig) throws DockerException, InterruptedException;

/**
* Remove a docker network.
Expand Down
28 changes: 20 additions & 8 deletions src/main/java/com/spotify/docker/client/messages/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,37 @@
@JsonAutoDetect(fieldVisibility = ANY, getterVisibility = NONE, setterVisibility = NONE)
public class Config {

@JsonProperty("Subnet") private String subnet;
@JsonProperty("IPRange") private String ipRange;
@JsonProperty("Gateway") private String gateway;
@JsonProperty("Subnet")
private String subnet;
@JsonProperty("IPRange")
private String ipRange;
@JsonProperty("Gateway")
private String gateway;

public String subnet() {
return subnet;
}

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

public String ipRange() {
return ipRange;
}

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

public String gateway() {
return gateway;
}

public void gateway(final String gateway) {
this.gateway = gateway;
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand All @@ -67,11 +82,8 @@ public int hashCode() {

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("subnet", subnet)
.add("ipRange", ipRange)
.add("gateway", gateway)
.toString();
return MoreObjects.toStringHelper(this).add("subnet", subnet).add("ipRange", ipRange)
.add("gateway", gateway).toString();
}

}
37 changes: 37 additions & 0 deletions src/main/java/com/spotify/docker/client/messages/Ipam.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.google.common.base.MoreObjects;
import com.google.common.base.Objects;

import java.util.ArrayList;
import java.util.List;

import static com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.ANY;
Expand All @@ -30,6 +31,14 @@
@JsonAutoDetect(fieldVisibility = ANY, getterVisibility = NONE, setterVisibility = NONE)
public class Ipam {

public Ipam() {
}

public Ipam(final String driver, final List<Config> config) {
this.driver = driver;
this.config = config;
}

@JsonProperty("Driver") private String driver;
@JsonProperty("Config") private List<Config> config;

Expand Down Expand Up @@ -69,4 +78,32 @@ public String toString() {
.toString();
}


public static Builder builder() {
return new Builder();
}


public static class Builder {
private String driver;
private List<Config> configs = new ArrayList<Config>();

public Builder driver(final String driver) {
this.driver = driver;
return this;
}

public Builder config(final String subnet, final String ipRange, final String gateway ) {
Config config = new Config();
config.subnet(subnet);
config.ipRange(ipRange);
config.gateway(gateway);
configs.add(config);
return this;
}

public Ipam build() {
return new Ipam(this.driver, this.configs);
}
}
}
19 changes: 11 additions & 8 deletions src/main/java/com/spotify/docker/client/messages/Network.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
import com.google.common.base.Objects;
import com.google.common.collect.ImmutableMap;

import java.util.HashMap;
import java.util.Map;

import static com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.ANY;
import static com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.NONE;

Expand All @@ -34,14 +37,14 @@ public class Network {
@JsonProperty("Scope") private String scope;
@JsonProperty("Driver") private String driver;
@JsonProperty("IPAM") private Ipam ipam;
@JsonProperty("Options") private ImmutableMap<String, String> options;
@JsonProperty("Options") private Map<String, String> options;

private Network() {
}

private Network(final Builder builder) {
this.name = builder.name;
this.scope = builder.scope;
this.options = builder.options;
this.driver = builder.driver;
}

Expand All @@ -60,7 +63,7 @@ public String scope() {
public String driver() {
return driver;
}
public ImmutableMap<String, String> options() {
public Map<String, String> options() {
return options;
}

Expand Down Expand Up @@ -109,15 +112,15 @@ public String toString() {
public static class Builder {

private String name;
private String scope;
private Map<String, String> options = new HashMap<String, String>();
private String driver;

private Builder() {
}

private Builder(Network network) {
this.name = network.name;
this.scope = network.scope;
this.options = network.options;
this.driver = network.driver;
}

Expand All @@ -128,9 +131,9 @@ public Builder name(final String name) {
return this;
}

public Builder scope(final String scope) {
if (scope != null && !scope.isEmpty()) {
this.scope = scope;
public Builder option(final String key, final String value) {
if (key != null && !key.isEmpty()) {
this.options.put(key, value);
}
return this;
}
Expand Down
Loading

0 comments on commit 81ac682

Please sign in to comment.