Issue 112: support instantiation

git-svn-id: http://jclouds.googlecode.com/svn/trunk@2245 3d8758e0-26b5-11de-8745-db77d3ebf521
This commit is contained in:
adrian.f.cole 2009-11-10 02:12:06 +00:00
parent 7f73d1761a
commit 37d5d4f071
11 changed files with 330 additions and 17 deletions

View File

@ -32,6 +32,8 @@ import org.jclouds.vcloud.domain.Task;
import org.jclouds.vcloud.domain.TaskStatus;
import org.joda.time.DateTime;
import com.google.inject.internal.Nullable;
/**
* Locations of resources in vCloud
*
@ -43,19 +45,21 @@ public class TaskImpl implements Task {
private final URI location;
private final TaskStatus status;
private final DateTime startTime;
@Nullable
private final DateTime endTime;
private final Link owner;
@Nullable
private final Link result;
public TaskImpl(String type, URI location, TaskStatus status, DateTime startTime,
DateTime endTime, Link owner, Link result) {
@Nullable DateTime endTime, Link owner, @Nullable Link result) {
this.type = checkNotNull(type, "type");
this.location = checkNotNull(location, "location");
this.status = checkNotNull(status, "status");
this.startTime = checkNotNull(startTime, "startTime");
this.endTime = checkNotNull(endTime, "endTime");
this.endTime = endTime;
this.owner = checkNotNull(owner, "owner");
this.result = checkNotNull(result, "result");
this.result = result;
}
public TaskStatus getStatus() {

View File

@ -25,17 +25,24 @@ package org.jclouds.vcloud.terremark;
import static org.jclouds.vcloud.VCloudMediaType.VDC_XML;
import java.net.URI;
import java.util.concurrent.Future;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import org.jclouds.rest.annotations.Endpoint;
import org.jclouds.rest.annotations.MapBinder;
import org.jclouds.rest.annotations.MapEntityParam;
import org.jclouds.rest.annotations.RequestFilters;
import org.jclouds.rest.annotations.XMLResponseParser;
import org.jclouds.vcloud.VCloudClient;
import org.jclouds.vcloud.domain.VDC;
import org.jclouds.vcloud.filters.SetVCloudTokenCookie;
import org.jclouds.vcloud.terremark.binders.BindInstantiateVAppTemplateParamsToXmlEntity;
import org.jclouds.vcloud.terremark.xml.TerremarkVDCHandler;
/**
@ -48,20 +55,19 @@ import org.jclouds.vcloud.terremark.xml.TerremarkVDCHandler;
@RequestFilters(SetVCloudTokenCookie.class)
public interface TerremarkVCloudClient extends VCloudClient {
@GET
@Endpoint(org.jclouds.vcloud.endpoints.VDC.class)
@XMLResponseParser(TerremarkVDCHandler.class)
@Consumes(VDC_XML)
Future<? extends VDC> getDefaultVDC();
//
// @GET
// @Endpoint(vDC.class)
// public Set<String> getvDCs();
//
// @GET
// @Endpoint(TasksList.class)
// public Set<String> getTasksLists();
@POST
@Endpoint(org.jclouds.vcloud.endpoints.VDC.class)
@Path("/action/instantiatevAppTemplate")
@Produces("application/vnd.vmware.vcloud.instantiateVAppTemplateParams+xml")
@MapBinder(BindInstantiateVAppTemplateParamsToXmlEntity.class)
String instantiateVAppTemplate(@MapEntityParam("name") String appName,
@MapEntityParam("template") URI vAppTemplate, @MapEntityParam("count") int cpuCount,
@MapEntityParam("megabytes") int megabytesMemory, @MapEntityParam("network") URI network);
}

View File

@ -0,0 +1,51 @@
package org.jclouds.vcloud.terremark.binders;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Map;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import org.jclouds.http.HttpRequest;
import org.jclouds.rest.MapBinder;
import org.jclouds.rest.binders.BindToStringEntity;
/**
*
* @author Adrian Cole
*
*/
@Singleton
public class BindInstantiateVAppTemplateParamsToXmlEntity implements MapBinder {
@Inject
@Named("InstantiateVAppTemplateParams")
String xmlTemplate;
@Inject
BindToStringEntity stringBinder;
public void bindToRequest(HttpRequest request, Map<String, String> postParams) {
String name = checkNotNull(postParams.get("name"), "name parameter not present");
String template = checkNotNull(postParams.get("template"), "template parameter not present");
String count = checkNotNull(postParams.get("count"), "count parameter not present");
String megabytes = checkNotNull(postParams.get("megabytes"),
"megabytes parameter not present");
String network = checkNotNull(postParams.get("network"), "network parameter not present");
String entity = xmlTemplate.replaceAll("\\{name\\}", name);
entity = entity.replaceAll("\\{template\\}", template);
entity = entity.replaceAll("\\{count\\}", count);
entity = entity.replaceAll("\\{megabytes\\}", megabytes);
entity = entity.replaceAll("\\{network\\}", network);
stringBinder.bindToRequest(request, entity);
}
public void bindToRequest(HttpRequest request, Object input) {
throw new IllegalStateException("InstantiateVAppTemplateParams is needs parameters");
}
}

View File

@ -23,11 +23,16 @@
*/
package org.jclouds.vcloud.terremark.config;
import java.io.IOException;
import java.io.InputStream;
import javax.inject.Named;
import javax.inject.Singleton;
import org.jclouds.http.RequiresHttp;
import org.jclouds.rest.ConfiguresRestClient;
import org.jclouds.rest.RestClientFactory;
import org.jclouds.util.Utils;
import org.jclouds.vcloud.VCloudClient;
import org.jclouds.vcloud.config.VCloudRestClientModule;
import org.jclouds.vcloud.terremark.TerremarkVCloudClient;
@ -54,4 +59,13 @@ public class TerremarkVCloudRestClientModule extends VCloudRestClientModule {
return factory.create(TerremarkVCloudClient.class);
}
@Singleton
@Provides
@Named("InstantiateVAppTemplateParams")
String provideInstantiateVAppTemplateParams() throws IOException {
InputStream is = getClass().getResourceAsStream(
"/terremark/InstantiateVAppTemplateParams.xml");
return Utils.toStringAndClose(is);
}
}

View File

@ -78,8 +78,9 @@ public class TasksListHandler extends ParseSax.HandlerWithResult<TasksList> {
status = TaskStatus.fromValue(attributes.getValue(attributes.getIndex("status")));
startTime = dateService.iso8601DateParse(attributes.getValue(attributes
.getIndex("startTime")));
endTime = dateService
.iso8601DateParse(attributes.getValue(attributes.getIndex("endTime")));
if (attributes.getIndex("endTime") != -1)
endTime = dateService.iso8601DateParse(attributes.getValue(attributes
.getIndex("endTime")));
} else if (qName.equals("Owner")) {
owner = Utils.newLink(attributes);
} else if (qName.equals("Result")) {

View File

@ -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="{template}" />
<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">{count}</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">{megabytes}</VirtualQuantity>
</Item>
</VirtualHardwareSection>
<NetworkConfigSection>
<NetworkConfig>
<NetworkAssociation href="{network}" />
</NetworkConfig>
</NetworkConfigSection>
</InstantiationParams>
</InstantiateVAppTemplateParams>

View File

@ -26,7 +26,10 @@ package org.jclouds.vcloud.terremark;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.testng.Assert.assertNotNull;
import java.net.URI;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.jclouds.logging.log4j.config.Log4JLoggingModule;
import org.jclouds.vcloud.VCloudClientLiveTest;
@ -41,24 +44,43 @@ import org.testng.annotations.Test;
*/
@Test(groups = "live", sequential = true, testName = "vcloud.TerremarkVCloudClientLiveTest")
public class TerremarkVCloudClientLiveTest extends VCloudClientLiveTest {
TerremarkVCloudClient tmClient;
public static final String PREFIX = System.getProperty("user.name") + "-terremark";
@Test
public void testDefaultVDC() throws Exception {
super.testDefaultVDC();
TerremarkVDC response = (TerremarkVDC) connection.getDefaultVDC().get(10, TimeUnit.SECONDS);
TerremarkVDC response = (TerremarkVDC) tmClient.getDefaultVDC().get(10, TimeUnit.SECONDS);
assertNotNull(response);
assertNotNull(response.getCatalog());
assertNotNull(response.getInternetServices());
assertNotNull(response.getPublicIps());
}
@Test(enabled = false)
// disabled until stop functionality is added
public void testInstantiate() throws InterruptedException, ExecutionException, TimeoutException {
URI template = tmClient.getCatalog().get(10, TimeUnit.SECONDS).get(
"Ubuntu Server 9.04 (32-bit)").getLocation();
URI network = tmClient.getDefaultVDC().get(10, TimeUnit.SECONDS).getAvailableNetworks()
.values().iterator().next().getLocation();
String response = tmClient.instantiateVAppTemplate("adriantest", template, 1, 512, network);
System.out.println(response);
}
@BeforeGroups(groups = { "live" })
@Override
public void setupClient() {
account = checkNotNull(System.getProperty("jclouds.test.user"), "jclouds.test.user");
String key = checkNotNull(System.getProperty("jclouds.test.key"), "jclouds.test.key");
connection = new TerremarkVCloudContextBuilder(new TerremarkVCloudPropertiesBuilder(account,
key).build()).withModules(new Log4JLoggingModule()).buildContext().getApi();
connection = tmClient = new TerremarkVCloudContextBuilder(
new TerremarkVCloudPropertiesBuilder(account, key).build()).withModules(
new Log4JLoggingModule()).buildContext().getApi();
}
}

View File

@ -26,17 +26,23 @@ package org.jclouds.vcloud.terremark;
import static org.testng.Assert.assertEquals;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.net.URI;
import javax.inject.Named;
import javax.inject.Provider;
import javax.inject.Singleton;
import org.apache.commons.io.IOUtils;
import org.jclouds.http.functions.ParseSax;
import org.jclouds.http.functions.ReturnStringIf200;
import org.jclouds.logging.Logger;
import org.jclouds.logging.Logger.LoggerFactory;
import org.jclouds.rest.RestClientTest;
import org.jclouds.rest.internal.GeneratedHttpRequest;
import org.jclouds.rest.internal.RestAnnotationProcessor;
import org.jclouds.util.Utils;
import org.jclouds.vcloud.endpoints.Catalog;
import org.jclouds.vcloud.endpoints.VDC;
import org.jclouds.vcloud.filters.SetVCloudTokenCookie;
@ -45,6 +51,7 @@ import org.testng.annotations.Test;
import com.google.inject.AbstractModule;
import com.google.inject.Module;
import com.google.inject.Provides;
import com.google.inject.TypeLiteral;
/**
@ -70,6 +77,30 @@ public class TerremarkVCloudClientTest extends RestClientTest<TerremarkVCloudCli
checkFilters(httpMethod);
}
public void testInstantiateVAppTemplate() throws SecurityException, NoSuchMethodException,
IOException {
Method method = TerremarkVCloudClient.class.getMethod("instantiateVAppTemplate",
String.class, URI.class, int.class, int.class, URI.class
);
GeneratedHttpRequest<TerremarkVCloudClient> httpMethod = processor.createRequest(method,
"name", URI.create("http://template"), 1, 512, URI.create("http://network"));
assertRequestLineEquals(httpMethod, "POST http://vdc/action/instantiatevAppTemplate HTTP/1.1");
assertHeadersEqual(
httpMethod,
"Content-Length: 2242\nContent-Type: application/vnd.vmware.vcloud.instantiateVAppTemplateParams+xml\n");
assertEntityEquals(httpMethod, IOUtils.toString(getClass().getResourceAsStream(
"/terremark/InstantiateVAppTemplateParams-test.xml")));
assertResponseParserClassEquals(method, httpMethod, ReturnStringIf200.class);
assertSaxResponseParserClassEquals(method, null);
assertExceptionParserClassEquals(method, null);
checkFilters(httpMethod);
}
@Override
protected void checkFilters(GeneratedHttpRequest<TerremarkVCloudClient> httpMethod) {
assertEquals(httpMethod.getFilters().size(), 1);
@ -105,6 +136,15 @@ public class TerremarkVCloudClientTest extends RestClientTest<TerremarkVCloudCli
});
}
@SuppressWarnings("unused")
@Singleton
@Provides
@Named("InstantiateVAppTemplateParams")
String provideInstantiateVAppTemplateParams() throws IOException {
InputStream is = getClass().getResourceAsStream(
"/terremark/InstantiateVAppTemplateParams.xml");
return Utils.toStringAndClose(is);
}
};
}

View File

@ -0,0 +1,91 @@
/**
*
* Copyright (C) 2009 Cloud Conscious,"LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF 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.vcloud.terremark.binders;
import static org.testng.Assert.assertEquals;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.Map;
import javax.inject.Named;
import javax.inject.Singleton;
import javax.ws.rs.core.HttpHeaders;
import org.apache.commons.io.IOUtils;
import org.jclouds.http.HttpRequest;
import org.jclouds.util.Utils;
import org.testng.annotations.Test;
import com.google.common.collect.Maps;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Provides;
/**
* Tests behavior of {@code BindInstantiateVAppTemplateParamsToXmlEntity}
*
* @author Adrian Cole
*/
@Test(groups = "unit", testName = "vcloud.BindInstantiateVAppTemplateParamsToXmlEntityTest")
public class BindInstantiateVAppTemplateParamsToXmlEntityTest {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
}
@SuppressWarnings("unused")
@Singleton
@Provides
@Named("InstantiateVAppTemplateParams")
String provideInstantiateVAppTemplateParams() throws IOException {
InputStream is = getClass().getResourceAsStream(
"/terremark/InstantiateVAppTemplateParams.xml");
return Utils.toStringAndClose(is);
}
});
public void testApplyInputStream() throws IOException {
String expected = IOUtils.toString(getClass().getResourceAsStream(
"/terremark/InstantiateVAppTemplateParams-test.xml"));
HttpRequest request = new HttpRequest("GET", URI.create("http://test"));
BindInstantiateVAppTemplateParamsToXmlEntity binder = injector
.getInstance(BindInstantiateVAppTemplateParamsToXmlEntity.class);
Map<String, String> map = Maps.newHashMap();
map.put("name", "name");
map.put("template", "http://template");
map.put("count", "1");
map.put("megabytes", "512");
map.put("network", "http://network");
binder.bindToRequest(request, map);
assertEquals(request.getFirstHeaderOrNull(HttpHeaders.CONTENT_TYPE), "application/unknown");
assertEquals(request.getFirstHeaderOrNull(HttpHeaders.CONTENT_LENGTH), "2242");
assertEquals(request.getEntity(), expected);
}
}

View File

@ -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://template" />
<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">1</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">512</VirtualQuantity>
</Item>
</VirtualHardwareSection>
<NetworkConfigSection>
<NetworkConfig>
<NetworkAssociation href="http://network" />
</NetworkConfig>
</NetworkConfigSection>
</InstantiationParams>
</InstantiateVAppTemplateParams>

View File

@ -0,0 +1,8 @@
<VApp href="https://services.vcloudexpress.terremark.com/api/v0.8/vapp/13775"
type="application/vnd.vmware.vcloud.vApp+xml" name="adriantest"
status="0" size="4" xmlns="http://www.vmware.com/vcloud/v1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Link rel="up"
href="https://services.vcloudexpress.terremark.com/api/v0.8/vdc/32"
type="application/vnd.vmware.vcloud.vdc+xml" />
</VApp>