diff --git a/vcloud/core/src/main/java/org/jclouds/vcloud/config/VCloudRestClientModule.java b/vcloud/core/src/main/java/org/jclouds/vcloud/config/VCloudRestClientModule.java index 9e42fb816b..fab0d5ae58 100644 --- a/vcloud/core/src/main/java/org/jclouds/vcloud/config/VCloudRestClientModule.java +++ b/vcloud/core/src/main/java/org/jclouds/vcloud/config/VCloudRestClientModule.java @@ -24,6 +24,9 @@ package org.jclouds.vcloud.config; import java.net.URI; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; import javax.inject.Singleton; @@ -33,8 +36,11 @@ import org.jclouds.rest.RestClientFactory; import org.jclouds.vcloud.VCloudClient; import org.jclouds.vcloud.VCloudDiscovery; import org.jclouds.vcloud.endpoints.Catalog; +import org.jclouds.vcloud.endpoints.Network; import org.jclouds.vcloud.endpoints.TasksList; +import org.jclouds.vcloud.endpoints.VCloud; import org.jclouds.vcloud.endpoints.VDC; +import org.jclouds.vcloud.endpoints.internal.CatalogItemRoot; import com.google.inject.AbstractModule; import com.google.inject.Provides; @@ -65,6 +71,15 @@ public class VCloudRestClientModule extends AbstractModule { return discovery.getOrganization().getCatalog().getLocation(); } + @Provides + @CatalogItemRoot + @Singleton + String provideCatalogItemRoot(@VCloud URI vcloudUri) { + return vcloudUri.toASCIIString()+"/catalogItem"; + } + + + @Provides @VDC @Singleton @@ -72,6 +87,15 @@ public class VCloudRestClientModule extends AbstractModule { return discovery.getOrganization().getVDCs().values().iterator().next().getLocation(); } + @Provides + @Network + @Singleton + protected URI provideDefaultNetwork(VCloudClient client) throws InterruptedException, + ExecutionException, TimeoutException { + return client.getDefaultVDC().get(45, TimeUnit.SECONDS).getAvailableNetworks().values() + .iterator().next().getLocation(); + } + @Provides @TasksList @Singleton diff --git a/vcloud/core/src/main/java/org/jclouds/vcloud/domain/Catalog.java b/vcloud/core/src/main/java/org/jclouds/vcloud/domain/Catalog.java index ab69446f28..a0d863e8f5 100644 --- a/vcloud/core/src/main/java/org/jclouds/vcloud/domain/Catalog.java +++ b/vcloud/core/src/main/java/org/jclouds/vcloud/domain/Catalog.java @@ -35,6 +35,6 @@ import com.google.inject.ImplementedBy; */ @org.jclouds.vcloud.endpoints.Catalog @ImplementedBy(CatalogImpl.class) -public interface Catalog extends NamedLink, Map { +public interface Catalog extends NamedLink, Map { } \ No newline at end of file diff --git a/vcloud/core/src/main/java/org/jclouds/vcloud/domain/NamedResource.java b/vcloud/core/src/main/java/org/jclouds/vcloud/domain/NamedResource.java new file mode 100644 index 0000000000..677e74c4e4 --- /dev/null +++ b/vcloud/core/src/main/java/org/jclouds/vcloud/domain/NamedResource.java @@ -0,0 +1,17 @@ +package org.jclouds.vcloud.domain; + +import org.jclouds.rest.domain.NamedLink; +import org.jclouds.vcloud.domain.internal.NamedResourceImpl; + +import com.google.inject.ImplementedBy; + +/** + * Location of a Rest resource + * + * @author Adrian Cole + * + */ +@ImplementedBy(NamedResourceImpl.class) +public interface NamedResource extends NamedLink, Comparable { + int getId(); +} \ No newline at end of file diff --git a/vcloud/core/src/main/java/org/jclouds/vcloud/domain/internal/CatalogImpl.java b/vcloud/core/src/main/java/org/jclouds/vcloud/domain/internal/CatalogImpl.java index 21d5ee1f9c..56ac3a8b56 100644 --- a/vcloud/core/src/main/java/org/jclouds/vcloud/domain/internal/CatalogImpl.java +++ b/vcloud/core/src/main/java/org/jclouds/vcloud/domain/internal/CatalogImpl.java @@ -26,12 +26,13 @@ package org.jclouds.vcloud.domain.internal; import static com.google.common.base.Preconditions.checkNotNull; import java.net.URI; -import java.util.HashMap; -import java.util.Map; +import java.util.SortedMap; +import java.util.TreeMap; import org.jclouds.rest.domain.NamedLink; import org.jclouds.rest.domain.internal.NamedLinkImpl; import org.jclouds.vcloud.domain.Catalog; +import org.jclouds.vcloud.domain.NamedResource; /** * Locations of resources in vCloud @@ -39,14 +40,15 @@ import org.jclouds.vcloud.domain.Catalog; * @author Adrian Cole * */ -public class CatalogImpl extends HashMap implements Catalog { +public class CatalogImpl extends TreeMap implements Catalog { /** The serialVersionUID */ private static final long serialVersionUID = 8464716396538298809L; private final NamedLink catalog; - public CatalogImpl(String name, String type, URI location, Map contents) { - super(contents.size()); + public CatalogImpl(String name, String type, URI location, + SortedMap contents) { + super(); this.catalog = new NamedLinkImpl(checkNotNull(name, "name"), checkNotNull(type, "type"), checkNotNull(location, "location")); putAll(checkNotNull(contents, "contents")); diff --git a/vcloud/core/src/main/java/org/jclouds/vcloud/domain/internal/NamedResourceImpl.java b/vcloud/core/src/main/java/org/jclouds/vcloud/domain/internal/NamedResourceImpl.java new file mode 100644 index 0000000000..d054677a91 --- /dev/null +++ b/vcloud/core/src/main/java/org/jclouds/vcloud/domain/internal/NamedResourceImpl.java @@ -0,0 +1,63 @@ +package org.jclouds.vcloud.domain.internal; + +import java.net.URI; + +import org.jclouds.rest.domain.internal.NamedLinkImpl; +import org.jclouds.vcloud.domain.NamedResource; + +/** + * Location of a Rest resource + * + * @author Adrian Cole + * + */ +public class NamedResourceImpl extends NamedLinkImpl implements NamedResource { + private final int id; + + public NamedResourceImpl(int id, String name, String type, URI location) { + super(name, type, location); + this.id = id; + } + + public int getId() { + return id; + } + + public int compareTo(NamedResource that) { + final int BEFORE = -1; + final int EQUAL = 0; + final int AFTER = 1; + + if (this == that) + return EQUAL; + + if (this.id < that.getId()) + return BEFORE; + if (this.id > that.getId()) + return AFTER; + return EQUAL; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = super.hashCode(); + result = prime * result + id; + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (!super.equals(obj)) + return false; + if (getClass() != obj.getClass()) + return false; + NamedResourceImpl other = (NamedResourceImpl) obj; + if (id != other.id) + return false; + return true; + } + +} \ No newline at end of file diff --git a/vcloud/core/src/main/java/org/jclouds/vcloud/domain/internal/TaskImpl.java b/vcloud/core/src/main/java/org/jclouds/vcloud/domain/internal/TaskImpl.java index 763a19d70d..67c98b1da6 100644 --- a/vcloud/core/src/main/java/org/jclouds/vcloud/domain/internal/TaskImpl.java +++ b/vcloud/core/src/main/java/org/jclouds/vcloud/domain/internal/TaskImpl.java @@ -50,7 +50,7 @@ public class TaskImpl extends LinkImpl implements Task { @Nullable private final NamedLink result; - public TaskImpl(String type, URI location, TaskStatus status, DateTime startTime, + public TaskImpl(String type, URI location, TaskStatus status, DateTime startTime, @Nullable DateTime endTime, NamedLink owner, @Nullable NamedLink result) { super(type, location); this.status = checkNotNull(status, "status"); diff --git a/vcloud/core/src/main/java/org/jclouds/vcloud/endpoints/Network.java b/vcloud/core/src/main/java/org/jclouds/vcloud/endpoints/Network.java new file mode 100644 index 0000000000..b7ba09d904 --- /dev/null +++ b/vcloud/core/src/main/java/org/jclouds/vcloud/endpoints/Network.java @@ -0,0 +1,44 @@ +/** + * + * Copyright (C) 2009 Cloud Conscious, LLC. + * + * ==================================================================== + * 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.endpoints; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import javax.inject.Qualifier; + +/** + * Related to a VCloud Network. + * + * @author Adrian Cole + * + */ +@Retention(value = RetentionPolicy.RUNTIME) +@Target(value = { ElementType.TYPE, ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD }) +@Qualifier +public @interface Network { + +} \ No newline at end of file diff --git a/vcloud/core/src/main/java/org/jclouds/vcloud/endpoints/internal/CatalogItemRoot.java b/vcloud/core/src/main/java/org/jclouds/vcloud/endpoints/internal/CatalogItemRoot.java new file mode 100644 index 0000000000..87061208c8 --- /dev/null +++ b/vcloud/core/src/main/java/org/jclouds/vcloud/endpoints/internal/CatalogItemRoot.java @@ -0,0 +1,44 @@ +/** + * + * Copyright (C) 2009 Cloud Conscious, LLC. + * + * ==================================================================== + * 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.endpoints.internal; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import javax.inject.Qualifier; + +/** + * Root path where all catalog items exist. + * + * @author Adrian Cole + * + */ +@Retention(value = RetentionPolicy.RUNTIME) +@Target(value = { ElementType.TYPE, ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD }) +@Qualifier +public @interface CatalogItemRoot { + +} \ No newline at end of file diff --git a/vcloud/core/src/main/java/org/jclouds/vcloud/functions/CatalogIdToUri.java b/vcloud/core/src/main/java/org/jclouds/vcloud/functions/CatalogIdToUri.java new file mode 100644 index 0000000000..56dc2e5c19 --- /dev/null +++ b/vcloud/core/src/main/java/org/jclouds/vcloud/functions/CatalogIdToUri.java @@ -0,0 +1,29 @@ +package org.jclouds.vcloud.functions; + +import static com.google.common.base.Preconditions.checkArgument; +import static com.google.common.base.Preconditions.checkNotNull; + +import javax.inject.Inject; +import javax.inject.Singleton; + +import org.jclouds.vcloud.endpoints.internal.CatalogItemRoot; + +import com.google.common.base.Function; + +/** + * @author Adrian Cole + */ +@Singleton +public class CatalogIdToUri implements Function { + @Inject + @CatalogItemRoot + private String catalogItemRoot; + + @Override + public String apply(Object from) { + checkArgument(checkNotNull(from, "from") instanceof Integer, + "this binder is only valid for Integers!"); + return String.format("%s/%d",catalogItemRoot,from); + } + +} \ No newline at end of file diff --git a/vcloud/core/src/main/java/org/jclouds/vcloud/terremark/TerremarkVCloudClient.java b/vcloud/core/src/main/java/org/jclouds/vcloud/terremark/TerremarkVCloudClient.java index 1203e47c42..809b8975e7 100644 --- a/vcloud/core/src/main/java/org/jclouds/vcloud/terremark/TerremarkVCloudClient.java +++ b/vcloud/core/src/main/java/org/jclouds/vcloud/terremark/TerremarkVCloudClient.java @@ -25,7 +25,6 @@ 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; @@ -37,13 +36,15 @@ 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.ParamParser; 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.functions.CatalogIdToUri; import org.jclouds.vcloud.terremark.domain.VApp; +import org.jclouds.vcloud.terremark.options.InstantiateVAppTemplateOptions; import org.jclouds.vcloud.terremark.xml.TerremarkVAppHandler; import org.jclouds.vcloud.terremark.xml.TerremarkVDCHandler; @@ -68,9 +69,8 @@ public interface TerremarkVCloudClient extends VCloudClient { @Path("/action/instantiatevAppTemplate") @Produces("application/vnd.vmware.vcloud.instantiateVAppTemplateParams+xml") @XMLResponseParser(TerremarkVAppHandler.class) - @MapBinder(BindInstantiateVAppTemplateParamsToXmlEntity.class) + @MapBinder(InstantiateVAppTemplateOptions.class) Future instantiateVAppTemplate(@MapEntityParam("name") String appName, - @MapEntityParam("template") URI vAppTemplate, @MapEntityParam("count") int cpuCount, - @MapEntityParam("megabytes") int megabytesMemory, @MapEntityParam("network") URI network); + @MapEntityParam("template") @ParamParser(CatalogIdToUri.class) int templateId); } diff --git a/vcloud/core/src/main/java/org/jclouds/vcloud/terremark/options/InstantiateVAppTemplateOptions.java b/vcloud/core/src/main/java/org/jclouds/vcloud/terremark/options/InstantiateVAppTemplateOptions.java new file mode 100644 index 0000000000..b36b26862a --- /dev/null +++ b/vcloud/core/src/main/java/org/jclouds/vcloud/terremark/options/InstantiateVAppTemplateOptions.java @@ -0,0 +1,37 @@ +package org.jclouds.vcloud.terremark.options; + +import java.net.URI; +import java.util.Map; + +import javax.inject.Inject; + +import org.jclouds.http.HttpRequest; +import org.jclouds.vcloud.endpoints.Network; +import org.jclouds.vcloud.terremark.binders.BindInstantiateVAppTemplateParamsToXmlEntity; + +import com.google.common.collect.Maps; + +/** + * + * @author Adrian Cole + * + */ +public class InstantiateVAppTemplateOptions extends BindInstantiateVAppTemplateParamsToXmlEntity { + @Inject + @Network + private URI defaultNetwork; + + public void bindToRequest(HttpRequest request, Map postParams) { + Map 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()); + + super.bindToRequest(request, copy); + } + +} diff --git a/vcloud/core/src/main/java/org/jclouds/vcloud/xml/CatalogHandler.java b/vcloud/core/src/main/java/org/jclouds/vcloud/xml/CatalogHandler.java index 0403b7fd60..fcf54b6c91 100644 --- a/vcloud/core/src/main/java/org/jclouds/vcloud/xml/CatalogHandler.java +++ b/vcloud/core/src/main/java/org/jclouds/vcloud/xml/CatalogHandler.java @@ -23,16 +23,20 @@ */ package org.jclouds.vcloud.xml; -import static org.jclouds.rest.util.Utils.putNamedLink; - import java.net.URI; import java.util.Map; +import java.util.SortedMap; + +import javax.inject.Inject; import org.jclouds.http.functions.ParseSax; import org.jclouds.rest.domain.NamedLink; import org.jclouds.rest.domain.internal.NamedLinkImpl; import org.jclouds.vcloud.domain.Catalog; +import org.jclouds.vcloud.domain.NamedResource; import org.jclouds.vcloud.domain.internal.CatalogImpl; +import org.jclouds.vcloud.domain.internal.NamedResourceImpl; +import org.jclouds.vcloud.endpoints.internal.CatalogItemRoot; import org.xml.sax.Attributes; import org.xml.sax.SAXException; @@ -43,7 +47,10 @@ import com.google.common.collect.Maps; */ public class CatalogHandler extends ParseSax.HandlerWithResult { private NamedLink Catalog; - private Map contents = Maps.newHashMap(); + private SortedMap contents = Maps.newTreeMap(); + @Inject + @CatalogItemRoot + private String catalogItemRoot; public Catalog getResult() { return new CatalogImpl(Catalog.getName(), Catalog.getType(), Catalog.getLocation(), contents); @@ -57,8 +64,19 @@ public class CatalogHandler extends ParseSax.HandlerWithResult { .getValue(attributes.getIndex("type")), URI.create(attributes.getValue(attributes .getIndex("href")))); } else if (qName.equals("CatalogItem")) { - putNamedLink(contents, attributes); + putNamedResource(contents, attributes); } } + public NamedResource newNamedResource(Attributes attributes) { + return new NamedResourceImpl(Integer.parseInt(attributes + .getValue(attributes.getIndex("href")).replace(catalogItemRoot+"/", "")), attributes + .getValue(attributes.getIndex("name")), attributes.getValue(attributes + .getIndex("type")), URI.create(attributes.getValue(attributes.getIndex("href")))); + } + + public void putNamedResource(Map map, Attributes attributes) { + map.put(attributes.getValue(attributes.getIndex("name")), newNamedResource(attributes)); + } + } diff --git a/vcloud/core/src/main/java/org/jclouds/vcloud/xml/TaskHandler.java b/vcloud/core/src/main/java/org/jclouds/vcloud/xml/TaskHandler.java index 68cd501f45..0a4d3a409e 100644 --- a/vcloud/core/src/main/java/org/jclouds/vcloud/xml/TaskHandler.java +++ b/vcloud/core/src/main/java/org/jclouds/vcloud/xml/TaskHandler.java @@ -68,17 +68,9 @@ public class TaskHandler extends ParseSax.HandlerWithResult { if (qName.equals("Task")) { taskLink = Utils.newLink(attributes); status = TaskStatus.fromValue(attributes.getValue(attributes.getIndex("status"))); - startTime = dateService.iso8601DateParse(attributes.getValue(attributes - .getIndex("startTime"))); + startTime = parseDate(attributes, "startTime"); if (attributes.getIndex("endTime") != -1) { - try { - endTime = dateService.iso8601DateParse(attributes.getValue(attributes - .getIndex("endTime"))); - } catch (RuntimeException e) { - if (!(e.getCause() instanceof ParseException)) // TODO.. format doesn't parse - // endTime="2009-11-11T02:27:25Z" - throw e; - } + endTime = parseDate(attributes, "endTime"); } } else if (qName.equals("Owner")) { owner = Utils.newNamedLink(attributes); @@ -87,6 +79,20 @@ public class TaskHandler extends ParseSax.HandlerWithResult { } } + private DateTime parseDate(Attributes attributes, String attribute) { + try { + return dateService.iso8601DateParse(attributes.getValue(attributes.getIndex(attribute))); + + } catch (RuntimeException e) { + if (e.getCause() instanceof ParseException) { + return dateService.iso8601SecondsDateParse(attributes.getValue(attributes + .getIndex(attribute))); + } else { + throw e; + } + } + } + @Override public void endElement(String uri, String localName, String qName) throws SAXException { if (qName.equals("Task")) { diff --git a/vcloud/core/src/test/java/org/jclouds/vcloud/VCloudClientTest.java b/vcloud/core/src/test/java/org/jclouds/vcloud/VCloudClientTest.java index 40af8855be..56e9633c53 100644 --- a/vcloud/core/src/test/java/org/jclouds/vcloud/VCloudClientTest.java +++ b/vcloud/core/src/test/java/org/jclouds/vcloud/VCloudClientTest.java @@ -41,6 +41,7 @@ import org.jclouds.rest.internal.RestAnnotationProcessor; import org.jclouds.vcloud.endpoints.Catalog; import org.jclouds.vcloud.endpoints.TasksList; import org.jclouds.vcloud.endpoints.VDC; +import org.jclouds.vcloud.endpoints.internal.CatalogItemRoot; import org.jclouds.vcloud.filters.SetVCloudTokenCookie; import org.jclouds.vcloud.xml.CatalogHandler; import org.jclouds.vcloud.xml.TaskHandler; @@ -294,6 +295,8 @@ public class VCloudClientTest extends RestClientTest { @Override protected void configure() { bind(URI.class).annotatedWith(Catalog.class).toInstance(URI.create("http://catalog")); + bind(String.class).annotatedWith(CatalogItemRoot.class) + .toInstance("http://catalogItem"); bind(URI.class).annotatedWith(VDC.class).toInstance(URI.create("http://vdc")); bind(URI.class).annotatedWith(TasksList.class).toInstance( URI.create("http://tasksList")); diff --git a/vcloud/core/src/test/java/org/jclouds/vcloud/config/RestVCloudAuthenticationModuleTest.java b/vcloud/core/src/test/java/org/jclouds/vcloud/config/VCloudDiscoveryRestClientModuleTest.java similarity index 97% rename from vcloud/core/src/test/java/org/jclouds/vcloud/config/RestVCloudAuthenticationModuleTest.java rename to vcloud/core/src/test/java/org/jclouds/vcloud/config/VCloudDiscoveryRestClientModuleTest.java index c25bf1ec25..1a3ec66329 100644 --- a/vcloud/core/src/test/java/org/jclouds/vcloud/config/RestVCloudAuthenticationModuleTest.java +++ b/vcloud/core/src/test/java/org/jclouds/vcloud/config/VCloudDiscoveryRestClientModuleTest.java @@ -52,8 +52,8 @@ import com.google.inject.Injector; /** * @author Adrian Cole */ -@Test(groups = "unit", testName = "vcloud.RestVCloudAuthenticationModuleTest") -public class RestVCloudAuthenticationModuleTest { +@Test(groups = "unit", testName = "vcloud.VCloudDiscoveryRestClientModuleTest") +public class VCloudDiscoveryRestClientModuleTest { Injector createInjector() { return Guice.createInjector(new VCloudDiscoveryRestClientModule(), new ParserModule(), diff --git a/vcloud/core/src/test/java/org/jclouds/vcloud/config/VCloudRestClientModuleTest.java b/vcloud/core/src/test/java/org/jclouds/vcloud/config/VCloudRestClientModuleTest.java new file mode 100644 index 0000000000..37ee845052 --- /dev/null +++ b/vcloud/core/src/test/java/org/jclouds/vcloud/config/VCloudRestClientModuleTest.java @@ -0,0 +1,69 @@ +/** + * + * Copyright (C) 2009 Cloud Conscious, LLC. + * + * ==================================================================== + * 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.config; + +import static org.jclouds.vcloud.reference.VCloudConstants.PROPERTY_VCLOUD_ENDPOINT; +import static org.jclouds.vcloud.reference.VCloudConstants.PROPERTY_VCLOUD_KEY; +import static org.jclouds.vcloud.reference.VCloudConstants.PROPERTY_VCLOUD_SESSIONINTERVAL; +import static org.jclouds.vcloud.reference.VCloudConstants.PROPERTY_VCLOUD_USER; +import static org.testng.Assert.assertEquals; + +import org.jclouds.http.functions.config.ParserModule; +import org.jclouds.util.Jsr330; +import org.jclouds.vcloud.endpoints.internal.CatalogItemRoot; +import org.testng.annotations.Test; + +import com.google.inject.AbstractModule; +import com.google.inject.Guice; +import com.google.inject.Injector; +import com.google.inject.Key; + +/** + * @author Adrian Cole + */ +@Test(groups = "unit", testName = "vcloud.VCloudRestClientModuleTest") +public class VCloudRestClientModuleTest extends VCloudDiscoveryRestClientModuleTest { + + Injector createInjector() { + return Guice.createInjector(new VCloudRestClientModule(), + new VCloudDiscoveryRestClientModule(), new ParserModule(), new AbstractModule() { + @Override + protected void configure() { + bindConstant().annotatedWith(Jsr330.named(PROPERTY_VCLOUD_USER)).to("user"); + bindConstant().annotatedWith(Jsr330.named(PROPERTY_VCLOUD_KEY)).to("secret"); + bindConstant().annotatedWith(Jsr330.named(PROPERTY_VCLOUD_ENDPOINT)).to( + "http://localhost"); + bindConstant().annotatedWith(Jsr330.named(PROPERTY_VCLOUD_SESSIONINTERVAL)) + .to("2"); + } + }); + } + + @Test + void testCatalogItemRoot() { + assertEquals(createInjector().getInstance(Key.get(String.class, CatalogItemRoot.class)), + "http://localhost/catalogItem"); + } + +} \ No newline at end of file diff --git a/vcloud/core/src/test/java/org/jclouds/vcloud/terremark/TerremarkVCloudClientLiveTest.java b/vcloud/core/src/test/java/org/jclouds/vcloud/terremark/TerremarkVCloudClientLiveTest.java index da5ce1ae85..bc80503271 100644 --- a/vcloud/core/src/test/java/org/jclouds/vcloud/terremark/TerremarkVCloudClientLiveTest.java +++ b/vcloud/core/src/test/java/org/jclouds/vcloud/terremark/TerremarkVCloudClientLiveTest.java @@ -69,19 +69,17 @@ public class TerremarkVCloudClientLiveTest extends VCloudClientLiveTest { @Test public void testInstantiate() throws InterruptedException, ExecutionException, TimeoutException { - String serverName = "adriantest4"; + String serverName = "adriantest"; int processorCount = 1; int memory = 512; String catalogOs = "Ubuntu Server 9.04 (32-bit)"; String expectedOs = "Ubuntu Linux (32-bit)"; - URI template = tmClient.getCatalog().get(45, TimeUnit.SECONDS).get(catalogOs).getLocation(); + int templateId = tmClient.getCatalog().get(45, TimeUnit.SECONDS).get(catalogOs).getId(); - URI network = tmClient.getDefaultVDC().get(45, TimeUnit.SECONDS).getAvailableNetworks() - .values().iterator().next().getLocation(); + VApp vApp = tmClient.instantiateVAppTemplate(serverName, templateId) + .get(45, TimeUnit.SECONDS); - VApp vApp = tmClient.instantiateVAppTemplate(serverName, template, processorCount, memory, - network).get(45, TimeUnit.SECONDS); assertEquals(vApp.getStatus(), VAppStatus.CREATING); Task instantiateTask = getLastTaskFor(vApp.getVDC().getLocation()); @@ -146,7 +144,7 @@ public class TerremarkVCloudClientLiveTest extends VCloudClientLiveTest { assertEquals(vApp.getStatus(), VAppStatus.OFF); tmClient.delete(deployTask.getResult().getLocation()).get(45, TimeUnit.SECONDS); - //TODO verify not present anymore + // TODO verify not present anymore } private void verifyConfigurationOfVApp(VApp vApp, String serverName, String expectedOs, diff --git a/vcloud/core/src/test/java/org/jclouds/vcloud/terremark/TerremarkVCloudClientTest.java b/vcloud/core/src/test/java/org/jclouds/vcloud/terremark/TerremarkVCloudClientTest.java index 9f3567cfbc..f02d9c00a5 100644 --- a/vcloud/core/src/test/java/org/jclouds/vcloud/terremark/TerremarkVCloudClientTest.java +++ b/vcloud/core/src/test/java/org/jclouds/vcloud/terremark/TerremarkVCloudClientTest.java @@ -43,7 +43,9 @@ 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.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.xml.TerremarkVAppHandler; import org.jclouds.vcloud.terremark.xml.TerremarkVDCHandler; @@ -80,17 +82,14 @@ public class TerremarkVCloudClientTest extends RestClientTest httpMethod = processor.createRequest(method, - "name", URI.create("http://template"), 1, 512, URI.create("http://network")); + "name", 3); assertRequestLineEquals(httpMethod, "POST http://vdc/action/instantiatevAppTemplate HTTP/1.1"); assertHeadersEqual( httpMethod, - "Content-Length: 2242\nContent-Type: application/vnd.vmware.vcloud.instantiateVAppTemplateParams+xml\n"); + "Content-Length: 2247\nContent-Type: application/vnd.vmware.vcloud.instantiateVAppTemplateParams+xml\n"); assertEntityEquals(httpMethod, IOUtils.toString(getClass().getResourceAsStream( "/terremark/InstantiateVAppTemplateParams-test.xml"))); @@ -119,7 +118,10 @@ public class TerremarkVCloudClientTest extends RestClientTest() { diff --git a/vcloud/core/src/test/java/org/jclouds/vcloud/terremark/binders/BindInstantiateVAppTemplateParamsToXmlEntityTest.java b/vcloud/core/src/test/java/org/jclouds/vcloud/terremark/binders/BindInstantiateVAppTemplateParamsToXmlEntityTest.java index d8df92e4ef..609b072542 100644 --- a/vcloud/core/src/test/java/org/jclouds/vcloud/terremark/binders/BindInstantiateVAppTemplateParamsToXmlEntityTest.java +++ b/vcloud/core/src/test/java/org/jclouds/vcloud/terremark/binders/BindInstantiateVAppTemplateParamsToXmlEntityTest.java @@ -78,13 +78,13 @@ public class BindInstantiateVAppTemplateParamsToXmlEntityTest { Map map = Maps.newHashMap(); map.put("name", "name"); - map.put("template", "http://template"); + map.put("template", "http://catalogItem/3"); 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.getFirstHeaderOrNull(HttpHeaders.CONTENT_LENGTH), "2247"); assertEquals(request.getEntity(), expected); } diff --git a/vcloud/core/src/test/java/org/jclouds/vcloud/xml/CatalogHandlerTest.java b/vcloud/core/src/test/java/org/jclouds/vcloud/xml/CatalogHandlerTest.java index 14d26c101d..380b845b4c 100644 --- a/vcloud/core/src/test/java/org/jclouds/vcloud/xml/CatalogHandlerTest.java +++ b/vcloud/core/src/test/java/org/jclouds/vcloud/xml/CatalogHandlerTest.java @@ -30,11 +30,18 @@ import java.io.InputStream; import java.net.URI; import org.jclouds.http.functions.BaseHandlerTest; -import org.jclouds.rest.domain.internal.NamedLinkImpl; +import org.jclouds.http.functions.ParseSax; +import org.jclouds.http.functions.config.ParserModule; import org.jclouds.vcloud.domain.Catalog; +import org.jclouds.vcloud.domain.internal.NamedResourceImpl; +import org.jclouds.vcloud.endpoints.internal.CatalogItemRoot; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; +import com.google.inject.AbstractModule; +import com.google.inject.Guice; +import com.google.inject.Provides; + /** * Tests behavior of {@code CatalogHandler} * @@ -46,7 +53,21 @@ public class CatalogHandlerTest extends BaseHandlerTest { @BeforeTest @Override protected void setUpInjector() { - super.setUpInjector(); + injector = Guice.createInjector(new ParserModule(), new AbstractModule() { + + @Override + protected void configure() { + } + + @SuppressWarnings("unused") + @Provides + @CatalogItemRoot + String provide() { + return "https://services.vcloudexpress.terremark.com/api/v0.8/catalogItem"; + } + + }); + factory = injector.getInstance(ParseSax.Factory.class); } public void testApplyInputStream() { @@ -59,77 +80,83 @@ public class CatalogHandlerTest extends BaseHandlerTest { .create("https://services.vcloudexpress.terremark.com/api/v0.8/vdc/32/catalog")); assertEquals(result.getType(), "application/vnd.vmware.vcloud.catalog+xml"); - assertEquals(result.get("CentOS 5.3 (32-bit)"), new NamedLinkImpl("CentOS 5.3 (32-bit)", - CATALOGITEM_XML, + assertEquals(result.get("CentOS 5.3 (32-bit)"), new NamedResourceImpl(5, + "CentOS 5.3 (32-bit)", CATALOGITEM_XML, URI.create("https://services.vcloudexpress.terremark.com/api/v0.8/catalogItem/5"))); - assertEquals(result.get("CentOS 5.3 (64-bit)"), new NamedLinkImpl("CentOS 5.3 (64-bit)", - CATALOGITEM_XML, + assertEquals(result.get("CentOS 5.3 (64-bit)"), new NamedResourceImpl(6, + "CentOS 5.3 (64-bit)", CATALOGITEM_XML, URI.create("https://services.vcloudexpress.terremark.com/api/v0.8/catalogItem/6"))); - assertEquals(result.get("RHEL 5.3 (32-bit)"), new NamedLinkImpl("RHEL 5.3 (32-bit)", + assertEquals(result.get("RHEL 5.3 (32-bit)"), new NamedResourceImpl(7, "RHEL 5.3 (32-bit)", CATALOGITEM_XML, URI.create("https://services.vcloudexpress.terremark.com/api/v0.8/catalogItem/7"))); - assertEquals(result.get("RHEL 5.3 (64-bit)"), new NamedLinkImpl("RHEL 5.3 (64-bit)", + assertEquals(result.get("RHEL 5.3 (64-bit)"), new NamedResourceImpl(8, "RHEL 5.3 (64-bit)", CATALOGITEM_XML, URI.create("https://services.vcloudexpress.terremark.com/api/v0.8/catalogItem/8"))); - assertEquals(result.get("Ubuntu JeOS 9.04 (32-bit)"), new NamedLinkImpl( + assertEquals(result.get("Ubuntu JeOS 9.04 (32-bit)"), new NamedResourceImpl(11, "Ubuntu JeOS 9.04 (32-bit)", CATALOGITEM_XML, URI.create("https://services.vcloudexpress.terremark.com/api/v0.8/catalogItem/11"))); - assertEquals(result.get("Ubuntu JeOS 9.04 (64-bit)"), new NamedLinkImpl( + assertEquals(result.get("Ubuntu JeOS 9.04 (64-bit)"), new NamedResourceImpl(12, "Ubuntu JeOS 9.04 (64-bit)", CATALOGITEM_XML, URI.create("https://services.vcloudexpress.terremark.com/api/v0.8/catalogItem/12"))); - assertEquals(result.get("Ubuntu Server 9.04 (32-bit)"), new NamedLinkImpl( + assertEquals(result.get("Ubuntu Server 9.04 (32-bit)"), new NamedResourceImpl(9, "Ubuntu Server 9.04 (32-bit)", CATALOGITEM_XML, URI.create("https://services.vcloudexpress.terremark.com/api/v0.8/catalogItem/9"))); - assertEquals(result.get("Ubuntu Server 9.04 (64-bit)"), new NamedLinkImpl( + assertEquals(result.get("Ubuntu Server 9.04 (64-bit)"), new NamedResourceImpl(10, "Ubuntu Server 9.04 (64-bit)", CATALOGITEM_XML, URI.create("https://services.vcloudexpress.terremark.com/api/v0.8/catalogItem/10"))); - assertEquals(result.get("Windows 2003 Enterprise R2 (32-bit)"), new NamedLinkImpl( + assertEquals(result.get("Windows 2003 Enterprise R2 (32-bit)"), new NamedResourceImpl(1, "Windows 2003 Enterprise R2 (32-bit)", CATALOGITEM_XML, URI.create("https://services.vcloudexpress.terremark.com/api/v0.8/catalogItem/1"))); - assertEquals(result.get("Windows 2003 Enterprise R2 (64-bit)"), new NamedLinkImpl( + assertEquals(result.get("Windows 2003 Enterprise R2 (64-bit)"), new NamedResourceImpl(2, "Windows 2003 Enterprise R2 (64-bit)", CATALOGITEM_XML, URI.create("https://services.vcloudexpress.terremark.com/api/v0.8/catalogItem/2"))); - assertEquals(result.get("Windows 2003 Standard R2 (32-bit)"), new NamedLinkImpl( + assertEquals(result.get("Windows 2003 Standard R2 (32-bit)"), new NamedResourceImpl(3, "Windows 2003 Standard R2 (32-bit)", CATALOGITEM_XML, URI.create("https://services.vcloudexpress.terremark.com/api/v0.8/catalogItem/3"))); - assertEquals(result.get("Windows 2003 Standard R2 (64-bit)"), new NamedLinkImpl( + assertEquals(result.get("Windows 2003 Standard R2 (64-bit)"), new NamedResourceImpl(4, "Windows 2003 Standard R2 (64-bit)", CATALOGITEM_XML, URI.create("https://services.vcloudexpress.terremark.com/api/v0.8/catalogItem/4"))); - assertEquals(result.get("Windows 2003 Standard R2 w.SQL 2008 Web (64-bit)"), new NamedLinkImpl( - "Windows 2003 Standard R2 w.SQL 2008 Web (64-bit)", CATALOGITEM_XML, - URI.create("https://services.vcloudexpress.terremark.com/api/v0.8/catalogItem/23"))); - assertEquals(result.get("Windows Server 2008 Enterprise (32-bit)"), new NamedLinkImpl( + assertEquals( + result.get("Windows 2003 Standard R2 w.SQL 2008 Web (64-bit)"), + new NamedResourceImpl( + 23, + "Windows 2003 Standard R2 w.SQL 2008 Web (64-bit)", + CATALOGITEM_XML, + URI + .create("https://services.vcloudexpress.terremark.com/api/v0.8/catalogItem/23"))); + assertEquals(result.get("Windows Server 2008 Enterprise (32-bit)"), new NamedResourceImpl(13, "Windows Server 2008 Enterprise (32-bit)", CATALOGITEM_XML, URI.create("https://services.vcloudexpress.terremark.com/api/v0.8/catalogItem/13"))); - assertEquals(result.get("Windows Server 2008 Enterprise (64-bit)"), new NamedLinkImpl( + assertEquals(result.get("Windows Server 2008 Enterprise (64-bit)"), new NamedResourceImpl(15, "Windows Server 2008 Enterprise (64-bit)", CATALOGITEM_XML, URI.create("https://services.vcloudexpress.terremark.com/api/v0.8/catalogItem/15"))); - assertEquals(result.get("Windows Server 2008 Enterprise R2 (64-bit)"), new NamedLinkImpl( - "Windows Server 2008 Enterprise R2 (64-bit)", CATALOGITEM_XML, + assertEquals(result.get("Windows Server 2008 Enterprise R2 (64-bit)"), new NamedResourceImpl( + 16, "Windows Server 2008 Enterprise R2 (64-bit)", CATALOGITEM_XML, URI.create("https://services.vcloudexpress.terremark.com/api/v0.8/catalogItem/16"))); - assertEquals(result.get("Windows Server 2008 Standard (32-bit)"), new NamedLinkImpl( + assertEquals(result.get("Windows Server 2008 Standard (32-bit)"), new NamedResourceImpl(17, "Windows Server 2008 Standard (32-bit)", CATALOGITEM_XML, URI.create("https://services.vcloudexpress.terremark.com/api/v0.8/catalogItem/17"))); - assertEquals(result.get("Windows Server 2008 Standard (64-bit)"), new NamedLinkImpl( + assertEquals(result.get("Windows Server 2008 Standard (64-bit)"), new NamedResourceImpl(18, "Windows Server 2008 Standard (64-bit)", CATALOGITEM_XML, URI.create("https://services.vcloudexpress.terremark.com/api/v0.8/catalogItem/18"))); - assertEquals(result.get("Windows Server 2008 Standard R2 (64-bit)"), new NamedLinkImpl( - "Windows Server 2008 Standard R2 (64-bit)", CATALOGITEM_XML, + assertEquals(result.get("Windows Server 2008 Standard R2 (64-bit)"), new NamedResourceImpl( + 19, "Windows Server 2008 Standard R2 (64-bit)", CATALOGITEM_XML, URI.create("https://services.vcloudexpress.terremark.com/api/v0.8/catalogItem/19"))); assertEquals( result.get("Windows Server 2008 Standard w.SQL 2008 Web (64-bit)"), - new NamedLinkImpl( + new NamedResourceImpl( + 14, "Windows Server 2008 Standard w.SQL 2008 Web (64-bit)", CATALOGITEM_XML, URI .create("https://services.vcloudexpress.terremark.com/api/v0.8/catalogItem/14"))); - assertEquals(result.get("Windows Web Server 2008 (32-bit)"), new NamedLinkImpl( + assertEquals(result.get("Windows Web Server 2008 (32-bit)"), new NamedResourceImpl(20, "Windows Web Server 2008 (32-bit)", CATALOGITEM_XML, URI.create("https://services.vcloudexpress.terremark.com/api/v0.8/catalogItem/20"))); - assertEquals(result.get("Windows Web Server 2008 (64-bit)"), new NamedLinkImpl( + assertEquals(result.get("Windows Web Server 2008 (64-bit)"), new NamedResourceImpl(21, "Windows Web Server 2008 (64-bit)", CATALOGITEM_XML, URI.create("https://services.vcloudexpress.terremark.com/api/v0.8/catalogItem/21"))); - assertEquals(result.get("Windows Web Server 2008 R2 (64-bit)"), new NamedLinkImpl( + assertEquals(result.get("Windows Web Server 2008 R2 (64-bit)"), new NamedResourceImpl(22, "Windows Web Server 2008 R2 (64-bit)", CATALOGITEM_XML, URI.create("https://services.vcloudexpress.terremark.com/api/v0.8/catalogItem/22"))); diff --git a/vcloud/core/src/test/resources/terremark/InstantiateVAppTemplateParams-test.xml b/vcloud/core/src/test/resources/terremark/InstantiateVAppTemplateParams-test.xml index c4c421333d..25ed6202f8 100644 --- a/vcloud/core/src/test/resources/terremark/InstantiateVAppTemplateParams-test.xml +++ b/vcloud/core/src/test/resources/terremark/InstantiateVAppTemplateParams-test.xml @@ -1,6 +1,6 @@ - +