From 0cea1efd2f530c4e100c5f5430c2ab7fa5b88872 Mon Sep 17 00:00:00 2001 From: Andrew Kennedy Date: Mon, 6 Apr 2015 16:34:51 +0100 Subject: [PATCH] Add template option to specify direct (unmapped) ports --- .../options/DockerTemplateOptions.java | 53 ++++++++++++------- .../strategy/DockerComputeServiceAdapter.java | 12 +++++ 2 files changed, 46 insertions(+), 19 deletions(-) diff --git a/apis/docker/src/main/java/org/jclouds/docker/compute/options/DockerTemplateOptions.java b/apis/docker/src/main/java/org/jclouds/docker/compute/options/DockerTemplateOptions.java index 355c617f9d..4c2035c6d0 100644 --- a/apis/docker/src/main/java/org/jclouds/docker/compute/options/DockerTemplateOptions.java +++ b/apis/docker/src/main/java/org/jclouds/docker/compute/options/DockerTemplateOptions.java @@ -54,6 +54,7 @@ public class DockerTemplateOptions extends TemplateOptions implements Cloneable protected Optional> commands = Optional.absent(); protected Optional> volumes = Optional.absent(); protected Optional> env = Optional.absent(); + protected Optional> directPorts = Optional.absent(); @Override public DockerTemplateOptions clone() { @@ -84,10 +85,13 @@ public class DockerTemplateOptions extends TemplateOptions implements Cloneable } if (cpuShares.isPresent()) { eTo.cpuShares(cpuShares.get()); - } + } if (env.isPresent()) { eTo.env(env.get()); - } + } + if (directPorts.isPresent()) { + eTo.directPorts(directPorts.get()); + } } } @@ -104,12 +108,13 @@ public class DockerTemplateOptions extends TemplateOptions implements Cloneable equal(this.memory, that.memory) && equal(this.commands, that.commands) && equal(this.cpuShares, that.cpuShares) && - equal(this.env, that.env); + equal(this.env, that.env) && + equal(this.directPorts, that.directPorts); } @Override public int hashCode() { - return Objects.hashCode(super.hashCode(), volumes, hostname, dns, memory, commands, cpuShares, env); + return Objects.hashCode(super.hashCode(), volumes, hostname, dns, memory, commands, cpuShares, env, directPorts); } @Override @@ -122,6 +127,7 @@ public class DockerTemplateOptions extends TemplateOptions implements Cloneable .add("commands", commands) .add("volumes", volumes) .add("env", env) + .add("directPorts", directPorts) .toString(); } @@ -168,35 +174,36 @@ public class DockerTemplateOptions extends TemplateOptions implements Cloneable return this; } - public Optional> getVolumes() { - return volumes; + public DockerTemplateOptions directPorts(List ports) { + this.directPorts = Optional.> of(ImmutableList.copyOf(ports)); + return this; } + public Optional> getVolumes() { return volumes; } + public Optional getDns() { return dns; } public Optional getHostname() { return hostname; } public Optional getMemory() { return memory; } - public Optional> getCommands() { - return commands; - } + public Optional> getCommands() { return commands; } public Optional getCpuShares() { return cpuShares; } - public Optional> getEnv() { - return env; - } + public Optional> getEnv() { return env; } + + public Optional> getDirectPorts() { return directPorts; } public static class Builder { - /** - * @see DockerTemplateOptions#volumes(java.util.Map) - */ - public static DockerTemplateOptions volumes(Map volumes) { - DockerTemplateOptions options = new DockerTemplateOptions(); - return DockerTemplateOptions.class.cast(options.volumes(volumes)); - } + /** + * @see DockerTemplateOptions#volumes(java.util.Map) + */ + public static DockerTemplateOptions volumes(Map volumes) { + DockerTemplateOptions options = new DockerTemplateOptions(); + return DockerTemplateOptions.class.cast(options.volumes(volumes)); + } /** * @see DockerTemplateOptions#dns(String) @@ -251,6 +258,14 @@ public class DockerTemplateOptions extends TemplateOptions implements Cloneable return DockerTemplateOptions.class.cast(options.env(env)); } + /** + * @see DockerTemplateOptions#directPorts(java.util.List) + */ + public static DockerTemplateOptions directPorts(List directPorts) { + DockerTemplateOptions options = new DockerTemplateOptions(); + return DockerTemplateOptions.class.cast(options.directPorts(directPorts)); + } + // methods that only facilitate returning the correct object type /** diff --git a/apis/docker/src/main/java/org/jclouds/docker/compute/strategy/DockerComputeServiceAdapter.java b/apis/docker/src/main/java/org/jclouds/docker/compute/strategy/DockerComputeServiceAdapter.java index 920071b178..61b5041388 100644 --- a/apis/docker/src/main/java/org/jclouds/docker/compute/strategy/DockerComputeServiceAdapter.java +++ b/apis/docker/src/main/java/org/jclouds/docker/compute/strategy/DockerComputeServiceAdapter.java @@ -19,6 +19,8 @@ package org.jclouds.docker.compute.strategy; import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.collect.Iterables.find; +import java.util.Arrays; +import java.util.List; import java.util.Map; import java.util.Set; @@ -49,7 +51,9 @@ import org.jclouds.logging.Logger; import com.google.common.base.Predicate; import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Lists; import com.google.common.collect.Maps; import com.google.common.collect.Sets; @@ -136,6 +140,14 @@ public class DockerComputeServiceAdapter implements .publishAllPorts(true) .privileged(true); + if (templateOptions.getDirectPorts().isPresent()) { + Map>> portBindings = Maps.newHashMap(); + for (Integer port : templateOptions.getDirectPorts().get()) { + portBindings.put(port + "/tcp", Lists.>newArrayList(ImmutableMap.of("HostPort", Integer.toString(port)))); + } + hostConfigBuilder.portBindings(portBindings); + } + if (templateOptions.getDns().isPresent()) { hostConfigBuilder.dns(templateOptions.getDns().get()); }