mirror of https://github.com/apache/jclouds.git
Issue 112: added options for instantiating a vapp template
git-svn-id: http://jclouds.googlecode.com/svn/trunk@2286 3d8758e0-26b5-11de-8745-db77d3ebf521
This commit is contained in:
parent
e5f84d7a41
commit
c7306a255f
|
@ -71,6 +71,7 @@ public interface TerremarkVCloudClient extends VCloudClient {
|
|||
@XMLResponseParser(TerremarkVAppHandler.class)
|
||||
@MapBinder(InstantiateVAppTemplateOptions.class)
|
||||
Future<? extends VApp> instantiateVAppTemplate(@MapEntityParam("name") String appName,
|
||||
@MapEntityParam("template") @ParamParser(CatalogIdToUri.class) int templateId);
|
||||
@MapEntityParam("template") @ParamParser(CatalogIdToUri.class) int templateId,
|
||||
InstantiateVAppTemplateOptions... options);
|
||||
|
||||
}
|
||||
|
|
|
@ -19,11 +19,12 @@ import org.jclouds.rest.binders.BindToStringEntity;
|
|||
*/
|
||||
@Singleton
|
||||
public class BindInstantiateVAppTemplateParamsToXmlEntity implements MapBinder {
|
||||
|
||||
@Inject
|
||||
@Named("InstantiateVAppTemplateParams")
|
||||
String xmlTemplate;
|
||||
private String xmlTemplate;
|
||||
@Inject
|
||||
BindToStringEntity stringBinder;
|
||||
private BindToStringEntity stringBinder;
|
||||
|
||||
public void bindToRequest(HttpRequest request, Map<String, String> postParams) {
|
||||
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
package org.jclouds.vcloud.terremark.options;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkArgument;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Map;
|
||||
|
||||
|
@ -9,6 +11,7 @@ import org.jclouds.http.HttpRequest;
|
|||
import org.jclouds.vcloud.endpoints.Network;
|
||||
import org.jclouds.vcloud.terremark.binders.BindInstantiateVAppTemplateParamsToXmlEntity;
|
||||
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import com.google.common.collect.Maps;
|
||||
|
||||
/**
|
||||
|
@ -21,17 +24,63 @@ public class InstantiateVAppTemplateOptions extends BindInstantiateVAppTemplateP
|
|||
@Network
|
||||
private URI defaultNetwork;
|
||||
|
||||
@VisibleForTesting
|
||||
String cpuCount = "1";
|
||||
@VisibleForTesting
|
||||
String megabytes = "512";
|
||||
@VisibleForTesting
|
||||
String network;
|
||||
|
||||
public void bindToRequest(HttpRequest request, Map<String, String> postParams) {
|
||||
Map<String, String> copy = Maps.newHashMap();
|
||||
copy.putAll(postParams);
|
||||
if (postParams.get("count") == null)
|
||||
copy.put("count", "1");
|
||||
if (postParams.get("megabytes") == null)
|
||||
copy.put("megabytes", "512");
|
||||
if (postParams.get("network") == null)
|
||||
copy.put("network", defaultNetwork.toASCIIString());
|
||||
|
||||
copy.put("count", cpuCount);
|
||||
copy.put("megabytes", megabytes);
|
||||
copy.put("network", network != null ? network : defaultNetwork.toASCIIString());
|
||||
super.bindToRequest(request, copy);
|
||||
}
|
||||
|
||||
public InstantiateVAppTemplateOptions cpuCount(int cpuCount) {
|
||||
checkArgument(cpuCount >= 1, "cpuCount must be positive");
|
||||
this.cpuCount = cpuCount + "";
|
||||
return this;
|
||||
}
|
||||
|
||||
public InstantiateVAppTemplateOptions megabytes(int megabytes) {
|
||||
checkArgument(megabytes % 512 == 0, "megabytes must be in an increment of 512");
|
||||
this.megabytes = megabytes + "";
|
||||
return this;
|
||||
}
|
||||
|
||||
public InstantiateVAppTemplateOptions inNetwork(URI networkLocation) {
|
||||
this.network = networkLocation.toASCIIString();
|
||||
return this;
|
||||
}
|
||||
|
||||
public static class Builder {
|
||||
|
||||
/**
|
||||
* @see InstantiateVAppTemplateOptions#cpuCount(int)
|
||||
*/
|
||||
public static InstantiateVAppTemplateOptions cpuCount(int cpuCount) {
|
||||
InstantiateVAppTemplateOptions options = new InstantiateVAppTemplateOptions();
|
||||
return options.cpuCount(cpuCount);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see InstantiateVAppTemplateOptions#megabytes(int)
|
||||
*/
|
||||
public static InstantiateVAppTemplateOptions megabytes(int megabytes) {
|
||||
InstantiateVAppTemplateOptions options = new InstantiateVAppTemplateOptions();
|
||||
return options.megabytes(megabytes);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see InstantiateVAppTemplateOptions#inNetwork(URI)
|
||||
*/
|
||||
public static InstantiateVAppTemplateOptions inNetwork(URI networkLocation) {
|
||||
InstantiateVAppTemplateOptions options = new InstantiateVAppTemplateOptions();
|
||||
return options.inNetwork(networkLocation);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,10 +23,12 @@
|
|||
*/
|
||||
package org.jclouds.vcloud.terremark;
|
||||
|
||||
import static org.jclouds.vcloud.terremark.options.InstantiateVAppTemplateOptions.Builder.cpuCount;
|
||||
import static org.testng.Assert.assertEquals;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.lang.reflect.Array;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.URI;
|
||||
|
||||
|
@ -47,6 +49,7 @@ import org.jclouds.vcloud.endpoints.Network;
|
|||
import org.jclouds.vcloud.endpoints.VDC;
|
||||
import org.jclouds.vcloud.endpoints.internal.CatalogItemRoot;
|
||||
import org.jclouds.vcloud.filters.SetVCloudTokenCookie;
|
||||
import org.jclouds.vcloud.terremark.options.InstantiateVAppTemplateOptions;
|
||||
import org.jclouds.vcloud.terremark.xml.TerremarkVAppHandler;
|
||||
import org.jclouds.vcloud.terremark.xml.TerremarkVDCHandler;
|
||||
import org.testng.annotations.Test;
|
||||
|
@ -61,7 +64,7 @@ import com.google.inject.TypeLiteral;
|
|||
*
|
||||
* @author Adrian Cole
|
||||
*/
|
||||
@Test(groups = "unit", testName = "vcloud.TerremarkVCloudClientTest")
|
||||
@Test(groups = "unit", sequential = true, testName = "vcloud.TerremarkVCloudClientTest")
|
||||
public class TerremarkVCloudClientTest extends RestClientTest<TerremarkVCloudClient> {
|
||||
|
||||
public void testGetDefaultVDC() throws SecurityException, NoSuchMethodException, IOException {
|
||||
|
@ -82,7 +85,8 @@ public class TerremarkVCloudClientTest extends RestClientTest<TerremarkVCloudCli
|
|||
public void testInstantiateVAppTemplate() throws SecurityException, NoSuchMethodException,
|
||||
IOException {
|
||||
Method method = TerremarkVCloudClient.class.getMethod("instantiateVAppTemplate",
|
||||
String.class, int.class);
|
||||
String.class, int.class, Array.newInstance(InstantiateVAppTemplateOptions.class, 0)
|
||||
.getClass());
|
||||
GeneratedHttpRequest<TerremarkVCloudClient> httpMethod = processor.createRequest(method,
|
||||
"name", 3);
|
||||
|
||||
|
@ -100,6 +104,28 @@ public class TerremarkVCloudClientTest extends RestClientTest<TerremarkVCloudCli
|
|||
checkFilters(httpMethod);
|
||||
}
|
||||
|
||||
public void testInstantiateVAppTemplateOptions() throws SecurityException,
|
||||
NoSuchMethodException, IOException {
|
||||
Method method = TerremarkVCloudClient.class.getMethod("instantiateVAppTemplate",
|
||||
String.class, int.class, Array.newInstance(InstantiateVAppTemplateOptions.class, 0)
|
||||
.getClass());
|
||||
GeneratedHttpRequest<TerremarkVCloudClient> httpMethod = processor.createRequest(method,
|
||||
"name", 3, cpuCount(4).megabytes(1024).inNetwork(URI.create("http://newnet")));
|
||||
|
||||
assertRequestLineEquals(httpMethod, "POST http://vdc/action/instantiatevAppTemplate HTTP/1.1");
|
||||
assertHeadersEqual(
|
||||
httpMethod,
|
||||
"Content-Length: 2247\nContent-Type: application/vnd.vmware.vcloud.instantiateVAppTemplateParams+xml\n");
|
||||
assertEntityEquals(httpMethod, IOUtils.toString(getClass().getResourceAsStream(
|
||||
"/terremark/InstantiateVAppTemplateParams-options-test.xml")));
|
||||
|
||||
assertResponseParserClassEquals(method, httpMethod, ParseSax.class);
|
||||
assertSaxResponseParserClassEquals(method, TerremarkVAppHandler.class);
|
||||
assertExceptionParserClassEquals(method, null);
|
||||
|
||||
checkFilters(httpMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void checkFilters(GeneratedHttpRequest<TerremarkVCloudClient> httpMethod) {
|
||||
assertEquals(httpMethod.getFilters().size(), 1);
|
||||
|
|
|
@ -0,0 +1,69 @@
|
|||
package org.jclouds.vcloud.terremark.options;
|
||||
|
||||
import static org.jclouds.vcloud.terremark.options.InstantiateVAppTemplateOptions.Builder.cpuCount;
|
||||
import static org.jclouds.vcloud.terremark.options.InstantiateVAppTemplateOptions.Builder.inNetwork;
|
||||
import static org.jclouds.vcloud.terremark.options.InstantiateVAppTemplateOptions.Builder.megabytes;
|
||||
import static org.testng.Assert.assertEquals;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import org.jclouds.http.functions.config.ParserModule;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import com.google.inject.Guice;
|
||||
import com.google.inject.Injector;
|
||||
|
||||
/**
|
||||
* Tests behavior of {@code InstantiateVAppTemplateOptions}
|
||||
*
|
||||
* @author Adrian Cole
|
||||
*/
|
||||
@Test(groups = "unit", testName = "vcloud.InstantiateVAppTemplateOptionsTest")
|
||||
public class InstantiateVAppTemplateOptionsTest {
|
||||
|
||||
Injector injector = Guice.createInjector(new ParserModule());
|
||||
|
||||
@Test
|
||||
public void testInNetwork() {
|
||||
InstantiateVAppTemplateOptions options = new InstantiateVAppTemplateOptions();
|
||||
options.inNetwork(URI.create("http://localhost"));
|
||||
assertEquals(options.network, "http://localhost");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInNetworkStatic() {
|
||||
InstantiateVAppTemplateOptions options = inNetwork(URI.create("http://localhost"));
|
||||
assertEquals(options.network, "http://localhost");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCpuCount() {
|
||||
InstantiateVAppTemplateOptions options = new InstantiateVAppTemplateOptions();
|
||||
options.cpuCount(3);
|
||||
assertEquals(options.cpuCount, "3");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCpuCountStatic() {
|
||||
InstantiateVAppTemplateOptions options = cpuCount(3);
|
||||
assertEquals(options.cpuCount, "3");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMegabytes() {
|
||||
InstantiateVAppTemplateOptions options = new InstantiateVAppTemplateOptions();
|
||||
options.megabytes(512);
|
||||
assertEquals(options.megabytes, "512");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMegabytesStatic() {
|
||||
InstantiateVAppTemplateOptions options = megabytes(512);
|
||||
assertEquals(options.megabytes, "512");
|
||||
}
|
||||
|
||||
@Test(expectedExceptions = IllegalArgumentException.class)
|
||||
public void testMegabytesStaticWrong() {
|
||||
megabytes(511);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
<InstantiateVAppTemplateParams name="name"
|
||||
xml:lang="en" xmlns="http://www.vmware.com/vcloud/v1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
<VAppTemplate href="http://catalogItem/3" />
|
||||
<InstantiationParams>
|
||||
<ProductSection xmlns:q1="http://www.vmware.com/vcloud/v1"
|
||||
xmlns:ovf="http://schemas.dmtf.org/ovf/envelope/1">
|
||||
<Property xmlns="http://schemas.dmtf.org/ovf/envelope/1"
|
||||
ovf:key="password" ovf:value="secretPassword" />
|
||||
<Property xmlns="http://schemas.dmtf.org/ovf/envelope/1"
|
||||
ovf:key="row" ovf:value="Row1" />
|
||||
<Property xmlns="http://schemas.dmtf.org/ovf/envelope/1"
|
||||
ovf:key="group" ovf:value="Group1" />
|
||||
</ProductSection>
|
||||
<VirtualHardwareSection xmlns:q1="http://www.vmware.com/vcloud/v1">
|
||||
<Item xmlns="http://schemas.dmtf.org/ovf/envelope/1">
|
||||
<InstanceID
|
||||
xmlns="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData">1</InstanceID>
|
||||
<ResourceType
|
||||
xmlns="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData">3</ResourceType>
|
||||
<VirtualQuantity
|
||||
xmlns="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData">4</VirtualQuantity>
|
||||
</Item>
|
||||
<Item xmlns="http://schemas.dmtf.org/ovf/envelope/1">
|
||||
<InstanceID
|
||||
xmlns="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData">2</InstanceID>
|
||||
<ResourceType
|
||||
xmlns="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData">4</ResourceType>
|
||||
<VirtualQuantity
|
||||
xmlns="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData">1024</VirtualQuantity>
|
||||
</Item>
|
||||
</VirtualHardwareSection>
|
||||
<NetworkConfigSection>
|
||||
<NetworkConfig>
|
||||
<NetworkAssociation href="http://newnet" />
|
||||
</NetworkConfig>
|
||||
</NetworkConfigSection>
|
||||
</InstantiationParams>
|
||||
</InstantiateVAppTemplateParams>
|
Loading…
Reference in New Issue