From d7814821a39a1afeb203fdaf1a2994acae23b588 Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Sun, 6 Mar 2011 15:33:22 -0500 Subject: [PATCH] Issue 500: added options overrideLoginUserWith and overrideLoginCredentialWith --- .../internal/EC2TemplateBuilderImpl.java | 20 -- .../compute/options/EC2TemplateOptions.java | 50 +++-- .../internal/VCloudTemplateBuilderImpl.java | 15 -- .../options/VCloudTemplateOptions.java | 20 +- .../TerremarkVCloudTemplateOptions.java | 26 ++- .../domain/internal/TemplateBuilderImpl.java | 27 +-- .../compute/options/RunScriptOptions.java | 63 ++++-- .../compute/options/TemplateOptions.java | 190 ++++++++++++++++-- .../compute/AWSEC2TemplateBuilderImpl.java | 18 -- .../ec2/compute/AWSEC2TemplateOptions.java | 44 +++- 10 files changed, 342 insertions(+), 131 deletions(-) diff --git a/apis/ec2/src/main/java/org/jclouds/ec2/compute/internal/EC2TemplateBuilderImpl.java b/apis/ec2/src/main/java/org/jclouds/ec2/compute/internal/EC2TemplateBuilderImpl.java index 8b534d3323..250203f7c5 100644 --- a/apis/ec2/src/main/java/org/jclouds/ec2/compute/internal/EC2TemplateBuilderImpl.java +++ b/apis/ec2/src/main/java/org/jclouds/ec2/compute/internal/EC2TemplateBuilderImpl.java @@ -37,7 +37,6 @@ import org.jclouds.compute.domain.internal.TemplateBuilderImpl; import org.jclouds.compute.options.TemplateOptions; import org.jclouds.domain.Location; import org.jclouds.ec2.compute.domain.RegionAndName; -import org.jclouds.ec2.compute.options.EC2TemplateOptions; import com.google.common.base.Supplier; import com.google.common.collect.ComputationException; @@ -59,25 +58,6 @@ public class EC2TemplateBuilderImpl extends TemplateBuilderImpl { this.imageMap = imageMap; } - @Override - protected void copyTemplateOptions(TemplateOptions from, TemplateOptions to) { - super.copyTemplateOptions(from, to); - if (from instanceof EC2TemplateOptions) { - EC2TemplateOptions eFrom = EC2TemplateOptions.class.cast(from); - EC2TemplateOptions eTo = EC2TemplateOptions.class.cast(to); - if (eFrom.getGroupIds().size() > 0) - eTo.securityGroups(eFrom.getGroupIds()); - if (eFrom.getKeyPair() != null) - eTo.keyPair(eFrom.getKeyPair()); - if (eFrom.getBlockDeviceMappings().size() > 0) - eTo.blockDeviceMappings(eFrom.getBlockDeviceMappings()); - if (!eFrom.shouldAutomaticallyCreateKeyPair()) - eTo.noKeyPair(); - if (eFrom.getUserData() != null) - eTo.userData(eFrom.getUserData()); - } - } - final Provider lazyImageProvider = new Provider() { @Override diff --git a/apis/ec2/src/main/java/org/jclouds/ec2/compute/options/EC2TemplateOptions.java b/apis/ec2/src/main/java/org/jclouds/ec2/compute/options/EC2TemplateOptions.java index 858575bcc9..d469ad3605 100644 --- a/apis/ec2/src/main/java/org/jclouds/ec2/compute/options/EC2TemplateOptions.java +++ b/apis/ec2/src/main/java/org/jclouds/ec2/compute/options/EC2TemplateOptions.java @@ -59,7 +59,31 @@ import com.google.common.collect.Iterables; * * @author Adrian Cole */ -public class EC2TemplateOptions extends TemplateOptions { +public class EC2TemplateOptions extends TemplateOptions implements Cloneable { + @Override + public EC2TemplateOptions clone() { + EC2TemplateOptions options = new EC2TemplateOptions(); + copyTo(options); + return options; + } + + @Override + public void copyTo(TemplateOptions to) { + super.copyTo(to); + if (to instanceof EC2TemplateOptions) { + EC2TemplateOptions eTo = EC2TemplateOptions.class.cast(to); + if (getGroupIds().size() > 0) + eTo.securityGroups(getGroupIds()); + if (getKeyPair() != null) + eTo.keyPair(getKeyPair()); + if (getBlockDeviceMappings().size() > 0) + eTo.blockDeviceMappings(getBlockDeviceMappings()); + if (!shouldAutomaticallyCreateKeyPair()) + eTo.noKeyPair(); + if (getUserData() != null) + eTo.userData(getUserData()); + } + } private Set groupIds = ImmutableSet.of(); private String keyPair = null; @@ -93,7 +117,7 @@ public class EC2TemplateOptions extends TemplateOptions { */ public EC2TemplateOptions userData(byte[] unencodedData) { checkArgument(checkNotNull(unencodedData, "unencodedData").length <= 16 * 1024, - "userData cannot be larger than 16kb"); + "userData cannot be larger than 16kb"); this.userData = unencodedData; return this; } @@ -122,16 +146,16 @@ public class EC2TemplateOptions extends TemplateOptions { * Specifies the block device mappings to be used to run the instance */ public EC2TemplateOptions mapEBSSnapshotToDeviceName(String deviceName, String snapshotId, - @Nullable Integer sizeInGib, boolean deleteOnTermination) { + @Nullable Integer sizeInGib, boolean deleteOnTermination) { checkNotNull(deviceName, "deviceName cannot be null"); Preconditions2.checkNotEmpty(deviceName, "deviceName must be non-empty"); checkNotNull(snapshotId, "snapshotId cannot be null"); Preconditions2.checkNotEmpty(snapshotId, "snapshotId must be non-empty"); com.google.common.collect.ImmutableSet.Builder mappings = ImmutableSet - . builder(); + . builder(); mappings.addAll(blockDeviceMappings); MapEBSSnapshotToDevice mapping = new MapEBSSnapshotToDevice(deviceName, snapshotId, sizeInGib, - deleteOnTermination); + deleteOnTermination); mappings.add(mapping); blockDeviceMappings = mappings.build(); return this; @@ -145,7 +169,7 @@ public class EC2TemplateOptions extends TemplateOptions { Preconditions2.checkNotEmpty(deviceName, "deviceName must be non-empty"); com.google.common.collect.ImmutableSet.Builder mappings = ImmutableSet - . builder(); + . builder(); mappings.addAll(blockDeviceMappings); MapNewVolumeToDevice mapping = new MapNewVolumeToDevice(deviceName, sizeInGib, deleteOnTermination); mappings.add(mapping); @@ -163,7 +187,7 @@ public class EC2TemplateOptions extends TemplateOptions { Preconditions2.checkNotEmpty(virtualName, "virtualName must be non-empty"); com.google.common.collect.ImmutableSet.Builder mappings = ImmutableSet - . builder(); + . builder(); mappings.addAll(blockDeviceMappings); MapEphemeralDeviceToDevice mapping = new MapEphemeralDeviceToDevice(deviceName, virtualName); mappings.add(mapping); @@ -179,7 +203,7 @@ public class EC2TemplateOptions extends TemplateOptions { Preconditions2.checkNotEmpty(deviceName, "deviceName must be non-empty"); com.google.common.collect.ImmutableSet.Builder mappings = ImmutableSet - . builder(); + . builder(); mappings.addAll(blockDeviceMappings); UnmapDeviceNamed mapping = new UnmapDeviceNamed(deviceName); mappings.add(mapping); @@ -208,7 +232,7 @@ public class EC2TemplateOptions extends TemplateOptions { * @see EC2TemplateOptions#mapEBSSnapshotToDeviceName */ public static EC2TemplateOptions mapEBSSnapshotToDeviceName(String deviceName, String snapshotId, - @Nullable Integer sizeInGib, boolean deleteOnTermination) { + @Nullable Integer sizeInGib, boolean deleteOnTermination) { EC2TemplateOptions options = new EC2TemplateOptions(); return options.mapEBSSnapshotToDeviceName(deviceName, snapshotId, sizeInGib, deleteOnTermination); } @@ -217,7 +241,7 @@ public class EC2TemplateOptions extends TemplateOptions { * @see EC2TemplateOptions#mapNewVolumeToDeviceName */ public static EC2TemplateOptions mapNewVolumeToDeviceName(String deviceName, int sizeInGib, - boolean deleteOnTermination) { + boolean deleteOnTermination) { EC2TemplateOptions options = new EC2TemplateOptions(); return options.mapNewVolumeToDeviceName(deviceName, sizeInGib, deleteOnTermination); } @@ -450,8 +474,8 @@ public class EC2TemplateOptions extends TemplateOptions { * {@inheritDoc} */ @Override - public EC2TemplateOptions withOverridingCredentials(Credentials overridingCredentials) { - return EC2TemplateOptions.class.cast(super.withOverridingCredentials(overridingCredentials)); + public EC2TemplateOptions overrideCredentialsWith(Credentials overridingCredentials) { + return EC2TemplateOptions.class.cast(super.overrideCredentialsWith(overridingCredentials)); } /** @@ -537,6 +561,6 @@ public class EC2TemplateOptions extends TemplateOptions { @Override public String toString() { return "[groupIds=" + groupIds + ", keyPair=" + keyPair + ", noKeyPair=" + noKeyPair + ", userData=" - + Arrays.toString(userData) + ", blockDeviceMappings=" + blockDeviceMappings + "]"; + + Arrays.toString(userData) + ", blockDeviceMappings=" + blockDeviceMappings + "]"; } } diff --git a/apis/vcloud/src/main/java/org/jclouds/vcloud/compute/internal/VCloudTemplateBuilderImpl.java b/apis/vcloud/src/main/java/org/jclouds/vcloud/compute/internal/VCloudTemplateBuilderImpl.java index 9383a4d3e4..4fed4ef775 100644 --- a/apis/vcloud/src/main/java/org/jclouds/vcloud/compute/internal/VCloudTemplateBuilderImpl.java +++ b/apis/vcloud/src/main/java/org/jclouds/vcloud/compute/internal/VCloudTemplateBuilderImpl.java @@ -32,7 +32,6 @@ import org.jclouds.compute.domain.TemplateBuilder; import org.jclouds.compute.domain.internal.TemplateBuilderImpl; import org.jclouds.compute.options.TemplateOptions; import org.jclouds.domain.Location; -import org.jclouds.vcloud.compute.options.VCloudTemplateOptions; import com.google.common.base.Supplier; @@ -49,18 +48,4 @@ public class VCloudTemplateBuilderImpl extends TemplateBuilderImpl { @Named("DEFAULT") Provider defaultTemplateProvider) { super(locations, images, sizes, defaultLocation, optionsProvider, defaultTemplateProvider); } - - @Override - protected void copyTemplateOptions(TemplateOptions from, TemplateOptions to) { - super.copyTemplateOptions(from, to); - if (from instanceof VCloudTemplateOptions) { - VCloudTemplateOptions eFrom = VCloudTemplateOptions.class.cast(from); - VCloudTemplateOptions eTo = VCloudTemplateOptions.class.cast(to); - if (eFrom.getCustomizationScript() != null) - eTo.customizationScript(eFrom.getCustomizationScript()); - if (eFrom.getIpAddressAllocationMode() != null) - eTo.ipAddressAllocationMode(eFrom.getIpAddressAllocationMode()); - } - } - } diff --git a/apis/vcloud/src/main/java/org/jclouds/vcloud/compute/options/VCloudTemplateOptions.java b/apis/vcloud/src/main/java/org/jclouds/vcloud/compute/options/VCloudTemplateOptions.java index 584ec7079f..d85af4cf03 100644 --- a/apis/vcloud/src/main/java/org/jclouds/vcloud/compute/options/VCloudTemplateOptions.java +++ b/apis/vcloud/src/main/java/org/jclouds/vcloud/compute/options/VCloudTemplateOptions.java @@ -43,7 +43,25 @@ import org.jclouds.vcloud.domain.network.IpAddressAllocationMode; * * @author Adrian Cole */ -public class VCloudTemplateOptions extends TemplateOptions { +public class VCloudTemplateOptions extends TemplateOptions implements Cloneable { + @Override + public VCloudTemplateOptions clone() { + VCloudTemplateOptions options = new VCloudTemplateOptions(); + copyTo(options); + return options; + } + + @Override + public void copyTo(TemplateOptions to) { + super.copyTo(to); + if (to instanceof VCloudTemplateOptions) { + VCloudTemplateOptions eTo = VCloudTemplateOptions.class.cast(to); + if (getCustomizationScript() != null) + eTo.customizationScript(getCustomizationScript()); + if (getIpAddressAllocationMode() != null) + eTo.ipAddressAllocationMode(getIpAddressAllocationMode()); + } + } private String customizationScript = null; private IpAddressAllocationMode ipAddressAllocationMode = null; diff --git a/common/trmk/src/main/java/org/jclouds/vcloud/terremark/compute/options/TerremarkVCloudTemplateOptions.java b/common/trmk/src/main/java/org/jclouds/vcloud/terremark/compute/options/TerremarkVCloudTemplateOptions.java index 111854c686..c6ab86717f 100644 --- a/common/trmk/src/main/java/org/jclouds/vcloud/terremark/compute/options/TerremarkVCloudTemplateOptions.java +++ b/common/trmk/src/main/java/org/jclouds/vcloud/terremark/compute/options/TerremarkVCloudTemplateOptions.java @@ -45,7 +45,25 @@ import org.jclouds.util.Preconditions2; * * @author Adrian Cole */ -public class TerremarkVCloudTemplateOptions extends TemplateOptions { +public class TerremarkVCloudTemplateOptions extends TemplateOptions implements Cloneable { + @Override + public TerremarkVCloudTemplateOptions clone() { + TerremarkVCloudTemplateOptions options = new TerremarkVCloudTemplateOptions(); + copyTo(options); + return options; + } + + @Override + public void copyTo(TemplateOptions to) { + super.copyTo(to); + if (to instanceof TerremarkVCloudTemplateOptions) { + TerremarkVCloudTemplateOptions eTo = TerremarkVCloudTemplateOptions.class.cast(to); + if (noKeyPair) + eTo.noKeyPair(); + if (keyPair != null) + eTo.keyPair = keyPair; + } + } private String keyPair = null; private boolean noKeyPair; @@ -276,9 +294,9 @@ public class TerremarkVCloudTemplateOptions extends TemplateOptions { @Override public String toString() { return "TerremarkVCloudTemplateOptions [keyPair=" + keyPair + ", noKeyPair=" + noKeyPair + ", inboundPorts=" - + Arrays.toString(inboundPorts) + ", privateKey=" + (privateKey != null) + ", publicKey=" - + (publicKey != null) + ", runScript=" + (script != null) + ", port:seconds=" + port + ":" + seconds - + ", metadata/details: " + includeMetadata + "]"; + + Arrays.toString(inboundPorts) + ", privateKey=" + (privateKey != null) + ", publicKey=" + + (publicKey != null) + ", runScript=" + (script != null) + ", port:seconds=" + port + ":" + seconds + + ", metadata/details: " + includeMetadata + "]"; } } diff --git a/compute/src/main/java/org/jclouds/compute/domain/internal/TemplateBuilderImpl.java b/compute/src/main/java/org/jclouds/compute/domain/internal/TemplateBuilderImpl.java index 77af5c8c3d..0b1cce8d25 100644 --- a/compute/src/main/java/org/jclouds/compute/domain/internal/TemplateBuilderImpl.java +++ b/compute/src/main/java/org/jclouds/compute/domain/internal/TemplateBuilderImpl.java @@ -27,7 +27,6 @@ import static org.jclouds.compute.util.ComputeServiceUtils.getCores; import static org.jclouds.compute.util.ComputeServiceUtils.getCoresAndSpeed; import static org.jclouds.compute.util.ComputeServiceUtils.getSpace; -import java.util.Arrays; import java.util.List; import java.util.NoSuchElementException; import java.util.Set; @@ -420,6 +419,7 @@ public class TemplateBuilderImpl implements TemplateBuilder { public TemplateBuilder fromTemplate(Template template) { fromHardware(template.getHardware()); fromImage(template.getImage()); + options(template.getOptions()); return this; } @@ -810,33 +810,10 @@ public class TemplateBuilderImpl implements TemplateBuilder { @Override public TemplateBuilder options(TemplateOptions options) { this.options = optionsProvider.get(); - copyTemplateOptions(checkNotNull(options, "options"), this.options); + checkNotNull(options, "options").copyTo(this.options); return this; } - protected void copyTemplateOptions(TemplateOptions from, TemplateOptions to) { - if (!Arrays.equals(to.getInboundPorts(), from.getInboundPorts())) - to.inboundPorts(from.getInboundPorts()); - if (from.getRunScript() != null) - to.runScript(from.getRunScript()); - if (from.getPrivateKey() != null) - to.installPrivateKey(from.getPrivateKey()); - if (from.getPublicKey() != null) - to.authorizePublicKey(from.getPublicKey()); - if (from.getPort() != -1) - to.blockOnPort(from.getPort(), from.getSeconds()); - if (from.isIncludeMetadata()) - to.withMetadata(); - if (!from.shouldBlockUntilRunning()) - to.blockUntilRunning(false); - if (!from.shouldBlockOnComplete()) - to.blockOnComplete(false); - if (from.getOverrideCredentials() != null) - to.withOverridingCredentials(from.getOverrideCredentials()); - if (from.getTaskName() != null) - to.nameTask(from.getTaskName()); - } - @VisibleForTesting boolean nothingChangedExceptOptions() { return osFamily == null && location == null && imageId == null && hardwareId == null && osName == null diff --git a/compute/src/main/java/org/jclouds/compute/options/RunScriptOptions.java b/compute/src/main/java/org/jclouds/compute/options/RunScriptOptions.java index e655102897..4d97e4559a 100644 --- a/compute/src/main/java/org/jclouds/compute/options/RunScriptOptions.java +++ b/compute/src/main/java/org/jclouds/compute/options/RunScriptOptions.java @@ -54,8 +54,8 @@ public class RunScriptOptions { } @Override - public Credentials getOverrideCredentials() { - return delegate.getOverrideCredentials(); + public Credentials getOverridingCredentials() { + return delegate.getOverridingCredentials(); } @@ -82,7 +82,22 @@ public class RunScriptOptions { } @Override - public RunScriptOptions withOverridingCredentials(Credentials overridingCredentials) { + public RunScriptOptions overrideLoginUserWith(String loginUser) { + throw new IllegalArgumentException("loginUser is immutable"); + } + + @Override + public RunScriptOptions overrideLoginCredentialWith(String loginCredential) { + throw new IllegalArgumentException("loginCredential is immutable"); + } + + @Override + public RunScriptOptions wrapInInitScript(boolean wrapInInitScript) { + throw new IllegalArgumentException("wrapInInitScript is immutable"); + } + + @Override + public RunScriptOptions overrideCredentialsWith(Credentials overridingCredentials) { throw new IllegalArgumentException("overridingCredentials is immutable"); } @@ -120,17 +135,31 @@ public class RunScriptOptions { protected boolean blockOnComplete = true; protected boolean wrapInInitScript = true; - public RunScriptOptions withOverridingCredentials(Credentials overridingCredentials) { + public RunScriptOptions overrideCredentialsWith(Credentials overridingCredentials) { checkNotNull(overridingCredentials, "overridingCredentials"); - checkNotNull(overridingCredentials.identity, "overridingCredentials.identity"); - checkNotNull(overridingCredentials.credential, "overridingCredentials.key"); this.overridingCredentials = overridingCredentials; return this; } + public RunScriptOptions overrideLoginUserWith(String loginUser) { + checkNotNull(loginUser, "loginUser"); + org.jclouds.domain.Credentials.Builder builder = overridingCredentials != null ? overridingCredentials + .toBuilder() : new Credentials.Builder(); + this.overridingCredentials = builder.identity(loginUser).build(); + return this; + } + + public RunScriptOptions overrideLoginCredentialWith(String loginCredential) { + checkNotNull(loginCredential, "loginCredential"); + org.jclouds.domain.Credentials.Builder builder = overridingCredentials != null ? overridingCredentials + .toBuilder() : new Credentials.Builder(); + this.overridingCredentials = builder.credential(loginCredential).build(); + return this; + } + /** - * @return What to call the task relating to this script; default {@code - * jclouds-script-timestamp} where timestamp is millis since epoch + * @return What to call the task relating to this script; default + * {@code jclouds-script-timestamp} where timestamp is millis since epoch * */ public RunScriptOptions nameTask(String name) { @@ -191,7 +220,7 @@ public class RunScriptOptions { * * @return value */ - public Credentials getOverrideCredentials() { + public Credentials getOverridingCredentials() { return overridingCredentials; } @@ -229,9 +258,19 @@ public class RunScriptOptions { return options.nameTask(name); } + public static RunScriptOptions overrideLoginUserWith(String user) { + RunScriptOptions options = new RunScriptOptions(); + return options.overrideLoginUserWith(user); + } + + public static RunScriptOptions overrideLoginCredentialWith(String credential) { + RunScriptOptions options = new RunScriptOptions(); + return options.overrideLoginCredentialWith(credential); + } + public static RunScriptOptions overrideCredentialsWith(Credentials credentials) { RunScriptOptions options = new RunScriptOptions(); - return options.withOverridingCredentials(credentials); + return options.overrideCredentialsWith(credentials); } public static RunScriptOptions runAsRoot(boolean value) { @@ -259,8 +298,8 @@ public class RunScriptOptions { @Override public String toString() { return "[overridingCredentials=" + (overridingCredentials != null) + ", port:seconds=" + port + ":" + seconds - + ", runAsRoot=" + runAsRoot + ", blockOnComplete=" + blockOnComplete + ", wrapInInitScript=" + wrapInInitScript - + "]"; + + ", runAsRoot=" + runAsRoot + ", blockOnComplete=" + blockOnComplete + ", wrapInInitScript=" + + wrapInInitScript + "]"; } } diff --git a/compute/src/main/java/org/jclouds/compute/options/TemplateOptions.java b/compute/src/main/java/org/jclouds/compute/options/TemplateOptions.java index 5f0d4c7d49..24fafc4b7d 100644 --- a/compute/src/main/java/org/jclouds/compute/options/TemplateOptions.java +++ b/compute/src/main/java/org/jclouds/compute/options/TemplateOptions.java @@ -49,13 +49,88 @@ import com.google.common.base.Throwables; * * @author Adrian Cole */ -public class TemplateOptions extends RunScriptOptions { +public class TemplateOptions extends RunScriptOptions implements Cloneable { + + @Override + public TemplateOptions clone() { + TemplateOptions options = new TemplateOptions(); + copyTo(options); + return options; + } + + public void copyTo(TemplateOptions to) { + if (!Arrays.equals(to.getInboundPorts(), this.getInboundPorts())) + to.inboundPorts(this.getInboundPorts()); + if (this.getRunScript() != null) + to.runScript(this.getRunScript()); + if (this.getPrivateKey() != null) + to.installPrivateKey(this.getPrivateKey()); + if (this.getPublicKey() != null) + to.authorizePublicKey(this.getPublicKey()); + if (this.getPort() != -1) + to.blockOnPort(this.getPort(), this.getSeconds()); + if (this.isIncludeMetadata()) + to.withMetadata(); + if (!this.shouldBlockUntilRunning()) + to.blockUntilRunning(false); + if (!this.shouldBlockOnComplete()) + to.blockOnComplete(false); + if (this.getOverridingCredentials() != null) + to.overrideCredentialsWith(this.getOverridingCredentials()); + if (this.getTaskName() != null) + to.nameTask(this.getTaskName()); + } public static final TemplateOptions NONE = new ImmutableTemplateOptions(new TemplateOptions()); public static class ImmutableTemplateOptions extends TemplateOptions { private final TemplateOptions delegate; + @Override + public TemplateOptions clone() { + return delegate.clone(); + } + + @Override + public String getTaskName() { + return delegate.getTaskName(); + } + + @Override + public int getPort() { + return delegate.getPort(); + } + + @Override + public int getSeconds() { + return delegate.getSeconds(); + } + + @Override + public Credentials getOverridingCredentials() { + return delegate.getOverridingCredentials(); + } + + @Override + public boolean shouldRunAsRoot() { + return delegate.shouldRunAsRoot(); + } + + @Override + public boolean shouldBlockOnComplete() { + return delegate.shouldBlockOnComplete(); + } + + @Override + public boolean shouldWrapInInitScript() { + return delegate.shouldWrapInInitScript(); + } + + @Override + public void copyTo(TemplateOptions to) { + delegate.copyTo(to); + } + public ImmutableTemplateOptions(TemplateOptions delegate) { this.delegate = delegate; } @@ -65,6 +140,72 @@ public class TemplateOptions extends RunScriptOptions { return delegate.toString(); } + @Override + public TemplateOptions runScript(Payload script) { + throw new IllegalArgumentException("script is immutable"); + } + + @Override + public TemplateOptions runScript(Statement script) { + throw new IllegalArgumentException("script is immutable"); + } + + @Override + public TemplateOptions installPrivateKey(Payload privateKey) { + throw new IllegalArgumentException("privateKey is immutable"); + } + + @Override + public TemplateOptions dontAuthorizePublicKey() { + throw new IllegalArgumentException("public key is immutable"); + } + + @Override + @Deprecated + public TemplateOptions authorizePublicKey(Payload publicKey) { + throw new IllegalArgumentException("public key is immutable"); + } + + @Override + public TemplateOptions blockOnPort(int port, int seconds) { + throw new IllegalArgumentException("ports are immutable"); + } + + @Override + public TemplateOptions nameTask(String name) { + throw new IllegalArgumentException("task name is immutable"); + } + + @Override + public TemplateOptions runAsRoot(boolean runAsRoot) { + throw new IllegalArgumentException("runAsRoot is immutable"); + } + + @Override + public TemplateOptions overrideCredentialsWith(Credentials overridingCredentials) { + throw new IllegalArgumentException("credentials are immutable"); + } + + @Override + public TemplateOptions overrideLoginUserWith(String loginUser) { + throw new IllegalArgumentException("credentials are immutable"); + } + + @Override + public TemplateOptions overrideLoginCredentialWith(String loginCredential) { + throw new IllegalArgumentException("credentials are immutable"); + } + + @Override + public TemplateOptions wrapInInitScript(boolean wrapInInitScript) { + throw new IllegalArgumentException("wrapInInitScript is immutable"); + } + + @Override + public TemplateOptions blockOnComplete(boolean blockOnComplete) { + throw new IllegalArgumentException("blockOnComplete is immutable"); + } + @Override public T as(Class clazz) { return delegate.as(clazz); @@ -72,7 +213,7 @@ public class TemplateOptions extends RunScriptOptions { @Override public TemplateOptions authorizePublicKey(String publicKey) { - throw new IllegalArgumentException("authorizePublicKey is immutable"); + throw new IllegalArgumentException("publicKey is immutable"); } @Override @@ -107,7 +248,7 @@ public class TemplateOptions extends RunScriptOptions { @Override public TemplateOptions inboundPorts(int... ports) { - throw new IllegalArgumentException("ports is immutable"); + throw new IllegalArgumentException("ports are immutable"); } @Override @@ -122,12 +263,12 @@ public class TemplateOptions extends RunScriptOptions { @Override public TemplateOptions runScript(byte[] script) { - throw new IllegalArgumentException("withMetadata is immutable"); + throw new IllegalArgumentException("script is immutable"); } @Override public TemplateOptions withMetadata() { - throw new IllegalArgumentException("withMetadata is immutable"); + throw new IllegalArgumentException("metadata is immutable"); } } @@ -210,7 +351,7 @@ public class TemplateOptions extends RunScriptOptions { */ public TemplateOptions installPrivateKey(String privateKey) { checkArgument(checkNotNull(privateKey, "privateKey").startsWith("-----BEGIN RSA PRIVATE KEY-----"), - "key should start with -----BEGIN RSA PRIVATE KEY-----"); + "key should start with -----BEGIN RSA PRIVATE KEY-----"); this.privateKey = privateKey; return this; } @@ -285,9 +426,19 @@ public class TemplateOptions extends RunScriptOptions { return options.nameTask(name); } + public static TemplateOptions overrideLoginUserWith(String user) { + TemplateOptions options = new TemplateOptions(); + return options.overrideLoginUserWith(user); + } + + public static TemplateOptions overrideLoginCredentialWith(String credential) { + TemplateOptions options = new TemplateOptions(); + return options.overrideLoginCredentialWith(credential); + } + public static TemplateOptions overrideCredentialsWith(Credentials credentials) { TemplateOptions options = new TemplateOptions(); - return options.withOverridingCredentials(credentials); + return options.overrideCredentialsWith(credentials); } public static TemplateOptions runAsRoot(boolean value) { @@ -406,9 +557,9 @@ public class TemplateOptions extends RunScriptOptions { @Override public String toString() { return "[inboundPorts=" + Arrays.toString(inboundPorts) + ", privateKey=" + (privateKey != null) + ", publicKey=" - + (publicKey != null) + ", runScript=" + (script != null) + ", blockUntilRunning=" + blockUntilRunning - + ", blockOnComplete=" + blockOnComplete + ", port:seconds=" + port + ":" + seconds - + ", metadata/details: " + includeMetadata + "]"; + + (publicKey != null) + ", runScript=" + (script != null) + ", blockUntilRunning=" + blockUntilRunning + + ", blockOnComplete=" + blockOnComplete + ", port:seconds=" + port + ":" + seconds + + ", metadata/details: " + includeMetadata + "]"; } public TemplateOptions blockUntilRunning(boolean blockUntilRunning) { @@ -486,8 +637,23 @@ public class TemplateOptions extends RunScriptOptions { } @Override - public TemplateOptions withOverridingCredentials(Credentials overridingCredentials) { - return TemplateOptions.class.cast(super.withOverridingCredentials(overridingCredentials)); + public TemplateOptions overrideCredentialsWith(Credentials overridingCredentials) { + return TemplateOptions.class.cast(super.overrideCredentialsWith(overridingCredentials)); + } + + @Override + public TemplateOptions overrideLoginUserWith(String loginUser) { + return TemplateOptions.class.cast(super.overrideLoginUserWith(loginUser)); + } + + @Override + public TemplateOptions overrideLoginCredentialWith(String loginCredential) { + return TemplateOptions.class.cast(super.overrideLoginCredentialWith(loginCredential)); + } + + @Override + public TemplateOptions wrapInInitScript(boolean wrapInInitScript) { + return TemplateOptions.class.cast(super.wrapInInitScript(wrapInInitScript)); } @Override diff --git a/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/compute/AWSEC2TemplateBuilderImpl.java b/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/compute/AWSEC2TemplateBuilderImpl.java index d01d2f1572..b26dc7f796 100644 --- a/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/compute/AWSEC2TemplateBuilderImpl.java +++ b/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/compute/AWSEC2TemplateBuilderImpl.java @@ -51,22 +51,4 @@ public class AWSEC2TemplateBuilderImpl extends EC2TemplateBuilderImpl { super(locations, images, sizes, defaultLocation, optionsProvider, defaultTemplateProvider, imageMap); } - @Override - protected void copyTemplateOptions(TemplateOptions from, TemplateOptions to) { - super.copyTemplateOptions(from, to); - if (from instanceof AWSEC2TemplateOptions) { - AWSEC2TemplateOptions eFrom = AWSEC2TemplateOptions.class.cast(from); - AWSEC2TemplateOptions eTo = AWSEC2TemplateOptions.class.cast(to); - - if (eFrom.getSubnetId() != null) - eTo.subnetId(eFrom.getSubnetId()); - if (eFrom.isMonitoringEnabled()) - eTo.enableMonitoring(); - if (!eFrom.shouldAutomaticallyCreatePlacementGroup()) - eTo.noPlacementGroup(); - if (eFrom.getPlacementGroup() != null) - eTo.placementGroup(eFrom.getPlacementGroup()); - } - } - } diff --git a/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/compute/AWSEC2TemplateOptions.java b/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/compute/AWSEC2TemplateOptions.java index 51cb7ae5f6..a6f221d181 100644 --- a/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/compute/AWSEC2TemplateOptions.java +++ b/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/compute/AWSEC2TemplateOptions.java @@ -52,7 +52,29 @@ import org.jclouds.util.Preconditions2; * * @author Adrian Cole */ -public class AWSEC2TemplateOptions extends EC2TemplateOptions { +public class AWSEC2TemplateOptions extends EC2TemplateOptions implements Cloneable { + @Override + public AWSEC2TemplateOptions clone() { + AWSEC2TemplateOptions options = new AWSEC2TemplateOptions(); + copyTo(options); + return options; + } + + @Override + public void copyTo(TemplateOptions to) { + super.copyTo(to); + if (to instanceof AWSEC2TemplateOptions) { + AWSEC2TemplateOptions eTo = AWSEC2TemplateOptions.class.cast(to); + if (getSubnetId() != null) + eTo.subnetId(getSubnetId()); + if (isMonitoringEnabled()) + eTo.enableMonitoring(); + if (!shouldAutomaticallyCreatePlacementGroup()) + eTo.noPlacementGroup(); + if (getPlacementGroup() != null) + eTo.placementGroup(getPlacementGroup()); + } + } private boolean monitoringEnabled; private String placementGroup = null; @@ -114,7 +136,7 @@ public class AWSEC2TemplateOptions extends EC2TemplateOptions { * @see EC2TemplateOptions#mapEBSSnapshotToDeviceName */ public static AWSEC2TemplateOptions mapEBSSnapshotToDeviceName(String deviceName, String snapshotId, - @Nullable Integer sizeInGib, boolean deleteOnTermination) { + @Nullable Integer sizeInGib, boolean deleteOnTermination) { AWSEC2TemplateOptions options = new AWSEC2TemplateOptions(); return options.mapEBSSnapshotToDeviceName(deviceName, snapshotId, sizeInGib, deleteOnTermination); } @@ -123,7 +145,7 @@ public class AWSEC2TemplateOptions extends EC2TemplateOptions { * @see EC2TemplateOptions#mapNewVolumeToDeviceName */ public static AWSEC2TemplateOptions mapNewVolumeToDeviceName(String deviceName, int sizeInGib, - boolean deleteOnTermination) { + boolean deleteOnTermination) { AWSEC2TemplateOptions options = new AWSEC2TemplateOptions(); return options.mapNewVolumeToDeviceName(deviceName, sizeInGib, deleteOnTermination); } @@ -290,9 +312,9 @@ public class AWSEC2TemplateOptions extends EC2TemplateOptions { @Override public AWSEC2TemplateOptions mapEBSSnapshotToDeviceName(String deviceName, String snapshotId, Integer sizeInGib, - boolean deleteOnTermination) { + boolean deleteOnTermination) { return AWSEC2TemplateOptions.class.cast(super.mapEBSSnapshotToDeviceName(deviceName, snapshotId, sizeInGib, - deleteOnTermination)); + deleteOnTermination)); } /** @@ -309,7 +331,7 @@ public class AWSEC2TemplateOptions extends EC2TemplateOptions { @Override public AWSEC2TemplateOptions mapNewVolumeToDeviceName(String deviceName, int sizeInGib, boolean deleteOnTermination) { return AWSEC2TemplateOptions.class.cast(super - .mapNewVolumeToDeviceName(deviceName, sizeInGib, deleteOnTermination)); + .mapNewVolumeToDeviceName(deviceName, sizeInGib, deleteOnTermination)); } /** @@ -471,8 +493,8 @@ public class AWSEC2TemplateOptions extends EC2TemplateOptions { * {@inheritDoc} */ @Override - public AWSEC2TemplateOptions withOverridingCredentials(Credentials overridingCredentials) { - return AWSEC2TemplateOptions.class.cast(super.withOverridingCredentials(overridingCredentials)); + public AWSEC2TemplateOptions overrideCredentialsWith(Credentials overridingCredentials) { + return AWSEC2TemplateOptions.class.cast(super.overrideCredentialsWith(overridingCredentials)); } /** @@ -544,9 +566,9 @@ public class AWSEC2TemplateOptions extends EC2TemplateOptions { public String toString() { return "[groupIds=" + getGroupIds() + ", keyPair=" + getKeyPair() + ", noKeyPair=" - + !shouldAutomaticallyCreateKeyPair() + ", monitoringEnabled=" + monitoringEnabled + ", placementGroup=" - + placementGroup + ", noPlacementGroup=" + noPlacementGroup + ", subnetId=" + subnetId + ", userData=" - + Arrays.toString(getUserData()) + ", blockDeviceMappings=" + getBlockDeviceMappings() + "]"; + + !shouldAutomaticallyCreateKeyPair() + ", monitoringEnabled=" + monitoringEnabled + ", placementGroup=" + + placementGroup + ", noPlacementGroup=" + noPlacementGroup + ", subnetId=" + subnetId + ", userData=" + + Arrays.toString(getUserData()) + ", blockDeviceMappings=" + getBlockDeviceMappings() + "]"; } }