From 06ff68706e89aa9c0f1b7f5d9db4489106e10787 Mon Sep 17 00:00:00 2001 From: Andrew Bayer Date: Sun, 4 Nov 2012 09:37:55 -0800 Subject: [PATCH] Adding assignVirtualMachine command --- .../features/VirtualMachineAsyncClient.java | 11 ++ .../features/VirtualMachineClient.java | 14 ++ .../options/AssignVirtualMachineOptions.java | 166 ++++++++++++++++++ .../VirtualMachineAsyncClientTest.java | 21 +++ 4 files changed, 212 insertions(+) create mode 100644 apis/cloudstack/src/main/java/org/jclouds/cloudstack/options/AssignVirtualMachineOptions.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 0322e59361..427eb5260a 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 @@ -28,6 +28,7 @@ import javax.ws.rs.core.MediaType; import org.jclouds.cloudstack.domain.AsyncCreateResponse; import org.jclouds.cloudstack.domain.VirtualMachine; 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.rest.annotations.ExceptionParser; @@ -158,4 +159,14 @@ public interface VirtualMachineAsyncClient { @ExceptionParser(ReturnNullOnNotFoundOr404.class) ListenableFuture destroyVirtualMachine(@QueryParam("id") String id); + /** + * @see VirtualMachineClient#assinVirtualMachine + */ + @GET + @QueryParams(keys = "command", values = "assignVirtualMachine") + @SelectJson("jobid") + @Consumes(MediaType.APPLICATION_JSON) + ListenableFuture assignVirtualMachine(@QueryParam("virtualmachineid") String virtualMachineId, + AssignVirtualMachineOptions... options); + } 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 df97a229b7..c214d2fc97 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 @@ -23,6 +23,7 @@ import java.util.concurrent.TimeUnit; import org.jclouds.cloudstack.domain.AsyncCreateResponse; 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.concurrent.Timeout; @@ -150,4 +151,17 @@ public interface VirtualMachineClient { * found */ String destroyVirtualMachine(String id); + + + /** + * Re-assign a virtual machine to a different account/domain. + * + * @param virtualMachineId + * VirtualMachine to re-assign + * @param options + * AssignVirtualMachineOptions specifying account and domain to transfer to, and optional network and security group IDs. + * @return VirtualMachine or null if not found + */ + VirtualMachine assignVirtualMachine(String virtualMachineId, AssignVirtualMachineOptions... options); + } diff --git a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/options/AssignVirtualMachineOptions.java b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/options/AssignVirtualMachineOptions.java new file mode 100644 index 0000000000..d52ad50a49 --- /dev/null +++ b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/options/AssignVirtualMachineOptions.java @@ -0,0 +1,166 @@ +/** + * 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 static com.google.common.base.Preconditions.checkArgument; +import static com.google.common.base.Preconditions.checkNotNull; + +import java.util.Map; + +import org.jclouds.encryption.internal.Base64; + +import com.google.common.base.Function; +import com.google.common.base.Joiner; +import com.google.common.base.Splitter; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Iterables; + +/** + * Options used to control what disk offerings are returned + * + * @see + * @author Adrian Cole + */ +public class AssignVirtualMachineOptions extends AccountInDomainOptions { + + public static final AssignVirtualMachineOptions NONE = new AssignVirtualMachineOptions(); + + /** + * @param networkId + * network id used by virtual machine + */ + public AssignVirtualMachineOptions networkId(String networkId) { + this.queryParameters.replaceValues("networkids", ImmutableSet.of(networkId + "")); + return this; + } + + /** + * @param networkIds + * network ids used by virtual machine + */ + public AssignVirtualMachineOptions networkIds(Iterable networkIds) { + this.queryParameters.replaceValues("networkids", ImmutableSet.of(Joiner.on(',').join(networkIds))); + return this; + } + + public Iterable getNetworkIds() { + if (queryParameters.get("networkids").size() == 1) { + return Iterables.transform( + Splitter.on(",").split(Iterables.getOnlyElement(queryParameters.get("networkids"))), + new Function() { + + @Override + public String apply(String arg0) { + return arg0; + } + + }); + } else { + return ImmutableSet. of(); + } + } + + /** + * @param securityGroupId + * security group applied to the virtual machine. Should be passed + * only when vm is created from a zone with Basic Network support + */ + public AssignVirtualMachineOptions securityGroupId(String securityGroupId) { + this.queryParameters.replaceValues("securitygroupids", ImmutableSet.of(securityGroupId + "")); + return this; + } + + /** + * @param securityGroupIds + * security groups applied to the virtual machine. Should be passed + * only when vm is created from a zone with Basic Network support + */ + public AssignVirtualMachineOptions securityGroupIds(Iterable securityGroupIds) { + this.queryParameters.replaceValues("securitygroupids", ImmutableSet.of(Joiner.on(',').join(securityGroupIds))); + return this; + } + + public static class Builder { + /** + * @see AssignVirtualMachineOptions#networkId + */ + public static AssignVirtualMachineOptions networkId(String id) { + AssignVirtualMachineOptions options = new AssignVirtualMachineOptions(); + return options.networkId(id); + } + + /** + * @see AssignVirtualMachineOptions#networkIds + */ + public static AssignVirtualMachineOptions networkIds(Iterable networkIds) { + AssignVirtualMachineOptions options = new AssignVirtualMachineOptions(); + return options.networkIds(networkIds); + } + + /** + * @see AssignVirtualMachineOptions#securityGroupId + */ + public static AssignVirtualMachineOptions securityGroupId(String id) { + AssignVirtualMachineOptions options = new AssignVirtualMachineOptions(); + return options.securityGroupId(id); + } + + /** + * @see AssignVirtualMachineOptions#securityGroupIds + */ + public static AssignVirtualMachineOptions securityGroupIds(Iterable securityGroupIds) { + AssignVirtualMachineOptions options = new AssignVirtualMachineOptions(); + return options.securityGroupIds(securityGroupIds); + } + + /** + * @see AssignVirtualMachineOptions#accountInDomain + */ + public static AssignVirtualMachineOptions accountInDomain(String account, String domain) { + AssignVirtualMachineOptions options = new AssignVirtualMachineOptions(); + return options.accountInDomain(account, domain); + } + + /** + * @see AssignVirtualMachineOptions#domainId + */ + public static AssignVirtualMachineOptions domainId(String domainId) { + AssignVirtualMachineOptions options = new AssignVirtualMachineOptions(); + return options.domainId(domainId); + } + } + + /** + * {@inheritDoc} + */ + @Override + public AssignVirtualMachineOptions accountInDomain(String account, String domain) { + return AssignVirtualMachineOptions.class.cast(super.accountInDomain(account, domain)); + } + + /** + * {@inheritDoc} + */ + @Override + public AssignVirtualMachineOptions domainId(String domainId) { + return AssignVirtualMachineOptions.class.cast(super.domainId(domainId)); + } +} 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 94f069d89d..650cd32ba6 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 @@ -22,6 +22,7 @@ import java.io.IOException; import java.lang.reflect.Method; import org.jclouds.cloudstack.internal.BaseCloudStackAsyncClientTest; +import org.jclouds.cloudstack.options.AssignVirtualMachineOptions; import org.jclouds.cloudstack.options.DeployVirtualMachineOptions; import org.jclouds.cloudstack.options.ListVirtualMachinesOptions; import org.jclouds.functions.IdentityFunction; @@ -239,6 +240,26 @@ public class VirtualMachineAsyncClientTest extends BaseCloudStackAsyncClientTest checkFilters(httpRequest); } + + public void testAssignVirtualMachine() throws SecurityException, NoSuchMethodException, IOException { + Method method = VirtualMachineAsyncClient.class.getMethod("assignVirtualMachine", String.class, + AssignVirtualMachineOptions[].class); + HttpRequest httpRequest = processor.createRequest(method, "abcd", + AssignVirtualMachineOptions.Builder.accountInDomain("adrian", "6")); + + assertRequestLineEquals( + httpRequest, + "GET http://localhost:8080/client/api?response=json&command=assignVirtualMachine&virtualmachineid=abcd&account=adrian&domainid=6 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); + + } @Override protected TypeLiteral> createTypeLiteral() {