Merge pull request #1328 from abayer/1.5.x

Add StopVirtualMachineOptions for forcing VM stop to CloudStack API
This commit is contained in:
Adrian Cole 2013-02-20 10:53:03 -08:00
commit 5f7d642d51
4 changed files with 103 additions and 0 deletions

View File

@ -31,6 +31,7 @@ import org.jclouds.cloudstack.filters.AuthenticationFilter;
import org.jclouds.cloudstack.options.AssignVirtualMachineOptions;
import org.jclouds.cloudstack.options.DeployVirtualMachineOptions;
import org.jclouds.cloudstack.options.ListVirtualMachinesOptions;
import org.jclouds.cloudstack.options.StopVirtualMachineOptions;
import org.jclouds.rest.annotations.ExceptionParser;
import org.jclouds.rest.annotations.OnlyElement;
import org.jclouds.rest.annotations.QueryParams;
@ -113,6 +114,16 @@ public interface VirtualMachineAsyncClient {
@Consumes(MediaType.APPLICATION_JSON)
ListenableFuture<String> stopVirtualMachine(@QueryParam("id") String id);
/**
* @see VirtualMachineClient#stopVirtualMachine
*/
@GET
@QueryParams(keys = "command", values = "stopVirtualMachine")
@SelectJson("jobid")
@Consumes(MediaType.APPLICATION_JSON)
ListenableFuture<String> stopVirtualMachine(@QueryParam("id") String id,
StopVirtualMachineOptions options);
/**
* @see VirtualMachineClient#resetPasswordForVirtualMachine
*/

View File

@ -26,6 +26,7 @@ import org.jclouds.cloudstack.domain.VirtualMachine;
import org.jclouds.cloudstack.options.AssignVirtualMachineOptions;
import org.jclouds.cloudstack.options.DeployVirtualMachineOptions;
import org.jclouds.cloudstack.options.ListVirtualMachinesOptions;
import org.jclouds.cloudstack.options.StopVirtualMachineOptions;
import org.jclouds.concurrent.Timeout;
/**
@ -100,6 +101,17 @@ public interface VirtualMachineClient {
*/
String stopVirtualMachine(String id);
/**
* Stops a virtual machine.
*
* @param id
* The ID of the virtual machine
* @param options
* If present, whether to force stop.
* @return job id related to destroying the VM
*/
String stopVirtualMachine(String id, StopVirtualMachineOptions options);
/**
* Resets the password for virtual machine. The virtual machine must be in a
* "Stopped" state and the template must already support this feature for

View File

@ -0,0 +1,58 @@
/**
* 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.cloudstack.options;
import org.jclouds.http.options.BaseHttpRequestOptions;
import com.google.common.collect.ImmutableSet;
/**
* Options for stopping virtual machines.
*
* @see <a
* href="http://download.cloud.com/releases/3.0.3/api_3.0.3/root_admin/stopVirtualMachine.html"
* />
* @author Adrian Cole
* @author Andrew Bayer
*/
public class StopVirtualMachineOptions extends BaseHttpRequestOptions {
public static final StopVirtualMachineOptions NONE = new StopVirtualMachineOptions();
/**
* @param forced
* Whether to force stop the virtual machine. Defaults to false.
*/
public StopVirtualMachineOptions forced(boolean forced) {
this.queryParameters.replaceValues("forced", ImmutableSet.of(forced + ""));
return this;
}
public static class Builder {
/**
* @see StopVirtualMachineOptions#forced
*/
public static StopVirtualMachineOptions forced(boolean forced) {
StopVirtualMachineOptions options = new StopVirtualMachineOptions();
return options.forced(forced);
}
}
}

View File

@ -24,6 +24,7 @@ import java.lang.reflect.Method;
import org.jclouds.cloudstack.internal.BaseCloudStackAsyncClientTest;
import org.jclouds.cloudstack.options.AssignVirtualMachineOptions;
import org.jclouds.cloudstack.options.ListVirtualMachinesOptions;
import org.jclouds.cloudstack.options.StopVirtualMachineOptions;
import org.jclouds.functions.IdentityFunction;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.functions.ParseFirstJsonValueNamed;
@ -138,6 +139,7 @@ public class VirtualMachineAsyncClientTest extends BaseCloudStackAsyncClientTest
public void testStopVirtualMachine() throws SecurityException, NoSuchMethodException, IOException {
Method method = VirtualMachineAsyncClient.class.getMethod("stopVirtualMachine", String.class);
HttpRequest httpRequest = processor.createRequest(method, 5);
assertRequestLineEquals(httpRequest,
@ -153,6 +155,26 @@ public class VirtualMachineAsyncClientTest extends BaseCloudStackAsyncClientTest
}
public void testStopVirtualMachineForced() throws SecurityException, NoSuchMethodException, IOException {
Method method = VirtualMachineAsyncClient.class.getMethod("stopVirtualMachine", String.class,
StopVirtualMachineOptions.class);
HttpRequest httpRequest = processor.createRequest(method, 5,
StopVirtualMachineOptions.Builder.forced(true));
assertRequestLineEquals(httpRequest,
"GET http://localhost:8080/client/api?response=json&command=stopVirtualMachine&id=5&forced=true HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, "Accept: application/json\n");
assertPayloadEquals(httpRequest, null, null, false);
assertResponseParserClassEquals(method, httpRequest, ParseFirstJsonValueNamed.class);
assertSaxResponseParserClassEquals(method, null);
assertExceptionParserClassEquals(method, MapHttp4xxCodesToExceptions.class);
checkFilters(httpRequest);
}
public void testResetPasswordForVirtualMachine() throws SecurityException, NoSuchMethodException, IOException {
Method method = VirtualMachineAsyncClient.class.getMethod("resetPasswordForVirtualMachine", String.class);
HttpRequest httpRequest = processor.createRequest(method, 5);