Adding assignVirtualMachine command

This commit is contained in:
Andrew Bayer 2012-11-04 09:37:55 -08:00 committed by Adrian Cole
parent 77c941ce3f
commit 83796f4421
4 changed files with 212 additions and 0 deletions

View File

@ -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<String> destroyVirtualMachine(@QueryParam("id") String id);
/**
* @see VirtualMachineClient#assinVirtualMachine
*/
@GET
@QueryParams(keys = "command", values = "assignVirtualMachine")
@SelectJson("jobid")
@Consumes(MediaType.APPLICATION_JSON)
ListenableFuture<VirtualMachine> assignVirtualMachine(@QueryParam("virtualmachineid") String virtualMachineId,
AssignVirtualMachineOptions... options);
}

View File

@ -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);
}

View File

@ -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 <a href=
* "http://download.cloud.com/releases/3.0.3/api_3.0.3/root_admin/assignVirtualMachine.html"
* />
* @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<String> networkIds) {
this.queryParameters.replaceValues("networkids", ImmutableSet.of(Joiner.on(',').join(networkIds)));
return this;
}
public Iterable<String> getNetworkIds() {
if (queryParameters.get("networkids").size() == 1) {
return Iterables.transform(
Splitter.on(",").split(Iterables.getOnlyElement(queryParameters.get("networkids"))),
new Function<String, String>() {
@Override
public String apply(String arg0) {
return arg0;
}
});
} else {
return ImmutableSet.<String> 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<String> 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<String> 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<String> 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));
}
}

View File

@ -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<RestAnnotationProcessor<VirtualMachineAsyncClient>> createTypeLiteral() {