[SoftLayer] fix SoftLayerTemplateOptions

This commit is contained in:
Andrea Turli 2015-07-24 16:27:20 +02:00
parent d96e34ea93
commit 1863f859f5
5 changed files with 298 additions and 182 deletions

View File

@ -16,19 +16,23 @@
*/
package org.jclouds.softlayer.binders;
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.Sets;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.inject.Inject;
import org.jclouds.http.HttpRequest;
import org.jclouds.json.Json;
import org.jclouds.rest.Binder;
import org.jclouds.softlayer.compute.strategy.SoftLayerComputeServiceAdapter;
import org.jclouds.softlayer.domain.SecuritySshKey;
import org.jclouds.softlayer.domain.VirtualGuest;
import org.jclouds.softlayer.domain.VirtualGuestBlockDevice;
import org.jclouds.softlayer.domain.VirtualGuestNetworkComponent;
import org.jclouds.softlayer.domain.internal.BlockDevice;
import org.jclouds.softlayer.domain.internal.BlockDeviceTemplateGroup;
import org.jclouds.softlayer.domain.internal.Datacenter;
@ -38,15 +42,13 @@ import org.jclouds.softlayer.domain.internal.PrimaryBackendNetworkComponent;
import org.jclouds.softlayer.domain.internal.PrimaryNetworkComponent;
import org.jclouds.softlayer.domain.internal.TemplateObject;
import javax.inject.Inject;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.base.Function;
import com.google.common.collect.FluentIterable;
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.Sets;
/**
* Converts a VirtualGuest into a json string valid for creating a CCI via softlayer api
@ -88,7 +90,17 @@ public class VirtualGuestToJson implements Binder {
boolean localDisk = virtualGuest.isLocalDiskFlag();
String datacenterName = checkNotNull(virtualGuest.getDatacenter().getName(), "datacenterName");
Set<NetworkComponent> networkComponents = createNetworkComponents(virtualGuest);
Set<VirtualGuestNetworkComponent> virtualGuestNetworkComponents = virtualGuest.getVirtualGuestNetworkComponents();
Set<NetworkComponent> networkComponents = Sets.newHashSet();
if (virtualGuestNetworkComponents != null) {
networkComponents = FluentIterable.from(virtualGuestNetworkComponents)
.transform(new Function<VirtualGuestNetworkComponent, NetworkComponent>() {
@Override
public NetworkComponent apply(VirtualGuestNetworkComponent virtualGuestNetworkComponent) {
return new NetworkComponent(virtualGuestNetworkComponent.getSpeed());
}
}).toSet();
}
templateObjectBuilder.hostname(hostname)
.domain(domain)
@ -98,9 +110,11 @@ public class VirtualGuestToJson implements Binder {
.localDiskFlag(localDisk)
.dedicatedAccountHostOnlyFlag(virtualGuest.isDedicatedAccountHostOnly())
.privateNetworkOnlyFlag(virtualGuest.isPrivateNetworkOnly())
.datacenter(new Datacenter(datacenterName))
.networkComponents(networkComponents);
.datacenter(new Datacenter(datacenterName));
if (!networkComponents.isEmpty()) {
templateObjectBuilder.networkComponents(networkComponents);
}
if (virtualGuest.getOperatingSystem() != null) {
String operatingSystemReferenceCode = checkNotNull(virtualGuest.getOperatingSystem()
.getOperatingSystemReferenceCode(), "operatingSystemReferenceCode");
@ -154,23 +168,6 @@ public class VirtualGuestToJson implements Binder {
return ImmutableList.copyOf(blockDevices);
}
private Set<NetworkComponent> createNetworkComponents(VirtualGuest virtualGuest) {
if (virtualGuest.getPrimaryNetworkComponent() == null && virtualGuest.getPrimaryBackendNetworkComponent() == null) {
return null;
}
ImmutableSet.Builder networkComponents = ImmutableSet.builder();
int maxSpeed = SoftLayerComputeServiceAdapter.DEFAULT_MAX_PORT_SPEED;
if (virtualGuest.getPrimaryNetworkComponent() != null && virtualGuest.getPrimaryNetworkComponent().getMaxSpeed() > maxSpeed) {
maxSpeed = virtualGuest.getPrimaryNetworkComponent().getMaxSpeed();
}
if (virtualGuest.getPrimaryBackendNetworkComponent() != null && virtualGuest.getPrimaryBackendNetworkComponent().getMaxSpeed() > maxSpeed) {
maxSpeed = virtualGuest.getPrimaryBackendNetworkComponent().getMaxSpeed();
}
networkComponents.add(new NetworkComponent(maxSpeed));
return networkComponents.build();
}
public class BlockDevicesComparator implements Comparator<BlockDevice> {
@Override

View File

@ -22,8 +22,10 @@ import java.util.List;
import java.util.Map;
import org.jclouds.compute.options.TemplateOptions;
import org.jclouds.domain.LoginCredentials;
import org.jclouds.javax.annotation.Nullable;
import org.jclouds.scriptbuilder.domain.Statement;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
import com.google.common.net.InternetDomainName;
@ -50,17 +52,17 @@ import com.google.common.net.InternetDomainName;
public class SoftLayerTemplateOptions extends TemplateOptions implements Cloneable {
protected String domainName = "jclouds.org";
protected Optional<List<Integer>> blockDevices = Optional.absent();
protected Optional<String> diskType = Optional.absent();
protected Optional<Integer> portSpeed = Optional.absent();
protected Optional<String> userData = Optional.absent();
protected Optional<Integer> primaryNetworkComponentNetworkVlanId = Optional.absent();
protected Optional<Integer> primaryBackendNetworkComponentNetworkVlanId = Optional.absent();
protected Optional<Boolean> hourlyBillingFlag = Optional.absent();
protected Optional<Boolean> dedicatedAccountHostOnlyFlag = Optional.absent();
protected Optional<Boolean> privateNetworkOnlyFlag = Optional.absent();
protected Optional<String> postInstallScriptUri = Optional.absent();
protected Optional<List<Integer>> sshKeys = Optional.absent();
protected List<Integer> blockDevices = ImmutableList.of();
protected String diskType;
protected Integer portSpeed;
protected String userData;
protected Integer primaryNetworkComponentNetworkVlanId;
protected Integer primaryBackendNetworkComponentNetworkVlanId;
protected Boolean hourlyBillingFlag;
protected Boolean dedicatedAccountHostOnlyFlag;
protected Boolean privateNetworkOnlyFlag;
protected String postInstallScriptUri;
protected List<Integer> sshKeys = ImmutableList.of();
@Override
public SoftLayerTemplateOptions clone() {
@ -75,35 +77,19 @@ public class SoftLayerTemplateOptions extends TemplateOptions implements Cloneab
if (to instanceof SoftLayerTemplateOptions) {
SoftLayerTemplateOptions eTo = SoftLayerTemplateOptions.class.cast(to);
eTo.domainName(domainName);
if (blockDevices.isPresent()) {
eTo.blockDevices(blockDevices.get());
if (!blockDevices.isEmpty()) {
eTo.blockDevices(blockDevices);
}
if (diskType.isPresent()) {
eTo.diskType(diskType.get());
}
if (portSpeed.isPresent()) {
eTo.portSpeed(portSpeed.get());
}
if (userData.isPresent()) {
eTo.userData(userData.get());
}
if (primaryNetworkComponentNetworkVlanId.isPresent()) {
eTo.primaryNetworkComponentNetworkVlanId(primaryNetworkComponentNetworkVlanId.get());
}
if (primaryBackendNetworkComponentNetworkVlanId.isPresent()) {
eTo.primaryBackendNetworkComponentNetworkVlanId(primaryBackendNetworkComponentNetworkVlanId.get());
}
if (hourlyBillingFlag.isPresent()) {
eTo.hourlyBillingFlag(hourlyBillingFlag.get());
}
if (dedicatedAccountHostOnlyFlag.isPresent()) {
eTo.dedicatedAccountHostOnlyFlag(dedicatedAccountHostOnlyFlag.get());
}
if (privateNetworkOnlyFlag.isPresent()) {
eTo.privateNetworkOnlyFlag(privateNetworkOnlyFlag.get());
}
if (sshKeys.isPresent()) {
eTo.sshKeys(sshKeys.get());
eTo.diskType(diskType);
eTo.portSpeed(portSpeed);
eTo.userData(userData);
eTo.primaryNetworkComponentNetworkVlanId(primaryNetworkComponentNetworkVlanId);
eTo.primaryBackendNetworkComponentNetworkVlanId(primaryBackendNetworkComponentNetworkVlanId);
eTo.hourlyBillingFlag(hourlyBillingFlag);
eTo.dedicatedAccountHostOnlyFlag(dedicatedAccountHostOnlyFlag);
eTo.privateNetworkOnlyFlag(privateNetworkOnlyFlag);
if (!sshKeys.isEmpty()) {
eTo.sshKeys(sshKeys);
}
}
}
@ -126,7 +112,7 @@ public class SoftLayerTemplateOptions extends TemplateOptions implements Cloneab
public SoftLayerTemplateOptions blockDevices(Iterable<Integer> capacities) {
for (Integer capacity : checkNotNull(capacities, "capacities"))
checkNotNull(capacity, "all block devices must be non-empty");
this.blockDevices = Optional.<List<Integer>> of(ImmutableList.copyOf(capacities));
this.blockDevices = ImmutableList.copyOf(capacities);
return this;
}
@ -134,64 +120,55 @@ public class SoftLayerTemplateOptions extends TemplateOptions implements Cloneab
return blockDevices(ImmutableList.copyOf(checkNotNull(capacities, "capacities")));
}
public SoftLayerTemplateOptions diskType(String diskType) {
checkNotNull(diskType, "diskType was null");
this.diskType = Optional.of(diskType);
public SoftLayerTemplateOptions diskType(@Nullable String diskType) {
this.diskType = diskType;
return this;
}
public SoftLayerTemplateOptions portSpeed(Integer portSpeed) {
checkNotNull(portSpeed, "portSpeed was null");
this.portSpeed = Optional.of(portSpeed);
public SoftLayerTemplateOptions portSpeed(@Nullable Integer portSpeed) {
this.portSpeed = portSpeed;
return this;
}
public SoftLayerTemplateOptions userData(String userData) {
checkNotNull(userData, "userData was null");
this.userData = Optional.of(userData);
public SoftLayerTemplateOptions userData(@Nullable String userData) {
this.userData = userData;
return this;
}
public SoftLayerTemplateOptions primaryNetworkComponentNetworkVlanId(Integer primaryNetworkComponentNetworkVlanId) {
checkNotNull(primaryNetworkComponentNetworkVlanId, "primaryNetworkComponentNetworkVlanId was null");
this.primaryNetworkComponentNetworkVlanId = Optional.of(primaryNetworkComponentNetworkVlanId);
public SoftLayerTemplateOptions primaryNetworkComponentNetworkVlanId(@Nullable Integer primaryNetworkComponentNetworkVlanId) {
this.primaryNetworkComponentNetworkVlanId = primaryNetworkComponentNetworkVlanId;
return this;
}
public SoftLayerTemplateOptions primaryBackendNetworkComponentNetworkVlanId(Integer primaryBackendNetworkComponentNetworkVlanId) {
checkNotNull(primaryBackendNetworkComponentNetworkVlanId, "primaryBackendNetworkComponentNetworkVlanId was null");
this.primaryBackendNetworkComponentNetworkVlanId = Optional.of(primaryBackendNetworkComponentNetworkVlanId);
public SoftLayerTemplateOptions primaryBackendNetworkComponentNetworkVlanId(@Nullable Integer primaryBackendNetworkComponentNetworkVlanId) {
this.primaryBackendNetworkComponentNetworkVlanId = primaryBackendNetworkComponentNetworkVlanId;
return this;
}
public SoftLayerTemplateOptions hourlyBillingFlag(boolean hourlyBillingFlag) {
checkNotNull(hourlyBillingFlag, "hourlyBillingFlag was null");
this.hourlyBillingFlag = Optional.of(hourlyBillingFlag);
public SoftLayerTemplateOptions hourlyBillingFlag(@Nullable Boolean hourlyBillingFlag) {
this.hourlyBillingFlag = hourlyBillingFlag;
return this;
}
public SoftLayerTemplateOptions dedicatedAccountHostOnlyFlag(boolean dedicatedAccountHostOnlyFlag) {
checkNotNull(dedicatedAccountHostOnlyFlag, "dedicatedAccountHostOnlyFlag was null");
this.dedicatedAccountHostOnlyFlag = Optional.of(dedicatedAccountHostOnlyFlag);
public SoftLayerTemplateOptions dedicatedAccountHostOnlyFlag(@Nullable Boolean dedicatedAccountHostOnlyFlag) {
this.dedicatedAccountHostOnlyFlag = dedicatedAccountHostOnlyFlag;
return this;
}
public SoftLayerTemplateOptions privateNetworkOnlyFlag(boolean privateNetworkOnlyFlag) {
checkNotNull(privateNetworkOnlyFlag, "privateNetworkOnlyFlag was null");
this.privateNetworkOnlyFlag = Optional.of(privateNetworkOnlyFlag);
public SoftLayerTemplateOptions privateNetworkOnlyFlag(@Nullable Boolean privateNetworkOnlyFlag) {
this.privateNetworkOnlyFlag = privateNetworkOnlyFlag;
return this;
}
public SoftLayerTemplateOptions postInstallScriptUri(String postInstallScriptUri) {
checkNotNull(postInstallScriptUri, "postInstallScriptUri was null");
this.postInstallScriptUri = Optional.of(postInstallScriptUri);
public SoftLayerTemplateOptions postInstallScriptUri(@Nullable String postInstallScriptUri) {
this.postInstallScriptUri = postInstallScriptUri;
return this;
}
public SoftLayerTemplateOptions sshKeys(Iterable<Integer> sshKeys) {
for (Integer sshKey : checkNotNull(sshKeys, "sshKeys"))
checkNotNull(sshKey, "sshKeys must be non-empty");
this.sshKeys = Optional.<List<Integer>> of(ImmutableList.copyOf(sshKeys));
this.sshKeys = ImmutableList.copyOf(sshKeys);
return this;
}
@ -203,38 +180,36 @@ public class SoftLayerTemplateOptions extends TemplateOptions implements Cloneab
return domainName;
}
public Optional<List<Integer>> getBlockDevices() {
public List<Integer> getBlockDevices() {
return blockDevices;
}
public Optional<String> getDiskType() {
public String getDiskType() {
return diskType;
}
public Optional<Integer> getPortSpeed() {
public Integer getPortSpeed() {
return portSpeed;
}
public Optional<String> getUserData() { return userData; }
public String getUserData() { return userData; }
public Optional<Integer> getPrimaryNetworkComponentNetworkVlanId() { return primaryNetworkComponentNetworkVlanId; }
public Integer getPrimaryNetworkComponentNetworkVlanId() { return primaryNetworkComponentNetworkVlanId; }
public Optional<Integer> getPrimaryBackendNetworkComponentNetworkVlanId() { return primaryBackendNetworkComponentNetworkVlanId; }
public Integer getPrimaryBackendNetworkComponentNetworkVlanId() { return primaryBackendNetworkComponentNetworkVlanId; }
public Optional<Boolean> isHourlyBillingFlag() { return hourlyBillingFlag; }
public Boolean isHourlyBillingFlag() { return hourlyBillingFlag; }
public Optional<Boolean> isDedicatedAccountHostOnlyFlag() { return dedicatedAccountHostOnlyFlag; }
public Boolean isDedicatedAccountHostOnlyFlag() { return dedicatedAccountHostOnlyFlag; }
public Optional<Boolean> isPrivateNetworkOnlyFlag() { return privateNetworkOnlyFlag; }
public Boolean isPrivateNetworkOnlyFlag() { return privateNetworkOnlyFlag; }
public Optional<String> getPostInstallScriptUri() { return postInstallScriptUri; }
public String getPostInstallScriptUri() { return postInstallScriptUri; }
public Optional<List<Integer>> getSshKeys() {
public List<Integer> getSshKeys() {
return sshKeys;
}
public static final SoftLayerTemplateOptions NONE = new SoftLayerTemplateOptions();
public static class Builder {
/**
@ -242,7 +217,7 @@ public class SoftLayerTemplateOptions extends TemplateOptions implements Cloneab
*/
public static SoftLayerTemplateOptions domainName(String domainName) {
SoftLayerTemplateOptions options = new SoftLayerTemplateOptions();
return SoftLayerTemplateOptions.class.cast(options.domainName(domainName));
return options.domainName(domainName);
}
/**
@ -250,12 +225,12 @@ public class SoftLayerTemplateOptions extends TemplateOptions implements Cloneab
*/
public static SoftLayerTemplateOptions blockDevices(Integer... capacities) {
SoftLayerTemplateOptions options = new SoftLayerTemplateOptions();
return SoftLayerTemplateOptions.class.cast(options.blockDevices(capacities));
return options.blockDevices(capacities);
}
public static SoftLayerTemplateOptions blockDevices(Iterable<Integer> capacities) {
SoftLayerTemplateOptions options = new SoftLayerTemplateOptions();
return SoftLayerTemplateOptions.class.cast(options.blockDevices(capacities));
return options.blockDevices(capacities);
}
/**
@ -263,7 +238,7 @@ public class SoftLayerTemplateOptions extends TemplateOptions implements Cloneab
*/
public static SoftLayerTemplateOptions diskType(String diskType) {
SoftLayerTemplateOptions options = new SoftLayerTemplateOptions();
return SoftLayerTemplateOptions.class.cast(options.diskType(diskType));
return options.diskType(diskType);
}
/**
@ -271,7 +246,7 @@ public class SoftLayerTemplateOptions extends TemplateOptions implements Cloneab
*/
public static SoftLayerTemplateOptions portSpeed(Integer portSpeed) {
SoftLayerTemplateOptions options = new SoftLayerTemplateOptions();
return SoftLayerTemplateOptions.class.cast(options.portSpeed(portSpeed));
return options.portSpeed(portSpeed);
}
/**
@ -279,7 +254,7 @@ public class SoftLayerTemplateOptions extends TemplateOptions implements Cloneab
*/
public static SoftLayerTemplateOptions userData(String userData) {
SoftLayerTemplateOptions options = new SoftLayerTemplateOptions();
return SoftLayerTemplateOptions.class.cast(options.userData(userData));
return options.userData(userData);
}
/**
@ -287,7 +262,7 @@ public class SoftLayerTemplateOptions extends TemplateOptions implements Cloneab
*/
public static SoftLayerTemplateOptions primaryNetworkComponentNetworkVlanId(Integer primaryNetworkComponentNetworkVlanId) {
SoftLayerTemplateOptions options = new SoftLayerTemplateOptions();
return SoftLayerTemplateOptions.class.cast(options.primaryNetworkComponentNetworkVlanId(primaryNetworkComponentNetworkVlanId));
return options.primaryNetworkComponentNetworkVlanId(primaryNetworkComponentNetworkVlanId);
}
/**
@ -295,7 +270,7 @@ public class SoftLayerTemplateOptions extends TemplateOptions implements Cloneab
*/
public static SoftLayerTemplateOptions primaryBackendNetworkComponentNetworkVlanId(Integer primaryBackendNetworkComponentNetworkVlanId) {
SoftLayerTemplateOptions options = new SoftLayerTemplateOptions();
return SoftLayerTemplateOptions.class.cast(options.primaryBackendNetworkComponentNetworkVlanId(primaryBackendNetworkComponentNetworkVlanId));
return options.primaryBackendNetworkComponentNetworkVlanId(primaryBackendNetworkComponentNetworkVlanId);
}
/**
@ -303,7 +278,7 @@ public class SoftLayerTemplateOptions extends TemplateOptions implements Cloneab
*/
public static SoftLayerTemplateOptions hourlyBillingFlag(boolean hourlyBillingFlag) {
SoftLayerTemplateOptions options = new SoftLayerTemplateOptions();
return SoftLayerTemplateOptions.class.cast(options.hourlyBillingFlag(hourlyBillingFlag));
return options.hourlyBillingFlag(hourlyBillingFlag);
}
/**
@ -311,7 +286,7 @@ public class SoftLayerTemplateOptions extends TemplateOptions implements Cloneab
*/
public static SoftLayerTemplateOptions dedicatedAccountHostOnlyFlag(boolean dedicatedAccountHostOnlyFlag) {
SoftLayerTemplateOptions options = new SoftLayerTemplateOptions();
return SoftLayerTemplateOptions.class.cast(options.dedicatedAccountHostOnlyFlag(dedicatedAccountHostOnlyFlag));
return options.dedicatedAccountHostOnlyFlag(dedicatedAccountHostOnlyFlag);
}
/**
@ -319,7 +294,7 @@ public class SoftLayerTemplateOptions extends TemplateOptions implements Cloneab
*/
public static SoftLayerTemplateOptions privateNetworkOnlyFlag(boolean privateNetworkOnlyFlag) {
SoftLayerTemplateOptions options = new SoftLayerTemplateOptions();
return SoftLayerTemplateOptions.class.cast(options.privateNetworkOnlyFlag(privateNetworkOnlyFlag));
return options.privateNetworkOnlyFlag(privateNetworkOnlyFlag);
}
/**
@ -327,7 +302,7 @@ public class SoftLayerTemplateOptions extends TemplateOptions implements Cloneab
*/
public static SoftLayerTemplateOptions postInstallScriptUri(String postInstallScriptUri) {
SoftLayerTemplateOptions options = new SoftLayerTemplateOptions();
return SoftLayerTemplateOptions.class.cast(options.postInstallScriptUri(postInstallScriptUri));
return options.postInstallScriptUri(postInstallScriptUri);
}
/**
@ -335,22 +310,20 @@ public class SoftLayerTemplateOptions extends TemplateOptions implements Cloneab
*/
public static SoftLayerTemplateOptions sshKeys(Integer... sshKeys) {
SoftLayerTemplateOptions options = new SoftLayerTemplateOptions();
return SoftLayerTemplateOptions.class.cast(options.sshKeys(sshKeys));
return options.sshKeys(sshKeys);
}
public static SoftLayerTemplateOptions sshKeys(Iterable<Integer> sshKeys) {
SoftLayerTemplateOptions options = new SoftLayerTemplateOptions();
return SoftLayerTemplateOptions.class.cast(options.blockDevices(sshKeys));
return options.blockDevices(sshKeys);
}
// methods that only facilitate returning the correct object type
/**
* @see TemplateOptions#inboundPorts(int...)
*/
public static SoftLayerTemplateOptions inboundPorts(int... ports) {
SoftLayerTemplateOptions options = new SoftLayerTemplateOptions();
return SoftLayerTemplateOptions.class.cast(options.inboundPorts(ports));
return options.inboundPorts(ports);
}
/**
@ -358,7 +331,23 @@ public class SoftLayerTemplateOptions extends TemplateOptions implements Cloneab
*/
public static SoftLayerTemplateOptions blockOnPort(int port, int seconds) {
SoftLayerTemplateOptions options = new SoftLayerTemplateOptions();
return SoftLayerTemplateOptions.class.cast(options.blockOnPort(port, seconds));
return options.blockOnPort(port, seconds);
}
/**
* @see TemplateOptions#installPrivateKey(String)
*/
public static SoftLayerTemplateOptions installPrivateKey(String rsaKey) {
SoftLayerTemplateOptions options = new SoftLayerTemplateOptions();
return options.installPrivateKey(rsaKey);
}
/**
* @see TemplateOptions#authorizePublicKey(String)
*/
public static SoftLayerTemplateOptions authorizePublicKey(String rsaKey) {
SoftLayerTemplateOptions options = new SoftLayerTemplateOptions();
return options.authorizePublicKey(rsaKey);
}
/**
@ -366,15 +355,7 @@ public class SoftLayerTemplateOptions extends TemplateOptions implements Cloneab
*/
public static SoftLayerTemplateOptions userMetadata(Map<String, String> userMetadata) {
SoftLayerTemplateOptions options = new SoftLayerTemplateOptions();
return SoftLayerTemplateOptions.class.cast(options.userMetadata(userMetadata));
}
/**
* @see TemplateOptions#userMetadata(String, String)
*/
public static SoftLayerTemplateOptions userMetadata(String key, String value) {
SoftLayerTemplateOptions options = new SoftLayerTemplateOptions();
return SoftLayerTemplateOptions.class.cast(options.userMetadata(key, value));
return options.userMetadata(userMetadata);
}
/**
@ -382,7 +363,7 @@ public class SoftLayerTemplateOptions extends TemplateOptions implements Cloneab
*/
public static SoftLayerTemplateOptions nodeNames(Iterable<String> nodeNames) {
SoftLayerTemplateOptions options = new SoftLayerTemplateOptions();
return SoftLayerTemplateOptions.class.cast(options.nodeNames(nodeNames));
return options.nodeNames(nodeNames);
}
/**
@ -390,14 +371,62 @@ public class SoftLayerTemplateOptions extends TemplateOptions implements Cloneab
*/
public static SoftLayerTemplateOptions networks(Iterable<String> networks) {
SoftLayerTemplateOptions options = new SoftLayerTemplateOptions();
return SoftLayerTemplateOptions.class.cast(options.networks(networks));
}
return options.networks(networks);
}
/**
* @see TemplateOptions#overrideLoginUser(String)
*/
public static SoftLayerTemplateOptions overrideLoginUser(String user) {
SoftLayerTemplateOptions options = new SoftLayerTemplateOptions();
return options.overrideLoginUser(user);
}
/**
* @see TemplateOptions#overrideLoginPassword(String)
*/
public static SoftLayerTemplateOptions overrideLoginPassword(String password) {
SoftLayerTemplateOptions options = new SoftLayerTemplateOptions();
return options.overrideLoginPassword(password);
}
/**
* @see TemplateOptions#overrideLoginPrivateKey(String)
*/
public static SoftLayerTemplateOptions overrideLoginPrivateKey(String privateKey) {
SoftLayerTemplateOptions options = new SoftLayerTemplateOptions();
return options.overrideLoginPrivateKey(privateKey);
}
/**
* @see TemplateOptions#overrideAuthenticateSudo(boolean)
*/
public static SoftLayerTemplateOptions overrideAuthenticateSudo(boolean authenticateSudo) {
SoftLayerTemplateOptions options = new SoftLayerTemplateOptions();
return options.overrideAuthenticateSudo(authenticateSudo);
}
/**
* @see TemplateOptions#overrideLoginCredentials(LoginCredentials)
*/
public static SoftLayerTemplateOptions overrideLoginCredentials(LoginCredentials credentials) {
SoftLayerTemplateOptions options = new SoftLayerTemplateOptions();
return options.overrideLoginCredentials(credentials);
}
/**
* @see TemplateOptions#blockUntilRunning(boolean)
*/
public static SoftLayerTemplateOptions blockUntilRunning(boolean blockUntilRunning) {
SoftLayerTemplateOptions options = new SoftLayerTemplateOptions();
return options.blockUntilRunning(blockUntilRunning);
}
}
// methods that only facilitate returning the correct object type
/**
* @see TemplateOptions#blockOnPort(int, int)
* {@inheritDoc}
*/
@Override
public SoftLayerTemplateOptions blockOnPort(int port, int seconds) {
@ -405,7 +434,7 @@ public class SoftLayerTemplateOptions extends TemplateOptions implements Cloneab
}
/**
* @see TemplateOptions#inboundPorts(int...)
* {@inheritDoc}
*/
@Override
public SoftLayerTemplateOptions inboundPorts(int... ports) {
@ -413,7 +442,7 @@ public class SoftLayerTemplateOptions extends TemplateOptions implements Cloneab
}
/**
* @see TemplateOptions#authorizePublicKey(String)
* {@inheritDoc}
*/
@Override
public SoftLayerTemplateOptions authorizePublicKey(String publicKey) {
@ -421,13 +450,93 @@ public class SoftLayerTemplateOptions extends TemplateOptions implements Cloneab
}
/**
* @see TemplateOptions#installPrivateKey(String)
* {@inheritDoc}
*/
@Override
public SoftLayerTemplateOptions installPrivateKey(String privateKey) {
return SoftLayerTemplateOptions.class.cast(super.installPrivateKey(privateKey));
}
/**
* {@inheritDoc}
*/
@Override
public SoftLayerTemplateOptions blockUntilRunning(boolean blockUntilRunning) {
return SoftLayerTemplateOptions.class.cast(super.blockUntilRunning(blockUntilRunning));
}
/**
* {@inheritDoc}
*/
@Override
public SoftLayerTemplateOptions dontAuthorizePublicKey() {
return SoftLayerTemplateOptions.class.cast(super.dontAuthorizePublicKey());
}
/**
* {@inheritDoc}
*/
@Override
public SoftLayerTemplateOptions nameTask(String name) {
return SoftLayerTemplateOptions.class.cast(super.nameTask(name));
}
/**
* {@inheritDoc}
*/
@Override
public SoftLayerTemplateOptions runAsRoot(boolean runAsRoot) {
return SoftLayerTemplateOptions.class.cast(super.runAsRoot(runAsRoot));
}
/**
* {@inheritDoc}
*/
@Override
public SoftLayerTemplateOptions runScript(Statement script) {
return SoftLayerTemplateOptions.class.cast(super.runScript(script));
}
/**
* {@inheritDoc}
*/
@Override
public SoftLayerTemplateOptions overrideLoginCredentials(LoginCredentials overridingCredentials) {
return SoftLayerTemplateOptions.class.cast(super.overrideLoginCredentials(overridingCredentials));
}
/**
* {@inheritDoc}
*/
@Override
public SoftLayerTemplateOptions overrideLoginPassword(String password) {
return SoftLayerTemplateOptions.class.cast(super.overrideLoginPassword(password));
}
/**
* {@inheritDoc}
*/
@Override
public SoftLayerTemplateOptions overrideLoginPrivateKey(String privateKey) {
return SoftLayerTemplateOptions.class.cast(super.overrideLoginPrivateKey(privateKey));
}
/**
* {@inheritDoc}
*/
@Override
public SoftLayerTemplateOptions overrideLoginUser(String loginUser) {
return SoftLayerTemplateOptions.class.cast(super.overrideLoginUser(loginUser));
}
/**
* {@inheritDoc}
*/
@Override
public SoftLayerTemplateOptions overrideAuthenticateSudo(boolean authenticateSudo) {
return SoftLayerTemplateOptions.class.cast(super.overrideAuthenticateSudo(authenticateSudo));
}
/**
* {@inheritDoc}
*/

View File

@ -30,7 +30,6 @@ import static org.jclouds.compute.util.ComputeServiceUtils.getSpace;
import static org.jclouds.softlayer.reference.SoftLayerConstants.PROPERTY_SOFTLAYER_VIRTUALGUEST_ACTIVE_TRANSACTIONS_DELAY;
import static org.jclouds.softlayer.reference.SoftLayerConstants.PROPERTY_SOFTLAYER_VIRTUALGUEST_LOGIN_DETAILS_DELAY;
import static org.jclouds.util.Predicates2.retry;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
@ -82,6 +81,7 @@ import com.google.common.base.Supplier;
import com.google.common.collect.ComparisonChain;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSet.Builder;
import com.google.common.collect.ImmutableSortedSet;
import com.google.common.collect.Sets;
@ -99,7 +99,6 @@ public class SoftLayerComputeServiceAdapter implements
private static final int USER_META_NOTES_MAX_LENGTH = 1000;
private static final String BOOTABLE_DEVICE = "0";
public static final String DEFAULT_DISK_TYPE = "LOCAL";
public static final int DEFAULT_MAX_PORT_SPEED = 100;
@Resource
@Named(ComputeServiceConstants.COMPUTE_LOGGER)
@ -136,9 +135,12 @@ public class SoftLayerComputeServiceAdapter implements
SoftLayerTemplateOptions templateOptions = template.getOptions().as(SoftLayerTemplateOptions.class);
String domainName = templateOptions.getDomainName();
String diskType = templateOptions.getDiskType().or(DEFAULT_DISK_TYPE);
boolean hourlyBillingFlag = templateOptions.isHourlyBillingFlag().or(true);
int maxPortSpeed = templateOptions.getPortSpeed().or(DEFAULT_MAX_PORT_SPEED);
String diskType = templateOptions.getDiskType() == null ? DEFAULT_DISK_TYPE : templateOptions.getDiskType();
boolean hourlyBillingFlag = templateOptions.isHourlyBillingFlag() == null ? true : templateOptions.isHourlyBillingFlag();
Integer portSpeed = templateOptions.getPortSpeed();
Set<VirtualGuestNetworkComponent> networkComponents = portSpeed != null ?
ImmutableSet.of(VirtualGuestNetworkComponent.builder().speed(portSpeed).build()) :
ImmutableSet.<VirtualGuestNetworkComponent>of();
final Datacenter datacenter = Datacenter.builder().name(template.getLocation().getId()).build();
final String imageId = template.getImage().getId();
int cores = (int) template.getHardware().getProcessors().get(0).getCores();
@ -151,10 +153,12 @@ public class SoftLayerComputeServiceAdapter implements
.startCpus(cores)
.maxMemory(template.getHardware().getRam())
.datacenter(datacenter)
.localDiskFlag(isLocalDisk(diskType));
.localDiskFlag(isLocalDisk(diskType))
.networkComponents(networkComponents);
// set operating system or blockDeviceTemplateGroup
Optional<OperatingSystem> optionalOperatingSystem = tryExtractOperatingSystemFrom(imageId);
if (optionalOperatingSystem.isPresent()) {
if (optionalOperatingSystem != null) {
virtualGuestBuilder.operatingSystem(optionalOperatingSystem.get());
// the imageId specified is an id of a public/private/flex image
} else {
@ -163,47 +167,47 @@ public class SoftLayerComputeServiceAdapter implements
virtualGuestBuilder.blockDeviceTemplateGroup(blockDeviceTemplateGroup).build();
}
// set multi-disks
if (templateOptions.getBlockDevices().isPresent()) {
List<VirtualGuestBlockDevice> blockDevices = getBlockDevices(templateOptions.getBlockDevices().get(), diskType);
if (!templateOptions.getBlockDevices().isEmpty()) {
List<VirtualGuestBlockDevice> blockDevices = getBlockDevices(templateOptions.getBlockDevices(), diskType);
virtualGuestBuilder.blockDevices(blockDevices);
}
// set dedicatedAccountHostOnlyFlag
if (templateOptions.isDedicatedAccountHostOnlyFlag().isPresent()) {
virtualGuestBuilder.dedicatedAccountHostOnly(templateOptions.isDedicatedAccountHostOnlyFlag().get());
if (templateOptions.isDedicatedAccountHostOnlyFlag() != null) {
virtualGuestBuilder.dedicatedAccountHostOnly(templateOptions.isDedicatedAccountHostOnlyFlag());
}
// set privateNetworkOnlyFlag
if (templateOptions.isPrivateNetworkOnlyFlag().isPresent()) {
virtualGuestBuilder.privateNetworkOnlyFlag(templateOptions.isPrivateNetworkOnlyFlag().get());
if (templateOptions.isPrivateNetworkOnlyFlag() != null) {
virtualGuestBuilder.privateNetworkOnlyFlag(templateOptions.isPrivateNetworkOnlyFlag());
}
// set primaryNetworkComponent.networkVlan.id
if (templateOptions.getPrimaryNetworkComponentNetworkVlanId().isPresent()) {
int primaryNetworkComponentNetworkVlanId = templateOptions.getPrimaryNetworkComponentNetworkVlanId().get();
if (templateOptions.getPrimaryNetworkComponentNetworkVlanId() != null) {
int primaryNetworkComponentNetworkVlanId = templateOptions.getPrimaryNetworkComponentNetworkVlanId();
virtualGuestBuilder.primaryNetworkComponent(
VirtualGuestNetworkComponent.builder()
.networkVlan(NetworkVlan.builder().id(primaryNetworkComponentNetworkVlanId).build())
.speed(maxPortSpeed).build());
.build());
}
// set primaryBackendNetworkComponent.networkVlan.id
if (templateOptions.getPrimaryBackendNetworkComponentNetworkVlanId().isPresent()) {
int primaryBackendNetworkComponentNetworkVlanId = templateOptions.getPrimaryBackendNetworkComponentNetworkVlanId().get();
if (templateOptions.getPrimaryBackendNetworkComponentNetworkVlanId() != null) {
int primaryBackendNetworkComponentNetworkVlanId = templateOptions.getPrimaryBackendNetworkComponentNetworkVlanId();
virtualGuestBuilder.primaryBackendNetworkComponent(
VirtualGuestNetworkComponent.builder()
.networkVlan(NetworkVlan.builder().id(primaryBackendNetworkComponentNetworkVlanId).build())
.speed(maxPortSpeed).build());
.build());
}
// set postInstallScriptUri
if (templateOptions.getPostInstallScriptUri().isPresent()) {
if (templateOptions.getPostInstallScriptUri() != null) {
// Specifies the uri location of the script to be downloaded and run after installation is complete.
virtualGuestBuilder.postInstallScriptUri(templateOptions.getPostInstallScriptUri().get());
virtualGuestBuilder.postInstallScriptUri(templateOptions.getPostInstallScriptUri());
}
// set userData
if (templateOptions.getUserData().isPresent()) {
virtualGuestBuilder.virtualGuestAttribute(VirtualGuestAttribute.builder().value(templateOptions.getUserData().get()).build());
if (templateOptions.getUserData() != null) {
virtualGuestBuilder.virtualGuestAttribute(VirtualGuestAttribute.builder().value(templateOptions.getUserData()).build());
}
// set sshKeys
if (templateOptions.getSshKeys().isPresent()) {
if (!templateOptions.getSshKeys().isEmpty()) {
Set<SecuritySshKey> sshKeys = Sets.newHashSet();
for (int sshKeyId : templateOptions.getSshKeys().get()) {
for (int sshKeyId : templateOptions.getSshKeys()) {
sshKeys.add(SecuritySshKey.builder().id(sshKeyId).build());
}
virtualGuestBuilder.sshKeys(sshKeys);
@ -215,7 +219,7 @@ public class SoftLayerComputeServiceAdapter implements
logger.trace("<< VirtualGuest(%s)", result.getId());
// tags
if (templateOptions.getTags() != null) {
if (!templateOptions.getTags().isEmpty()) {
api.getVirtualGuestApi().setTags(result.getId(), templateOptions.getTags());
}

View File

@ -25,9 +25,11 @@ import org.jclouds.softlayer.domain.Datacenter;
import org.jclouds.softlayer.domain.OperatingSystem;
import org.jclouds.softlayer.domain.VirtualGuest;
import org.jclouds.softlayer.domain.VirtualGuestBlockDeviceTemplateGroup;
import org.jclouds.softlayer.domain.VirtualGuestNetworkComponent;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableSet;
import com.google.gson.Gson;
@Test(groups = "unit", testName = "VirtualGuestToJsonTest")
@ -56,6 +58,7 @@ public class VirtualGuestToJsonTest {
.operatingSystemReferenceCode("UBUNTU_12_64")
.build())
.localDiskFlag(true)
.networkComponents(ImmutableSet.<VirtualGuestNetworkComponent>of())
.build();
request = binder.bindToRequest(request, virtualGuestWithOS);
@ -80,6 +83,7 @@ public class VirtualGuestToJsonTest {
.globalIdentifier("ffaafa98-4b4a-4fa7-b9f7-b1bad5ec50f0")
.build())
.localDiskFlag(true)
.networkComponents(ImmutableSet.<VirtualGuestNetworkComponent>of())
.build();
request = binder.bindToRequest(request, virtualGuestWithVirtualGuestBlockDeviceTemplateGroup);

View File

@ -67,9 +67,11 @@ public class SoftLayerComputeServiceContextLiveTest extends BaseComputeServiceCo
// test passing custom options
SoftLayerTemplateOptions options = template.getOptions().as(SoftLayerTemplateOptions.class);
options.domainName("live.org");
options.portSpeed(100);
//tags
options.tags(ImmutableList.of("jclouds"));
Set<? extends NodeMetadata> nodes = context.getComputeService().createNodesInGroup(name, numNodes, template);
assertEquals(numNodes, nodes.size(), "wrong number of nodes");
for (NodeMetadata node : nodes) {