implemented poweroff, poweron for vm's, added missing tests for firewall operations.

This commit is contained in:
Kedar Dave 2011-04-28 16:32:54 -05:00
parent 44b0eefd87
commit 6c370f558d
6 changed files with 199 additions and 5 deletions

View File

@ -23,6 +23,7 @@ import java.net.URI;
import javax.annotation.Nullable;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
@ -93,4 +94,22 @@ public interface VMAsyncClient {
@XMLResponseParser(TaskHandler.class)
@ExceptionParser(ReturnNullOnNotFoundOr404.class)
ListenableFuture<Task> removeVM(@EndpointParam URI vm);
/**
* @see VMClient#powerOffVM
*/
@POST
@XMLResponseParser(TaskHandler.class)
@Path("action/powerOff")
@ExceptionParser(ReturnNullOnNotFoundOr404.class)
ListenableFuture<Task> powerOffVM(@EndpointParam URI vm);
/**
* @see VMClient#powerOnVM
*/
@POST
@XMLResponseParser(TaskHandler.class)
@Path("action/powerOn")
@ExceptionParser(ReturnNullOnNotFoundOr404.class)
ListenableFuture<Task> powerOnVM(@EndpointParam URI vm);
}

View File

@ -89,4 +89,22 @@ public interface VMClient {
* @see #removeVMFromVDC
*/
Task removeVM(URI vm);
/**
* Power off a VM
*
* @param vm
* href of the vm
* @return
*/
Task powerOffVM(URI vm);
/**
* Power on a VM
*
* @param vm
* href of the vm
* @return
*/
Task powerOnVM(URI vm);
}

View File

@ -18,7 +18,16 @@
*/
package org.jclouds.savvis.vpdc.features;
import java.io.IOException;
import java.lang.reflect.Method;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.functions.ParseSax;
import org.jclouds.rest.functions.ReturnNullOnNotFoundOr404;
import org.jclouds.rest.internal.RestAnnotationProcessor;
import org.jclouds.savvis.vpdc.domain.FirewallRule;
import org.jclouds.savvis.vpdc.xml.TaskHandler;
import org.jclouds.util.Strings2;
import org.testng.annotations.Test;
import com.google.inject.TypeLiteral;
@ -31,6 +40,38 @@ import com.google.inject.TypeLiteral;
@Test(groups = "unit")
public class FirewallAsyncClientTest extends BaseVPDCAsyncClientTest<FirewallAsyncClient> {
public void testAddFirewallRule() throws NoSuchMethodException, IOException{
Method method = FirewallAsyncClient.class.getMethod("addFirewallRule", String.class, String.class, FirewallRule.class);
HttpRequest request = processor.createRequest(method, "11", "22", FirewallRule.builder().firewallType("SERVER_TIER_FIREWALL").isEnabled(true).source("internet")
.destination("VM Tier01").port("22").protocol("Tcp").policy("allow").description("Server Tier Firewall Rule").isLogged(false).build());
assertRequestLineEquals(request, "PUT https://api.symphonyvpdc.savvis.net/rest/api/v0.8/org/11/vdc/22/FirewallService/ HTTP/1.1");
assertNonPayloadHeadersEqual(request, "");
assertPayloadEquals(request, Strings2.toStringAndClose(getClass().getResourceAsStream("/firewallService-default.xml")),
"application/xml", false);
assertResponseParserClassEquals(method, request, ParseSax.class);
assertSaxResponseParserClassEquals(method, TaskHandler.class);
checkFilters(request);
}
public void testDeleteFirewallRule() throws NoSuchMethodException, IOException{
Method method = FirewallAsyncClient.class.getMethod("deleteFirewallRule", String.class, String.class, FirewallRule.class);
HttpRequest request = processor.createRequest(method, "11", "22", FirewallRule.builder().firewallType("SERVER_TIER_FIREWALL").isEnabled(true).source("internet")
.destination("VM Tier01").port("22").protocol("Tcp").policy("allow").description("Server Tier Firewall Rule").isLogged(false).build());
assertRequestLineEquals(request, "DELETE https://api.symphonyvpdc.savvis.net/rest/api/v0.8/org/11/vdc/22/FirewallService/ HTTP/1.1");
assertNonPayloadHeadersEqual(request, "");
assertPayloadEquals(request, Strings2.toStringAndClose(getClass().getResourceAsStream("/firewallService-default.xml")),
"application/xml", false);
assertResponseParserClassEquals(method, request, ParseSax.class);
assertSaxResponseParserClassEquals(method, TaskHandler.class);
assertExceptionParserClassEquals(method, ReturnNullOnNotFoundOr404.class);
checkFilters(request);
}
@Override
protected TypeLiteral<RestAnnotationProcessor<FirewallAsyncClient>> createTypeLiteral() {

View File

@ -18,13 +18,9 @@
*/
package org.jclouds.savvis.vpdc.features;
import java.util.concurrent.TimeUnit;
import org.jclouds.predicates.RetryablePredicate;
import org.jclouds.savvis.vpdc.domain.FirewallRule;
import org.jclouds.savvis.vpdc.domain.Resource;
import org.jclouds.savvis.vpdc.domain.Task;
import org.jclouds.savvis.vpdc.predicates.TaskSuccess;
import org.testng.annotations.AfterGroups;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.Test;

View File

@ -141,6 +141,40 @@ public class VMAsyncClientTest extends BaseVPDCAsyncClientTest<VMAsyncClient> {
checkFilters(request);
}
public void testPowerOffVM() throws SecurityException, NoSuchMethodException, IOException {
Method method = VMAsyncClient.class.getMethod("powerOffVM", URI.class);
HttpRequest request = processor.createRequest(method, URI
.create("https://api.symphonyvpdc.savvis.net/rest/api/v0.8/org/11/vdc/22/vApp/33"));
assertRequestLineEquals(request,
"POST https://api.symphonyvpdc.savvis.net/rest/api/v0.8/org/11/vdc/22/vApp/33/action/powerOff HTTP/1.1");
assertNonPayloadHeadersEqual(request, "");
assertPayloadEquals(request, null, null, false);
assertResponseParserClassEquals(method, request, ParseSax.class);
assertSaxResponseParserClassEquals(method, TaskHandler.class);
assertExceptionParserClassEquals(method, ReturnNullOnNotFoundOr404.class);
checkFilters(request);
}
public void testPowerOnVM() throws SecurityException, NoSuchMethodException, IOException {
Method method = VMAsyncClient.class.getMethod("powerOnVM", URI.class);
HttpRequest request = processor.createRequest(method, URI
.create("https://api.symphonyvpdc.savvis.net/rest/api/v0.8/org/11/vdc/22/vApp/33"));
assertRequestLineEquals(request,
"POST https://api.symphonyvpdc.savvis.net/rest/api/v0.8/org/11/vdc/22/vApp/33/action/powerOn HTTP/1.1");
assertNonPayloadHeadersEqual(request, "");
assertPayloadEquals(request, null, null, false);
assertResponseParserClassEquals(method, request, ParseSax.class);
assertSaxResponseParserClassEquals(method, TaskHandler.class);
assertExceptionParserClassEquals(method, ReturnNullOnNotFoundOr404.class);
checkFilters(request);
}
@Override
protected TypeLiteral<RestAnnotationProcessor<VMAsyncClient>> createTypeLiteral() {

View File

@ -21,6 +21,7 @@ package org.jclouds.savvis.vpdc.features;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.testng.Assert.assertEquals;
import java.net.URI;
import java.util.concurrent.TimeUnit;
import org.jclouds.cim.OSType;
@ -34,7 +35,10 @@ import org.jclouds.savvis.vpdc.domain.Resource;
import org.jclouds.savvis.vpdc.domain.Task;
import org.jclouds.savvis.vpdc.domain.VDC;
import org.jclouds.savvis.vpdc.domain.VM;
import org.jclouds.savvis.vpdc.domain.VM.Status;
import org.jclouds.savvis.vpdc.domain.VMSpec;
import org.jclouds.savvis.vpdc.options.GetVMOptions;
import org.jclouds.savvis.vpdc.reference.VCloudMediaType;
import org.jclouds.ssh.SshClient;
import org.jclouds.util.InetAddresses2;
import org.testng.annotations.AfterGroups;
@ -114,7 +118,7 @@ public class VMClientLiveTest extends BaseVPDCClientLiveTest {
.operatingSystem(os).memoryInGig(2).addDataDrive("/data01", 25).build());
// make sure there's no error
assert task.getId() != null && task.getError() != null : task;
assert task.getId() != null && task.getError() == null : task;
assert this.taskTester.apply(task.getId());
vm = restContext.getApi().getBrowsingClient().getVMInVDC(billingSiteId, vpdcId, task.getOwner().getId());
@ -148,6 +152,88 @@ public class VMClientLiveTest extends BaseVPDCClientLiveTest {
}
}
public void testPowerOffVM() throws Exception {
billingSiteId = restContext.getApi().getBrowsingClient().getOrg(null).getId();// default
vpdcId = Iterables.find(restContext.getApi().getBrowsingClient().getOrg(billingSiteId).getVDCs(),
new Predicate<Resource>() {
// try to find the first VDC owned by the current user
// check here for what the email property might be, or in
// the jclouds-wire.log
@Override
public boolean apply(Resource arg0) {
String description = restContext.getApi().getBrowsingClient().getVDCInOrg(billingSiteId,
arg0.getId()).getDescription();
return description.indexOf(email) != -1;
}
}).getId();
VDC vpdc = restContext.getApi().getBrowsingClient().getVDCInOrg(billingSiteId, vpdcId);
URI vmURI = Iterables.find(vpdc.getResourceEntities(), new Predicate<Resource>() {
@Override
public boolean apply(Resource arg0) {
if(VCloudMediaType.VAPP_XML.equals(arg0.getType())){
VM response1 = restContext.getApi().getBrowsingClient().getVM(arg0.getHref(), (GetVMOptions[]) null);
System.out.printf("powering off vm - %s%n", response1.getName());
if(response1.getStatus().equals(Status.ON)){
return true;
}
}
return false;
}
}).getHref();
Task task = client.powerOffVM(vmURI);
// make sure there's no error
assert task.getId() != null && task.getError() == null : task;
assert this.taskTester.apply(task.getId());
}
public void testPowerOnVM() throws Exception {
billingSiteId = restContext.getApi().getBrowsingClient().getOrg(null).getId();// default
vpdcId = Iterables.find(restContext.getApi().getBrowsingClient().getOrg(billingSiteId).getVDCs(),
new Predicate<Resource>() {
// try to find the first VDC owned by the current user
// check here for what the email property might be, or in
// the jclouds-wire.log
@Override
public boolean apply(Resource arg0) {
String description = restContext.getApi().getBrowsingClient().getVDCInOrg(billingSiteId,
arg0.getId()).getDescription();
return description.indexOf(email) != -1;
}
}).getId();
VDC vpdc = restContext.getApi().getBrowsingClient().getVDCInOrg(billingSiteId, vpdcId);
URI vmURI = Iterables.find(vpdc.getResourceEntities(), new Predicate<Resource>() {
@Override
public boolean apply(Resource arg0) {
if(VCloudMediaType.VAPP_XML.equals(arg0.getType())){
VM response1 = restContext.getApi().getBrowsingClient().getVM(arg0.getHref(), (GetVMOptions[]) null);
System.out.printf("powering on vm - %s%n", response1.getName());
if(response1.getStatus().equals(Status.OFF)){
return true;
}
}
return false;
}
}).getHref();
Task task = client.powerOnVM(vmURI);
// make sure there's no error
assert task.getId() != null && task.getError() == null : task;
assert this.taskTester.apply(task.getId());
}
@AfterGroups(groups = "live")
protected void tearDown() {
if (vm != null) {