Merge pull request #28 from grkvlt/master

thanks for this
This commit is contained in:
Adrian Cole 2011-05-06 00:38:38 -07:00
commit 93633a630d
5 changed files with 122 additions and 112 deletions

View File

@ -0,0 +1,88 @@
/**
*
* Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed 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.gogrid.compute;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Provider;
import javax.inject.Singleton;
import org.jclouds.Constants;
import org.jclouds.collect.Memoized;
import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.compute.domain.Hardware;
import org.jclouds.compute.domain.Image;
import org.jclouds.compute.domain.NodeMetadata;
import org.jclouds.compute.domain.TemplateBuilder;
import org.jclouds.compute.internal.BaseComputeService;
import org.jclouds.compute.options.TemplateOptions;
import org.jclouds.compute.reference.ComputeServiceConstants.Timeouts;
import org.jclouds.compute.strategy.CreateNodesInGroupThenAddToSet;
import org.jclouds.compute.strategy.DestroyNodeStrategy;
import org.jclouds.compute.strategy.GetNodeMetadataStrategy;
import org.jclouds.compute.strategy.InitializeRunScriptOnNodeOrPlaceInBadMap;
import org.jclouds.compute.strategy.ListNodesStrategy;
import org.jclouds.compute.strategy.RebootNodeStrategy;
import org.jclouds.compute.strategy.ResumeNodeStrategy;
import org.jclouds.compute.strategy.SuspendNodeStrategy;
import org.jclouds.domain.Credentials;
import org.jclouds.domain.Location;
import org.jclouds.gogrid.compute.options.GoGridTemplateOptions;
import com.google.common.base.Predicate;
import com.google.common.base.Supplier;
/**
* @author Andrew Kennedy
*/
@Singleton
public class GoGridComputeService extends BaseComputeService {
@Inject
protected GoGridComputeService(ComputeServiceContext context, Map<String, Credentials> credentialStore,
@Memoized Supplier<Set<? extends Image>> images,
@Memoized Supplier<Set<? extends Hardware>> hardwareProfiles,
@Memoized Supplier<Set<? extends Location>> locations, ListNodesStrategy listNodesStrategy,
GetNodeMetadataStrategy getNodeMetadataStrategy,
CreateNodesInGroupThenAddToSet runNodesAndAddToSetStrategy, RebootNodeStrategy rebootNodeStrategy,
DestroyNodeStrategy destroyNodeStrategy, ResumeNodeStrategy resumeNodeStrategy,
SuspendNodeStrategy suspendNodeStrategy, Provider<TemplateBuilder> templateBuilderProvider,
Provider<TemplateOptions> templateOptionsProvider,
@Named("NODE_RUNNING") Predicate<NodeMetadata> nodeRunning,
@Named("NODE_TERMINATED") Predicate<NodeMetadata> nodeTerminated,
@Named("NODE_SUSPENDED") Predicate<NodeMetadata> nodeSuspended,
InitializeRunScriptOnNodeOrPlaceInBadMap.Factory initScriptRunnerFactory, Timeouts timeouts,
@Named(Constants.PROPERTY_USER_THREADS) ExecutorService executor) {
super(context, credentialStore, images, hardwareProfiles, locations, listNodesStrategy, getNodeMetadataStrategy,
runNodesAndAddToSetStrategy, rebootNodeStrategy, destroyNodeStrategy, resumeNodeStrategy, suspendNodeStrategy,
templateBuilderProvider, templateOptionsProvider, nodeRunning, nodeTerminated, nodeSuspended,
initScriptRunnerFactory, timeouts, executor);
}
/**
* Returns template options, except of type {@link GoGridTemplateOptions}.
*/
@Override
public GoGridTemplateOptions templateOptions() {
return GoGridTemplateOptions.class.cast(super.templateOptions());
}
}

View File

@ -18,21 +18,24 @@
*/ */
package org.jclouds.gogrid.compute.config; package org.jclouds.gogrid.compute.config;
import static org.jclouds.compute.util.ComputeServiceUtils.getCores; import static org.jclouds.compute.util.ComputeServiceUtils.*;
import static org.jclouds.compute.util.ComputeServiceUtils.getSpace;
import java.util.Map; import java.util.Map;
import javax.inject.Singleton; import javax.inject.Singleton;
import org.jclouds.compute.ComputeService;
import org.jclouds.compute.ComputeServiceContext; import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.compute.domain.Hardware; import org.jclouds.compute.domain.Hardware;
import org.jclouds.compute.domain.NodeMetadata; import org.jclouds.compute.domain.NodeMetadata;
import org.jclouds.compute.domain.NodeState; import org.jclouds.compute.domain.NodeState;
import org.jclouds.compute.internal.ComputeServiceContextImpl; import org.jclouds.compute.internal.ComputeServiceContextImpl;
import org.jclouds.compute.options.TemplateOptions;
import org.jclouds.gogrid.GoGridAsyncClient; import org.jclouds.gogrid.GoGridAsyncClient;
import org.jclouds.gogrid.GoGridClient; import org.jclouds.gogrid.GoGridClient;
import org.jclouds.gogrid.compute.GoGridComputeService;
import org.jclouds.gogrid.compute.functions.ServerToNodeMetadata; import org.jclouds.gogrid.compute.functions.ServerToNodeMetadata;
import org.jclouds.gogrid.compute.options.GoGridTemplateOptions;
import org.jclouds.gogrid.domain.Server; import org.jclouds.gogrid.domain.Server;
import org.jclouds.gogrid.domain.ServerState; import org.jclouds.gogrid.domain.ServerState;
import org.jclouds.rest.RestContext; import org.jclouds.rest.RestContext;
@ -49,11 +52,12 @@ import com.google.inject.TypeLiteral;
/** /**
* @author Oleksiy Yarmula * @author Oleksiy Yarmula
* @author Adrian Cole * @author Adrian Cole
* @author Andrew Kennedy
*/ */
public class GoGridComputeServiceDependenciesModule extends AbstractModule { public class GoGridComputeServiceDependenciesModule extends AbstractModule {
@Override
protected void configure() { protected void configure() {
bind(TemplateOptions.class).to(GoGridTemplateOptions.class);
bind(ComputeService.class).to(GoGridComputeService.class);
bind(new TypeLiteral<Function<Server, NodeMetadata>>() { bind(new TypeLiteral<Function<Server, NodeMetadata>>() {
}).to(ServerToNodeMetadata.class); }).to(ServerToNodeMetadata.class);
bind(new TypeLiteral<ComputeServiceContext>() { bind(new TypeLiteral<ComputeServiceContext>() {

View File

@ -18,27 +18,28 @@
*/ */
package org.jclouds.gogrid.compute.options; package org.jclouds.gogrid.compute.options;
import java.util.Arrays; import org.jclouds.compute.ComputeService;
import org.jclouds.compute.options.TemplateOptions; import org.jclouds.compute.options.TemplateOptions;
import org.jclouds.gogrid.domain.IpType;
import org.jclouds.io.Payload; import org.jclouds.io.Payload;
/** /**
* Contains options supported in the {@code ComputeService#runNode} operation on the "gogrid" * Contains options supported by the {@link ComputeService#createNodesInGroup(String, int, TemplateOptions)}
* provider. * and {@link ComputeService#runNodesWithTag(String, int, TemplateOptions)} operations on
* the <em>gogrid</em> provider.
* *
* <h2>Usage</h2> * <h2>Usage</h2>
* The recommended way to instantiate a {@link GoGridTemplateOptions} object is to statically * The recommended way to instantiate a {@link GoGridTemplateOptions} object is to statically
* import {@code GoGridTemplateOptions.*} and invoke a static creation method followed by an * import {@code GoGridTemplateOptions.*} and invoke a static creation method followed by an
* instance mutator (if needed): * instance mutator (if needed):
* <p> * <p>
* <code> * <pre>
* import static org.jclouds.compute.options.GoGridTemplateOptions.Builder.*; * import static org.jclouds.compute.options.GoGridTemplateOptions.Builder.*;
* ComputeService client = // get connection * ComputeService client = // get connection
* templateBuilder.options(inboundPorts(22, 80, 8080, 443)); * templateBuilder.options(inboundPorts(22, 80, 8080, 443));
* Set<? extends NodeMetadata> set = client.runNodesWithTag(tag, 2, templateBuilder.build()); * Set&lt;? extends NodeMetadata&gt; set = client.runNodesWithTag(tag, 2, templateBuilder.build());
* <code> * </pre>
*
* TODO add GoGrid specific options
* *
* @author Adrian Cole * @author Adrian Cole
* @author Andrew Kennedy * @author Andrew Kennedy
@ -55,36 +56,18 @@ public class GoGridTemplateOptions extends TemplateOptions implements Cloneable
public void copyTo(TemplateOptions to) { public void copyTo(TemplateOptions to) {
super.copyTo(to); super.copyTo(to);
if (to instanceof GoGridTemplateOptions) { if (to instanceof GoGridTemplateOptions) {
@SuppressWarnings("unused")
GoGridTemplateOptions eTo = GoGridTemplateOptions.class.cast(to); GoGridTemplateOptions eTo = GoGridTemplateOptions.class.cast(to);
if (getIpType() != null)
eTo.ipType(getIpType());
} }
} }
private IpType ipType = null;
public static final GoGridTemplateOptions NONE = new GoGridTemplateOptions(); public static final GoGridTemplateOptions NONE = new GoGridTemplateOptions();
/**
* Specifies the ipType used for network interfaces on the VMs
*/
public GoGridTemplateOptions ipType(IpType ipType) {
this.ipType = ipType;
return this;
}
public static class Builder { public static class Builder {
/**
* @see GoGridTemplateOptions#ipAddressAllocationMode
*/
public static GoGridTemplateOptions ipType(IpType ipType) {
GoGridTemplateOptions options = new GoGridTemplateOptions();
return GoGridTemplateOptions.class.cast(options.ipType(ipType));
}
// methods that only facilitate returning the correct object type // methods that only facilitate returning the correct object type
/** /**
* @see TemplateOptions#inboundPorts * @see TemplateOptions#inboundPorts(int...)
*/ */
public static GoGridTemplateOptions inboundPorts(int... ports) { public static GoGridTemplateOptions inboundPorts(int... ports) {
GoGridTemplateOptions options = new GoGridTemplateOptions(); GoGridTemplateOptions options = new GoGridTemplateOptions();
@ -92,7 +75,7 @@ public class GoGridTemplateOptions extends TemplateOptions implements Cloneable
} }
/** /**
* @see TemplateOptions#port * @see TemplateOptions#blockOnPort(int, int)
*/ */
public static GoGridTemplateOptions blockOnPort(int port, int seconds) { public static GoGridTemplateOptions blockOnPort(int port, int seconds) {
GoGridTemplateOptions options = new GoGridTemplateOptions(); GoGridTemplateOptions options = new GoGridTemplateOptions();
@ -100,7 +83,7 @@ public class GoGridTemplateOptions extends TemplateOptions implements Cloneable
} }
/** /**
* @see TemplateOptions#runScript * @see TemplateOptions#runScript(Payload)
*/ */
public static GoGridTemplateOptions runScript(Payload script) { public static GoGridTemplateOptions runScript(Payload script) {
GoGridTemplateOptions options = new GoGridTemplateOptions(); GoGridTemplateOptions options = new GoGridTemplateOptions();
@ -108,41 +91,36 @@ public class GoGridTemplateOptions extends TemplateOptions implements Cloneable
} }
/** /**
* @see TemplateOptions#installPrivateKey * @see TemplateOptions#installPrivateKey(Payload)
*/ */
@Deprecated
public static GoGridTemplateOptions installPrivateKey(Payload rsaKey) { public static GoGridTemplateOptions installPrivateKey(Payload rsaKey) {
GoGridTemplateOptions options = new GoGridTemplateOptions(); GoGridTemplateOptions options = new GoGridTemplateOptions();
return GoGridTemplateOptions.class.cast(options.installPrivateKey(rsaKey)); return GoGridTemplateOptions.class.cast(options.installPrivateKey(rsaKey));
} }
/** /**
* @see TemplateOptions#authorizePublicKey * @see TemplateOptions#authorizePublicKey(Payload)
*/ */
@Deprecated
public static GoGridTemplateOptions authorizePublicKey(Payload rsaKey) { public static GoGridTemplateOptions authorizePublicKey(Payload rsaKey) {
GoGridTemplateOptions options = new GoGridTemplateOptions(); GoGridTemplateOptions options = new GoGridTemplateOptions();
return GoGridTemplateOptions.class.cast(options.authorizePublicKey(rsaKey)); return GoGridTemplateOptions.class.cast(options.authorizePublicKey(rsaKey));
} }
/** /**
* @see TemplateOptions#withDetails * @see TemplateOptions#withMetadata()
*/ */
public static GoGridTemplateOptions withDetails() { public static GoGridTemplateOptions withMetadata() {
GoGridTemplateOptions options = new GoGridTemplateOptions(); GoGridTemplateOptions options = new GoGridTemplateOptions();
return GoGridTemplateOptions.class.cast(options.withMetadata()); return GoGridTemplateOptions.class.cast(options.withMetadata());
} }
} }
/**
* @return ipType on the vms
*/
public IpType getIpType() {
return ipType;
}
// methods that only facilitate returning the correct object type // methods that only facilitate returning the correct object type
/** /**
* @see TemplateOptions#blockOnPort * @see TemplateOptions#blockOnPort(int, int)
*/ */
@Override @Override
public GoGridTemplateOptions blockOnPort(int port, int seconds) { public GoGridTemplateOptions blockOnPort(int port, int seconds) {
@ -150,12 +128,7 @@ public class GoGridTemplateOptions extends TemplateOptions implements Cloneable
} }
/** /**
* * @see TemplateOptions#inboundPorts(int...)
* special thing is that we do assume if you are passing groups that you have everything you need
* already defined. for example, our option inboundPorts normally creates ingress rules
* accordingly but if we notice you've specified securityGroups, we do not mess with rules at all
*
* @see TemplateOptions#inboundPorts
*/ */
@Override @Override
public GoGridTemplateOptions inboundPorts(int... ports) { public GoGridTemplateOptions inboundPorts(int... ports) {
@ -214,40 +187,10 @@ public class GoGridTemplateOptions extends TemplateOptions implements Cloneable
} }
/** /**
* @see TemplateOptions#withMetadata * @see TemplateOptions#withMetadata()
*/ */
@Override @Override
public GoGridTemplateOptions withMetadata() { public GoGridTemplateOptions withMetadata() {
return GoGridTemplateOptions.class.cast(super.withMetadata()); return GoGridTemplateOptions.class.cast(super.withMetadata());
} }
@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + ((ipType == null) ? 0 : ipType.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!super.equals(obj))
return false;
if (getClass() != obj.getClass())
return false;
GoGridTemplateOptions other = (GoGridTemplateOptions) obj;
if (ipType != other.ipType)
return false;
return true;
}
@Override
public String toString() {
return "[" + (ipType != null ? "ipType=" + ipType : "") + ", inboundPorts=" + Arrays.toString(inboundPorts) + ", privateKey="
+ (privateKey != null) + ", publicKey=" + (publicKey != null) + ", runScript=" + (script != null)
+ ", port:seconds=" + port + ":" + seconds + ", metadata/details: " + includeMetadata + "]";
}
} }

View File

@ -32,7 +32,6 @@ import org.jclouds.compute.domain.Template;
import org.jclouds.compute.reference.ComputeServiceConstants.Timeouts; import org.jclouds.compute.reference.ComputeServiceConstants.Timeouts;
import org.jclouds.compute.strategy.CreateNodeWithGroupEncodedIntoName; import org.jclouds.compute.strategy.CreateNodeWithGroupEncodedIntoName;
import org.jclouds.gogrid.GoGridClient; import org.jclouds.gogrid.GoGridClient;
import org.jclouds.gogrid.compute.options.GoGridTemplateOptions;
import org.jclouds.gogrid.domain.Ip; import org.jclouds.gogrid.domain.Ip;
import org.jclouds.gogrid.domain.IpType; import org.jclouds.gogrid.domain.IpType;
import org.jclouds.gogrid.domain.PowerCommand; import org.jclouds.gogrid.domain.PowerCommand;
@ -78,11 +77,8 @@ public class FindIpThenCreateNodeInGroup implements CreateNodeWithGroupEncodedIn
int numOfRetries = 20; int numOfRetries = 20;
GetIpListOptions unassignedIps = new GetIpListOptions() GetIpListOptions unassignedIps = new GetIpListOptions()
.onlyUnassigned() .onlyUnassigned()
.inDatacenter(template.getLocation().getId()); .inDatacenter(template.getLocation().getId())
if (template.getOptions() instanceof GoGridTemplateOptions) { .onlyWithType(IpType.PUBLIC);
IpType ipType = GoGridTemplateOptions.class.cast(template.getOptions()).getIpType();
unassignedIps = unassignedIps.onlyWithType(ipType);
}
// lock-free consumption of a shared resource: IP address pool // lock-free consumption of a shared resource: IP address pool
while (notStarted) { // TODO: replace with Predicate-based thread while (notStarted) { // TODO: replace with Predicate-based thread
// collision avoidance for simplicity // collision avoidance for simplicity

View File

@ -18,41 +18,20 @@
*/ */
package org.jclouds.gogrid.compute.options; package org.jclouds.gogrid.compute.options;
import static org.jclouds.gogrid.compute.options.GoGridTemplateOptions.Builder.*;
import static org.testng.Assert.*; import static org.testng.Assert.*;
import org.jclouds.compute.options.TemplateOptions; import org.jclouds.compute.options.TemplateOptions;
import org.jclouds.gogrid.domain.IpType;
import org.testng.annotations.Test; import org.testng.annotations.Test;
/** /**
* Tests possible uses of {@code GoGridTemplateOptions} and {@code GoGridTemplateOptions.Builder.*} * Tests possible uses of {@code GoGridTemplateOptions} and {@code GoGridTemplateOptions.Builder.*}.
* with {@link IpType} option.
* *
* @author Andrew Kennedy * @author Andrew Kennedy
*/ */
public class GoGridTemplateOptionsTest { public class GoGridTemplateOptionsTest {
@Test @Test
public void testipTypePrivate() {
GoGridTemplateOptions options = new GoGridTemplateOptions();
options.ipType(IpType.PRIVATE);
assertEquals(options.getIpType(), IpType.PRIVATE);
}
@Test
public void testipTypePublic() {
GoGridTemplateOptions options = ipType(IpType.PUBLIC);
assertEquals(options.getIpType(), IpType.PUBLIC);
}
public void testAs() { public void testAs() {
TemplateOptions options = new GoGridTemplateOptions(); TemplateOptions options = new GoGridTemplateOptions();
assertEquals(options.as(GoGridTemplateOptions.class), options); assertEquals(options.as(GoGridTemplateOptions.class), options);
} }
@Test
public void testNullIpType() {
GoGridTemplateOptions options = new GoGridTemplateOptions();
assertEquals(options.getIpType(), null);
}
} }