Add template option to specify direct (unmapped) ports

This commit is contained in:
Andrew Kennedy 2015-04-06 16:34:51 +01:00 committed by Andrea Turli
parent 944f14c687
commit 0cea1efd2f
2 changed files with 46 additions and 19 deletions

View File

@ -54,6 +54,7 @@ public class DockerTemplateOptions extends TemplateOptions implements Cloneable
protected Optional<List<String>> commands = Optional.absent();
protected Optional<Map<String, String>> volumes = Optional.absent();
protected Optional<List<String>> env = Optional.absent();
protected Optional<List<Integer>> 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<Map<String, String>> getVolumes() {
return volumes;
public DockerTemplateOptions directPorts(List<Integer> ports) {
this.directPorts = Optional.<List<Integer>> of(ImmutableList.copyOf(ports));
return this;
}
public Optional<Map<String, String>> getVolumes() { return volumes; }
public Optional<String> getDns() { return dns; }
public Optional<String> getHostname() { return hostname; }
public Optional<Integer> getMemory() { return memory; }
public Optional<List<String>> getCommands() {
return commands;
}
public Optional<List<String>> getCommands() { return commands; }
public Optional<Integer> getCpuShares() { return cpuShares; }
public Optional<List<String>> getEnv() {
return env;
}
public Optional<List<String>> getEnv() { return env; }
public Optional<List<Integer>> getDirectPorts() { return directPorts; }
public static class Builder {
/**
* @see DockerTemplateOptions#volumes(java.util.Map)
*/
public static DockerTemplateOptions volumes(Map<String, String> volumes) {
DockerTemplateOptions options = new DockerTemplateOptions();
return DockerTemplateOptions.class.cast(options.volumes(volumes));
}
/**
* @see DockerTemplateOptions#volumes(java.util.Map)
*/
public static DockerTemplateOptions volumes(Map<String, String> 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<Integer> directPorts) {
DockerTemplateOptions options = new DockerTemplateOptions();
return DockerTemplateOptions.class.cast(options.directPorts(directPorts));
}
// methods that only facilitate returning the correct object type
/**

View File

@ -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<String, List<Map<String, String>>> portBindings = Maps.newHashMap();
for (Integer port : templateOptions.getDirectPorts().get()) {
portBindings.put(port + "/tcp", Lists.<Map<String, String>>newArrayList(ImmutableMap.of("HostPort", Integer.toString(port))));
}
hostConfigBuilder.portBindings(portBindings);
}
if (templateOptions.getDns().isPresent()) {
hostConfigBuilder.dns(templateOptions.getDns().get());
}