implemented first cut of glesys computeservice

This commit is contained in:
Adrian Cole 2012-01-31 01:31:21 -08:00
parent ddec84db8e
commit 19ff2eb89b
23 changed files with 1650 additions and 38 deletions

View File

@ -21,8 +21,9 @@ package org.jclouds.glesys;
import java.util.List;
import java.util.Properties;
import org.jclouds.compute.ComputeServiceContextBuilder;
import org.jclouds.glesys.compute.config.GleSYSComputeServiceContextModule;
import org.jclouds.glesys.config.GleSYSRestClientModule;
import org.jclouds.rest.RestContextBuilder;
import com.google.inject.Module;
@ -30,12 +31,17 @@ import com.google.inject.Module;
*
* @author Adrian Cole
*/
public class GleSYSContextBuilder extends RestContextBuilder<GleSYSClient, GleSYSAsyncClient> {
public class GleSYSContextBuilder extends ComputeServiceContextBuilder<GleSYSClient, GleSYSAsyncClient> {
public GleSYSContextBuilder(Properties props) {
super(GleSYSClient.class, GleSYSAsyncClient.class, props);
}
@Override
protected void addContextModule(List<Module> modules) {
modules.add(new GleSYSComputeServiceContextModule());
}
@Override
protected void addClientModule(List<Module> modules) {
modules.add(new GleSYSRestClientModule());

View File

@ -0,0 +1,219 @@
/**
* 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.glesys.compute;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Set;
import java.util.Map.Entry;
import javax.annotation.Resource;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Provider;
import javax.inject.Singleton;
import org.jclouds.collect.FindResourceInSet;
import org.jclouds.collect.Memoized;
import org.jclouds.compute.ComputeService;
import org.jclouds.compute.ComputeServiceAdapter;
import org.jclouds.compute.domain.Hardware;
import org.jclouds.compute.domain.HardwareBuilder;
import org.jclouds.compute.domain.Processor;
import org.jclouds.compute.domain.Template;
import org.jclouds.compute.domain.Volume;
import org.jclouds.compute.domain.internal.VolumeImpl;
import org.jclouds.compute.predicates.ImagePredicates;
import org.jclouds.compute.reference.ComputeServiceConstants;
import org.jclouds.domain.Location;
import org.jclouds.domain.LoginCredentials;
import org.jclouds.glesys.GleSYSClient;
import org.jclouds.glesys.compute.options.GleSYSTemplateOptions;
import org.jclouds.glesys.domain.AllowedArgumentsForCreateServer;
import org.jclouds.glesys.domain.OSTemplate;
import org.jclouds.glesys.domain.ServerDetails;
import org.jclouds.glesys.domain.ServerSpec;
import org.jclouds.glesys.options.CreateServerOptions;
import org.jclouds.glesys.options.DestroyServerOptions;
import org.jclouds.location.predicates.LocationPredicates;
import org.jclouds.logging.Logger;
import com.google.common.base.Function;
import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
/**
* defines the connection between the {@link GleSYSClient} implementation and the jclouds
* {@link ComputeService}
*
*/
@Singleton
public class GleSYSComputeServiceAdapter implements ComputeServiceAdapter<ServerDetails, Hardware, OSTemplate, String> {
@Resource
@Named(ComputeServiceConstants.COMPUTE_LOGGER)
protected Logger logger = Logger.NULL;
private final GleSYSClient client;
private final Supplier<Set<? extends Location>> locations;
private final Provider<String> passwordProvider;
@Inject
public GleSYSComputeServiceAdapter(GleSYSClient client, @Memoized Supplier<Set<? extends Location>> locations,
@Named("PASSWORD") Provider<String> passwordProvider) {
this.client = checkNotNull(client, "client");
this.locations = checkNotNull(locations, "locations");
this.passwordProvider = checkNotNull(passwordProvider, "passwordProvider");
}
@Override
public NodeAndInitialCredentials<ServerDetails> createNodeWithGroupEncodedIntoName(String group, String name,
Template template) {
checkNotNull(template, "template was null");
checkNotNull(template.getOptions(), "template options was null");
checkArgument(template.getOptions().getClass().isAssignableFrom(GleSYSTemplateOptions.class),
"options class %s should have been assignable from GleSYSTemplateOptions", template.getOptions()
.getClass());
GleSYSTemplateOptions templateOptions = template.getOptions().as(GleSYSTemplateOptions.class);
CreateServerOptions createServerOptions = new CreateServerOptions();
createServerOptions.ip(templateOptions.getIp());
createServerOptions.description(name); // TODO: add to templateOptions and set if present
ServerSpec.Builder builder = ServerSpec.builder();
builder.datacenter(template.getLocation().getId());
builder.platform(template.getHardware().getHypervisor());
builder.diskSizeGB(Math.round(template.getHardware().getVolumes().get(0).getSize()));
builder.cpuCores((int) template.getHardware().getProcessors().get(0).getCores());
builder.transferGB(50);// TODO: add to template options with default value
ServerSpec spec = builder.build();
String password = passwordProvider.get(); // TODO: add to templateOptions and set if present
logger.debug(">> creating new Server spec(%s) name(%s) options(%s)", spec, name, createServerOptions);
ServerDetails result = client.getServerClient().createServerWithHostnameAndRootPassword(spec, name, password,
createServerOptions);
logger.trace("<< ServerDetails(%s)", result.getId());
return new NodeAndInitialCredentials<ServerDetails>(result, result.getId() + "", LoginCredentials.builder()
.password(password).build());
}
@Singleton
public static class FindLocationForServerSpec extends FindResourceInSet<ServerSpec, Location> {
@Inject
public FindLocationForServerSpec(@Memoized Supplier<Set<? extends Location>> location) {
super(location);
}
@Override
public boolean matches(ServerSpec from, Location input) {
return input.getId().equals(from.getDatacenter());
}
}
@Override
public Iterable<Hardware> listHardwareProfiles() {
Set<? extends Location> locationsSet = locations.get();
ImmutableSet.Builder<Hardware> hardwareToReturn = ImmutableSet.<Hardware> builder();
// do this loop after dupes are filtered, else OOM
Set<OSTemplate> images = listImages();
for (Entry<String, AllowedArgumentsForCreateServer> platformToArgs : client.getServerClient()
.getAllowedArgumentsForCreateServerByPlatform().entrySet())
for (String datacenter : platformToArgs.getValue().getDataCenters())
for (int diskSizeGB : platformToArgs.getValue().getDiskSizesInGB())
for (int cpuCores : platformToArgs.getValue().getCpuCoreOptions())
for (int memorySizeMB : platformToArgs.getValue().getMemorySizesInMB()) {
ImmutableSet.Builder<String> templatesSupportedBuilder = ImmutableSet.<String> builder();
for (OSTemplate template : images) {
if (diskSizeGB >= template.getMinDiskSize() && memorySizeMB >= template.getMinMemSize())
templatesSupportedBuilder.add(template.getName());
}
ImmutableSet<String> templatesSupported = templatesSupportedBuilder.build();
if (templatesSupported.size() > 0)
hardwareToReturn.add(new HardwareBuilder().ids(
String.format("datacenter(%s)platform(%s)cpuCores(%d)memorySizeMB(%d)diskSizeGB(%d)",
datacenter, platformToArgs.getKey(), cpuCores, cpuCores, diskSizeGB)).ram(
memorySizeMB).processors(ImmutableList.of(new Processor(cpuCores, 1.0))).volumes(
ImmutableList.<Volume> of(new VolumeImpl((float) diskSizeGB, true, true))).hypervisor(
platformToArgs.getKey()).location(
Iterables.find(locationsSet, LocationPredicates.idEquals(datacenter))).supportsImage(
ImagePredicates.idIn(templatesSupported)).build());
}
return hardwareToReturn.build();
}
@Override
public Set<OSTemplate> listImages() {
return client.getServerClient().listTemplates();
}
@Override
public Iterable<ServerDetails> listNodes() {
return ImmutableSet.of();
}
@Override
public Set<String> listLocations() {
return ImmutableSet.copyOf(Iterables.concat(Iterables.transform(client.getServerClient()
.getAllowedArgumentsForCreateServerByPlatform().values(),
new Function<AllowedArgumentsForCreateServer, Set<String>>() {
@Override
public Set<String> apply(AllowedArgumentsForCreateServer arg0) {
return arg0.getDataCenters();
}
})));
}
@Override
public ServerDetails getNode(String id) {
return client.getServerClient().getServerDetails(id);
}
@Override
public void destroyNode(String id) {
client.getServerClient().destroyServer(id, DestroyServerOptions.Builder.discardIp());
}
@Override
public void rebootNode(String id) {
client.getServerClient().rebootServer(id);
}
@Override
public void resumeNode(String id) {
client.getServerClient().startServer(id);
}
@Override
public void suspendNode(String id) {
client.getServerClient().stopServer(id);
}
}

View File

@ -0,0 +1,112 @@
/**
* 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.glesys.compute.config;
import java.security.SecureRandom;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Provider;
import javax.inject.Singleton;
import org.jclouds.compute.ComputeServiceAdapter;
import org.jclouds.compute.config.ComputeServiceAdapterContextModule;
import org.jclouds.compute.domain.Hardware;
import org.jclouds.compute.domain.NodeMetadata;
import org.jclouds.compute.domain.OsFamily;
import org.jclouds.compute.domain.OsFamilyVersion64Bit;
import org.jclouds.compute.domain.TemplateBuilder;
import org.jclouds.compute.options.TemplateOptions;
import org.jclouds.domain.Location;
import org.jclouds.functions.IdentityFunction;
import org.jclouds.glesys.GleSYSAsyncClient;
import org.jclouds.glesys.GleSYSClient;
import org.jclouds.glesys.compute.GleSYSComputeServiceAdapter;
import org.jclouds.glesys.compute.functions.DatacenterToLocation;
import org.jclouds.glesys.compute.functions.OSTemplateToImage;
import org.jclouds.glesys.compute.functions.ParseOsFamilyVersion64BitFromImageName;
import org.jclouds.glesys.compute.functions.ServerDetailsToNodeMetadata;
import org.jclouds.glesys.compute.options.GleSYSTemplateOptions;
import org.jclouds.glesys.domain.OSTemplate;
import org.jclouds.glesys.domain.ServerDetails;
import org.jclouds.location.suppliers.OnlyLocationOrFirstZone;
import com.google.common.base.Function;
import com.google.common.base.Supplier;
import com.google.inject.Injector;
import com.google.inject.Scopes;
import com.google.inject.TypeLiteral;
import com.google.inject.name.Names;
/**
*
* @author Adrian Cole
*/
public class GleSYSComputeServiceContextModule
extends
ComputeServiceAdapterContextModule<GleSYSClient, GleSYSAsyncClient, ServerDetails, Hardware, OSTemplate, String> {
public GleSYSComputeServiceContextModule() {
super(GleSYSClient.class, GleSYSAsyncClient.class);
}
@SuppressWarnings("unchecked")
@Override
protected void configure() {
super.configure();
bind(new TypeLiteral<ComputeServiceAdapter<ServerDetails, Hardware, OSTemplate, String>>() {
}).to(GleSYSComputeServiceAdapter.class);
bind(new TypeLiteral<Function<ServerDetails, NodeMetadata>>() {
}).to(ServerDetailsToNodeMetadata.class);
bind(new TypeLiteral<Function<OSTemplate, org.jclouds.compute.domain.Image>>() {
}).to(OSTemplateToImage.class);
bind(new TypeLiteral<Function<Hardware, Hardware>>() {
}).to((Class) IdentityFunction.class);
bind(new TypeLiteral<Function<String, Location>>() {
}).to(DatacenterToLocation.class);
bind(new TypeLiteral<Function<String, OsFamilyVersion64Bit>>() {
}).to(ParseOsFamilyVersion64BitFromImageName.class);
bind(new TypeLiteral<Supplier<Location>>() {
}).to(OnlyLocationOrFirstZone.class);
bind(TemplateOptions.class).to(GleSYSTemplateOptions.class);
bind(String.class).annotatedWith(Names.named("PASSWORD")).toProvider(PasswordProvider.class).in(Scopes.SINGLETON);
}
// 128MB is perhaps too little ram
@Override
protected TemplateBuilder provideTemplate(Injector injector, TemplateBuilder template) {
return template.minRam(512).osFamily(OsFamily.UBUNTU).osVersionMatches("1[10].[10][04]").os64Bit(true);
}
@Named("PASSWORD")
@Singleton
public static class PasswordProvider implements Provider<String> {
private final SecureRandom random;
@Inject
protected PasswordProvider() {
this.random = new SecureRandom();
}
@Override
public String get() {
return random.nextLong() + "";
}
}
}

View File

@ -0,0 +1,52 @@
/**
* 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.glesys.compute.functions;
import static com.google.common.base.Preconditions.checkNotNull;
import javax.inject.Inject;
import org.jclouds.domain.Location;
import org.jclouds.domain.LocationBuilder;
import org.jclouds.domain.LocationScope;
import org.jclouds.location.suppliers.JustProvider;
import com.google.common.base.Function;
import com.google.common.collect.Iterables;
/**
* Converts an ServerSpec into a Location.
*/
public class DatacenterToLocation implements Function<String, Location> {
private final JustProvider provider;
// allow us to lazy discover the provider of a resource
@Inject
public DatacenterToLocation(JustProvider provider) {
this.provider = checkNotNull(provider, "provider");
}
@Override
public Location apply(String datacenter) {
return new LocationBuilder().scope(LocationScope.ZONE).description(datacenter).id(datacenter)
// TODO: iso3166Codes
.parent(Iterables.getOnlyElement(provider.get())).build();
}
}

View File

@ -0,0 +1,48 @@
/**
* 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.glesys.compute.functions;
import java.util.Set;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.jclouds.collect.FindResourceInSet;
import org.jclouds.collect.Memoized;
import org.jclouds.domain.Location;
import org.jclouds.glesys.domain.ServerSpec;
import com.google.common.base.Supplier;
/**
* @author Adrian Cole
*/
@Singleton
public class FindLocationForServerSpec extends FindResourceInSet<ServerSpec, Location> {
@Inject
public FindLocationForServerSpec(@Memoized Supplier<Set<? extends Location>> location) {
super(location);
}
@Override
public boolean matches(ServerSpec from, Location input) {
return input.getId().equals(from.getDatacenter());
}
}

View File

@ -0,0 +1,57 @@
/**
* 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
* templateific language governing permissions and limitations
* under the License.
*/
package org.jclouds.glesys.compute.functions;
import static com.google.common.base.Preconditions.checkNotNull;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.jclouds.compute.domain.Image;
import org.jclouds.compute.domain.ImageBuilder;
import org.jclouds.compute.domain.OperatingSystem;
import org.jclouds.compute.domain.OsFamilyVersion64Bit;
import org.jclouds.compute.domain.OperatingSystem.Builder;
import org.jclouds.glesys.domain.OSTemplate;
import com.google.common.base.Function;
/**
* @author Adrian Cole
*/
@Singleton
public class OSTemplateToImage implements Function<OSTemplate, Image> {
private final Function<String, OsFamilyVersion64Bit> imageParser;
@Inject
public OSTemplateToImage(Function<String, OsFamilyVersion64Bit> imageParser) {
this.imageParser = imageParser;
}
@Override
public Image apply(OSTemplate template) {
checkNotNull(template, "template");
OsFamilyVersion64Bit parsed = imageParser.apply(template.getName());
Builder builder = OperatingSystem.builder();
builder.name(template.getName()).description(template.getName()).is64Bit(parsed.is64Bit).version(parsed.version)
.family(parsed.family);
return new ImageBuilder().ids(template.getName()).description(template.getName())
.operatingSystem(builder.build()).build();
}
}

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.glesys.compute.functions;
import static com.google.common.base.Predicates.containsPattern;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.jclouds.compute.domain.OsFamily;
import org.jclouds.compute.domain.OsFamilyVersion64Bit;
import org.jclouds.compute.util.ComputeServiceUtils;
import com.google.common.base.Function;
/**
* Defaults to version null and 64bit, if the operating system is unrecognized and the pattern
* "32bit" isn't in the string.
*
* @author Adrian Cole
*
*/
@Singleton
public class ParseOsFamilyVersion64BitFromImageName implements Function<String, OsFamilyVersion64Bit> {
private final Map<OsFamily, Map<String, String>> osVersionMap;
@Inject
public ParseOsFamilyVersion64BitFromImageName(Map<OsFamily, Map<String, String>> osVersionMap) {
this.osVersionMap = osVersionMap;
}
// ex Debian 6.0 64-bit
// ex. Ubuntu 10.04 LTS 32-bit
public static final Pattern PATTERN = Pattern.compile("([^ ]+).* ([0-9.]+)( [36][24]-bit)?$");
@Override
public OsFamilyVersion64Bit apply(String input) {
boolean is64Bit = containsPattern("64-bit").apply(input);
Matcher matcher = PATTERN.matcher(input);
if (matcher.find()) {
OsFamily fam = OsFamily.fromValue(matcher.group(1).toLowerCase());
switch (fam) {
case UNRECOGNIZED:
return new OsFamilyVersion64Bit(OsFamily.UNRECOGNIZED, null, is64Bit);
}
return new OsFamilyVersion64Bit(fam, ComputeServiceUtils.parseVersionOrReturnEmptyString(fam, matcher
.group(2), osVersionMap), is64Bit);
} else {
return new OsFamilyVersion64Bit(OsFamily.UNRECOGNIZED, null, is64Bit);
}
}
}

View File

@ -0,0 +1,150 @@
/**
* 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.glesys.compute.functions;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.jclouds.compute.util.ComputeServiceUtils.parseGroupFromName;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import javax.annotation.Resource;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import org.jclouds.collect.FindResourceInSet;
import org.jclouds.collect.Memoized;
import org.jclouds.compute.domain.HardwareBuilder;
import org.jclouds.compute.domain.Image;
import org.jclouds.compute.domain.NodeMetadata;
import org.jclouds.compute.domain.NodeMetadataBuilder;
import org.jclouds.compute.domain.NodeState;
import org.jclouds.compute.domain.OperatingSystem;
import org.jclouds.compute.domain.Processor;
import org.jclouds.compute.domain.Volume;
import org.jclouds.compute.domain.internal.VolumeImpl;
import org.jclouds.compute.reference.ComputeServiceConstants;
import org.jclouds.domain.Location;
import org.jclouds.glesys.GleSYSClient;
import org.jclouds.glesys.domain.Ip;
import org.jclouds.glesys.domain.ServerDetails;
import org.jclouds.glesys.options.ServerStatusOptions;
import org.jclouds.logging.Logger;
import org.jclouds.util.InetAddresses2.IsPrivateIPAddress;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
/**
* @author Adrian Cole
*/
@Singleton
public class ServerDetailsToNodeMetadata implements Function<ServerDetails, NodeMetadata> {
@Resource
@Named(ComputeServiceConstants.COMPUTE_LOGGER)
protected Logger logger = Logger.NULL;
public static final Map<ServerDetails.State, NodeState> serverStateToNodeState = ImmutableMap
.<ServerDetails.State, NodeState> builder().put(ServerDetails.State.STOPPED, NodeState.SUSPENDED).put(
ServerDetails.State.RUNNING, NodeState.RUNNING).put(ServerDetails.State.UNRECOGNIZED,
NodeState.UNRECOGNIZED).build();
protected final Supplier<Set<? extends Image>> images;
protected final GleSYSClient client;
protected final FindLocationForServerDetails findLocationForServerDetails;
private static class FindImageForServer implements Predicate<Image> {
private final ServerDetails instance;
private FindImageForServer(ServerDetails instance) {
this.instance = instance;
}
@Override
public boolean apply(Image input) {
return input.getProviderId().equals(instance.getTemplateName());
}
}
@Inject
ServerDetailsToNodeMetadata(GleSYSClient client, FindLocationForServerDetails findLocationForServerDetails,
@Memoized Supplier<Set<? extends Image>> images) {
this.client = checkNotNull(client, "client");
this.findLocationForServerDetails = checkNotNull(findLocationForServerDetails, "findLocationForServerDetails");
this.images = checkNotNull(images, "images");
}
@Override
public NodeMetadata apply(ServerDetails from) {
NodeMetadataBuilder builder = new NodeMetadataBuilder();
builder.ids(from.getId() + "");
builder.name(from.getHostname());
builder.hostname(from.getHostname());
builder.location(findLocationForServerDetails.apply(from));
builder.group(parseGroupFromName(from.getHostname()));
builder.imageId(from.getTemplateName() + "");
builder.operatingSystem(parseOperatingSystem(from));
builder.hardware(new HardwareBuilder().ids(from.getId() + "").ram(from.getMemorySizeMB()).processors(
ImmutableList.of(new Processor(from.getCpuCores(), 1.0))).volumes(
ImmutableList.<Volume> of(new VolumeImpl((float) from.getDiskSizeGB(), true, true))).hypervisor(
from.getPlatform()).build());
builder.state(serverStateToNodeState.get(client.getServerClient().getServerStatus(from.getId(),
ServerStatusOptions.Builder.state()).getState()));
Iterable<String> addresses = Iterables.transform(from.getIps(), new Function<Ip, String>() {
@Override
public String apply(Ip arg0) {
return arg0.getIp();
}
});
builder.publicAddresses(Iterables.filter(addresses, Predicates.not(IsPrivateIPAddress.INSTANCE)));
builder.privateAddresses(Iterables.filter(addresses, IsPrivateIPAddress.INSTANCE));
return builder.build();
}
protected OperatingSystem parseOperatingSystem(ServerDetails from) {
try {
return Iterables.find(images.get(), new FindImageForServer(from)).getOperatingSystem();
} catch (NoSuchElementException e) {
logger.warn("could not find a matching image for server %s", from);
}
return null;
}
@Singleton
public static class FindLocationForServerDetails extends FindResourceInSet<ServerDetails, Location> {
@Inject
public FindLocationForServerDetails(@Memoized Supplier<Set<? extends Location>> location) {
super(location);
}
@Override
public boolean matches(ServerDetails from, Location input) {
return input.getId().equals(from.getDatacenter());
}
}
}

View File

@ -0,0 +1,59 @@
/**
* 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.glesys.compute.functions;
import static com.google.common.base.Preconditions.checkNotNull;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.jclouds.compute.domain.Hardware;
import org.jclouds.compute.domain.HardwareBuilder;
import org.jclouds.compute.domain.Processor;
import org.jclouds.compute.domain.Volume;
import org.jclouds.compute.domain.internal.VolumeImpl;
import org.jclouds.compute.predicates.ImagePredicates;
import org.jclouds.glesys.domain.ServerSpec;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableList;
/**
*
* @author Adrian Cole
*/
@Singleton
public class ServerSpecToHardware implements Function<ServerSpec, Hardware> {
private final FindLocationForServerSpec findLocationForServerSpec;
@Inject
ServerSpecToHardware(FindLocationForServerSpec findLocationForServerSpec) {
this.findLocationForServerSpec = checkNotNull(findLocationForServerSpec, "findLocationForServerSpec");
}
@Override
public Hardware apply(ServerSpec spec) {
return new HardwareBuilder().ids(spec.toString()).ram(spec.getMemorySizeMB()).processors(
ImmutableList.of(new Processor(spec.getCpuCores(), 1.0))).volumes(
ImmutableList.<Volume> of(new VolumeImpl((float) spec.getDiskSizeGB(), true, true))).hypervisor(
spec.getPlatform()).location(findLocationForServerSpec.apply(spec)).supportsImage(
ImagePredicates.idEquals(spec.getTemplateName())).build();
}
}

View File

@ -0,0 +1,201 @@
/**
* 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.glesys.compute.options;
import static com.google.common.base.Preconditions.checkArgument;
import java.util.Map;
import org.jclouds.compute.ComputeService;
import org.jclouds.compute.options.TemplateOptions;
import org.jclouds.glesys.features.ServerClient;
import org.jclouds.io.Payload;
import com.google.common.net.InetAddresses;
/**
* Contains options supported by the
* {@link ComputeService#createNodesInGroup(String, int, TemplateOptions)} and
* {@link ComputeService#createNodesInGroup(String, int, TemplateOptions)} operations on the
* <em>gogrid</em> provider.
*
* <h2>Usage</h2> The recommended way to instantiate a {@link GleSYSTemplateOptions} object is to
* statically import {@code GleSYSTemplateOptions.*} and invoke a static creation method followed by
* an instance mutator (if needed):
* <p>
*
* <pre>
* import static org.jclouds.compute.options.GleSYSTemplateOptions.Builder.*;
* ComputeService client = // get connection
* templateBuilder.options(inboundPorts(22, 80, 8080, 443));
* Set&lt;? extends NodeMetadata&gt; set = client.createNodesInGroup(tag, 2, templateBuilder.build());
* </pre>
*
* @author Adrian Cole
*/
public class GleSYSTemplateOptions extends TemplateOptions implements Cloneable {
protected String ip;
@Override
public GleSYSTemplateOptions clone() {
GleSYSTemplateOptions options = new GleSYSTemplateOptions();
copyTo(options);
return options;
}
@Override
public void copyTo(TemplateOptions to) {
super.copyTo(to);
if (to instanceof GleSYSTemplateOptions) {
GleSYSTemplateOptions eTo = GleSYSTemplateOptions.class.cast(to);
eTo.ip(ip);
}
}
/**
*
* @see ServerClient#createServerWithHostnameAndRootPassword
* @see InetAddresses#isInetAddress
*/
public TemplateOptions ip(String ip) {
if (ip != null)
checkArgument(InetAddresses.isInetAddress(ip), "ip %s is not valid", ip);
this.ip = ip;
return this;
}
public String getIp() {
return ip;
}
public static final GleSYSTemplateOptions NONE = new GleSYSTemplateOptions();
public static class Builder {
/**
* @see #ip
*/
public static GleSYSTemplateOptions ip(String ip) {
GleSYSTemplateOptions options = new GleSYSTemplateOptions();
return GleSYSTemplateOptions.class.cast(options.ip(ip));
}
// methods that only facilitate returning the correct object type
/**
* @see TemplateOptions#inboundPorts(int...)
*/
public static GleSYSTemplateOptions inboundPorts(int... ports) {
GleSYSTemplateOptions options = new GleSYSTemplateOptions();
return GleSYSTemplateOptions.class.cast(options.inboundPorts(ports));
}
/**
* @see TemplateOptions#blockOnPort(int, int)
*/
public static GleSYSTemplateOptions blockOnPort(int port, int seconds) {
GleSYSTemplateOptions options = new GleSYSTemplateOptions();
return GleSYSTemplateOptions.class.cast(options.blockOnPort(port, seconds));
}
/**
* @see TemplateOptions#runScript(Payload)
*/
public static GleSYSTemplateOptions runScript(Payload script) {
GleSYSTemplateOptions options = new GleSYSTemplateOptions();
return GleSYSTemplateOptions.class.cast(options.runScript(script));
}
/**
* @see TemplateOptions#userMetadata(Map)
*/
public static GleSYSTemplateOptions userMetadata(Map<String, String> userMetadata) {
GleSYSTemplateOptions options = new GleSYSTemplateOptions();
return GleSYSTemplateOptions.class.cast(options.userMetadata(userMetadata));
}
/**
* @see TemplateOptions#userMetadata(String, String)
*/
public static GleSYSTemplateOptions userMetadata(String key, String value) {
GleSYSTemplateOptions options = new GleSYSTemplateOptions();
return GleSYSTemplateOptions.class.cast(options.userMetadata(key, value));
}
}
// methods that only facilitate returning the correct object type
/**
* @see TemplateOptions#blockOnPort(int, int)
*/
@Override
public GleSYSTemplateOptions blockOnPort(int port, int seconds) {
return GleSYSTemplateOptions.class.cast(super.blockOnPort(port, seconds));
}
/**
* @see TemplateOptions#inboundPorts(int...)
*/
@Override
public GleSYSTemplateOptions inboundPorts(int... ports) {
return GleSYSTemplateOptions.class.cast(super.inboundPorts(ports));
}
/**
* @see TemplateOptions#authorizePublicKey(String)
*/
@Override
public GleSYSTemplateOptions authorizePublicKey(String publicKey) {
return GleSYSTemplateOptions.class.cast(super.authorizePublicKey(publicKey));
}
/**
* @see TemplateOptions#installPrivateKey(String)
*/
@Override
public GleSYSTemplateOptions installPrivateKey(String privateKey) {
return GleSYSTemplateOptions.class.cast(super.installPrivateKey(privateKey));
}
/**
* @see TemplateOptions#runScript(Payload)
*/
@Deprecated
@Override
public GleSYSTemplateOptions runScript(Payload script) {
return GleSYSTemplateOptions.class.cast(super.runScript(script));
}
/**
* {@inheritDoc}
*/
@Override
public GleSYSTemplateOptions userMetadata(Map<String, String> userMetadata) {
return GleSYSTemplateOptions.class.cast(super.userMetadata(userMetadata));
}
/**
* {@inheritDoc}
*/
@Override
public GleSYSTemplateOptions userMetadata(String key, String value) {
return GleSYSTemplateOptions.class.cast(super.userMetadata(key, value));
}
}

View File

@ -0,0 +1,46 @@
/**
* 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.glesys.compute;
import static org.testng.Assert.assertEquals;
import org.jclouds.glesys.compute.internal.BaseGleSYSComputeServiceExpectTest;
import org.testng.annotations.Test;
import com.google.common.collect.Iterables;
/**
*
* @author Adrian Cole
*/
@Test(groups = "live", singleThreaded = true, testName = "GleSYSComputeServiceAdapterExpectTest")
public class GleSYSComputeServiceAdapterExpectTest extends BaseGleSYSComputeServiceExpectTest {
@Test
public void testListImages() {
GleSYSComputeServiceAdapter adapter = injectorForKnownArgumentsAndConstantPassword().getInstance(GleSYSComputeServiceAdapter.class);
assertEquals(Iterables.size(adapter.listImages()), 34);
}
//TODO: most importantly createServer and listHardwareProfiles tests
}

View File

@ -0,0 +1,60 @@
/**
* 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.glesys.compute;
import org.jclouds.compute.BaseComputeServiceLiveTest;
import org.jclouds.compute.ComputeServiceContextFactory;
import org.jclouds.compute.domain.NodeMetadata;
import org.jclouds.glesys.GleSYSAsyncClient;
import org.jclouds.glesys.GleSYSClient;
import org.jclouds.rest.RestContext;
import org.jclouds.sshj.config.SshjSshClientModule;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableMap;
import com.google.inject.Module;
/**
*
* TODO: enable this
*
* @author Adrian Cole
*/
@Test(groups = "live", enabled = false, singleThreaded = true)
public class GleSYSComputeServiceLiveTest extends BaseComputeServiceLiveTest {
public GleSYSComputeServiceLiveTest() {
provider = "glesys";
}
@Override
protected Module getSshModule() {
return new SshjSshClientModule();
}
public void testAssignability() throws Exception {
@SuppressWarnings("unused")
RestContext<GleSYSClient, GleSYSAsyncClient> tmContext = new ComputeServiceContextFactory()
.createContext(provider, identity, credential).getProviderSpecificContext();
}
// GleSYS does not support metadata
@Override
protected void checkUserMetadataInNodeEquals(NodeMetadata node, ImmutableMap<String, String> userMetadata) {
}
}

View File

@ -0,0 +1,49 @@
/**
* 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.glesys.compute;
import static org.testng.Assert.assertEquals;
import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.glesys.compute.internal.BaseGleSYSComputeServiceExpectTest;
import org.testng.annotations.Test;
/**
*
* @author Adrian Cole
*/
@Test(groups = "live", singleThreaded = true, testName = "GleSYSExperimentLiveTest")
public class GleSYSExperimentExpectTest extends BaseGleSYSComputeServiceExpectTest {
@Test
public void testAndExperiment() {
ComputeServiceContext context = null;
try {
context = computeContextForKnownArgumentsAndConstantPassword();
assertEquals(context.getComputeService().listAssignableLocations().size(), 4);
} finally {
if (context != null)
context.close();
}
}
}

View File

@ -0,0 +1,59 @@
/**
* 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.glesys.compute;
import static org.testng.Assert.assertEquals;
import org.jclouds.compute.BaseVersionedServiceLiveTest;
import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.compute.ComputeServiceContextFactory;
import org.jclouds.logging.log4j.config.Log4JLoggingModule;
import org.jclouds.sshj.config.SshjSshClientModule;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableSet;
import com.google.inject.Module;
/**
*
* @author Adrian Cole
*/
@Test(groups = "live", singleThreaded = true, testName = "GleSYSExperimentLiveTest")
public class GleSYSExperimentLiveTest extends BaseVersionedServiceLiveTest {
public GleSYSExperimentLiveTest() {
provider = "glesys";
}
@Test
public void testAndExperiment() {
ComputeServiceContext context = null;
try {
context = new ComputeServiceContextFactory().createContext(provider, identity, credential, ImmutableSet
.<Module> of(new Log4JLoggingModule(), new SshjSshClientModule()));
assertEquals(context.getComputeService().listAssignableLocations().size(), 6);
} finally {
if (context != null)
context.close();
}
}
}

View File

@ -0,0 +1,97 @@
/**
* 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.glesys.compute;
import static org.jclouds.compute.util.ComputeServiceUtils.getCores;
import static org.jclouds.compute.util.ComputeServiceUtils.getSpace;
import static org.testng.Assert.assertEquals;
import java.io.IOException;
import java.util.Set;
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.compute.domain.Volume;
import org.jclouds.glesys.compute.options.GleSYSTemplateOptions;
import org.testng.annotations.Test;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.collect.ImmutableSet;
/**
*
* @author Adrian Cole
*/
@Test(groups = "live", testName = "GleSYSTemplateBuilderLiveTest")
public class GleSYSTemplateBuilderLiveTest extends BaseTemplateBuilderLiveTest {
public GleSYSTemplateBuilderLiveTest() {
provider = "glesys";
}
// / allows us to break when a new os is added
@Override
protected Predicate<OsFamilyVersion64Bit> defineUnsupportedOperatingSystems() {
return Predicates.not(new Predicate<OsFamilyVersion64Bit>() {
@Override
public boolean apply(OsFamilyVersion64Bit input) {
// TODO: this is an incomplete list until ParseOsFamilyVersion64BitFromImageName is
// complete
switch (input.family) {
case UBUNTU:
return (input.version.equals("")|| input.version.equals("11.04")) && input.is64Bit;
case DEBIAN:
return input.version.equals("") || input.version.matches("[56].0");
case FEDORA:
return input.version.equals("") || input.version.equals("13") || input.version.equals("15");
case CENTOS:
return input.version.equals("") || input.version.equals("5") || input.version.equals("5.0")
|| input.version.equals("6.0");
default:
return false;
}
}
});
}
@Test
public void testDefaultTemplateBuilder() throws IOException {
Template defaultTemplate = context.getComputeService().templateBuilder().build();
assertEquals(defaultTemplate.getImage().getOperatingSystem().getVersion(), "11.04");
assertEquals(defaultTemplate.getImage().getOperatingSystem().is64Bit(), true);
assertEquals(defaultTemplate.getImage().getOperatingSystem().getFamily(), OsFamily.UBUNTU);
assertEquals(getCores(defaultTemplate.getHardware()), 1.0d);
assertEquals(defaultTemplate.getHardware().getRam(), 512);
assertEquals(defaultTemplate.getHardware().getHypervisor(), "Xen");
assertEquals(getSpace(defaultTemplate.getHardware()), 5.0d);
assertEquals(defaultTemplate.getHardware().getVolumes().get(0).getType(), Volume.Type.LOCAL);
// test that we bound the correct templateoptions in guice
assertEquals(defaultTemplate.getOptions().getClass(), GleSYSTemplateOptions.class);
}
@Override
protected Set<String> getIso3166Codes() {
return ImmutableSet.<String> of("US-CA", "US-VA", "BR-SP");
}
}

View File

@ -0,0 +1,80 @@
/**
* 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.glesys.compute.functions;
import static com.google.common.collect.Iterables.transform;
import static com.google.common.collect.Lists.newArrayList;
import static org.testng.Assert.assertEquals;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.Map.Entry;
import org.jclouds.compute.config.BaseComputeServiceContextModule;
import org.jclouds.compute.domain.OsFamilyVersion64Bit;
import org.jclouds.compute.reference.ComputeServiceConstants;
import org.jclouds.json.Json;
import org.jclouds.json.config.GsonModule;
import org.jclouds.json.internal.GsonWrapper;
import org.jclouds.util.Strings2;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import com.google.common.base.Function;
import com.google.gson.Gson;
import com.google.inject.Guice;
import com.google.inject.TypeLiteral;
/**
*
* @author Adrian Cole
*/
@Test(groups = "unit", testName = "ParseOsFamilyVersion64BitFromImageNameTest")
public class ParseOsFamilyVersion64BitFromImageNameTest {
Json json = new GsonWrapper(new Gson());
//TODO: update osmatches corresponding with server_templates.json, this may imply an update to ComputeServiceConstants and/or OsFamily
@DataProvider(name = "data")
public Object[][] createData() throws IOException {
InputStream is = ParseOsFamilyVersion64BitFromImageNameTest.class.getResourceAsStream("/osmatches.json");
Map<String, OsFamilyVersion64Bit> values = json.fromJson(Strings2.toStringAndClose(is),
new TypeLiteral<Map<String, OsFamilyVersion64Bit>>() {
}.getType());
return newArrayList(
transform(values.entrySet(), new Function<Map.Entry<String, OsFamilyVersion64Bit>, Object[]>() {
@Override
public Object[] apply(Entry<String, OsFamilyVersion64Bit> input) {
return new Object[] { input.getKey(), input.getValue() };
}
})).toArray(new Object[][] {});
}
ParseOsFamilyVersion64BitFromImageName parser = new ParseOsFamilyVersion64BitFromImageName(new BaseComputeServiceContextModule() {
}.provideOsVersionMap(new ComputeServiceConstants.ReferenceData(), Guice.createInjector(new GsonModule())
.getInstance(Json.class)));
@Test(dataProvider = "data")
public void test(String input, OsFamilyVersion64Bit expected) {
assertEquals(parser.apply(input), expected);
}
}

View File

@ -0,0 +1,39 @@
/**
* 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.glesys.compute.functions;
import org.jclouds.glesys.compute.internal.BaseGleSYSComputeServiceExpectTest;
import org.testng.annotations.Test;
/**
* TODO
*
*/
@Test(groups = "unit")
public class ServerSpecToHardwareTest extends BaseGleSYSComputeServiceExpectTest {
@Test
public void testHardwareRequest() {
ServerSpecToHardware toTest = injectorForKnownArgumentsAndConstantPassword().getInstance(ServerSpecToHardware.class);
}
}

View File

@ -0,0 +1,92 @@
/**
* 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.glesys.compute.internal;
import java.net.URI;
import java.util.Properties;
import org.jclouds.compute.ComputeService;
import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.compute.ComputeServiceContextFactory;
import org.jclouds.glesys.compute.config.GleSYSComputeServiceContextModule.PasswordProvider;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse;
import org.jclouds.logging.config.NullLoggingModule;
import org.jclouds.rest.BaseRestClientExpectTest;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableSet;
import com.google.inject.AbstractModule;
import com.google.inject.Injector;
import com.google.inject.Module;
/**
* Base class for writing GleSYS Expect tests for ComputeService operations
*
* @author Adrian Cole
*/
public abstract class BaseGleSYSComputeServiceExpectTest extends BaseRestClientExpectTest<ComputeService> {
public BaseGleSYSComputeServiceExpectTest() {
provider = "glesys";
}
@Override
public ComputeService createClient(Function<HttpRequest, HttpResponse> fn, Module module, Properties props) {
return new ComputeServiceContextFactory(setupRestProperties()).createContext(provider, identity, credential,
ImmutableSet.<Module> of(new ExpectModule(fn), new NullLoggingModule(), module), props)
.getComputeService();
}
protected PasswordProvider passwordGenerator() {
// make sure we can predict passwords generated for createServer requests
return new PasswordProvider() {
public String get() {
return "foo";
}
};
}
protected Injector injectorForKnownArgumentsAndConstantPassword() {
return computeContextForKnownArgumentsAndConstantPassword().utils().injector();
}
protected ComputeServiceContext computeContextForKnownArgumentsAndConstantPassword() {
return requestsSendResponses(
HttpRequest.builder().method("GET").endpoint(
URI.create("https://api.glesys.com/server/templates/format/json")).headers(
ImmutableMultimap.<String, String> builder().put("Accept", "application/json").put(
"Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build()).build(),
HttpResponse.builder().statusCode(200).payload(payloadFromResource("/server_templates.json")).build(),
HttpRequest.builder().method("GET").endpoint(
URI.create("https://api.glesys.com/server/allowedarguments/format/json")).headers(
ImmutableMultimap.<String, String> builder().put("Accept", "application/json").put(
"Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build()).build(),
HttpResponse.builder().statusCode(204).payload(payloadFromResource("/server_allowed_arguments.json"))
.build(), new AbstractModule() {
@Override
protected void configure() {
bind(PasswordProvider.class).toInstance(passwordGenerator());
}
}).getContext();
}
}

View File

@ -0,0 +1,64 @@
/**
* 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.glesys.compute.options;
import static org.jclouds.glesys.compute.options.GleSYSTemplateOptions.Builder.ip;
import static org.testng.Assert.assertEquals;
import org.jclouds.compute.options.TemplateOptions;
import org.testng.annotations.Test;
/**
* Tests possible uses of {@code GleSYSTemplateOptions} and {@code
* GleSYSTemplateOptions.Builder.*}.
*
* @author Adrian Cole
*/
// NOTE:without testName, this will not call @Before* and fail w/NPE during surefire
@Test(groups = "unit", testName = "GleSYSTemplateOptionsTest")
public class GleSYSTemplateOptionsTest {
@Test
public void testAs() {
TemplateOptions options = new GleSYSTemplateOptions();
assertEquals(options.as(GleSYSTemplateOptions.class), options);
}
@Test
public void testDefaultip() {
TemplateOptions options = new GleSYSTemplateOptions();
assertEquals(options.as(GleSYSTemplateOptions.class).getIp(), null);
}
@Test
public void testip() {
TemplateOptions options = new GleSYSTemplateOptions().ip("1.1.1.1");
assertEquals(options.as(GleSYSTemplateOptions.class).getIp(), "1.1.1.1");
}
@Test
public void testipStatic() {
TemplateOptions options = ip("1.1.1.1");
assertEquals(options.as(GleSYSTemplateOptions.class).getIp(), "1.1.1.1");
}
@Test(expectedExceptions = IllegalArgumentException.class)
public void testipIsInvalidThrowsIllegalArgument() {
new GleSYSTemplateOptions().ip("foo");
}
}

View File

@ -117,26 +117,26 @@ public class ServerClientExpectTest extends BaseRestClientExpectTest<GleSYSClien
.put("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build()).build(),
HttpResponse.builder().statusCode(200).payload(payloadFromResource("/server_templates.json")).build()).getServerClient();
ImmutableSet.Builder<Template> expectedBuilder = ImmutableSet.<Template> builder();
ImmutableSet.Builder<OSTemplate> expectedBuilder = ImmutableSet.<OSTemplate> builder();
for (String name : new String[] { "Centos 5", "Centos 5 64-bit", "Centos 6 32-bit", "Centos 6 64-bit",
"Debian 5.0 32-bit", "Debian 5.0 64-bit", "Debian 6.0 32-bit", "Debian 6.0 64-bit", "Fedora Core 11",
"Fedora Core 11 64-bit", "Gentoo", "Gentoo 64-bit", "Scientific Linux 6", "Scientific Linux 6 64-bit",
"Slackware 12", "Ubuntu 10.04 LTS 32-bit", "Ubuntu 10.04 LTS 64-bit", "Ubuntu 11.04 64-bit" }) {
expectedBuilder.add(new Template(name, 5, 128, "linux", "OpenVZ"));
expectedBuilder.add(new OSTemplate(name, 5, 128, "linux", "OpenVZ"));
}
for (String name : new String[] { "CentOS 5.5 x64", "CentOS 5.5 x86", "Centos 6 x64", "Centos 6 x86",
"Debian-6 x64", "Debian 5.0.1 x64", "FreeBSD 8.2", "Gentoo 10.1 x64", "Ubuntu 8.04 x64",
"Ubuntu 10.04 LTS 64-bit", "Ubuntu 10.10 x64", "Ubuntu 11.04 x64" }) {
expectedBuilder.add(new Template(name, 5, 512, name.startsWith("FreeBSD") ? "freebsd" : "linux", "Xen"));
expectedBuilder.add(new OSTemplate(name, 5, 512, name.startsWith("FreeBSD") ? "freebsd" : "linux", "Xen"));
}
for (String name : new String[] { "Windows Server 2008 R2 x64 std", "Windows Server 2008 R2 x64 web",
"Windows Server 2008 x64 web", "Windows Server 2008 x86 web" }) {
expectedBuilder.add(new Template(name, 20, 1024, "windows", "Xen"));
expectedBuilder.add(new OSTemplate(name, 20, 1024, "windows", "Xen"));
}
assertEquals(client.getTemplates(), expectedBuilder.build());
assertEquals(client.listTemplates(), expectedBuilder.build());
}
public void testGetServerDetailsWhenResponseIs2xx() throws Exception {
@ -202,7 +202,7 @@ public class ServerClientExpectTest extends BaseRestClientExpectTest<GleSYSClien
assertEquals(
client.createServerWithHostnameAndRootPassword(
ServerSpec.builder().datacenter("Falkenberg").platform("OpenVZ").templateName("Ubuntu 32-bit")
.diskSizeGB(5).memorySizeMB(512).cpuCores(1).transfer(50).build(), "jclouds-test", "password").toString(),
.diskSizeGB(5).memorySizeMB(512).cpuCores(1).transferGB(50).build(), "jclouds-test", "password").toString(),
expected.toString());
}
@ -231,7 +231,7 @@ public class ServerClientExpectTest extends BaseRestClientExpectTest<GleSYSClien
assertEquals(client.createServerWithHostnameAndRootPassword(ServerSpec.builder().datacenter("Falkenberg")
.platform("OpenVZ").templateName("Ubuntu 32-bit").diskSizeGB(5).memorySizeMB(512).cpuCores(1).transfer(50)
.platform("OpenVZ").templateName("Ubuntu 32-bit").diskSizeGB(5).memorySizeMB(512).cpuCores(1).transferGB(50)
.build(), "jclouds-test", "password", options), expectedServerDetails());
}

View File

@ -33,7 +33,7 @@ import org.jclouds.glesys.domain.AllowedArgumentsForCreateServer;
import org.jclouds.glesys.domain.ServerDetails;
import org.jclouds.glesys.domain.ServerLimit;
import org.jclouds.glesys.domain.ServerStatus;
import org.jclouds.glesys.domain.Template;
import org.jclouds.glesys.domain.OSTemplate;
import org.jclouds.glesys.internal.BaseGleSYSClientLiveTest;
import org.jclouds.glesys.options.CloneServerOptions;
import org.jclouds.glesys.options.DestroyServerOptions;
@ -99,24 +99,24 @@ public class ServerClientLiveTest extends BaseGleSYSClientLiveTest {
assertNotNull(t);
assert t.getDataCenters().size() > 0 : t;
assert t.getCpuCores().size() > 0 : t;
assert t.getDiskSizes().size() > 0 : t;
assert t.getMemorySizes().size() > 0 : t;
assert t.getTemplates().size() > 0 : t;
assert t.getTransfers().size() > 0 : t;
assert t.getTransfers().size() > 0 : t;
assert t.getCpuCoreOptions().size() > 0 : t;
assert t.getDiskSizesInGB().size() > 0 : t;
assert t.getMemorySizesInMB().size() > 0 : t;
assert t.getTemplateNames().size() > 0 : t;
assert t.getTransfersInGB().size() > 0 : t;
assert t.getTransfersInGB().size() > 0 : t;
}
@Test
public void testListTemplates() throws Exception {
Set<Template> templates = client.getTemplates();
Set<OSTemplate> oSTemplates = client.listTemplates();
for(Template template : templates) {
checkTemplate(template);
for(OSTemplate oSTemplate : oSTemplates) {
checkTemplate(oSTemplate);
}
}
private void checkTemplate(Template t) {
private void checkTemplate(OSTemplate t) {
assertNotNull(t);
assertNotNull(t.getName());
assertNotNull(t.getOs());
@ -149,10 +149,10 @@ public class ServerClientLiveTest extends BaseGleSYSClientLiveTest {
assertEquals("Ubuntu 10.04 LTS 32-bit", details.getTemplateName());
assertEquals("Falkenberg", details.getDatacenter());
assertEquals("OpenVZ", details.getPlatform());
assertEquals(5, details.getDiskSize());
assertEquals(512, details.getMemorySize());
assertEquals(5, details.getDiskSizeGB());
assertEquals(512, details.getMemorySizeMB());
assertEquals(1, details.getCpuCores());
assertEquals(50, details.getTransfer());
assertEquals(50, details.getTransferGB());
}
@Test
@ -259,10 +259,10 @@ public class ServerClientLiveTest extends BaseGleSYSClientLiveTest {
private void checkServer(ServerDetails server) {
// description can be null
assert server.getCpuCores() > 0 : server;
assert server.getDiskSize() > 0 : server;
assert server.getMemorySize() > 0 : server;
assert server.getDiskSizeGB() > 0 : server;
assert server.getMemorySizeMB() > 0 : server;
assert server.getCost() != null;
assert server.getTransfer() > 0 : server;
assert server.getTransferGB() > 0 : server;
assertNotNull(server.getTemplateName());
assertNotNull(server.getIps());

View File

@ -18,7 +18,6 @@
*/
package org.jclouds.glesys.internal;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotNull;
@ -26,6 +25,9 @@ import static org.testng.Assert.assertTrue;
import java.util.concurrent.TimeUnit;
import org.jclouds.compute.BaseVersionedServiceLiveTest;
import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.compute.ComputeServiceContextFactory;
import org.jclouds.glesys.GleSYSAsyncClient;
import org.jclouds.glesys.GleSYSClient;
import org.jclouds.glesys.domain.Server;
@ -38,7 +40,6 @@ import org.jclouds.glesys.options.ServerStatusOptions;
import org.jclouds.logging.log4j.config.Log4JLoggingModule;
import org.jclouds.predicates.RetryablePredicate;
import org.jclouds.rest.RestContext;
import org.jclouds.rest.RestContextFactory;
import org.jclouds.sshj.config.SshjSshClientModule;
import org.testng.annotations.AfterGroups;
import org.testng.annotations.BeforeGroups;
@ -54,19 +55,18 @@ import com.google.inject.Module;
* @author Adrian Cole, Adam Lowe
*/
@Test(groups = "live")
public class BaseGleSYSClientLiveTest {
public class BaseGleSYSClientLiveTest extends BaseVersionedServiceLiveTest {
protected ComputeServiceContext computeContext;
protected RestContext<GleSYSClient, GleSYSAsyncClient> context;
@BeforeGroups(groups = { "live" })
public void setupClient() {
if (context == null) {
String identity = checkNotNull(System.getProperty("test.glesys.identity"), "test.glesys.identity");
String credential = checkNotNull(System.getProperty("test.glesys.credential"), "test.glesys.credential");
context = new RestContextFactory().createContext("glesys", identity, credential,
ImmutableSet.<Module> of(new Log4JLoggingModule(), new SshjSshClientModule()));
}
setupCredentials();
computeContext = new ComputeServiceContextFactory(setupRestProperties()).
createContext(provider, ImmutableSet.<Module> of(
new Log4JLoggingModule(), new SshjSshClientModule()), setupProperties());
context = computeContext.getProviderSpecificContext();
}
@AfterGroups(groups = "live")
@ -97,7 +97,7 @@ public class BaseGleSYSClientLiveTest {
ServerDetails testServer = client.createServerWithHostnameAndRootPassword(
ServerSpec.builder().datacenter("Falkenberg").platform("OpenVZ").templateName("Ubuntu 10.04 LTS 32-bit")
.diskSizeGB(5).memorySizeMB(512).cpuCores(1).transfer(50).build(), hostName, "password");
.diskSizeGB(5).memorySizeMB(512).cpuCores(1).transferGB(50).build(), hostName, "password");
assertNotNull(testServer.getId());
assertEquals(testServer.getHostname(), hostName);

View File

@ -0,0 +1,48 @@
{
"Centos 5": {
"family": "CENTOS",
"version": "5.0",
"is64Bit": false
},
"Centos 5 64-bit": {
"family": "CENTOS",
"version": "5.0",
"is64Bit": true
},
"Centos 6 32-bit": {
"family": "CENTOS",
"version": "6.0",
"is64Bit": false
},
"Centos 6 64-bit": {
"family": "CENTOS",
"version": "6.0",
"is64Bit": true
},
"Debian 5.0": {
"family": "DEBIAN",
"version": "5.0",
"is64Bit": false
},
"Debian 5.0 64-bit": {
"family": "DEBIAN",
"version": "5.0",
"is64Bit": true
},
"Debian 6.0 32-bit": {
"family": "DEBIAN",
"version": "6.0",
"is64Bit": false
},
"Debian 6.0 64-bit": {
"family": "DEBIAN",
"version": "6.0",
"is64Bit": true
},
"Ubuntu 11.04 64-bit": {
"family": "UBUNTU",
"version": "11.04",
"is64Bit": true
}
}