Issue 695: Service call for getVirtualMachines

This commit is contained in:
Jason King 2011-11-18 14:39:28 +00:00
parent 7206ca1892
commit e7847fa3cc
5 changed files with 128 additions and 3 deletions

View File

@ -0,0 +1,68 @@
/**
* 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.tmrk.enterprisecloud.domain;
import com.google.common.collect.Sets;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;
/**
* Wraps individual VirtualMachine elements.
* Needed because parsing is done with JAXB and it does not handle Generic collections
* @author Jason King
*/
@XmlRootElement(name="VirtualMachines")
public class VirtualMachines {
private LinkedHashSet<VirtualMachine> virtualMachines = Sets.newLinkedHashSet();
@XmlElement(name = "VirtualMachine")
void setVirtualMachine(VirtualMachine nic) {
this.virtualMachines.add(nic);
}
public Set<VirtualMachine> getVirtualMachines() {
return Collections.unmodifiableSet(virtualMachines);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
VirtualMachines vms1 = (VirtualMachines) o;
if (!virtualMachines.equals(vms1.virtualMachines)) return false;
return true;
}
@Override
public int hashCode() {
return virtualMachines.hashCode();
}
public String toString() {
return "["+ virtualMachines.toString()+"]";
}
}

View File

@ -26,6 +26,7 @@ import org.jclouds.rest.annotations.JAXBResponseParser;
import org.jclouds.rest.annotations.RequestFilters;
import org.jclouds.rest.functions.ReturnNullOnNotFoundOr404;
import org.jclouds.tmrk.enterprisecloud.domain.VirtualMachine;
import org.jclouds.tmrk.enterprisecloud.domain.VirtualMachines;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
@ -46,6 +47,16 @@ import javax.ws.rs.PathParam;
@Headers(keys = "x-tmrk-version", values = "{jclouds.api-version}")
public interface VirtualMachineAsyncClient {
/**
* @see VirtualMachineClient#getVirtualMachines
*/
@GET
@Path("/virtualMachines/computePools/{id}")
@Consumes("application/vnd.tmrk.cloud.virtualMachine; type=collection")
@JAXBResponseParser
@ExceptionParser(ReturnNullOnNotFoundOr404.class)
ListenableFuture<VirtualMachines> getVirtualMachines(@PathParam("id") long id);
/**
* @see VirtualMachineClient#getVirtualMachine
*/

View File

@ -20,6 +20,7 @@ package org.jclouds.tmrk.enterprisecloud.features;
import org.jclouds.concurrent.Timeout;
import org.jclouds.tmrk.enterprisecloud.domain.VirtualMachine;
import org.jclouds.tmrk.enterprisecloud.domain.VirtualMachines;
import java.util.concurrent.TimeUnit;
@ -36,10 +37,17 @@ import java.util.concurrent.TimeUnit;
@Timeout(duration = 180, timeUnit = TimeUnit.SECONDS)
public interface VirtualMachineClient {
/**
* returns information regarding virtual machines defined in a compute pool
* @param id the i of the compute pool
* @return the virtual machines
*/
VirtualMachines getVirtualMachines(long id);
/**
* The Get Virtual Machines by ID call returns information regarding a
* specified virtual machine defined in an environment.
*
* @param id the id of the virtual machine
* @return the virtual Machine or null if not found
*/
VirtualMachine getVirtualMachine(long id);

View File

@ -38,7 +38,7 @@ public class VirtualMachineAsyncClientTest extends BaseTerremarkEnterpriseCloudA
public void testGetVirtualMachine() throws SecurityException, NoSuchMethodException, IOException {
Method method = VirtualMachineAsyncClient.class.getMethod("getVirtualMachine", long.class);
HttpRequest httpRequest = processor.createRequest(method, 1);
HttpRequest httpRequest = processor.createRequest(method,1);
assertRequestLineEquals(httpRequest, "GET https://services-beta.enterprisecloud.terremark.com/cloudapi/ecloud/virtualMachines/1 HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, "Accept: application/vnd.tmrk.cloud.virtualMachine\nx-tmrk-version: 2011-07-01\n");
@ -48,7 +48,20 @@ public class VirtualMachineAsyncClientTest extends BaseTerremarkEnterpriseCloudA
assertExceptionParserClassEquals(method, ReturnNullOnNotFoundOr404.class);
checkFilters(httpRequest);
}
public void testGetVirtualMachines() throws SecurityException, NoSuchMethodException, IOException {
Method method = VirtualMachineAsyncClient.class.getMethod("getVirtualMachines", long.class);
HttpRequest httpRequest = processor.createRequest(method,567);
assertRequestLineEquals(httpRequest, "GET https://services-beta.enterprisecloud.terremark.com/cloudapi/ecloud/virtualMachines/computePools/567 HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, "Accept: application/vnd.tmrk.cloud.virtualMachine; type=collection\nx-tmrk-version: 2011-07-01\n");
assertPayloadEquals(httpRequest, null, null, false);
assertResponseParserClassEquals(method, httpRequest, ParseXMLWithJAXB.class);
assertExceptionParserClassEquals(method, ReturnNullOnNotFoundOr404.class);
checkFilters(httpRequest);
}
@Override

View File

@ -19,9 +19,12 @@
package org.jclouds.tmrk.enterprisecloud.features;
import org.jclouds.tmrk.enterprisecloud.domain.VirtualMachine;
import org.jclouds.tmrk.enterprisecloud.domain.VirtualMachines;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.Test;
import java.net.URI;
import static org.testng.Assert.assertEquals;
/**
@ -40,10 +43,32 @@ public class VirtualMachineClientLiveTest extends BaseTerremarkEnterpriseCloudCl
private VirtualMachineClient client;
@Test
public void testGetVirtualMachine() {
public void testGetVirtualMachines() throws Exception {
// TODO: don't hard-code id
VirtualMachines virtualMachines = client.getVirtualMachines(89);
for( VirtualMachine vm : virtualMachines.getVirtualMachines()) {
VirtualMachine virtualMachine = client.getVirtualMachine(parse(vm.getHref()));
assert null != virtualMachine;
assertEquals(virtualMachine.getStatus(),VirtualMachine.VirtualMachineStatus.DEPLOYED);
}
}
@Test
public void testGetVirtualMachine() throws Exception {
// TODO: don't hard-code id
VirtualMachine virtualMachine = client.getVirtualMachine(5504);
assert null != virtualMachine;
assertEquals(virtualMachine.getStatus(),VirtualMachine.VirtualMachineStatus.DEPLOYED);
}
// TODO: We are not supposed to parse the href's
// The alternative is to use URI's on the method calls.
// But this has the risk of exposing strings like "/virtualmachines/5504" and "/computepools/89" to users
// Also - would need to figure out how to configure the tests
// to add on the endpoint so that the @EndpointParam is converted into a proper request.
private long parse(URI uri) {
String path = uri.getPath();
path = path.substring(path.lastIndexOf("/")+1);
return Long.parseLong(path);
}
}