Changed Addresses and Server domain objects do deserialize current nova response. Updated tests.

This commit is contained in:
vicglarson 2011-04-15 16:30:13 +04:00 committed by Dmitri Babaev
parent eb4dedca62
commit b17b860279
7 changed files with 460 additions and 438 deletions

View File

@ -18,38 +18,31 @@
*/
package org.jclouds.openstack.nova.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.openstack.nova.domain.Server;
import org.jclouds.openstack.nova.domain.ServerStatus;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.base.Supplier;
import com.google.common.collect.Iterables;
import org.jclouds.collect.Memoized;
import org.jclouds.compute.domain.Hardware;
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.*;
import org.jclouds.compute.reference.ComputeServiceConstants;
import org.jclouds.domain.Credentials;
import org.jclouds.domain.Location;
import org.jclouds.domain.LocationBuilder;
import org.jclouds.domain.LocationScope;
import org.jclouds.logging.Logger;
import org.jclouds.openstack.nova.domain.Server;
import org.jclouds.openstack.nova.domain.ServerStatus;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.base.Supplier;
import com.google.common.collect.Iterables;
import javax.annotation.Resource;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.jclouds.compute.util.ComputeServiceUtils.parseGroupFromName;
/**
* @author Adrian Cole
@ -75,7 +68,7 @@ public class ServerToNodeMetadata implements Function<Server, NodeMetadata> {
@Override
public boolean apply(Image input) {
return input.getProviderId().equals(instance.getImageId() + "");
return input.getProviderId().equals(instance.getImageRef() + "");
}
}
@ -88,7 +81,7 @@ public class ServerToNodeMetadata implements Function<Server, NodeMetadata> {
@Override
public boolean apply(Hardware input) {
return input.getProviderId().equals(instance.getFlavorId() + "");
return input.getProviderId().equals(instance.getFlavorRef() + "");
}
}
@ -112,7 +105,7 @@ public class ServerToNodeMetadata implements Function<Server, NodeMetadata> {
from.getHostId()).parent(location.get()).build());
builder.userMetadata(from.getMetadata());
builder.group(parseGroupFromName(from.getName()));
builder.imageId(from.getImageId() + "");
builder.imageId(from.getImageRef() + "");
builder.operatingSystem(parseOperatingSystem(from));
builder.hardware(parseHardware(from));
builder.state(serverToNodeState.get(from.getStatus()));

View File

@ -18,44 +18,76 @@
*/
package org.jclouds.openstack.nova.domain;
import java.util.Set;
import com.google.common.collect.Sets;
import com.google.gson.annotations.SerializedName;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
/**
*
* @author Adrian Cole
*/
public class Addresses {
@SerializedName("public")
private Set<String> publicAddresses = Sets.newLinkedHashSet();
private Set<Map<String, String>> publicAddresses = Sets.newHashSet();
@SerializedName("private")
private Set<String> privateAddresses = Sets.newLinkedHashSet();
private Set<Map<String, String>> privateAddresses = Sets.newHashSet();
public Addresses() {
}
// public Addresses(Set<Map<String, String>> publicAddresses, Set<Map<String, String>> privateAddresses) {
// this.publicAddresses = publicAddresses;
// this.privateAddresses = privateAddresses;
// }
public Addresses(Set<String> publicAddresses, Set<String> privateAddresses) {
this.publicAddresses = publicAddresses;
this.privateAddresses = privateAddresses;
this.publicAddresses.clear();
this.privateAddresses.clear();
for (String address : publicAddresses) {
HashMap<String, String> addressMap = new HashMap<String, String>();
addressMap.put("version", "4");
addressMap.put("addr", "address");
this.publicAddresses.add(addressMap);
}
for (String address : privateAddresses) {
HashMap<String, String> addressMap = new HashMap<String, String>();
addressMap.put("version", "4");
addressMap.put("addr", "address");
this.privateAddresses.add(addressMap);
}
public void setPublicAddresses(Set<String> publicAddresses) {
}
public void setPublicAddresses(Set<Map<String, String>> publicAddresses) {
this.publicAddresses = publicAddresses;
}
public Set<String> getPublicAddresses() {
return publicAddresses;
HashSet<String> addresses = new HashSet<String>();
for (Map<String, String> address : publicAddresses) {
if (address.containsKey("addr")) {
addresses.add(address.get("addr"));
}
}
return addresses;
}
public void setPrivateAddresses(Set<String> privateAddresses) {
public void setPrivateAddresses(Set<Map<String, String>> privateAddresses) {
this.privateAddresses = privateAddresses;
}
public Set<String> getPrivateAddresses() {
return privateAddresses;
HashSet<String> addresses = new HashSet<String>();
for (Map<String, String> address : privateAddresses) {
if (address.containsKey("addr")) {
addresses.add(address.get("addr"));
}
}
return addresses;
}
@Override

View File

@ -18,10 +18,10 @@
*/
package org.jclouds.openstack.nova.domain;
import java.util.Map;
import com.google.common.collect.Maps;
import java.util.Map;
/**
* A server is a virtual machine instance in the OpenStack Nova system. Flavor and image are
* requisite elements when creating a server.
@ -32,13 +32,34 @@ public class Server extends Resource {
private int id;
private String name;
// private Map<String, Map<String,String>> metadata = Maps.newHashMap();
public Map<String, String> getMetadata() {
return metadata;
}
public void setMetadata(Map<String, String> metadata) {
this.metadata = metadata;
}
private Map<String, String> metadata = Maps.newHashMap();
private Addresses addresses;
private String adminPass;
private Integer flavorId;
private String flavorRef;
private String hostId;
private Integer imageId;
private String imageRef;
public String getAffinityId() {
return affinityId;
}
public void setAffinityId(String affinityId) {
this.affinityId = affinityId;
}
private String affinityId;
private Integer progress;
private ServerStatus status;
@ -51,13 +72,13 @@ public class Server extends Resource {
this.name = name;
}
public void setMetadata(Map<String, String> metadata) {
this.metadata = metadata;
}
// public void setMetadata(Map<String, String> metadata) {
// this.metadata.put("values", metadata);
// }
public Map<String, String> getMetadata() {
return metadata;
}
// public Map<String, String> getMetadata() {
// return metadata.get("values");
// }
public void setAddresses(Addresses addresses) {
this.addresses = addresses;
@ -75,12 +96,12 @@ public class Server extends Resource {
return adminPass;
}
public void setFlavorId(Integer flavorId) {
this.flavorId = flavorId;
public void setFlavorRef(String flavorRef) {
this.flavorRef = flavorRef;
}
public Integer getFlavorId() {
return flavorId;
public String getFlavorRef() {
return flavorRef;
}
public void setHostId(String hostId) {
@ -103,12 +124,12 @@ public class Server extends Resource {
return id;
}
public void setImageId(Integer imageId) {
this.imageId = imageId;
public void getImageRef(String imageRef) {
this.imageRef = imageRef;
}
public Integer getImageId() {
return imageId;
public String getImageRef() {
return imageRef;
}
public String getName() {
@ -141,10 +162,10 @@ public class Server extends Resource {
int result = 1;
result = prime * result + ((addresses == null) ? 0 : addresses.hashCode());
result = prime * result + ((adminPass == null) ? 0 : adminPass.hashCode());
result = prime * result + ((flavorId == null) ? 0 : flavorId.hashCode());
result = prime * result + ((flavorRef == null) ? 0 : flavorRef.hashCode());
result = prime * result + ((hostId == null) ? 0 : hostId.hashCode());
result = prime * result + id;
result = prime * result + ((imageId == null) ? 0 : imageId.hashCode());
result = prime * result + ((imageRef == null) ? 0 : imageRef.hashCode());
result = prime * result + ((metadata == null) ? 0 : metadata.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
@ -169,10 +190,10 @@ public class Server extends Resource {
return false;
} else if (!adminPass.equals(other.adminPass))
return false;
if (flavorId == null) {
if (other.flavorId != null)
if (flavorRef == null) {
if (other.flavorRef != null)
return false;
} else if (!flavorId.equals(other.flavorId))
} else if (!flavorRef.equals(other.flavorRef))
return false;
if (hostId == null) {
if (other.hostId != null)
@ -181,10 +202,10 @@ public class Server extends Resource {
return false;
if (id != other.id)
return false;
if (imageId == null) {
if (other.imageId != null)
if (imageRef == null) {
if (other.imageRef != null)
return false;
} else if (!imageId.equals(other.imageId))
} else if (!imageRef.equals(other.imageRef))
return false;
if (metadata == null) {
if (other.metadata != null)
@ -205,8 +226,8 @@ public class Server extends Resource {
@Override
public String toString() {
return "Server [addresses=" + addresses + ", adminPass=" + adminPass + ", flavorId="
+ flavorId + ", hostId=" + hostId + ", id=" + id + ", imageId=" + imageId
return "Server [addresses=" + addresses + ", adminPass=" + adminPass + ", flavorRef="
+ flavorRef + ", hostId=" + hostId + ", id=" + id + ", imageRef=" + imageRef
+ ", metadata=" + metadata + ", name=" + name + "]";
}

View File

@ -275,11 +275,11 @@ public class NovaClientLiveTest {
"rackspace".getBytes()).withMetadata(metadata));
assertNotNull(server.getAdminPass());
assertEquals(server.getStatus(), ServerStatus.BUILD);
serverId = server.getId();
adminPass = server.getAdminPass();
ip = server.getAddresses().getPublicAddresses().iterator().next();
assertEquals(server.getStatus(), ServerStatus.BUILD);
blockUntilServerActive(serverId);
ip = client.getServer(serverId).getAddresses().getPublicAddresses().iterator().next();
}
private void blockUntilServerActive(int serverId) throws InterruptedException {
@ -316,8 +316,8 @@ public class NovaClientLiveTest {
assertNotNull(server.getHostId());
assertEquals(server.getStatus(), ServerStatus.ACTIVE);
assert server.getProgress() >= 0 : "newDetails.getProgress()" + server.getProgress();
assertEquals(new Integer(14362), server.getImageId());
assertEquals(new Integer(1), server.getFlavorId());
assertEquals("14362", server.getImageRef());
assertEquals("1", server.getFlavorRef());
assertNotNull(server.getAddresses());
// listAddresses tests..
assertEquals(client.getAddresses(serverId), server.getAddresses());
@ -328,21 +328,12 @@ public class NovaClientLiveTest {
// check metadata
assertEquals(server.getMetadata(), metadata);
checkPassOk(server, adminPass);
}
/**
* this tests "personality" as the file looked up was sent during server creation
*/
private void checkPassOk(Server newDetails, String pass) throws IOException {
doCheckPass(newDetails, pass);
assertPassword(server, adminPass);
}
private void doCheckPass(Server newDetails, String pass) throws IOException {
IPSocket socket = new IPSocket(Iterables.get(newDetails.getAddresses().getPublicAddresses(), 0), 22);
private void assertPassword(Server server, String pass) throws IOException {
IPSocket socket = new IPSocket(Iterables.get(server.getAddresses().getPublicAddresses(), 0), 22);
socketTester.apply(socket);
SshClient client = sshFactory.create(socket, new Credentials("root", pass));
@ -383,7 +374,7 @@ public class NovaClientLiveTest {
public void testChangePassword() throws Exception {
client.changeAdminPass(serverId, "elmo");
blockUntilServerActive(serverId);
checkPassOk(client.getServer(serverId), "elmo");
assertPassword(client.getServer(serverId), "elmo");
this.adminPass = "elmo";
}
@ -420,7 +411,7 @@ public class NovaClientLiveTest {
client.rebuildServer(serverId, new RebuildServerOptions().withImage(imageId));
blockUntilServerActive(serverId);
// issue Web Hosting #119580 imageId comes back incorrect after rebuild
assertEquals(new Integer(imageId), client.getServer(serverId).getImageId());
assertEquals(imageId, client.getServer(serverId).getImageRef());
}
@Test(enabled = false, timeOut = 10 * 60 * 1000, dependsOnMethods = "testRebuildServer")
@ -441,7 +432,7 @@ public class NovaClientLiveTest {
blockUntilServerVerifyResize(serverId);
client.revertResizeServer(serverId);
blockUntilServerActive(serverId);
assertEquals(new Integer(1), client.getServer(serverId).getFlavorId());
assertEquals(new Integer(1), client.getServer(serverId).getFlavorRef());
}
@Test(enabled = false, timeOut = 10 * 60 * 1000, dependsOnMethods = "testRebootSoft")
@ -450,7 +441,7 @@ public class NovaClientLiveTest {
blockUntilServerVerifyResize(serverId2);
client.confirmResizeServer(serverId2);
blockUntilServerActive(serverId2);
assertEquals(new Integer(2), client.getServer(serverId2).getFlavorId());
assertEquals(new Integer(2), client.getServer(serverId2).getFlavorRef());
}
@Test(enabled = false, timeOut = 10 * 60 * 1000, dependsOnMethods = {"testRebootSoft", "testRevertResize",

View File

@ -18,37 +18,26 @@
*/
package org.jclouds.openstack.nova.compute.functions;
import static org.testng.Assert.assertEquals;
import com.google.common.base.Suppliers;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import org.jclouds.compute.domain.*;
import org.jclouds.domain.Credentials;
import org.jclouds.domain.Location;
import org.jclouds.domain.LocationBuilder;
import org.jclouds.domain.LocationScope;
import org.jclouds.openstack.nova.compute.config.NovaComputeServiceDependenciesModule;
import org.jclouds.openstack.nova.domain.Server;
import org.jclouds.openstack.nova.domain.ServerStatus;
import org.jclouds.openstack.nova.functions.ParseServerFromJsonResponseTest;
import org.testng.annotations.Test;
import java.net.UnknownHostException;
import java.util.Map;
import java.util.Set;
import org.jclouds.openstack.nova.compute.config.NovaComputeServiceDependenciesModule;
import org.jclouds.openstack.nova.domain.Server;
import org.jclouds.openstack.nova.domain.ServerStatus;
import org.jclouds.openstack.nova.functions.ParseServerFromJsonResponseTest;
import org.jclouds.compute.domain.Hardware;
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.OperatingSystemBuilder;
import org.jclouds.compute.domain.OsFamily;
import org.jclouds.compute.domain.Processor;
import org.jclouds.compute.domain.Volume;
import org.jclouds.compute.domain.VolumeBuilder;
import org.jclouds.domain.Credentials;
import org.jclouds.domain.Location;
import org.jclouds.domain.LocationBuilder;
import org.jclouds.domain.LocationScope;
import org.testng.annotations.Test;
import com.google.common.base.Suppliers;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import static org.testng.Assert.assertEquals;
/**
* @author Adrian Cole
@ -58,7 +47,7 @@ public class ServerToNodeMetadataTest {
Location provider = new LocationBuilder().scope(LocationScope.ZONE).id("dallas").description("description").build();
@Test
public void testApplyWhereImageAndHardwareNotFoundButCredentialsFound() throws UnknownHostException {
public void testApplyWhereImageAndHardwareNotFoundButCredentialsFound() throws UnknownHostException, NoSuchMethodException, ClassNotFoundException {
Credentials creds = new Credentials("root", "abdce");
Map<ServerStatus, NodeState> serverStateToNodeState = NovaComputeServiceDependenciesModule.serverToNodeState;
@ -81,7 +70,7 @@ public class ServerToNodeMetadataTest {
}
@Test
public void testApplyWhereImageAndHardwareNotFound() throws UnknownHostException {
public void testApplyWhereImageAndHardwareNotFound() throws UnknownHostException, NoSuchMethodException, ClassNotFoundException {
Map<ServerStatus, NodeState> serverStateToNodeState = NovaComputeServiceDependenciesModule.serverToNodeState;
Set<org.jclouds.compute.domain.Image> images = ImmutableSet.of();
Set<org.jclouds.compute.domain.Hardware> hardwares = ImmutableSet.of();
@ -103,7 +92,7 @@ public class ServerToNodeMetadataTest {
}
@Test
public void testApplyWhereImageFoundAndHardwareNotFound() throws UnknownHostException {
public void testApplyWhereImageFoundAndHardwareNotFound() throws UnknownHostException, NoSuchMethodException, ClassNotFoundException {
Map<ServerStatus, NodeState> serverStateToNodeState = NovaComputeServiceDependenciesModule.serverToNodeState;
org.jclouds.compute.domain.Image jcImage = NovaImageToImageTest.convertImage();
Set<org.jclouds.compute.domain.Image> images = ImmutableSet.of(jcImage);
@ -128,7 +117,7 @@ public class ServerToNodeMetadataTest {
}
@Test
public void testApplyWhereImageAndHardwareFound() throws UnknownHostException {
public void testApplyWhereImageAndHardwareFound() throws UnknownHostException, NoSuchMethodException, ClassNotFoundException {
Map<ServerStatus, NodeState> serverStateToNodeState = NovaComputeServiceDependenciesModule.serverToNodeState;
Set<org.jclouds.compute.domain.Image> images = ImmutableSet.of(NovaImageToImageTest.convertImage());
Set<org.jclouds.compute.domain.Hardware> hardwares = ImmutableSet.of(FlavorToHardwareTest.convertFlavor());

View File

@ -49,37 +49,35 @@ import static org.testng.Assert.assertEquals;
public class ParseServerFromJsonResponseTest {
@Test
public void testApplyInputStreamDetails() throws UnknownHostException {
public void testApplyInputStreamDetails() throws UnknownHostException, NoSuchMethodException, ClassNotFoundException {
Server response = parseServer();
assertEquals(response.getId(), 1234);
assertEquals(response.getName(), "sample-server");
assertEquals(response.getImageId().intValue(), 1234);
assertEquals(response.getFlavorId().intValue(), 1);
assertEquals(response.getImageId(), "https://servers.api.rackspacecloud.com/v1.1/32278/images/1234");
assertEquals(response.getFlavorId(), "https://servers.api.rackspacecloud.com/v1.1/32278/flavors/1");
assertEquals(response.getImageRef(), "https://servers.api.rackspacecloud.com/v1.1/32278/images/1234");
assertEquals(response.getFlavorRef(), "https://servers.api.rackspacecloud.com/v1.1/32278/flavors/1");
assertEquals(response.getHostId(), "e4d909c290d0fb1ca068ffaddf22cbd0");
assertEquals(true, false, "Uncomment next line");
//assertEquals(response.getAffinityId(), "fc88bcf8394db9c8d0564e08ca6a9724188a84d1");
assertEquals(response.getAffinityId(), "fc88bcf8394db9c8d0564e08ca6a9724188a84d1");
assertEquals(response.getStatus(), ServerStatus.BUILD);
assertEquals(response.getProgress(), new Integer(60));
List<String> publicAddresses = ImmutableList.of("67.23.10.132", "::babe:67.23.10.132", "67.23.10.131", "::babe:4317:0A83");
List<String> privateAddresses = ImmutableList.of("10.176.42.16", "::babe:10.176.42.16");
Addresses addresses1 = new Addresses(new HashSet<String>(publicAddresses), new HashSet<String>(privateAddresses));
assertEquals(response.getAddresses(), addresses1);
assertEquals(response.getMetadata(), ImmutableMap.of("Server Label", "Web Head 1", "Image Version", "2.1"));
assertEquals(response.getAddresses(), addresses1);
}
public static Server parseServer() {
public static Server parseServer() throws NoSuchMethodException, ClassNotFoundException {
Injector i = Guice.createInjector(new GsonModule());
InputStream is = ParseServerFromJsonResponseTest.class.getResourceAsStream("/test_get_server_detail.json");
UnwrapOnlyJsonValue<Server> parser = i.getInstance(Key.get(new TypeLiteral<UnwrapOnlyJsonValue<Server>>() {
}));
Server response = parser.apply(new HttpResponse(200, "ok", Payloads.newInputStreamPayload(is)));
return response;
//Function<HttpResponse, ?> parser = i.getInstance(getParserOrThrowException(NovaClient.class.getMethod("getServer", int.class)));
return (Server) parser.apply(new HttpResponse(200, "ok", Payloads.newInputStreamPayload(is)));
}
}

View File

@ -75,10 +75,8 @@ public class ParseServerListFromJsonResponseTest {
assertEquals(response.get(0).getId(), 1234);
assertEquals(response.get(0).getName(), "sample-server");
assertEquals(response.get(0).getImageId().intValue(), 1234);
assertEquals(response.get(0).getFlavorId().intValue(), 1);
assertEquals(response.get(0).getImageId(), "https://servers.api.rackspacecloud.com/v1.1/32278/images/1234");
assertEquals(response.get(0).getFlavorId(), "https://servers.api.rackspacecloud.com/v1.1/32278/flavors/1");
assertEquals(response.get(0).getImageRef(), "https://servers.api.rackspacecloud.com/v1.1/32278/images/1234");
assertEquals(response.get(0).getFlavorRef(), "https://servers.api.rackspacecloud.com/v1.1/32278/flavors/1");
assertEquals(true, false, "Uncomment next line");
//assertEquals(response.getAffinityId(), "fc88bcf8394db9c8d0564e08ca6a9724188a84d1");
assertEquals(response.get(0).getHostId(), "e4d909c290d0fb1ca068ffaddf22cbd0");
@ -93,8 +91,8 @@ public class ParseServerListFromJsonResponseTest {
assertEquals(response.get(0).getMetadata(), ImmutableMap.of("Server Label", "Web Head 1", "Image Version", "2.1"));
assertEquals(response.get(1).getId(), 5678);
assertEquals(response.get(1).getName(), "sample-server2");
assertEquals(response.get(0).getImageId(), "https://servers.api.rackspacecloud.com/v1.1/32278/images/1");
assertEquals(response.get(0).getFlavorId(), "https://servers.api.rackspacecloud.com/v1.1/32278/flavors/1");
assertEquals(response.get(0).getImageRef(), "https://servers.api.rackspacecloud.com/v1.1/32278/images/1");
assertEquals(response.get(0).getFlavorRef(), "https://servers.api.rackspacecloud.com/v1.1/32278/flavors/1");
assertEquals(true, false, "Uncomment next line");
//assertEquals(response.getAffinityId(), "b414fa41cb37b97dcb58d6c76112af1258e9eae2");
assertEquals(response.get(1).getHostId(), "9e107d9d372bb6826bd81d3542a419d6");