From b756af1543543b2aa4f4ae55f75e32b5b91ebd7b Mon Sep 17 00:00:00 2001 From: Andrew Bayer Date: Wed, 20 Feb 2013 10:18:32 -0800 Subject: [PATCH] Add StopVirtualMachineOptions for forcing VM stop to CloudStack API --- .../features/VirtualMachineAsyncClient.java | 11 ++++ .../features/VirtualMachineClient.java | 12 ++++ .../options/StopVirtualMachineOptions.java | 58 +++++++++++++++++++ .../VirtualMachineAsyncClientTest.java | 25 +++++++- 4 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 apis/cloudstack/src/main/java/org/jclouds/cloudstack/options/StopVirtualMachineOptions.java diff --git a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/VirtualMachineAsyncClient.java b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/VirtualMachineAsyncClient.java index b0826d3ebc..8e4b466c99 100644 --- a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/VirtualMachineAsyncClient.java +++ b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/VirtualMachineAsyncClient.java @@ -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 stopVirtualMachine(@QueryParam("id") String id); + /** + * @see VirtualMachineClient#stopVirtualMachine + */ + @GET + @QueryParams(keys = "command", values = "stopVirtualMachine") + @SelectJson("jobid") + @Consumes(MediaType.APPLICATION_JSON) + ListenableFuture stopVirtualMachine(@QueryParam("id") String id, + StopVirtualMachineOptions options); + /** * @see VirtualMachineClient#resetPasswordForVirtualMachine */ diff --git a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/VirtualMachineClient.java b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/VirtualMachineClient.java index 92f86b8988..f0ed3a0d14 100644 --- a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/VirtualMachineClient.java +++ b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/VirtualMachineClient.java @@ -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 diff --git a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/options/StopVirtualMachineOptions.java b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/options/StopVirtualMachineOptions.java new file mode 100644 index 0000000000..1c6abac022 --- /dev/null +++ b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/options/StopVirtualMachineOptions.java @@ -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 + * @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); + } + + } +} diff --git a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/VirtualMachineAsyncClientTest.java b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/VirtualMachineAsyncClientTest.java index d35f34a3c0..fa4d60ba77 100644 --- a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/VirtualMachineAsyncClientTest.java +++ b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/VirtualMachineAsyncClientTest.java @@ -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. of(5)); + GeneratedHttpRequest httpRequest = processor.createRequest(method, ImmutableList. 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. 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. of(5));