Fix TemplateOptions.clone; adds RunScriptOptions.copyTo

Previously not all fields of RunScriptOptions were included in copyTo
(e.g. runAsRoot and initScript).

Also options.equals(options.clone()) failed if options.loginPassword
was originally null - in the cloned object, it would be Optional.absent.

Fixes RunScriptOptions.toString, to only say “loginPasswordPresent”
if optional.isPresent().
This commit is contained in:
Aled Sage 2016-01-24 10:32:46 +00:00 committed by Ignasi Barrera
parent 0248931cc1
commit 680ab59316
2 changed files with 34 additions and 19 deletions

View File

@ -42,6 +42,25 @@ public class RunScriptOptions {
*/ */
public static final RunScriptOptions NONE = new ImmutableRunScriptOptions(new RunScriptOptions()); public static final RunScriptOptions NONE = new ImmutableRunScriptOptions(new RunScriptOptions());
public void copyTo(RunScriptOptions to) {
if (this.getPort() != -1)
to.blockOnPort(this.getPort(), this.getSeconds());
if (this.getTaskName() != null)
to.nameTask(this.getTaskName());
to.runAsRoot(this.shouldRunAsRoot());
to.blockOnComplete(this.shouldBlockOnComplete());
to.wrapInInitScript(this.shouldWrapInInitScript());
if (this.hasLoginPasswordOption())
to.overrideLoginPassword(this.loginPassword);
if (this.hasLoginPrivateKeyOption())
to.overrideLoginPrivateKey(this.loginPrivateKey);
if (this.getLoginUser() != null)
to.overrideLoginUser(this.getLoginUser());
if (this.shouldAuthenticateSudo() != null) {
to.overrideAuthenticateSudo(this.shouldAuthenticateSudo());
}
}
public static class ImmutableRunScriptOptions extends RunScriptOptions { public static class ImmutableRunScriptOptions extends RunScriptOptions {
private final RunScriptOptions delegate; private final RunScriptOptions delegate;
@ -197,6 +216,18 @@ public class RunScriptOptions {
return this; return this;
} }
public RunScriptOptions overrideLoginPassword(Optional<String> password) {
checkNotNull(password, "password");
this.loginPassword = password;
return this;
}
public RunScriptOptions overrideLoginPrivateKey(Optional<String> privateKey) {
checkNotNull(privateKey, "privateKey");
this.loginPrivateKey = privateKey;
return this;
}
public RunScriptOptions overrideAuthenticateSudo(boolean authenticateSudo) { public RunScriptOptions overrideAuthenticateSudo(boolean authenticateSudo) {
this.authenticateSudo = authenticateSudo; this.authenticateSudo = authenticateSudo;
return this; return this;
@ -453,9 +484,9 @@ public class RunScriptOptions {
protected ToStringHelper string() { protected ToStringHelper string() {
ToStringHelper toString = Objects.toStringHelper("").omitNullValues(); ToStringHelper toString = Objects.toStringHelper("").omitNullValues();
toString.add("loginUser", loginUser); toString.add("loginUser", loginUser);
if (loginPassword != null) if (loginPassword != null && loginPassword.isPresent())
toString.add("loginPasswordPresent", true); toString.add("loginPasswordPresent", true);
if (loginPrivateKey != null) if (loginPrivateKey != null && loginPrivateKey.isPresent())
toString.add("loginPrivateKeyPresent", true); toString.add("loginPrivateKeyPresent", true);
toString.add("authenticateSudo", authenticateSudo); toString.add("authenticateSudo", authenticateSudo);
if (port != -1 && seconds != -1) // TODO: not primitives if (port != -1 && seconds != -1) // TODO: not primitives

View File

@ -64,6 +64,7 @@ public class TemplateOptions extends RunScriptOptions implements Cloneable {
} }
public void copyTo(TemplateOptions to) { public void copyTo(TemplateOptions to) {
super.copyTo(to);
if (!Arrays.equals(to.getInboundPorts(), this.getInboundPorts())) if (!Arrays.equals(to.getInboundPorts(), this.getInboundPorts()))
to.inboundPorts(this.getInboundPorts()); to.inboundPorts(this.getInboundPorts());
if (this.getRunScript() != null) if (this.getRunScript() != null)
@ -74,8 +75,6 @@ public class TemplateOptions extends RunScriptOptions implements Cloneable {
to.installPrivateKey(this.getPrivateKey()); to.installPrivateKey(this.getPrivateKey());
if (this.getPublicKey() != null) if (this.getPublicKey() != null)
to.authorizePublicKey(this.getPublicKey()); to.authorizePublicKey(this.getPublicKey());
if (this.getPort() != -1)
to.blockOnPort(this.getPort(), this.getSeconds());
if (!this.getUserMetadata().isEmpty()) if (!this.getUserMetadata().isEmpty())
to.userMetadata(this.getUserMetadata()); to.userMetadata(this.getUserMetadata());
if (!this.getTags().isEmpty()) if (!this.getTags().isEmpty())
@ -84,21 +83,6 @@ public class TemplateOptions extends RunScriptOptions implements Cloneable {
to.nodeNames(getNodeNames()); to.nodeNames(getNodeNames());
if (!this.shouldBlockUntilRunning()) if (!this.shouldBlockUntilRunning())
to.blockUntilRunning(false); to.blockUntilRunning(false);
if (!this.shouldBlockOnComplete())
to.blockOnComplete(false);
LoginCredentials fromCreds = new LoginCredentials.Builder().
user(this.getLoginUser()).
password(this.hasLoginPassword() ? this.getLoginPassword() : null).
privateKey(this.hasLoginPrivateKeyOption() ? this.getLoginPrivateKey() : null).
authenticateSudo(authenticateSudo == null ? false : authenticateSudo).
build();
if (fromCreds != null) {
to.overrideLoginCredentials(fromCreds);
}
if (this.getTaskName() != null)
to.nameTask(this.getTaskName());
if (!this.getNetworks().isEmpty()) if (!this.getNetworks().isEmpty())
to.networks(this.getNetworks()); to.networks(this.getNetworks());
} }