Skip to content

Commit

Permalink
Merge pull request spotify#307 from damianopezzotti/master
Browse files Browse the repository at this point in the history
Docker 1.21 API - networks support
  • Loading branch information
davidxia committed Dec 29, 2015
2 parents 9500e87 + 2fdac4f commit 3fc4b5f
Show file tree
Hide file tree
Showing 10 changed files with 971 additions and 94 deletions.
95 changes: 89 additions & 6 deletions src/main/java/com/spotify/docker/client/DefaultDockerClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
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.NetworkConfig;
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;
Expand Down Expand Up @@ -74,6 +77,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
Expand Down Expand Up @@ -179,6 +183,9 @@ public void progress(ProgressMessage message) throws DockerException {
private static final GenericType<List<Image>> IMAGE_LIST =
new GenericType<List<Image>>() {};

private static final GenericType<List<Network>> NETWORK_LIST =
new GenericType<List<Network>>() {};

private static final GenericType<List<ImageSearchResult>> IMAGES_SEARCH_RESULT_LIST =
new GenericType<List<ImageSearchResult>>() {};

Expand Down Expand Up @@ -303,7 +310,7 @@ private Registry<ConnectionSocketFactory> getSchemeRegistry(final Builder builde
final RegistryBuilder<ConnectionSocketFactory> registryBuilder = RegistryBuilder
.<ConnectionSocketFactory>create()
.register("https", https)
.register("http", PlainConnectionSocketFactory.getSocketFactory());
.register("http", PlainConnectionSocketFactory.getSocketFactory());

if (builder.uri.getScheme().equals(UNIX_SCHEME)) {
registryBuilder.register(UNIX_SCHEME, new UnixConnectionSocketFactory(builder.uri));
Expand Down Expand Up @@ -508,7 +515,7 @@ public void stopContainer(final String containerId, final int secondsToWaitBefor
try {
final WebTarget resource = noTimeoutResource()
.path("containers").path(containerId).path("stop")
.queryParam("t", String.valueOf(secondsToWaitBeforeKilling));
.queryParam("t", String.valueOf(secondsToWaitBeforeKilling));
request(POST, resource, resource.request());
} catch (DockerRequestException e) {
switch (e.status()) {
Expand Down Expand Up @@ -646,7 +653,7 @@ public ContainerCreation commitContainer(final String containerId,
.path("commit")
.queryParam("container", containerId)
.queryParam("repo", repo)
.queryParam("comment", comment);
.queryParam("comment", comment);

if (!isNullOrEmpty(author)) {
resource = resource.queryParam("author", author);
Expand Down Expand Up @@ -782,7 +789,7 @@ public void pull(final String image, final AuthConfig authConfig, final Progress
request(POST, ProgressStream.class, resource,
resource
.request(APPLICATION_JSON_TYPE)
.header("X-Registry-Auth", authHeader(authConfig)))) {
.header("X-Registry-Auth", authHeader(authConfig)))) {
pull.tail(handler, POST, resource.getUri());
} catch (IOException e) {
throw new DockerException(e);
Expand Down Expand Up @@ -958,7 +965,7 @@ public List<RemovedImage> removeImage(String image, boolean force, boolean noPru
try {
final WebTarget resource = resource().path("images").path(image)
.queryParam("force", String.valueOf(force))
.queryParam("noprune", String.valueOf(noPrune));
.queryParam("noprune", String.valueOf(noPrune));
return request(DELETE, REMOVED_IMAGE_LIST, resource, resource.request(APPLICATION_JSON_TYPE));
} catch (DockerRequestException e) {
switch (e.status()) {
Expand Down Expand Up @@ -1137,6 +1144,82 @@ public ContainerStats stats(final String containerId)
}
}

@Override
public List<Network> listNetworks() throws DockerException, InterruptedException {
final WebTarget resource = resource().path("networks");
return request(GET, NETWORK_LIST, resource, resource.request(APPLICATION_JSON_TYPE));
}

@Override
public Network inspectNetwork(String networkId) throws DockerException, InterruptedException {
final WebTarget resource = resource().path("networks").path(networkId);
try {
return request(GET, Network.class, resource, resource.request(APPLICATION_JSON_TYPE));
} catch (DockerRequestException e) {
switch (e.status()) {
case 404:
throw new NetworkNotFoundException(networkId, e);
default:
throw e;
}
}
}

@Override
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(networkConfig));
}

@Override
public void removeNetwork(String networkId) throws DockerException, InterruptedException {
try {
final WebTarget resource = resource().path("networks").path(networkId);
request(DELETE, resource, resource.request(APPLICATION_JSON_TYPE));
} catch (DockerRequestException e) {
switch (e.status()) {
case 404:
throw new NetworkNotFoundException(networkId, e);
default:
throw e;
}
}
}

@Override
public void connectToNetwork(String containerId, String networkId)
throws DockerException, InterruptedException {
manageNetworkConnection(containerId, "connect", networkId);
}

@Override
public void disconnectFromNetwork(String containerId, String networkId)
throws DockerException, InterruptedException {
manageNetworkConnection(containerId, "disconnect", networkId);
}

private void manageNetworkConnection(String containerId, String methodname, String networkId)
throws DockerException, InterruptedException {
final WebTarget resource = resource().path("networks").path(networkId).path(methodname);

Map<String, String> request = new HashMap<>();
request.put("Container", containerId);
Response response =
request(POST, Response.class, resource, resource.request(APPLICATION_JSON_TYPE),
Entity.json(request));
switch (response.getStatus()) {
case 200:
return;
case 404:
throw new ContainerNotFoundException(containerId);
case 500:
throw new DockerException(response.readEntity(String.class));
}
}

private WebTarget resource() {
final WebTarget target = client.target(uri);
if (!isNullOrEmpty(apiVersion)) {
Expand Down Expand Up @@ -1222,7 +1305,7 @@ private RuntimeException propagate(final String method, final WebTarget resource
throw new DockerRequestException(method, resource.getUri(), response.getStatus(),
message(response), cause);
} else if ((cause instanceof SocketTimeoutException) ||
(cause instanceof ConnectTimeoutException)) {
(cause instanceof ConnectTimeoutException)) {
throw new DockerTimeoutException(method, resource.getUri(), e);
} else if ((cause instanceof InterruptedIOException)
|| (cause instanceof InterruptedException)) {
Expand Down
Loading

0 comments on commit 3fc4b5f

Please sign in to comment.