added missing provider id field, default location to hardware/image, fixed packages

This commit is contained in:
Adrian Cole 2012-03-09 19:53:22 -08:00
parent 4cc5e03ae8
commit 4581b924cf
11 changed files with 116 additions and 88 deletions

View File

@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.jclouds.openstack.nova.v1_1.compute.strategy;
package org.jclouds.openstack.nova.v1_1.compute;
import java.util.Set;
@ -75,6 +75,8 @@ public class NovaComputeServiceAdapter implements ComputeServiceAdapter<Server,
public NodeAndInitialCredentials<Server> createNodeWithGroupEncodedIntoName(String tag, String name, Template template)
{
ServerClient serverClient = template.getLocation() != null ? novaClient.getServerClientForRegion(template.getLocation().getId()) : defaultLocationServerClient;
// TODO: make NovaTemplateOptions with the following:
// security group, key pair, floating ip (attach post server-create?)
Server server = serverClient.createServer(name, template.getImage().getId(), template.getHardware().getId());
return new NodeAndInitialCredentials<Server>(server, server.getId() + "", LoginCredentials.builder().password(server.getAdminPass()).build());
}

View File

@ -28,11 +28,11 @@ import org.jclouds.domain.Location;
import org.jclouds.functions.IdentityFunction;
import org.jclouds.openstack.nova.v1_1.NovaAsyncClient;
import org.jclouds.openstack.nova.v1_1.NovaClient;
import org.jclouds.openstack.nova.v1_1.compute.NovaComputeServiceAdapter;
import org.jclouds.openstack.nova.v1_1.compute.functions.FlavorToHardware;
import org.jclouds.openstack.nova.v1_1.compute.functions.NovaImageToImage;
import org.jclouds.openstack.nova.v1_1.compute.functions.NovaImageToOperatingSystem;
import org.jclouds.openstack.nova.v1_1.compute.functions.ServerToNodeMetadata;
import org.jclouds.openstack.nova.v1_1.compute.strategy.NovaComputeServiceAdapter;
import org.jclouds.openstack.nova.v1_1.domain.Flavor;
import org.jclouds.openstack.nova.v1_1.domain.Server;

View File

@ -18,13 +18,17 @@
*/
package org.jclouds.openstack.nova.v1_1.compute.functions;
import javax.inject.Inject;
import org.jclouds.compute.domain.Hardware;
import org.jclouds.compute.domain.HardwareBuilder;
import org.jclouds.compute.domain.Processor;
import org.jclouds.compute.domain.internal.VolumeImpl;
import org.jclouds.domain.Location;
import org.jclouds.openstack.nova.v1_1.domain.Flavor;
import com.google.common.base.Function;
import com.google.common.base.Supplier;
/**
* A function for transforming the nova specific Flavor object to the generic Hardware object.
@ -33,15 +37,27 @@ import com.google.common.base.Function;
*/
public class FlavorToHardware implements Function<Flavor, Hardware>
{
private final Supplier<Location> defaultLocation;
@Inject
public FlavorToHardware(Supplier<Location> defaultLocation)
{
this.defaultLocation = defaultLocation;
}
@Override
public Hardware apply(Flavor flavor)
{
return new HardwareBuilder()
.id(flavor.getId())
// TODO: scope id to region, if there's a chance for conflict
.id(flavor.getId())
.providerId(flavor.getId())
.name(flavor.getName())
.ram(flavor.getRam())
.processor(new Processor(flavor.getVcpus(), 1.0))
.volume(new VolumeImpl(Float.valueOf(flavor.getDisk()), true, true))
.location(defaultLocation.get())
.build();
}
}

View File

@ -23,8 +23,10 @@ import javax.inject.Inject;
import org.jclouds.compute.domain.Image;
import org.jclouds.compute.domain.ImageBuilder;
import org.jclouds.compute.domain.OperatingSystem;
import org.jclouds.domain.Location;
import com.google.common.base.Function;
import com.google.common.base.Supplier;
/**
* A function for transforming a nova-specific Image into a generic Image object.
@ -34,21 +36,26 @@ import com.google.common.base.Function;
public class NovaImageToImage implements Function<org.jclouds.openstack.nova.v1_1.domain.Image, Image>
{
private final Function<org.jclouds.openstack.nova.v1_1.domain.Image, OperatingSystem> imageToOs;
private final Supplier<Location> defaultLocation;
@Inject
public NovaImageToImage(Function<org.jclouds.openstack.nova.v1_1.domain.Image, OperatingSystem> imageToOs)
public NovaImageToImage(Function<org.jclouds.openstack.nova.v1_1.domain.Image, OperatingSystem> imageToOs, Supplier<Location> defaultLocation)
{
this.imageToOs = imageToOs;
this.defaultLocation = defaultLocation;
}
@Override
public Image apply(org.jclouds.openstack.nova.v1_1.domain.Image image)
{
return new ImageBuilder()
.id(image.getId())
// TODO: scope id to region, if there's a chance for conflict
.id(image.getId())
.providerId(image.getId())
.name(image.getName())
.operatingSystem(imageToOs.apply(image))
.description(image.getName())
.location(defaultLocation.get())
.build();
}
}

View File

@ -59,7 +59,9 @@ public class ServerToNodeMetadata implements Function<Server, NodeMetadata>
public NodeMetadata apply(Server server)
{
return new NodeMetadataBuilder()
.id(server.getId())
// TODO: scope id to region, if there's a chance for conflict
.id(server.getId())
.providerId(server.getId())
.name(server.getName())
.publicAddresses(Iterables.transform(server.getPublicAddresses(), new AddressToStringTransformationFunction()))
.privateAddresses(Iterables.transform(server.getPrivateAddresses(), new AddressToStringTransformationFunction()))

View File

@ -43,9 +43,10 @@ import com.google.common.collect.LinkedHashMultimap;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import com.google.gson.annotations.SerializedName;
/**
* A server is a virtual machine instance in the compute system. Flavor and
* image are requisite elements when creating a server.
* A server is a virtual machine instance in the compute system. Flavor and image are requisite
* elements when creating a server.
*
* @author Adrian Cole
* @see <a href=
@ -109,7 +110,7 @@ public class Server extends Resource {
this.created = created;
return this;
}
/**
* @see Server#getHostId()
*/
@ -117,7 +118,7 @@ public class Server extends Resource {
this.hostId = hostId;
return this;
}
/**
* @see Server#getAccessIPv4()
*/
@ -133,7 +134,7 @@ public class Server extends Resource {
this.accessIPv6 = accessIPv6;
return this;
}
/**
* @see Server#getStatus()
*/
@ -173,13 +174,12 @@ public class Server extends Resource {
this.metadata = ImmutableMap.copyOf(metadata);
return this;
}
/**
* @see Server#getAddresses()
*/
public Builder addresses(Multimap<Address.Type, Address> addresses) {
this.addresses = ImmutableMultimap.copyOf(checkNotNull(addresses,
"addresses"));
this.addresses = ImmutableMultimap.copyOf(checkNotNull(addresses, "addresses"));
return this;
}
@ -187,16 +187,15 @@ public class Server extends Resource {
* @see Server#getPrivateAddresses()
*/
public Builder privateAddresses(Address... privateAddresses) {
return privateAddresses(ImmutableSet.copyOf(checkNotNull(
privateAddresses, "privateAddresses")));
return privateAddresses(ImmutableSet.copyOf(checkNotNull(privateAddresses, "privateAddresses")));
}
/**
* @see Server#getPrivateAddresses()
*/
public Builder privateAddresses(Set<Address> privateAddresses) {
this.addresses.replaceValues(Address.Type.PRIVATE, ImmutableSet
.copyOf(checkNotNull(privateAddresses, "privateAddresses")));
this.addresses.replaceValues(Address.Type.PRIVATE, ImmutableSet.copyOf(checkNotNull(privateAddresses,
"privateAddresses")));
return this;
}
@ -204,16 +203,15 @@ public class Server extends Resource {
* @see Server#getPublicAddresses()
*/
public Builder publicAddresses(Address... publicAddresses) {
return publicAddresses(ImmutableSet.copyOf(checkNotNull(
publicAddresses, "publicAddresses")));
return publicAddresses(ImmutableSet.copyOf(checkNotNull(publicAddresses, "publicAddresses")));
}
/**
* @see Server#getPublicAddresses()
*/
public Builder publicAddresses(Set<Address> publicAddresses) {
this.addresses.replaceValues(Address.Type.PUBLIC, ImmutableSet
.copyOf(checkNotNull(publicAddresses, "publicAddresses")));
this.addresses.replaceValues(Address.Type.PUBLIC, ImmutableSet.copyOf(checkNotNull(publicAddresses,
"publicAddresses")));
return this;
}
@ -221,15 +219,14 @@ public class Server extends Resource {
* @see Server#getAdminPass()
*/
public Builder adminPass(String adminPass) {
this.adminPass = adminPass;
return this;
this.adminPass = adminPass;
return this;
}
public Server build() {
// return new Server(id, name, links, addresses);
return new Server(id, name, links, tenantId, userId, updated,
created, hostId, accessIPv4, accessIPv6, status, progress,
image, flavor, adminPass, addresses, metadata);
return new Server(id, name, links, tenantId, userId, updated, created, hostId, accessIPv4, accessIPv6, status,
progress, image, flavor, adminPass, addresses, metadata);
}
public Builder fromServer(Server in) {
@ -268,7 +265,7 @@ public class Server extends Resource {
return Builder.class.cast(super.fromResource(in));
}
}
@SerializedName("tenant_id")
protected String tenantId;
@SerializedName("user_id")
@ -287,11 +284,10 @@ public class Server extends Resource {
protected final Map<Address.Type, Set<Address>> addresses;
protected Map<String, String> metadata;
protected Server(String id, String name, Set<Link> links, String tenantId,
String userId, Date updated, Date created, String hostId,
String accessIPv4, String accessIPv6, ServerStatus status,
int progress, Resource image, Resource flavor, String adminPass,
Multimap<Address.Type, Address> addresses, Map<String, String> metadata) {
protected Server(String id, String name, Set<Link> links, String tenantId, String userId, Date updated,
Date created, String hostId, String accessIPv4, String accessIPv6, ServerStatus status, int progress,
Resource image, Resource flavor, String adminPass, Multimap<Address.Type, Address> addresses,
Map<String, String> metadata) {
super(id, name, links);
this.tenantId = tenantId;
this.userId = userId;
@ -305,8 +301,7 @@ public class Server extends Resource {
this.image = image;
this.flavor = flavor;
this.metadata = Maps.newHashMap(metadata);
this.addresses = Multimaps2.toOldSchool(ImmutableMultimap
.copyOf(checkNotNull(addresses, "addresses")));
this.addresses = Multimaps2.toOldSchool(ImmutableMultimap.copyOf(checkNotNull(addresses, "addresses")));
this.adminPass = adminPass;
}
@ -357,77 +352,73 @@ public class Server extends Resource {
public Map<String, String> getMetadata() {
return this.metadata;
}
/**
* @return the private ip addresses assigned to the server
*/
public Set<Address> getPrivateAddresses() {
Collection<Address> privateAddresses = getAddresses().get(Address.Type.PRIVATE);
if (privateAddresses == null) {
return ImmutableSet.<Address> of();
} else {
return ImmutableSet.copyOf(privateAddresses);
}
Collection<Address> privateAddresses = getAddresses().get(Address.Type.PRIVATE);
if (privateAddresses == null) {
return ImmutableSet.<Address> of();
} else {
return ImmutableSet.copyOf(privateAddresses);
}
}
/**
* @return the public ip addresses assigned to the server
*/
public Set<Address> getPublicAddresses() {
Collection<Address> publicAddrs = getAddresses().get(Address.Type.PUBLIC);
if (publicAddrs == null) {
return ImmutableSet.<Address> of();
} else {
return ImmutableSet.copyOf(publicAddrs);
}
Collection<Address> publicAddrs = getAddresses().get(Address.Type.PUBLIC);
if (publicAddrs == null) {
return ImmutableSet.<Address> of();
} else {
return ImmutableSet.copyOf(publicAddrs);
}
}
/**
* @return the ip addresses assigned to the server
*/
public Multimap<Type, Address> getAddresses() {
ImmutableSetMultimap.Builder returnMapBuilder = new ImmutableSetMultimap.Builder<Type, Address>();
Set<Address> publicAddresses = addresses.get(Address.Type.PUBLIC);
Set<Address> privateAddresses = addresses.get(Address.Type.PRIVATE);
ImmutableSetMultimap.Builder<Type, Address> returnMapBuilder = new ImmutableSetMultimap.Builder<Type, Address>();
if (privateAddresses.size() > 1) {
if (publicAddresses != null) {
returnMapBuilder.putAll(Address.Type.PUBLIC, Iterables.filter(publicAddresses, Predicates.not(IsPrivateAddress.INSTANCE)));
}
returnMapBuilder.putAll(Address.Type.PRIVATE, Iterables.filter(privateAddresses, IsPrivateAddress.INSTANCE));
returnMapBuilder.putAll(Address.Type.PUBLIC, Iterables.filter(privateAddresses, Predicates.not(IsPrivateAddress.INSTANCE)));
} else {
return Multimaps2.fromOldSchool(addresses);
}
return returnMapBuilder.build();
Set<Address> publicAddresses = addresses.get(Address.Type.PUBLIC);
Set<Address> privateAddresses = addresses.get(Address.Type.PRIVATE);
if (publicAddresses != null) {
returnMapBuilder.putAll(Address.Type.PUBLIC, Iterables.filter(publicAddresses, Predicates
.not(IsPrivateAddress.INSTANCE)));
}
if (privateAddresses != null) {
returnMapBuilder.putAll(Address.Type.PRIVATE, Iterables.filter(privateAddresses, IsPrivateAddress.INSTANCE));
returnMapBuilder.putAll(Address.Type.PUBLIC, Iterables.filter(privateAddresses, Predicates
.not(IsPrivateAddress.INSTANCE)));
}
ImmutableSetMultimap<Type, Address> returnMap = returnMapBuilder.build();
return returnMap.size() > 0 ? returnMap : Multimaps2.fromOldSchool(addresses);
}
private static enum IsPrivateAddress implements Predicate<Address> {
INSTANCE;
public boolean apply (Address in) {
return InetAddresses2.IsPrivateIPAddress.INSTANCE.apply(in.getAddr());
}
}
private static enum IsPrivateAddress implements Predicate<Address> {
INSTANCE;
public boolean apply(Address in) {
return InetAddresses2.IsPrivateIPAddress.INSTANCE.apply(in.getAddr());
}
}
/**
* @return the administrative password for this server.
*/
public String getAdminPass() {
return adminPass;
return adminPass;
}
@Override
public String toString() {
return toStringHelper("").add("id", id).add("name", name)
.add("tenantId", tenantId).add("userId", userId).add("hostId", hostId)
.add("updated", updated).add("created", created)
.add("accessIPv4", accessIPv4).add("accessIPv6", accessIPv6)
.add("status", status).add("progress", progress)
.add("image", image).add("flavor", flavor)
.add("metadata", metadata)
.add("links", links).add("addresses", addresses)
.add("adminPass",adminPass).toString();
return toStringHelper("").add("id", id).add("name", name).add("tenantId", tenantId).add("userId", userId).add(
"hostId", hostId).add("updated", updated).add("created", created).add("accessIPv4", accessIPv4).add(
"accessIPv6", accessIPv6).add("status", status).add("progress", progress).add("image", image).add(
"flavor", flavor).add("metadata", metadata).add("links", links).add("addresses", addresses).add(
"adminPass", adminPass).toString();
}
}

View File

@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.jclouds.openstack.nova.v1_1.features;
package org.jclouds.openstack.nova.v1_1.compute;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;

View File

@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.jclouds.openstack.nova.v1_1.functions;
package org.jclouds.openstack.nova.v1_1.compute.functions;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
@ -25,10 +25,13 @@ import static org.testng.Assert.assertNotNull;
import java.util.UUID;
import org.jclouds.compute.domain.Hardware;
import org.jclouds.domain.Location;
import org.jclouds.openstack.nova.v1_1.compute.functions.FlavorToHardware;
import org.jclouds.openstack.nova.v1_1.domain.Flavor;
import org.testng.annotations.Test;
import com.google.common.base.Suppliers;
/**
* Tests the function used to transform Flavor objects into Hardware objects
*
@ -48,10 +51,11 @@ public class FlavorToHardwareTest
.vcpus(16)
.build();
Hardware converted = new FlavorToHardware().apply(flavorToConvert);
Hardware converted = new FlavorToHardware(Suppliers.<Location>ofInstance(null)).apply(flavorToConvert);
assertEquals(converted.getName(), flavorToConvert.getName());
assertEquals(converted.getId(), flavorToConvert.getId());
assertEquals(converted.getProviderId(), flavorToConvert.getId());
assertEquals(converted.getRam(), flavorToConvert.getRam());
assertNotNull(converted.getProcessors());
@ -61,5 +65,6 @@ public class FlavorToHardwareTest
assertNotNull(converted.getVolumes());
assertFalse(converted.getVolumes().isEmpty());
assertEquals(converted.getVolumes().iterator().next().getSize(), Float.valueOf(flavorToConvert.getDisk()));
}
}

View File

@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.jclouds.openstack.nova.v1_1.functions;
package org.jclouds.openstack.nova.v1_1.compute.functions;
import static org.testng.Assert.assertEquals;
@ -26,11 +26,13 @@ import javax.annotation.Nullable;
import org.jclouds.compute.domain.OperatingSystem;
import org.jclouds.compute.domain.OsFamily;
import org.jclouds.domain.Location;
import org.jclouds.openstack.nova.v1_1.compute.functions.NovaImageToImage;
import org.jclouds.openstack.nova.v1_1.domain.Image;
import org.testng.annotations.Test;
import com.google.common.base.Function;
import com.google.common.base.Suppliers;
/**
* Tests the function that transforms nova-specific images to generic images.
@ -45,10 +47,12 @@ public class NovaImageToImageTest
UUID id = UUID.randomUUID();
Image novaImageToConvert = Image.builder().id(id.toString()).name("Test Image " + id).build();
OperatingSystem operatingSystem = new OperatingSystem(OsFamily.UBUNTU, "My Test OS", "My Test Version", "x86", "My Test OS", true);
NovaImageToImage converter = new NovaImageToImage(new MockImageToOsConverter(operatingSystem));
NovaImageToImage converter = new NovaImageToImage(new MockImageToOsConverter(operatingSystem),Suppliers.<Location>ofInstance(null));
org.jclouds.compute.domain.Image convertedImage = converter.apply(novaImageToConvert);
assertEquals(convertedImage.getId(), novaImageToConvert.getId());
assertEquals(convertedImage.getProviderId(), novaImageToConvert.getId());
assertEquals(convertedImage.getName(), novaImageToConvert.getName());
assertEquals(convertedImage.getOperatingSystem(), operatingSystem);
}

View File

@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.jclouds.openstack.nova.v1_1.functions;
package org.jclouds.openstack.nova.v1_1.compute.functions;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;

View File

@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.jclouds.openstack.nova.v1_1.functions;
package org.jclouds.openstack.nova.v1_1.compute.functions;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
@ -45,7 +45,7 @@ public class ServerToNodeMetadataTest
.id(id.toString())
.name("Test Server " + id)
.privateAddresses(Address.createV4("10.0.0.1"))
.publicAddresses(Address.createV4("10.0.1.1"))
.publicAddresses(Address.createV4("1.0.1.1"))
.status(ServerStatus.ACTIVE)
.build();
@ -54,6 +54,7 @@ public class ServerToNodeMetadataTest
NodeMetadata convertedNodeMetadata = converter.apply(serverToConvert);
assertEquals(serverToConvert.getId(), convertedNodeMetadata.getId());
assertEquals(serverToConvert.getId(), convertedNodeMetadata.getProviderId());
assertEquals(serverToConvert.getName(), convertedNodeMetadata.getName());
assertEquals(serverToConvert.getStatus().getNodeState(), convertedNodeMetadata.getState());
@ -63,6 +64,6 @@ public class ServerToNodeMetadataTest
assertNotNull(convertedNodeMetadata.getPublicAddresses());
assertEquals(convertedNodeMetadata.getPublicAddresses().size(), 1);
assertEquals(convertedNodeMetadata.getPublicAddresses().iterator().next(), "10.0.1.1");
assertEquals(convertedNodeMetadata.getPublicAddresses().iterator().next(), "1.0.1.1");
}
}