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,126 +18,119 @@
*/
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
*/
@Singleton
public class ServerToNodeMetadata implements Function<Server, NodeMetadata> {
@Resource
@Named(ComputeServiceConstants.COMPUTE_LOGGER)
protected Logger logger = Logger.NULL;
@Resource
@Named(ComputeServiceConstants.COMPUTE_LOGGER)
protected Logger logger = Logger.NULL;
protected final Supplier<Location> location;
protected final Map<String, Credentials> credentialStore;
protected final Map<ServerStatus, NodeState> serverToNodeState;
protected final Supplier<Set<? extends Image>> images;
protected final Supplier<Set<? extends Hardware>> hardwares;
protected final Supplier<Location> location;
protected final Map<String, Credentials> credentialStore;
protected final Map<ServerStatus, NodeState> serverToNodeState;
protected final Supplier<Set<? extends Image>> images;
protected final Supplier<Set<? extends Hardware>> hardwares;
private static class FindImageForServer implements Predicate<Image> {
private final Server instance;
private static class FindImageForServer implements Predicate<Image> {
private final Server instance;
private FindImageForServer(Server instance) {
this.instance = instance;
}
private FindImageForServer(Server instance) {
this.instance = instance;
}
@Override
public boolean apply(Image input) {
return input.getProviderId().equals(instance.getImageId() + "");
}
}
@Override
public boolean apply(Image input) {
return input.getProviderId().equals(instance.getImageRef() + "");
}
}
private static class FindHardwareForServer implements Predicate<Hardware> {
private final Server instance;
private static class FindHardwareForServer implements Predicate<Hardware> {
private final Server instance;
private FindHardwareForServer(Server instance) {
this.instance = instance;
}
private FindHardwareForServer(Server instance) {
this.instance = instance;
}
@Override
public boolean apply(Hardware input) {
return input.getProviderId().equals(instance.getFlavorId() + "");
}
}
@Override
public boolean apply(Hardware input) {
return input.getProviderId().equals(instance.getFlavorRef() + "");
}
}
@Inject
ServerToNodeMetadata(Map<ServerStatus, NodeState> serverStateToNodeState, Map<String, Credentials> credentialStore,
@Memoized Supplier<Set<? extends Image>> images, Supplier<Location> location,
@Memoized Supplier<Set<? extends Hardware>> hardwares) {
this.serverToNodeState = checkNotNull(serverStateToNodeState, "serverStateToNodeState");
this.credentialStore = checkNotNull(credentialStore, "credentialStore");
this.images = checkNotNull(images, "images");
this.location = checkNotNull(location, "location");
this.hardwares = checkNotNull(hardwares, "hardwares");
}
@Inject
ServerToNodeMetadata(Map<ServerStatus, NodeState> serverStateToNodeState, Map<String, Credentials> credentialStore,
@Memoized Supplier<Set<? extends Image>> images, Supplier<Location> location,
@Memoized Supplier<Set<? extends Hardware>> hardwares) {
this.serverToNodeState = checkNotNull(serverStateToNodeState, "serverStateToNodeState");
this.credentialStore = checkNotNull(credentialStore, "credentialStore");
this.images = checkNotNull(images, "images");
this.location = checkNotNull(location, "location");
this.hardwares = checkNotNull(hardwares, "hardwares");
}
@Override
public NodeMetadata apply(Server from) {
NodeMetadataBuilder builder = new NodeMetadataBuilder();
builder.ids(from.getId() + "");
builder.name(from.getName());
builder.location(new LocationBuilder().scope(LocationScope.HOST).id(from.getHostId()).description(
from.getHostId()).parent(location.get()).build());
builder.userMetadata(from.getMetadata());
builder.group(parseGroupFromName(from.getName()));
builder.imageId(from.getImageId() + "");
builder.operatingSystem(parseOperatingSystem(from));
builder.hardware(parseHardware(from));
builder.state(serverToNodeState.get(from.getStatus()));
builder.publicAddresses(from.getAddresses().getPublicAddresses());
builder.privateAddresses(from.getAddresses().getPrivateAddresses());
builder.credentials(credentialStore.get("node#" + from.getId()));
builder.uri(from.getURI());
return builder.build();
}
@Override
public NodeMetadata apply(Server from) {
NodeMetadataBuilder builder = new NodeMetadataBuilder();
builder.ids(from.getId() + "");
builder.name(from.getName());
builder.location(new LocationBuilder().scope(LocationScope.HOST).id(from.getHostId()).description(
from.getHostId()).parent(location.get()).build());
builder.userMetadata(from.getMetadata());
builder.group(parseGroupFromName(from.getName()));
builder.imageId(from.getImageRef() + "");
builder.operatingSystem(parseOperatingSystem(from));
builder.hardware(parseHardware(from));
builder.state(serverToNodeState.get(from.getStatus()));
builder.publicAddresses(from.getAddresses().getPublicAddresses());
builder.privateAddresses(from.getAddresses().getPrivateAddresses());
builder.credentials(credentialStore.get("node#" + from.getId()));
builder.uri(from.getURI());
return builder.build();
}
protected Hardware parseHardware(Server from) {
try {
return Iterables.find(hardwares.get(), new FindHardwareForServer(from));
} catch (NoSuchElementException e) {
logger.warn("could not find a matching hardware for server %s", from);
}
return null;
}
protected Hardware parseHardware(Server from) {
try {
return Iterables.find(hardwares.get(), new FindHardwareForServer(from));
} catch (NoSuchElementException e) {
logger.warn("could not find a matching hardware for server %s", from);
}
return null;
}
protected OperatingSystem parseOperatingSystem(Server 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 in location %s", from, location);
}
return null;
}
protected OperatingSystem parseOperatingSystem(Server 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 in location %s", from, location);
}
return null;
}
}

View File

@ -18,81 +18,113 @@
*/
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();
@SerializedName("private")
private Set<String> privateAddresses = Sets.newLinkedHashSet();
@SerializedName("public")
private Set<Map<String, String>> publicAddresses = Sets.newHashSet();
@SerializedName("private")
private Set<Map<String, String>> privateAddresses = Sets.newHashSet();
public Addresses() {
}
public Addresses() {
}
public Addresses(Set<String> publicAddresses, Set<String> privateAddresses) {
this.publicAddresses = publicAddresses;
this.privateAddresses = privateAddresses;
}
// public Addresses(Set<Map<String, String>> publicAddresses, Set<Map<String, String>> privateAddresses) {
// this.publicAddresses = publicAddresses;
// this.privateAddresses = privateAddresses;
// }
public void setPublicAddresses(Set<String> publicAddresses) {
this.publicAddresses = publicAddresses;
}
public Addresses(Set<String> publicAddresses, Set<String> 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 Set<String> getPublicAddresses() {
return publicAddresses;
}
}
public void setPrivateAddresses(Set<String> privateAddresses) {
this.privateAddresses = privateAddresses;
}
public void setPublicAddresses(Set<Map<String, String>> publicAddresses) {
this.publicAddresses = publicAddresses;
}
public Set<String> getPrivateAddresses() {
return privateAddresses;
}
public Set<String> getPublicAddresses() {
HashSet<String> addresses = new HashSet<String>();
for (Map<String, String> address : publicAddresses) {
if (address.containsKey("addr")) {
addresses.add(address.get("addr"));
}
}
return addresses;
}
@Override
public String toString() {
return "Addresses [privateAddresses=" + privateAddresses + ", publicAddresses="
+ publicAddresses + "]";
}
public void setPrivateAddresses(Set<Map<String, String>> privateAddresses) {
this.privateAddresses = privateAddresses;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((privateAddresses == null) ? 0 : privateAddresses.hashCode());
result = prime * result + ((publicAddresses == null) ? 0 : publicAddresses.hashCode());
return result;
}
public Set<String> getPrivateAddresses() {
HashSet<String> addresses = new HashSet<String>();
for (Map<String, String> address : privateAddresses) {
if (address.containsKey("addr")) {
addresses.add(address.get("addr"));
}
}
return addresses;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Addresses other = (Addresses) obj;
if (privateAddresses == null) {
if (other.privateAddresses != null)
@Override
public String toString() {
return "Addresses [privateAddresses=" + privateAddresses + ", publicAddresses="
+ publicAddresses + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((privateAddresses == null) ? 0 : privateAddresses.hashCode());
result = prime * result + ((publicAddresses == null) ? 0 : publicAddresses.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
} else if (!privateAddresses.equals(other.privateAddresses))
return false;
if (publicAddresses == null) {
if (other.publicAddresses != null)
if (getClass() != obj.getClass())
return false;
} else if (!publicAddresses.equals(other.publicAddresses))
return false;
return true;
}
Addresses other = (Addresses) obj;
if (privateAddresses == null) {
if (other.privateAddresses != null)
return false;
} else if (!privateAddresses.equals(other.privateAddresses))
return false;
if (publicAddresses == null) {
if (other.publicAddresses != null)
return false;
} else if (!publicAddresses.equals(other.publicAddresses))
return false;
return true;
}
}

View File

@ -18,196 +18,217 @@
*/
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.
*
*
* @author Adrian Cole
*/
public class Server extends Resource {
private int id;
private String name;
private int id;
private String name;
private Map<String, String> metadata = Maps.newHashMap();
// private Map<String, Map<String,String>> metadata = Maps.newHashMap();
private Addresses addresses;
private String adminPass;
private Integer flavorId;
private String hostId;
private Integer imageId;
public Map<String, String> getMetadata() {
return metadata;
}
private Integer progress;
private ServerStatus status;
public void setMetadata(Map<String, String> metadata) {
this.metadata = metadata;
}
public Server() {
}
private Map<String, String> metadata = Maps.newHashMap();
public Server(int id, String name) {
this.id = id;
this.name = name;
}
private Addresses addresses;
public void setMetadata(Map<String, String> metadata) {
this.metadata = metadata;
}
private String adminPass;
private String flavorRef;
private String hostId;
private String imageRef;
public Map<String, String> getMetadata() {
return metadata;
}
public String getAffinityId() {
return affinityId;
}
public void setAddresses(Addresses addresses) {
this.addresses = addresses;
}
public void setAffinityId(String affinityId) {
this.affinityId = affinityId;
}
public Addresses getAddresses() {
return addresses;
}
private String affinityId;
public void setAdminPass(String adminPass) {
this.adminPass = adminPass;
}
private Integer progress;
private ServerStatus status;
public String getAdminPass() {
return adminPass;
}
public Server() {
}
public void setFlavorId(Integer flavorId) {
this.flavorId = flavorId;
}
public Server(int id, String name) {
this.id = id;
this.name = name;
}
public Integer getFlavorId() {
return flavorId;
}
// public void setMetadata(Map<String, String> metadata) {
// this.metadata.put("values", metadata);
// }
public void setHostId(String hostId) {
this.hostId = hostId;
}
// public Map<String, String> getMetadata() {
// return metadata.get("values");
// }
/**
* The OpenStack Nova provisioning algorithm has an anti-affinity property that attempts to spread
* out customer VMs across hosts. Under certain situations, VMs from the same customer may be
* placed on the same host. hostId represents the host your cloud server runs on and can be used
* to determine this scenario if it's relevant to your application.
* <p/>
* Note: hostId is unique PER ACCOUNT and is not globally unique.
*/
public String getHostId() {
return hostId;
}
public void setAddresses(Addresses addresses) {
this.addresses = addresses;
}
public int getId() {
return id;
}
public Addresses getAddresses() {
return addresses;
}
public void setImageId(Integer imageId) {
this.imageId = imageId;
}
public void setAdminPass(String adminPass) {
this.adminPass = adminPass;
}
public Integer getImageId() {
return imageId;
}
public String getAdminPass() {
return adminPass;
}
public String getName() {
return name;
}
public void setFlavorRef(String flavorRef) {
this.flavorRef = flavorRef;
}
public void setProgress(Integer progress) {
this.progress = progress;
}
public String getFlavorRef() {
return flavorRef;
}
public Integer getProgress() {
return progress;
}
public void setHostId(String hostId) {
this.hostId = hostId;
}
public void setStatus(ServerStatus status) {
this.status = status;
}
/**
* The OpenStack Nova provisioning algorithm has an anti-affinity property that attempts to spread
* out customer VMs across hosts. Under certain situations, VMs from the same customer may be
* placed on the same host. hostId represents the host your cloud server runs on and can be used
* to determine this scenario if it's relevant to your application.
* <p/>
* Note: hostId is unique PER ACCOUNT and is not globally unique.
*/
public String getHostId() {
return hostId;
}
/**
* Servers contain a status attribute that can be used as an indication of the current server
* state. Servers with an ACTIVE status are available for use.
*/
public ServerStatus getStatus() {
return status;
}
public int getId() {
return id;
}
@Override
public int hashCode() {
final int prime = 31;
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 + ((hostId == null) ? 0 : hostId.hashCode());
result = prime * result + id;
result = prime * result + ((imageId == null) ? 0 : imageId.hashCode());
result = prime * result + ((metadata == null) ? 0 : metadata.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
public void getImageRef(String imageRef) {
this.imageRef = imageRef;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Server other = (Server) obj;
if (addresses == null) {
if (other.addresses != null)
public String getImageRef() {
return imageRef;
}
public String getName() {
return name;
}
public void setProgress(Integer progress) {
this.progress = progress;
}
public Integer getProgress() {
return progress;
}
public void setStatus(ServerStatus status) {
this.status = status;
}
/**
* Servers contain a status attribute that can be used as an indication of the current server
* state. Servers with an ACTIVE status are available for use.
*/
public ServerStatus getStatus() {
return status;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((addresses == null) ? 0 : addresses.hashCode());
result = prime * result + ((adminPass == null) ? 0 : adminPass.hashCode());
result = prime * result + ((flavorRef == null) ? 0 : flavorRef.hashCode());
result = prime * result + ((hostId == null) ? 0 : hostId.hashCode());
result = prime * result + id;
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;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
} else if (!addresses.equals(other.addresses))
return false;
if (adminPass == null) {
if (other.adminPass != null)
if (getClass() != obj.getClass())
return false;
} else if (!adminPass.equals(other.adminPass))
return false;
if (flavorId == null) {
if (other.flavorId != null)
Server other = (Server) obj;
if (addresses == null) {
if (other.addresses != null)
return false;
} else if (!addresses.equals(other.addresses))
return false;
} else if (!flavorId.equals(other.flavorId))
return false;
if (hostId == null) {
if (other.hostId != null)
if (adminPass == null) {
if (other.adminPass != null)
return false;
} else if (!adminPass.equals(other.adminPass))
return false;
} else if (!hostId.equals(other.hostId))
return false;
if (id != other.id)
return false;
if (imageId == null) {
if (other.imageId != null)
if (flavorRef == null) {
if (other.flavorRef != null)
return false;
} else if (!flavorRef.equals(other.flavorRef))
return false;
} else if (!imageId.equals(other.imageId))
return false;
if (metadata == null) {
if (other.metadata != null)
if (hostId == null) {
if (other.hostId != null)
return false;
} else if (!hostId.equals(other.hostId))
return false;
} else if (!metadata.equals(other.metadata))
return false;
if (name == null) {
if (other.name != null)
if (id != other.id)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
if (imageRef == null) {
if (other.imageRef != null)
return false;
} else if (!imageRef.equals(other.imageRef))
return false;
if (metadata == null) {
if (other.metadata != null)
return false;
} else if (!metadata.equals(other.metadata))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
public void setName(String name) {
this.name = name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Server [addresses=" + addresses + ", adminPass=" + adminPass + ", flavorId="
+ flavorId + ", hostId=" + hostId + ", id=" + id + ", imageId=" + imageId
+ ", metadata=" + metadata + ", name=" + name + "]";
}
@Override
public String toString() {
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,139 +18,128 @@
*/
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
*/
@Test(groups = "unit")
public class ServerToNodeMetadataTest {
Location provider = new LocationBuilder().scope(LocationScope.ZONE).id("dallas").description("description").build();
Location provider = new LocationBuilder().scope(LocationScope.ZONE).id("dallas").description("description").build();
@Test
public void testApplyWhereImageAndHardwareNotFoundButCredentialsFound() throws UnknownHostException {
Credentials creds = new Credentials("root", "abdce");
@Test
public void testApplyWhereImageAndHardwareNotFoundButCredentialsFound() throws UnknownHostException, NoSuchMethodException, ClassNotFoundException {
Credentials creds = new Credentials("root", "abdce");
Map<ServerStatus, NodeState> serverStateToNodeState = NovaComputeServiceDependenciesModule.serverToNodeState;
Set<org.jclouds.compute.domain.Image> images = ImmutableSet.of();
Set<org.jclouds.compute.domain.Hardware> hardwares = ImmutableSet.of();
Server server = ParseServerFromJsonResponseTest.parseServer();
Map<ServerStatus, NodeState> serverStateToNodeState = NovaComputeServiceDependenciesModule.serverToNodeState;
Set<org.jclouds.compute.domain.Image> images = ImmutableSet.of();
Set<org.jclouds.compute.domain.Hardware> hardwares = ImmutableSet.of();
Server server = ParseServerFromJsonResponseTest.parseServer();
ServerToNodeMetadata parser = new ServerToNodeMetadata(serverStateToNodeState, ImmutableMap
.<String, Credentials> of("node#1234", creds), Suppliers.<Set<? extends Image>> ofInstance(images),
Suppliers.ofInstance(provider), Suppliers.<Set<? extends Hardware>> ofInstance(hardwares));
ServerToNodeMetadata parser = new ServerToNodeMetadata(serverStateToNodeState, ImmutableMap
.<String, Credentials>of("node#1234", creds), Suppliers.<Set<? extends Image>>ofInstance(images),
Suppliers.ofInstance(provider), Suppliers.<Set<? extends Hardware>>ofInstance(hardwares));
NodeMetadata metadata = parser.apply(server);
NodeMetadata metadata = parser.apply(server);
assertEquals(metadata, new NodeMetadataBuilder().state(NodeState.PENDING).publicAddresses(
ImmutableSet.of("67.23.10.132", "67.23.10.131")).privateAddresses(ImmutableSet.of("10.176.42.16"))
.imageId("2").id("1234").providerId("1234").name("sample-server").credentials(creds).location(
assertEquals(metadata, new NodeMetadataBuilder().state(NodeState.PENDING).publicAddresses(
ImmutableSet.of("67.23.10.132", "67.23.10.131")).privateAddresses(ImmutableSet.of("10.176.42.16"))
.imageId("2").id("1234").providerId("1234").name("sample-server").credentials(creds).location(
new LocationBuilder().scope(LocationScope.HOST).id("e4d909c290d0fb1ca068ffaddf22cbd0")
.description("e4d909c290d0fb1ca068ffaddf22cbd0").parent(provider).build())
.userMetadata(ImmutableMap.of("Server Label", "Web Head 1", "Image Version", "2.1")).build());
}
.description("e4d909c290d0fb1ca068ffaddf22cbd0").parent(provider).build())
.userMetadata(ImmutableMap.of("Server Label", "Web Head 1", "Image Version", "2.1")).build());
}
@Test
public void testApplyWhereImageAndHardwareNotFound() throws UnknownHostException {
Map<ServerStatus, NodeState> serverStateToNodeState = NovaComputeServiceDependenciesModule.serverToNodeState;
Set<org.jclouds.compute.domain.Image> images = ImmutableSet.of();
Set<org.jclouds.compute.domain.Hardware> hardwares = ImmutableSet.of();
Server server = ParseServerFromJsonResponseTest.parseServer();
@Test
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();
Server server = ParseServerFromJsonResponseTest.parseServer();
ServerToNodeMetadata parser = new ServerToNodeMetadata(serverStateToNodeState, ImmutableMap
.<String, Credentials> of(), Suppliers.<Set<? extends Image>> ofInstance(images), Suppliers
.ofInstance(provider), Suppliers.<Set<? extends Hardware>> ofInstance(hardwares));
ServerToNodeMetadata parser = new ServerToNodeMetadata(serverStateToNodeState, ImmutableMap
.<String, Credentials>of(), Suppliers.<Set<? extends Image>>ofInstance(images), Suppliers
.ofInstance(provider), Suppliers.<Set<? extends Hardware>>ofInstance(hardwares));
NodeMetadata metadata = parser.apply(server);
NodeMetadata metadata = parser.apply(server);
assertEquals(metadata, new NodeMetadataBuilder().state(NodeState.PENDING).publicAddresses(
ImmutableSet.of("67.23.10.132", "67.23.10.131")).privateAddresses(ImmutableSet.of("10.176.42.16"))
.imageId("2").id("1234").providerId("1234").name("sample-server").location(
assertEquals(metadata, new NodeMetadataBuilder().state(NodeState.PENDING).publicAddresses(
ImmutableSet.of("67.23.10.132", "67.23.10.131")).privateAddresses(ImmutableSet.of("10.176.42.16"))
.imageId("2").id("1234").providerId("1234").name("sample-server").location(
new LocationBuilder().scope(LocationScope.HOST).id("e4d909c290d0fb1ca068ffaddf22cbd0")
.description("e4d909c290d0fb1ca068ffaddf22cbd0").parent(provider).build())
.userMetadata(ImmutableMap.of("Server Label", "Web Head 1", "Image Version", "2.1")).build());
.description("e4d909c290d0fb1ca068ffaddf22cbd0").parent(provider).build())
.userMetadata(ImmutableMap.of("Server Label", "Web Head 1", "Image Version", "2.1")).build());
}
}
@Test
public void testApplyWhereImageFoundAndHardwareNotFound() throws UnknownHostException {
Map<ServerStatus, NodeState> serverStateToNodeState = NovaComputeServiceDependenciesModule.serverToNodeState;
org.jclouds.compute.domain.Image jcImage = NovaImageToImageTest.convertImage();
Set<org.jclouds.compute.domain.Image> images = ImmutableSet.of(jcImage);
Set<org.jclouds.compute.domain.Hardware> hardwares = ImmutableSet.of();
Server server = ParseServerFromJsonResponseTest.parseServer();
@Test
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);
Set<org.jclouds.compute.domain.Hardware> hardwares = ImmutableSet.of();
Server server = ParseServerFromJsonResponseTest.parseServer();
ServerToNodeMetadata parser = new ServerToNodeMetadata(serverStateToNodeState, ImmutableMap
.<String, Credentials> of(), Suppliers.<Set<? extends Image>> ofInstance(images), Suppliers
.ofInstance(provider), Suppliers.<Set<? extends Hardware>> ofInstance(hardwares));
ServerToNodeMetadata parser = new ServerToNodeMetadata(serverStateToNodeState, ImmutableMap
.<String, Credentials>of(), Suppliers.<Set<? extends Image>>ofInstance(images), Suppliers
.ofInstance(provider), Suppliers.<Set<? extends Hardware>>ofInstance(hardwares));
NodeMetadata metadata = parser.apply(server);
NodeMetadata metadata = parser.apply(server);
assertEquals(metadata, new NodeMetadataBuilder().state(NodeState.PENDING).publicAddresses(
ImmutableSet.of("67.23.10.132", "67.23.10.131")).privateAddresses(ImmutableSet.of("10.176.42.16"))
.imageId("2").operatingSystem(
assertEquals(metadata, new NodeMetadataBuilder().state(NodeState.PENDING).publicAddresses(
ImmutableSet.of("67.23.10.132", "67.23.10.131")).privateAddresses(ImmutableSet.of("10.176.42.16"))
.imageId("2").operatingSystem(
new OperatingSystemBuilder().family(OsFamily.CENTOS).description("CentOS 5.2").version("5.2")
.is64Bit(true).build()).id("1234").providerId("1234").name("sample-server").location(
.is64Bit(true).build()).id("1234").providerId("1234").name("sample-server").location(
new LocationBuilder().scope(LocationScope.HOST).id("e4d909c290d0fb1ca068ffaddf22cbd0")
.description("e4d909c290d0fb1ca068ffaddf22cbd0").parent(provider).build())
.userMetadata(ImmutableMap.of("Server Label", "Web Head 1", "Image Version", "2.1")).build());
.description("e4d909c290d0fb1ca068ffaddf22cbd0").parent(provider).build())
.userMetadata(ImmutableMap.of("Server Label", "Web Head 1", "Image Version", "2.1")).build());
}
}
@Test
public void testApplyWhereImageAndHardwareFound() throws UnknownHostException {
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());
Server server = ParseServerFromJsonResponseTest.parseServer();
@Test
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());
Server server = ParseServerFromJsonResponseTest.parseServer();
ServerToNodeMetadata parser = new ServerToNodeMetadata(serverStateToNodeState, ImmutableMap
.<String, Credentials> of(), Suppliers.<Set<? extends Image>> ofInstance(images), Suppliers
.ofInstance(provider), Suppliers.<Set<? extends Hardware>> ofInstance(hardwares));
ServerToNodeMetadata parser = new ServerToNodeMetadata(serverStateToNodeState, ImmutableMap
.<String, Credentials>of(), Suppliers.<Set<? extends Image>>ofInstance(images), Suppliers
.ofInstance(provider), Suppliers.<Set<? extends Hardware>>ofInstance(hardwares));
NodeMetadata metadata = parser.apply(server);
NodeMetadata metadata = parser.apply(server);
assertEquals(metadata, new NodeMetadataBuilder().state(NodeState.PENDING).publicAddresses(
ImmutableSet.of("67.23.10.132", "67.23.10.131")).privateAddresses(ImmutableSet.of("10.176.42.16"))
.imageId("2").hardware(
assertEquals(metadata, new NodeMetadataBuilder().state(NodeState.PENDING).publicAddresses(
ImmutableSet.of("67.23.10.132", "67.23.10.131")).privateAddresses(ImmutableSet.of("10.176.42.16"))
.imageId("2").hardware(
new HardwareBuilder().ids("1").name("256 MB Server").processors(
ImmutableList.of(new Processor(1.0, 1.0))).ram(256).volumes(
ImmutableList.of(new VolumeBuilder().type(Volume.Type.LOCAL).size(10.0f).durable(true)
.bootDevice(true).build())).build()).operatingSystem(
ImmutableList.of(new Processor(1.0, 1.0))).ram(256).volumes(
ImmutableList.of(new VolumeBuilder().type(Volume.Type.LOCAL).size(10.0f).durable(true)
.bootDevice(true).build())).build()).operatingSystem(
new OperatingSystemBuilder().family(OsFamily.CENTOS).description("CentOS 5.2").version("5.2")
.is64Bit(true).build()).id("1234").providerId("1234").name("sample-server").location(
.is64Bit(true).build()).id("1234").providerId("1234").name("sample-server").location(
new LocationBuilder().scope(LocationScope.HOST).id("e4d909c290d0fb1ca068ffaddf22cbd0")
.description("e4d909c290d0fb1ca068ffaddf22cbd0").parent(provider).build())
.userMetadata(ImmutableMap.of("Server Label", "Web Head 1", "Image Version", "2.1")).build());
}
.description("e4d909c290d0fb1ca068ffaddf22cbd0").parent(provider).build())
.userMetadata(ImmutableMap.of("Server Label", "Web Head 1", "Image Version", "2.1")).build());
}
}

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");