Add StopVirtualMachineOptions for forcing VM stop to CloudStack API

This commit is contained in:
Andrew Bayer 2013-02-20 10:18:32 -08:00 committed by adriancole
parent b0cebe85d2
commit b756af1543
4 changed files with 104 additions and 2 deletions

View File

@ -34,6 +34,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.Fallback;
import org.jclouds.rest.annotations.OnlyElement;
import org.jclouds.rest.annotations.QueryParams;
@ -119,6 +120,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

@ -24,6 +24,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;
/**
* Provides synchronous access to CloudStack VirtualMachine features.
@ -96,6 +97,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,10 +24,11 @@ import java.io.IOException;
import org.jclouds.Fallbacks.EmptySetOnNotFoundOr404;
import org.jclouds.Fallbacks.NullOnNotFoundOr404;
import org.jclouds.fallbacks.MapHttp4xxCodesToExceptions;
import org.jclouds.cloudstack.internal.BaseCloudStackAsyncClientTest;
import org.jclouds.cloudstack.options.AssignVirtualMachineOptions;
import org.jclouds.cloudstack.options.ListVirtualMachinesOptions;
import org.jclouds.fallbacks.MapHttp4xxCodesToExceptions;
import org.jclouds.cloudstack.options.StopVirtualMachineOptions;
import org.jclouds.functions.IdentityFunction;
import org.jclouds.http.functions.ParseFirstJsonValueNamed;
import org.jclouds.rest.internal.GeneratedHttpRequest;
@ -137,7 +138,7 @@ public class VirtualMachineAsyncClientTest extends BaseCloudStackAsyncClientTest
public void testStopVirtualMachine() throws SecurityException, NoSuchMethodException, IOException {
Invokable<?, ?> method = method(VirtualMachineAsyncClient.class, "stopVirtualMachine", String.class);
GeneratedHttpRequest httpRequest = processor.createRequest(method, ImmutableList.<Object> of(5));
GeneratedHttpRequest httpRequest = processor.createRequest(method, ImmutableList.<Object> of("5"));
assertRequestLineEquals(httpRequest,
"GET http://localhost:8080/client/api?response=json&command=stopVirtualMachine&id=5 HTTP/1.1");
@ -152,6 +153,26 @@ public class VirtualMachineAsyncClientTest extends BaseCloudStackAsyncClientTest
}
public void testStopVirtualMachineForced() throws SecurityException, NoSuchMethodException, IOException {
Invokable<?, ?> method = method(VirtualMachineAsyncClient.class, "stopVirtualMachine", String.class,
StopVirtualMachineOptions.class);
GeneratedHttpRequest httpRequest = processor.createRequest(method, ImmutableList.<Object> of("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);
assertFallbackClassEquals(method, MapHttp4xxCodesToExceptions.class);
checkFilters(httpRequest);
}
public void testResetPasswordForVirtualMachine() throws SecurityException, NoSuchMethodException, IOException {
Invokable<?, ?> method = method(VirtualMachineAsyncClient.class, "resetPasswordForVirtualMachine", String.class);
GeneratedHttpRequest httpRequest = processor.createRequest(method, ImmutableList.<Object> of(5));