added option to specify domainName as an option when using softlayer

This commit is contained in:
Adrian Cole 2011-09-29 10:11:00 -07:00 committed by Jason King
parent 1eaddcde78
commit 0847c667ee
6 changed files with 372 additions and 36 deletions

View File

@ -18,15 +18,14 @@
*/
package org.jclouds.softlayer.compute.config;
import com.google.common.base.Function;
import com.google.common.base.Supplier;
import com.google.inject.Injector;
import com.google.inject.TypeLiteral;
import java.util.Set;
import org.jclouds.compute.ComputeServiceAdapter;
import org.jclouds.compute.config.ComputeServiceAdapterContextModule;
import org.jclouds.compute.domain.NodeMetadata;
import org.jclouds.compute.domain.OsFamily;
import org.jclouds.compute.domain.TemplateBuilder;
import org.jclouds.compute.options.TemplateOptions;
import org.jclouds.domain.Location;
import org.jclouds.location.suppliers.OnlyLocationOrFirstZone;
import org.jclouds.softlayer.SoftLayerAsyncClient;
@ -35,12 +34,16 @@ import org.jclouds.softlayer.compute.functions.DatacenterToLocation;
import org.jclouds.softlayer.compute.functions.ProductItemToImage;
import org.jclouds.softlayer.compute.functions.ProductItemsToHardware;
import org.jclouds.softlayer.compute.functions.VirtualGuestToNodeMetadata;
import org.jclouds.softlayer.compute.options.SoftLayerTemplateOptions;
import org.jclouds.softlayer.compute.strategy.SoftLayerComputeServiceAdapter;
import org.jclouds.softlayer.domain.Datacenter;
import org.jclouds.softlayer.domain.ProductItem;
import org.jclouds.softlayer.domain.VirtualGuest;
import java.util.Set;
import com.google.common.base.Function;
import com.google.common.base.Supplier;
import com.google.inject.Injector;
import com.google.inject.TypeLiteral;
/**
*
@ -68,6 +71,7 @@ public class SoftLayerComputeServiceContextModule extends
.to(DatacenterToLocation.class);
bind(new TypeLiteral<Supplier<Location>>() {})
.to(OnlyLocationOrFirstZone.class);
bind(TemplateOptions.class).to(SoftLayerTemplateOptions.class);
}
protected TemplateBuilder provideTemplate(Injector injector, TemplateBuilder template) {

View File

@ -0,0 +1,231 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jclouds.softlayer.compute.options;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import org.jclouds.compute.ComputeService;
import org.jclouds.compute.options.TemplateOptions;
import org.jclouds.io.Payload;
import org.jclouds.softlayer.features.VirtualGuestClient;
import com.google.common.net.InternetDomainName;
/**
* Contains options supported by the
* {@link ComputeService#createNodesInGroup(String, int, TemplateOptions)} and
* {@link ComputeService#runNodesWithTag(String, int, TemplateOptions)} operations on the
* <em>gogrid</em> provider.
*
* <h2>Usage</h2> The recommended way to instantiate a {@link SoftLayerTemplateOptions} object is to
* statically import {@code SoftLayerTemplateOptions.*} and invoke a static creation method followed
* by an instance mutator (if needed):
* <p>
*
* <pre>
* import static org.jclouds.compute.options.SoftLayerTemplateOptions.Builder.*;
* ComputeService client = // get connection
* templateBuilder.options(inboundPorts(22, 80, 8080, 443));
* Set&lt;? extends NodeMetadata&gt; set = client.runNodesWithTag(tag, 2, templateBuilder.build());
* </pre>
*
* @author Adrian Cole
*/
public class SoftLayerTemplateOptions extends TemplateOptions implements Cloneable {
protected String domainName = "jclouds.org";
@Override
public SoftLayerTemplateOptions clone() {
SoftLayerTemplateOptions options = new SoftLayerTemplateOptions();
copyTo(options);
return options;
}
@Override
public void copyTo(TemplateOptions to) {
super.copyTo(to);
if (to instanceof SoftLayerTemplateOptions) {
SoftLayerTemplateOptions eTo = SoftLayerTemplateOptions.class.cast(to);
eTo.domainName(domainName);
}
}
/**
* will replace the default domain used when ordering virtual guests. Note this needs to contain
* a public suffix!
*
* @see VirtualGuestClient#orderVirtualGuest
* @see InternetDomainName#hasPublicSuffix
*/
public TemplateOptions domainName(String domainName) {
checkNotNull(domainName, "domainName was null");
checkArgument(InternetDomainName.from(domainName).hasPublicSuffix(), "domainName %s has no public suffix",
domainName);
this.domainName = domainName;
return this;
}
public String getDomainName() {
return domainName;
}
public static final SoftLayerTemplateOptions NONE = new SoftLayerTemplateOptions();
public static class Builder {
/**
* @see #domainName
*/
public static SoftLayerTemplateOptions domainName(String domainName) {
SoftLayerTemplateOptions options = new SoftLayerTemplateOptions();
return SoftLayerTemplateOptions.class.cast(options.domainName(domainName));
}
// 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));
}
/**
* @see TemplateOptions#blockOnPort(int, int)
*/
public static SoftLayerTemplateOptions blockOnPort(int port, int seconds) {
SoftLayerTemplateOptions options = new SoftLayerTemplateOptions();
return SoftLayerTemplateOptions.class.cast(options.blockOnPort(port, seconds));
}
/**
* @see TemplateOptions#runScript(Payload)
*/
public static SoftLayerTemplateOptions runScript(Payload script) {
SoftLayerTemplateOptions options = new SoftLayerTemplateOptions();
return SoftLayerTemplateOptions.class.cast(options.runScript(script));
}
/**
* @see TemplateOptions#installPrivateKey(Payload)
*/
@Deprecated
public static SoftLayerTemplateOptions installPrivateKey(Payload rsaKey) {
SoftLayerTemplateOptions options = new SoftLayerTemplateOptions();
return SoftLayerTemplateOptions.class.cast(options.installPrivateKey(rsaKey));
}
/**
* @see TemplateOptions#authorizePublicKey(Payload)
*/
@Deprecated
public static SoftLayerTemplateOptions authorizePublicKey(Payload rsaKey) {
SoftLayerTemplateOptions options = new SoftLayerTemplateOptions();
return SoftLayerTemplateOptions.class.cast(options.authorizePublicKey(rsaKey));
}
/**
* @see TemplateOptions#withMetadata()
*/
public static SoftLayerTemplateOptions withMetadata() {
SoftLayerTemplateOptions options = new SoftLayerTemplateOptions();
return SoftLayerTemplateOptions.class.cast(options.withMetadata());
}
}
// methods that only facilitate returning the correct object type
/**
* @see TemplateOptions#blockOnPort(int, int)
*/
@Override
public SoftLayerTemplateOptions blockOnPort(int port, int seconds) {
return SoftLayerTemplateOptions.class.cast(super.blockOnPort(port, seconds));
}
/**
* @see TemplateOptions#inboundPorts(int...)
*/
@Override
public SoftLayerTemplateOptions inboundPorts(int... ports) {
return SoftLayerTemplateOptions.class.cast(super.inboundPorts(ports));
}
/**
* @see TemplateOptions#authorizePublicKey(String)
*/
@Override
public SoftLayerTemplateOptions authorizePublicKey(String publicKey) {
return SoftLayerTemplateOptions.class.cast(super.authorizePublicKey(publicKey));
}
/**
* @see TemplateOptions#authorizePublicKey(Payload)
*/
@Override
@Deprecated
public SoftLayerTemplateOptions authorizePublicKey(Payload publicKey) {
return SoftLayerTemplateOptions.class.cast(super.authorizePublicKey(publicKey));
}
/**
* @see TemplateOptions#installPrivateKey(String)
*/
@Override
public SoftLayerTemplateOptions installPrivateKey(String privateKey) {
return SoftLayerTemplateOptions.class.cast(super.installPrivateKey(privateKey));
}
/**
* @see TemplateOptions#installPrivateKey(Payload)
*/
@Override
@Deprecated
public SoftLayerTemplateOptions installPrivateKey(Payload privateKey) {
return SoftLayerTemplateOptions.class.cast(super.installPrivateKey(privateKey));
}
/**
* @see TemplateOptions#runScript(Payload)
*/
@Override
public SoftLayerTemplateOptions runScript(Payload script) {
return SoftLayerTemplateOptions.class.cast(super.runScript(script));
}
/**
* @see TemplateOptions#runScript(byte[])
*/
@Override
@Deprecated
public SoftLayerTemplateOptions runScript(byte[] script) {
return SoftLayerTemplateOptions.class.cast(super.runScript(script));
}
/**
* @see TemplateOptions#withMetadata()
*/
@Override
public SoftLayerTemplateOptions withMetadata() {
return SoftLayerTemplateOptions.class.cast(super.withMetadata());
}
}

View File

@ -18,32 +18,46 @@
*/
package org.jclouds.softlayer.compute.strategy;
import com.google.common.base.Predicates;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.jclouds.softlayer.predicates.ProductItemPredicates.categoryCode;
import static org.jclouds.softlayer.predicates.ProductItemPredicates.matches;
import static org.jclouds.softlayer.predicates.ProductItemPredicates.units;
import static org.jclouds.softlayer.predicates.ProductPackagePredicates.named;
import java.util.Map;
import java.util.Set;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import org.jclouds.compute.ComputeService;
import org.jclouds.compute.ComputeServiceAdapter;
import org.jclouds.compute.domain.Template;
import org.jclouds.domain.Credentials;
import org.jclouds.softlayer.SoftLayerClient;
import org.jclouds.softlayer.compute.functions.ProductItems;
import org.jclouds.softlayer.domain.*;
import org.jclouds.softlayer.compute.options.SoftLayerTemplateOptions;
import org.jclouds.softlayer.domain.BillingItemVirtualGuest;
import org.jclouds.softlayer.domain.Datacenter;
import org.jclouds.softlayer.domain.OperatingSystem;
import org.jclouds.softlayer.domain.Password;
import org.jclouds.softlayer.domain.ProductItem;
import org.jclouds.softlayer.domain.ProductItemPrice;
import org.jclouds.softlayer.domain.ProductOrder;
import org.jclouds.softlayer.domain.ProductPackage;
import org.jclouds.softlayer.domain.VirtualGuest;
import org.jclouds.softlayer.features.AccountClient;
import org.jclouds.softlayer.features.ProductPackageClient;
import org.jclouds.softlayer.reference.SoftLayerConstants;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import java.util.Map;
import java.util.Set;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.jclouds.softlayer.predicates.ProductItemPredicates.*;
import static org.jclouds.softlayer.predicates.ProductPackagePredicates.named;
import com.google.common.base.Predicates;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
/**
* defines the connection between the {@link SoftLayerClient} implementation and the jclouds
@ -70,7 +84,12 @@ public class SoftLayerComputeServiceAdapter implements
@Override
public VirtualGuest createNodeWithGroupEncodedIntoNameThenStoreCredentials(String group, String name,
Template template, Map<String, Credentials> credentialStore) {
checkNotNull(template, "template was null");
checkNotNull(template.getOptions(), "template options was null");
checkArgument(template.getOptions().getClass().isAssignableFrom(SoftLayerTemplateOptions.class),
"options class %s should have been assignable from SoftLayerTemplateOptions", template.getOptions()
.getClass());
Iterable<VirtualGuest> existing = findVirtualGuests(name,group);
if(!Iterables.isEmpty(existing)) {
throw new IllegalStateException(
@ -78,7 +97,7 @@ public class SoftLayerComputeServiceAdapter implements
}
VirtualGuest newGuest = VirtualGuest.builder()
.domain(group)
.domain(template.getOptions().as(SoftLayerTemplateOptions.class).getDomainName())
.hostname(name)
.build();

View File

@ -30,6 +30,7 @@ import org.jclouds.compute.domain.ExecResponse;
import org.jclouds.compute.domain.Template;
import org.jclouds.domain.Credentials;
import org.jclouds.net.IPSocket;
import org.jclouds.softlayer.compute.options.SoftLayerTemplateOptions;
import org.jclouds.softlayer.compute.strategy.SoftLayerComputeServiceAdapter;
import org.jclouds.softlayer.domain.ProductItem;
import org.jclouds.softlayer.domain.VirtualGuest;
@ -64,16 +65,19 @@ public class SoftLayerComputeServiceAdapterLiveTest extends BaseSoftLayerClientL
@Test
public void testCreateNodeWithGroupEncodedIntoNameThenStoreCredentials() {
String group = "jclouds.org";
String group = "foo";
String name = "foo-ef4";
Template template = computeContext.getComputeService().templateBuilder()
.locationId("3") // the default (singapore) doesn't work.
.build();
// test passing custom options
template.getOptions().as(SoftLayerTemplateOptions.class).domainName("me.org");
Map<String, Credentials> credentialStore = Maps.newLinkedHashMap();
guest = adapter.createNodeWithGroupEncodedIntoNameThenStoreCredentials(group, name, template, credentialStore);
assertEquals(guest.getHostname(), name);
assertEquals(guest.getDomain(), group);
assertEquals(guest.getDomain(), template.getOptions().as(SoftLayerTemplateOptions.class).getDomainName());
// check other things, like cpu correct, mem correct, image/os is correct
// (as possible)
assert credentialStore.containsKey("node#" + guest.getId()) : "credentials to log into guest not found " + guest;

View File

@ -18,20 +18,22 @@
*/
package org.jclouds.softlayer.compute;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.collect.ImmutableSet;
import org.jclouds.compute.BaseTemplateBuilderLiveTest;
import org.jclouds.compute.domain.OsFamily;
import org.jclouds.compute.domain.OsFamilyVersion64Bit;
import org.jclouds.compute.domain.Template;
import org.testng.annotations.Test;
import static org.jclouds.compute.util.ComputeServiceUtils.getCores;
import static org.testng.Assert.assertEquals;
import java.io.IOException;
import java.util.Set;
import static org.jclouds.compute.util.ComputeServiceUtils.getCores;
import static org.testng.Assert.assertEquals;
import org.jclouds.compute.BaseTemplateBuilderLiveTest;
import org.jclouds.compute.domain.OsFamily;
import org.jclouds.compute.domain.OsFamilyVersion64Bit;
import org.jclouds.compute.domain.Template;
import org.jclouds.softlayer.compute.options.SoftLayerTemplateOptions;
import org.testng.annotations.Test;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.collect.ImmutableSet;
/**
*
@ -80,6 +82,8 @@ public class SoftLayerTemplateBuilderLiveTest extends BaseTemplateBuilderLiveTes
assertEquals(defaultTemplate.getImage().getOperatingSystem().is64Bit(), true);
assertEquals(defaultTemplate.getImage().getOperatingSystem().getFamily(), OsFamily.UBUNTU);
assertEquals(getCores(defaultTemplate.getHardware()), 2.0d);
// test that we bound the correct templateoptions in guice
assertEquals(defaultTemplate.getOptions().getClass(), SoftLayerTemplateOptions.class);
}
@Override

View File

@ -0,0 +1,74 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jclouds.softlayer.compute.options;
import static org.jclouds.softlayer.compute.options.SoftLayerTemplateOptions.Builder.domainName;
import static org.testng.Assert.assertEquals;
import org.jclouds.compute.options.TemplateOptions;
import org.testng.annotations.Test;
/**
* Tests possible uses of {@code SoftLayerTemplateOptions} and {@code
* SoftLayerTemplateOptions.Builder.*}.
*
* @author Adrian Cole
*/
// NOTE:without testName, this will not call @Before* and fail w/NPE during surefire
@Test(groups = "unit", testName = "SoftLayerTemplateOptionsTest")
public class SoftLayerTemplateOptionsTest {
@Test
public void testAs() {
TemplateOptions options = new SoftLayerTemplateOptions();
assertEquals(options.as(SoftLayerTemplateOptions.class), options);
}
@Test
public void testDefaultDomainName() {
TemplateOptions options = new SoftLayerTemplateOptions();
assertEquals(options.as(SoftLayerTemplateOptions.class).getDomainName(), "jclouds.org");
}
@Test
public void testDomainName() {
TemplateOptions options = new SoftLayerTemplateOptions().domainName("me.com");
assertEquals(options.as(SoftLayerTemplateOptions.class).getDomainName(), "me.com");
}
@Test
public void testDomainNameStatic() {
TemplateOptions options = domainName("me.com");
assertEquals(options.as(SoftLayerTemplateOptions.class).getDomainName(), "me.com");
}
@Test
public void testDomainNameNullHasDecentMessage() {
try {
new SoftLayerTemplateOptions().domainName(null);
assert false : "should NPE";
} catch (NullPointerException e) {
assertEquals(e.getMessage(), "domainName was null");
}
}
@Test(expectedExceptions = IllegalArgumentException.class)
public void testDomainNameIsInvalidThrowsIllegalArgument() {
new SoftLayerTemplateOptions().domainName("foo");
}
}