From bd69558081513b82b680cfde45f7b8b63650a2d5 Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Wed, 25 May 2011 16:03:15 -0600 Subject: [PATCH] Issue 573:Set vApp description in vcloud driver --- ...antiateVAppTemplateParamsToXmlPayload.java | 48 ++++++------ .../options/VCloudTemplateOptions.java | 62 +++++++++++---- ...IntoNameThenCustomizeDeployAndPowerOn.java | 4 +- .../InstantiateVAppTemplateOptions.java | 33 +++++++- .../org/jclouds/vcloud/xml/VAppHandler.java | 47 ++++++----- .../VCloudGuestCustomizationLiveTest.java | 22 ++++-- ...ateVAppTemplateParamsToXmlPayloadTest.java | 77 +++++++++++++------ .../options/VCloudTemplateOptionsTest.java | 17 +++- .../InstantiateVAppTemplateOptionsTest.java | 13 ++++ .../jclouds/vcloud/xml/VAppHandlerTest.java | 2 +- .../instantiationparams-description.xml | 1 + .../test/resources/vapp-rhel-off-static.xml | 2 +- 12 files changed, 233 insertions(+), 95 deletions(-) create mode 100644 apis/vcloud/src/test/resources/instantiationparams-description.xml diff --git a/apis/vcloud/src/main/java/org/jclouds/vcloud/binders/BindInstantiateVAppTemplateParamsToXmlPayload.java b/apis/vcloud/src/main/java/org/jclouds/vcloud/binders/BindInstantiateVAppTemplateParamsToXmlPayload.java index 9c0a63b482..d14b1d5975 100644 --- a/apis/vcloud/src/main/java/org/jclouds/vcloud/binders/BindInstantiateVAppTemplateParamsToXmlPayload.java +++ b/apis/vcloud/src/main/java/org/jclouds/vcloud/binders/BindInstantiateVAppTemplateParamsToXmlPayload.java @@ -66,8 +66,6 @@ import com.jamesmurty.utils.XMLBuilder; */ @Singleton public class BindInstantiateVAppTemplateParamsToXmlPayload implements MapBinder { - @Resource - protected Logger logger = Logger.NULL; protected final String ns; protected final String schema; @@ -79,9 +77,9 @@ public class BindInstantiateVAppTemplateParamsToXmlPayload implements MapBinder @Inject public BindInstantiateVAppTemplateParamsToXmlPayload(DefaultNetworkNameInTemplate defaultNetworkNameInTemplate, - BindToStringPayload stringBinder, @Named(PROPERTY_VCLOUD_XML_NAMESPACE) String ns, - @Named(PROPERTY_VCLOUD_XML_SCHEMA) String schema, @Network URI network, - @Named(PROPERTY_VCLOUD_DEFAULT_FENCEMODE) String fenceMode, VCloudClient client) { + BindToStringPayload stringBinder, @Named(PROPERTY_VCLOUD_XML_NAMESPACE) String ns, + @Named(PROPERTY_VCLOUD_XML_SCHEMA) String schema, @Network URI network, + @Named(PROPERTY_VCLOUD_DEFAULT_FENCEMODE) String fenceMode, VCloudClient client) { this.defaultNetworkNameInTemplate = defaultNetworkNameInTemplate; this.ns = ns; this.schema = schema; @@ -94,7 +92,7 @@ public class BindInstantiateVAppTemplateParamsToXmlPayload implements MapBinder @Override public R bindToRequest(R request, Map postParams) { checkArgument(checkNotNull(request, "request") instanceof GeneratedHttpRequest, - "this binder is only valid for GeneratedHttpRequests!"); + "this binder is only valid for GeneratedHttpRequests!"); GeneratedHttpRequest gRequest = (GeneratedHttpRequest) request; checkState(gRequest.getArgs() != null, "args should be initialized at this point"); String name = checkNotNull(postParams.remove("name"), "name"); @@ -107,14 +105,14 @@ public class BindInstantiateVAppTemplateParamsToXmlPayload implements MapBinder Set networkConfig = null; NetworkConfigDecorator networknetworkConfigDecorator = new NetworkConfigDecorator(template, defaultNetwork, - defaultFenceMode, defaultNetworkNameInTemplate); + defaultFenceMode, defaultNetworkNameInTemplate); InstantiateVAppTemplateOptions options = findOptionsInArgsOrNull(gRequest); if (options != null) { if (options.getNetworkConfig().size() > 0) networkConfig = Sets.newLinkedHashSet(Iterables.transform(options.getNetworkConfig(), - networknetworkConfigDecorator)); + networknetworkConfigDecorator)); deploy = ifNullDefaultTo(options.shouldDeploy(), deploy); powerOn = ifNullDefaultTo(options.shouldPowerOn(), powerOn); customizeOnInstantiate = options.shouldCustomizeOnInstantiate(); @@ -124,8 +122,10 @@ public class BindInstantiateVAppTemplateParamsToXmlPayload implements MapBinder networkConfig = ImmutableSet.of(networknetworkConfigDecorator.apply(null)); try { - return stringBinder.bindToRequest(request, generateXml(name, deploy, powerOn, template, networkConfig, - customizeOnInstantiate)); + return stringBinder.bindToRequest( + request, + generateXml(name, options.getDescription(), deploy, powerOn, template, networkConfig, + customizeOnInstantiate)); } catch (ParserConfigurationException e) { throw new RuntimeException(e); } catch (FactoryConfigurationError e) { @@ -155,7 +155,7 @@ public class BindInstantiateVAppTemplateParamsToXmlPayload implements MapBinder private final DefaultNetworkNameInTemplate defaultNetworkNameInTemplate; protected NetworkConfigDecorator(URI template, URI defaultNetwork, FenceMode defaultFenceMode, - DefaultNetworkNameInTemplate defaultNetworkNameInTemplate) { + DefaultNetworkNameInTemplate defaultNetworkNameInTemplate) { this.template = checkNotNull(template, "template"); this.defaultNetwork = checkNotNull(defaultNetwork, "defaultNetwork"); this.defaultFenceMode = checkNotNull(defaultFenceMode, "defaultFenceMode"); @@ -169,7 +169,7 @@ public class BindInstantiateVAppTemplateParamsToXmlPayload implements MapBinder URI network = ifNullDefaultTo(from.getParentNetwork(), defaultNetwork); FenceMode fenceMode = ifNullDefaultTo(from.getFenceMode(), defaultFenceMode); String networkName = from.getNetworkName() != null ? from.getNetworkName() : defaultNetworkNameInTemplate - .apply(template); + .apply(template); return new NetworkConfig(networkName, network, fenceMode); } } @@ -200,10 +200,12 @@ public class BindInstantiateVAppTemplateParamsToXmlPayload implements MapBinder } } - protected String generateXml(String name, boolean deploy, boolean powerOn, URI template, - Iterable networkConfig, @Nullable Boolean customizeOnInstantiate) - throws ParserConfigurationException, FactoryConfigurationError, TransformerException { + protected String generateXml(String name, @Nullable String description, boolean deploy, boolean powerOn, + URI template, Iterable networkConfig, @Nullable Boolean customizeOnInstantiate) + throws ParserConfigurationException, FactoryConfigurationError, TransformerException { XMLBuilder rootBuilder = buildRoot(name).a("deploy", deploy + "").a("powerOn", powerOn + ""); + if (description != null) + rootBuilder.e("Description").t(description); XMLBuilder instantiationParamsBuilder = rootBuilder.e("InstantiationParams"); addNetworkConfig(instantiationParamsBuilder, networkConfig); addCustomizationConfig(instantiationParamsBuilder, customizeOnInstantiate); @@ -217,19 +219,20 @@ public class BindInstantiateVAppTemplateParamsToXmlPayload implements MapBinder protected void addCustomizationConfig(XMLBuilder instantiationParamsBuilder, Boolean customizeOnInstantiate) { if (customizeOnInstantiate != null) { -// XMLBuilder customizationSectionBuilder = instantiationParamsBuilder.e("CustomizationSection"); -// customizationSectionBuilder.e("ovf:Info").t("VApp template customization section"); -// customizationSectionBuilder.e("CustomizeOnInstantiate").t(customizeOnInstantiate.toString()); + // XMLBuilder customizationSectionBuilder = + // instantiationParamsBuilder.e("CustomizationSection"); + // customizationSectionBuilder.e("ovf:Info").t("VApp template customization section"); + // customizationSectionBuilder.e("CustomizeOnInstantiate").t(customizeOnInstantiate.toString()); } } protected void addNetworkConfig(XMLBuilder instantiationParamsBuilder, - Iterable networkConfig) { + Iterable networkConfig) { XMLBuilder networkConfigBuilder = instantiationParamsBuilder.e("NetworkConfigSection"); networkConfigBuilder.e("ovf:Info").t("Configuration parameters for logical networks"); for (NetworkConfig n : networkConfig) { XMLBuilder configurationBuilder = networkConfigBuilder.e("NetworkConfig").a("networkName", n.getNetworkName()) - .e("Configuration"); + .e("Configuration"); configurationBuilder.e("ParentNetwork").a("href", n.getParentNetwork().toASCIIString()); if (n.getFenceMode() != null) { configurationBuilder.e("FenceMode").t(n.getFenceMode().toString()); @@ -238,8 +241,8 @@ public class BindInstantiateVAppTemplateParamsToXmlPayload implements MapBinder } protected XMLBuilder buildRoot(String name) throws ParserConfigurationException, FactoryConfigurationError { - return XMLBuilder.create("InstantiateVAppTemplateParams").a("name", name).a("xmlns", ns).a("xmlns:ovf", - "http://schemas.dmtf.org/ovf/envelope/1"); + return XMLBuilder.create("InstantiateVAppTemplateParams").a("name", name).a("xmlns", ns) + .a("xmlns:ovf", "http://schemas.dmtf.org/ovf/envelope/1"); } protected InstantiateVAppTemplateOptions findOptionsInArgsOrNull(GeneratedHttpRequest gRequest) { @@ -253,6 +256,7 @@ public class BindInstantiateVAppTemplateParamsToXmlPayload implements MapBinder } return null; } + @Override public R bindToRequest(R request, Object input) { throw new IllegalStateException("InstantiateVAppTemplateParams is needs parameters"); diff --git a/apis/vcloud/src/main/java/org/jclouds/vcloud/compute/options/VCloudTemplateOptions.java b/apis/vcloud/src/main/java/org/jclouds/vcloud/compute/options/VCloudTemplateOptions.java index 00eb11d7f6..83229270dd 100644 --- a/apis/vcloud/src/main/java/org/jclouds/vcloud/compute/options/VCloudTemplateOptions.java +++ b/apis/vcloud/src/main/java/org/jclouds/vcloud/compute/options/VCloudTemplateOptions.java @@ -26,11 +26,11 @@ import org.jclouds.util.Preconditions2; import org.jclouds.vcloud.domain.network.IpAddressAllocationMode; /** - * Contains options supported in the {@code ComputeService#runNode} operation on the "vcloud" - * provider.

- * Usage

The recommended way to instantiate a VCloudTemplateOptions object is to statically - * import VCloudTemplateOptions.* and invoke a static creation method followed by an instance - * mutator (if needed): + * Contains options supported in the {@code ComputeService#runNode} operation on + * the "vcloud" provider.

+ * Usage

The recommended way to instantiate a VCloudTemplateOptions object + * is to statically import VCloudTemplateOptions.* and invoke a static creation + * method followed by an instance mutator (if needed): *

* * import static org.jclouds.compute.options.VCloudTemplateOptions.Builder.*; @@ -57,16 +57,28 @@ public class VCloudTemplateOptions extends TemplateOptions implements Cloneable VCloudTemplateOptions eTo = VCloudTemplateOptions.class.cast(to); if (getCustomizationScript() != null) eTo.customizationScript(getCustomizationScript()); + if (getDescription() != null) + eTo.description(getDescription()); if (getIpAddressAllocationMode() != null) eTo.ipAddressAllocationMode(getIpAddressAllocationMode()); } } + private String description = null; private String customizationScript = null; private IpAddressAllocationMode ipAddressAllocationMode = null; public static final VCloudTemplateOptions NONE = new VCloudTemplateOptions(); + /** + * Optional description. Used for the Description of the vApp created by this + * instantiation. + */ + public VCloudTemplateOptions description(String description) { + this.description = description; + return this; + } + /** * Specifies the customizationScript used to run instances with */ @@ -77,7 +89,8 @@ public class VCloudTemplateOptions extends TemplateOptions implements Cloneable } /** - * Specifies the ipAddressAllocationMode used to for network interfaces on the VMs + * Specifies the ipAddressAllocationMode used to for network interfaces on + * the VMs */ public VCloudTemplateOptions ipAddressAllocationMode(IpAddressAllocationMode ipAddressAllocationMode) { this.ipAddressAllocationMode = ipAddressAllocationMode; @@ -85,6 +98,14 @@ public class VCloudTemplateOptions extends TemplateOptions implements Cloneable } public static class Builder { + /** + * @see VCloudTemplateOptions#description + */ + public static VCloudTemplateOptions description(String description) { + VCloudTemplateOptions options = new VCloudTemplateOptions(); + return VCloudTemplateOptions.class.cast(options.description(description)); + } + /** * @see VCloudTemplateOptions#customizationScript */ @@ -152,6 +173,13 @@ public class VCloudTemplateOptions extends TemplateOptions implements Cloneable } + /** + * @return description of the vApp + */ + public String getDescription() { + return description; + } + /** * @return customizationScript on the vms */ @@ -178,9 +206,10 @@ public class VCloudTemplateOptions extends TemplateOptions implements Cloneable /** * - * special thing is that we do assume if you are passing groups that you have everything you need - * already defined. for example, our option inboundPorts normally creates ingress rules - * accordingly but if we notice you've specified securityGroups, we do not mess with rules at all + * special thing is that we do assume if you are passing groups that you have + * everything you need already defined. for example, our option inboundPorts + * normally creates ingress rules accordingly but if we notice you've + * specified securityGroups, we do not mess with rules at all * * @see TemplateOptions#inboundPorts */ @@ -253,6 +282,7 @@ public class VCloudTemplateOptions extends TemplateOptions implements Cloneable final int prime = 31; int result = super.hashCode(); result = prime * result + ((customizationScript == null) ? 0 : customizationScript.hashCode()); + result = prime * result + ((description == null) ? 0 : description.hashCode()); result = prime * result + ((ipAddressAllocationMode == null) ? 0 : ipAddressAllocationMode.hashCode()); return result; } @@ -271,6 +301,11 @@ public class VCloudTemplateOptions extends TemplateOptions implements Cloneable return false; } else if (!customizationScript.equals(other.customizationScript)) return false; + if (description == null) { + if (other.description != null) + return false; + } else if (!description.equals(other.description)) + return false; if (ipAddressAllocationMode != other.ipAddressAllocationMode) return false; return true; @@ -278,10 +313,11 @@ public class VCloudTemplateOptions extends TemplateOptions implements Cloneable @Override public String toString() { - return "[customizationScript=" + (customizationScript != null) + ", ipAddressAllocationMode=" - + ipAddressAllocationMode + ", inboundPorts=" + Arrays.toString(inboundPorts) + ", privateKey=" - + (privateKey != null) + ", publicKey=" + (publicKey != null) + ", runScript=" + (script != null) - + ", port:seconds=" + port + ":" + seconds + ", metadata/details: " + includeMetadata + "]"; + return "[customizationScript=" + (customizationScript != null) + ", description=" + description + + ", ipAddressAllocationMode=" + ipAddressAllocationMode + ", inboundPorts=" + + Arrays.toString(inboundPorts) + ", privateKey=" + (privateKey != null) + ", publicKey=" + + (publicKey != null) + ", runScript=" + (script != null) + ", port:seconds=" + port + ":" + seconds + + ", metadata/details: " + includeMetadata + "]"; } } diff --git a/apis/vcloud/src/main/java/org/jclouds/vcloud/compute/strategy/InstantiateVAppTemplateWithGroupEncodedIntoNameThenCustomizeDeployAndPowerOn.java b/apis/vcloud/src/main/java/org/jclouds/vcloud/compute/strategy/InstantiateVAppTemplateWithGroupEncodedIntoNameThenCustomizeDeployAndPowerOn.java index fb6c9e00a7..75dd7d2702 100644 --- a/apis/vcloud/src/main/java/org/jclouds/vcloud/compute/strategy/InstantiateVAppTemplateWithGroupEncodedIntoNameThenCustomizeDeployAndPowerOn.java +++ b/apis/vcloud/src/main/java/org/jclouds/vcloud/compute/strategy/InstantiateVAppTemplateWithGroupEncodedIntoNameThenCustomizeDeployAndPowerOn.java @@ -18,13 +18,14 @@ */ package org.jclouds.vcloud.compute.strategy; +import static org.jclouds.compute.util.ComputeServiceUtils.getCores; + import java.net.URI; import javax.annotation.Resource; import javax.inject.Inject; import javax.inject.Named; import javax.inject.Singleton; -import static org.jclouds.compute.util.ComputeServiceUtils.getCores; import org.jclouds.compute.domain.NodeMetadata; import org.jclouds.compute.domain.Template; @@ -82,6 +83,7 @@ public class InstantiateVAppTemplateWithGroupEncodedIntoNameThenCustomizeDeployA IpAddressAllocationMode ipAddressAllocationMode = VCloudTemplateOptions.class.cast(template.getOptions()) .getIpAddressAllocationMode(); + options.description(VCloudTemplateOptions.class.cast(template.getOptions()).getDescription()); options.customizeOnInstantiate(false); options.deploy(false); options.powerOn(false); diff --git a/apis/vcloud/src/main/java/org/jclouds/vcloud/options/InstantiateVAppTemplateOptions.java b/apis/vcloud/src/main/java/org/jclouds/vcloud/options/InstantiateVAppTemplateOptions.java index a67f227668..37f125434b 100644 --- a/apis/vcloud/src/main/java/org/jclouds/vcloud/options/InstantiateVAppTemplateOptions.java +++ b/apis/vcloud/src/main/java/org/jclouds/vcloud/options/InstantiateVAppTemplateOptions.java @@ -35,11 +35,15 @@ public class InstantiateVAppTemplateOptions { private Set networkConfig = Sets.newLinkedHashSet(); private Boolean customizeOnInstantiate; - + private String description = null; private boolean block = true; private boolean deploy = true; private boolean powerOn = true; + public String getDescription() { + return description; + } + public boolean shouldBlock() { return block; } @@ -52,6 +56,15 @@ public class InstantiateVAppTemplateOptions { return powerOn; } + /** + * Optional description. Used for the Description of the vApp created by this + * instantiation. + */ + public InstantiateVAppTemplateOptions description(String description) { + this.description = description; + return this; + } + /** * deploy the vapp after it is instantiated? */ @@ -124,6 +137,14 @@ public class InstantiateVAppTemplateOptions { return options.block(block); } + /** + * @see InstantiateVAppTemplateOptions#description + */ + public static InstantiateVAppTemplateOptions description(String description) { + InstantiateVAppTemplateOptions options = new InstantiateVAppTemplateOptions(); + return options.description(description); + } + /** * @see InstantiateVAppTemplateOptions#deploy */ @@ -160,8 +181,8 @@ public class InstantiateVAppTemplateOptions { @Override public String toString() { - return "[networkConfig=" + networkConfig + ", customizeOnInstantiate=" + customizeOnInstantiate + ", deploy=" - + (deploy) + ", powerOn=" + (powerOn) + "]"; + return "[networkConfig=" + networkConfig + ", customizeOnInstantiate=" + customizeOnInstantiate + + ", description=" + description + ", block=" + block + ", deploy=" + deploy + ", powerOn=" + powerOn + "]"; } @Override @@ -171,6 +192,7 @@ public class InstantiateVAppTemplateOptions { result = prime * result + (block ? 1231 : 1237); result = prime * result + ((customizeOnInstantiate == null) ? 0 : customizeOnInstantiate.hashCode()); result = prime * result + (deploy ? 1231 : 1237); + result = prime * result + ((description == null) ? 0 : description.hashCode()); result = prime * result + ((networkConfig == null) ? 0 : networkConfig.hashCode()); result = prime * result + (powerOn ? 1231 : 1237); return result; @@ -194,6 +216,11 @@ public class InstantiateVAppTemplateOptions { return false; if (deploy != other.deploy) return false; + if (description == null) { + if (other.description != null) + return false; + } else if (!description.equals(other.description)) + return false; if (networkConfig == null) { if (other.networkConfig != null) return false; diff --git a/apis/vcloud/src/main/java/org/jclouds/vcloud/xml/VAppHandler.java b/apis/vcloud/src/main/java/org/jclouds/vcloud/xml/VAppHandler.java index c161ba400f..9b380209b8 100644 --- a/apis/vcloud/src/main/java/org/jclouds/vcloud/xml/VAppHandler.java +++ b/apis/vcloud/src/main/java/org/jclouds/vcloud/xml/VAppHandler.java @@ -18,6 +18,7 @@ */ package org.jclouds.vcloud.xml; +import static org.jclouds.util.SaxUtils.equalsOrSuffix; import static org.jclouds.vcloud.util.Utils.newReferenceType; import java.util.List; @@ -69,47 +70,55 @@ public class VAppHandler extends ParseSax.HandlerWithResult { public VApp getResult() { return new VAppImpl(template.getName(), template.getType(), template.getHref(), status, vdc, description, tasks, - ovfDescriptorUploaded, children); + ovfDescriptorUploaded, children); } + protected int depth = 0; + @Override public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException { Map attributes = SaxUtils.cleanseAttributes(attrs); - if (qName.endsWith("Children")) { - inChildren = true; - } else if (qName.endsWith("Tasks")) { - inTasks = true; + depth++; + if (depth == 2) { + if (equalsOrSuffix(qName, "Children")) { + inChildren = true; + } else if (equalsOrSuffix(qName, "Tasks")) { + inTasks = true; + } } if (inChildren) { vmHandler.startElement(uri, localName, qName, attrs); } else if (inTasks) { taskHandler.startElement(uri, localName, qName, attrs); - } else if (qName.equals("VApp")) { + } else if (equalsOrSuffix(qName, "VApp")) { template = newReferenceType(attributes); if (attributes.containsKey("status")) this.status = Status.fromValue(Integer.parseInt(attributes.get("status"))); - } else if (qName.equals("Link") && "up".equals(attributes.get("rel"))) { + } else if (equalsOrSuffix(qName, "Link") && "up".equals(attributes.get("rel"))) { vdc = newReferenceType(attributes); } } public void endElement(String uri, String name, String qName) { - if (qName.endsWith("Children")) { - inChildren = false; - this.children.add(vmHandler.getResult()); - } else if (qName.endsWith("Tasks")) { - inTasks = false; - this.tasks.add(taskHandler.getResult()); + depth--; + if (depth == 1) { + if (equalsOrSuffix(qName, "Children")) { + inChildren = false; + this.children.add(vmHandler.getResult()); + } else if (equalsOrSuffix(qName, "Tasks")) { + inTasks = false; + this.tasks.add(taskHandler.getResult()); + } else if (equalsOrSuffix(qName, "Description")) { + description = SaxUtils.currentOrNull(currentText); + } } if (inChildren) { vmHandler.endElement(uri, name, qName); } else if (inTasks) { taskHandler.endElement(uri, name, qName); - } else if (qName.equals("Description")) { - description = currentOrNull(); - } else if (qName.equals("ovfDescriptorUploaded")) { - ovfDescriptorUploaded = Boolean.parseBoolean(currentOrNull()); + } else if (equalsOrSuffix(qName, "ovfDescriptorUploaded")) { + ovfDescriptorUploaded = Boolean.parseBoolean(SaxUtils.currentOrNull(currentText)); } currentText = new StringBuilder(); } @@ -122,8 +131,4 @@ public class VAppHandler extends ParseSax.HandlerWithResult { vmHandler.characters(ch, start, length); } - protected String currentOrNull() { - String returnVal = currentText.toString().trim(); - return returnVal.equals("") ? null : returnVal; - } } diff --git a/apis/vcloud/src/test/java/org/jclouds/vcloud/VCloudGuestCustomizationLiveTest.java b/apis/vcloud/src/test/java/org/jclouds/vcloud/VCloudGuestCustomizationLiveTest.java index 557debb35c..fa0f2f2d2f 100644 --- a/apis/vcloud/src/test/java/org/jclouds/vcloud/VCloudGuestCustomizationLiveTest.java +++ b/apis/vcloud/src/test/java/org/jclouds/vcloud/VCloudGuestCustomizationLiveTest.java @@ -43,6 +43,7 @@ import org.jclouds.ssh.SshClient; import org.jclouds.ssh.SshClient.Factory; import org.jclouds.ssh.jsch.config.JschSshClientModule; import org.jclouds.vcloud.compute.options.VCloudTemplateOptions; +import org.jclouds.vcloud.domain.VApp; import org.jclouds.vcloud.domain.Vm; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeGroups; @@ -54,9 +55,10 @@ import com.google.inject.Guice; import com.google.inject.Module; /** - * This tests that we can use guest customization as an alternative to bootstrapping with ssh. There - * are a few advantages to this, including the fact that it can work inside google appengine where - * network sockets (ssh:22) are prohibited. + * This tests that we can use guest customization as an alternative to + * bootstrapping with ssh. There are a few advantages to this, including the + * fact that it can work inside google appengine where network sockets (ssh:22) + * are prohibited. * * @author Adrian Cole */ @@ -117,7 +119,8 @@ public class VCloudGuestCustomizationLiveTest { return new JschSshClientModule(); } - // make sure the script has a lot of screwy characters, knowing our parser throws-out \r + // make sure the script has a lot of screwy characters, knowing our parser + // throws-out \r private String iLoveAscii = "I '\"love\"' {asc|!}*&"; String script = "cat > /root/foo.txt< request = createMock(GeneratedHttpRequest.class); expect(request.getEndpoint()).andReturn(URI.create("http://localhost/key")).anyTimes(); - expect(request.getArgs()).andReturn(ImmutableList.of()).atLeastOnce(); + expect(request.getArgs()).andReturn(ImmutableList. of(new InstantiateVAppTemplateOptions())) + .atLeastOnce(); request.setPayload(expected); expect(template.getNetworkSection()).andReturn(net).atLeastOnce(); - expect(net.getNetworks()) - .andReturn( - ImmutableSet - . of(new org.jclouds.ovf.Network( - "vAppNet-vApp Internal", null))); + expect(net.getNetworks()).andReturn( + ImmutableSet. of(new org.jclouds.ovf.Network("vAppNet-vApp Internal", null))); replay(request); replay(template); replay(net); BindInstantiateVAppTemplateParamsToXmlPayload binder = createInjector(templateUri, template).getInstance( - BindInstantiateVAppTemplateParamsToXmlPayload.class); + BindInstantiateVAppTemplateParamsToXmlPayload.class); + + Map map = Maps.newHashMap(); + map.put("name", "my-vapp"); + map.put("template", templateUri.toASCIIString()); + binder.bindToRequest(request, map); + + verify(request); + verify(template); + verify(net); + + } + + public void testDescription() throws IOException { + URI templateUri = URI.create("https://vcenterprise.bluelock.com/api/v1.0/vAppTemplate/3"); + VAppTemplate template = createMock(VAppTemplate.class); + VCloudNetworkSection net = createMock(VCloudNetworkSection.class); + + String expected = Strings2.toStringAndClose(getClass().getResourceAsStream("/instantiationparams-description.xml")); + GeneratedHttpRequest request = createMock(GeneratedHttpRequest.class); + expect(request.getEndpoint()).andReturn(URI.create("http://localhost/key")).anyTimes(); + expect(request.getArgs()).andReturn( + ImmutableList. of(new InstantiateVAppTemplateOptions().description("my foo"))).atLeastOnce(); + request.setPayload(expected); + + expect(template.getNetworkSection()).andReturn(net).atLeastOnce(); + expect(net.getNetworks()).andReturn( + ImmutableSet. of(new org.jclouds.ovf.Network("vAppNet-vApp Internal", null))); + + replay(request); + replay(template); + replay(net); + + BindInstantiateVAppTemplateParamsToXmlPayload binder = createInjector(templateUri, template).getInstance( + BindInstantiateVAppTemplateParamsToXmlPayload.class); Map map = Maps.newHashMap(); map.put("name", "my-vapp"); @@ -130,12 +162,12 @@ public class BindInstantiateVAppTemplateParamsToXmlPayloadTest { String expected = Strings2.toStringAndClose(getClass().getResourceAsStream("/instantiationparams.xml")); GeneratedHttpRequest request = createMock(GeneratedHttpRequest.class); expect(request.getEndpoint()).andReturn(URI.create("http://localhost/key")).anyTimes(); - expect(request.getArgs()).andReturn(ImmutableList.of()).atLeastOnce(); + expect(request.getArgs()).andReturn(ImmutableList. of()).atLeastOnce(); request.setPayload(expected); replay(request); BindInstantiateVAppTemplateParamsToXmlPayload binder = createInjector(templateUri, template).getInstance( - BindInstantiateVAppTemplateParamsToXmlPayload.class); + BindInstantiateVAppTemplateParamsToXmlPayload.class); Map map = Maps.newHashMap(); map.put("name", "my-vapp"); @@ -156,12 +188,12 @@ public class BindInstantiateVAppTemplateParamsToXmlPayloadTest { String expected = Strings2.toStringAndClose(getClass().getResourceAsStream("/instantiationparams.xml")); GeneratedHttpRequest request = createMock(GeneratedHttpRequest.class); expect(request.getEndpoint()).andReturn(URI.create("http://localhost/key")).anyTimes(); - expect(request.getArgs()).andReturn(ImmutableList.of( options)).atLeastOnce(); + expect(request.getArgs()).andReturn(ImmutableList. of(options)).atLeastOnce(); request.setPayload(expected); replay(request); BindInstantiateVAppTemplateParamsToXmlPayload binder = createInjector(templateUri, template).getInstance( - BindInstantiateVAppTemplateParamsToXmlPayload.class); + BindInstantiateVAppTemplateParamsToXmlPayload.class); Map map = Maps.newHashMap(); map.put("name", "my-vapp"); @@ -176,19 +208,19 @@ public class BindInstantiateVAppTemplateParamsToXmlPayloadTest { URI templateUri = URI.create("https://vcenterprise.bluelock.com/api/v1.0/vAppTemplate/3"); VAppTemplate template = null; - InstantiateVAppTemplateOptions options = addNetworkConfig(new NetworkConfig("aloha", URI - .create("https://vcenterprise.bluelock.com/api/v1.0/network/1991"), FenceMode.NAT_ROUTED)); + InstantiateVAppTemplateOptions options = addNetworkConfig(new NetworkConfig("aloha", + URI.create("https://vcenterprise.bluelock.com/api/v1.0/network/1991"), FenceMode.NAT_ROUTED)); String expected = Strings2.toStringAndClose(getClass().getResourceAsStream("/instantiationparams-network.xml")); GeneratedHttpRequest request = createMock(GeneratedHttpRequest.class); expect(request.getEndpoint()).andReturn(URI.create("http://localhost/key")).anyTimes(); - expect(request.getArgs()).andReturn(ImmutableList.of( options)).atLeastOnce(); + expect(request.getArgs()).andReturn(ImmutableList. of(options)).atLeastOnce(); request.setPayload(expected); replay(request); BindInstantiateVAppTemplateParamsToXmlPayload binder = createInjector(templateUri, template).getInstance( - BindInstantiateVAppTemplateParamsToXmlPayload.class); + BindInstantiateVAppTemplateParamsToXmlPayload.class); Map map = Maps.newHashMap(); map.put("name", "my-vapp"); @@ -205,27 +237,24 @@ public class BindInstantiateVAppTemplateParamsToXmlPayloadTest { VCloudNetworkSection net = createMock(VCloudNetworkSection.class); InstantiateVAppTemplateOptions options = customizeOnInstantiate(true); - String expected = Strings2 - .toStringAndClose(getClass().getResourceAsStream("/instantiationparams-customization.xml")); + String expected = Strings2.toStringAndClose(getClass().getResourceAsStream( + "/instantiationparams-customization.xml")); GeneratedHttpRequest request = createMock(GeneratedHttpRequest.class); expect(request.getEndpoint()).andReturn(URI.create("http://localhost/key")).anyTimes(); - expect(request.getArgs()).andReturn(ImmutableList.of( options)).atLeastOnce(); + expect(request.getArgs()).andReturn(ImmutableList. of(options)).atLeastOnce(); request.setPayload(expected); expect(template.getNetworkSection()).andReturn(net).atLeastOnce(); - expect(net.getNetworks()) - .andReturn( - ImmutableSet - . of(new org.jclouds.ovf.Network( - "vAppNet-vApp Internal", null))); + expect(net.getNetworks()).andReturn( + ImmutableSet. of(new org.jclouds.ovf.Network("vAppNet-vApp Internal", null))); replay(request); replay(template); replay(net); BindInstantiateVAppTemplateParamsToXmlPayload binder = createInjector(templateUri, template).getInstance( - BindInstantiateVAppTemplateParamsToXmlPayload.class); + BindInstantiateVAppTemplateParamsToXmlPayload.class); Map map = Maps.newHashMap(); map.put("name", "my-vapp"); diff --git a/apis/vcloud/src/test/java/org/jclouds/vcloud/compute/options/VCloudTemplateOptionsTest.java b/apis/vcloud/src/test/java/org/jclouds/vcloud/compute/options/VCloudTemplateOptionsTest.java index 72bdf8663c..5a8096608d 100644 --- a/apis/vcloud/src/test/java/org/jclouds/vcloud/compute/options/VCloudTemplateOptionsTest.java +++ b/apis/vcloud/src/test/java/org/jclouds/vcloud/compute/options/VCloudTemplateOptionsTest.java @@ -21,6 +21,7 @@ package org.jclouds.vcloud.compute.options; import static org.jclouds.vcloud.compute.options.VCloudTemplateOptions.Builder.authorizePublicKey; import static org.jclouds.vcloud.compute.options.VCloudTemplateOptions.Builder.blockOnPort; import static org.jclouds.vcloud.compute.options.VCloudTemplateOptions.Builder.customizationScript; +import static org.jclouds.vcloud.compute.options.VCloudTemplateOptions.Builder.description; import static org.jclouds.vcloud.compute.options.VCloudTemplateOptions.Builder.inboundPorts; import static org.jclouds.vcloud.compute.options.VCloudTemplateOptions.Builder.installPrivateKey; import static org.jclouds.vcloud.compute.options.VCloudTemplateOptions.Builder.ipAddressAllocationMode; @@ -34,7 +35,8 @@ import org.jclouds.vcloud.domain.network.IpAddressAllocationMode; import org.testng.annotations.Test; /** - * Tests possible uses of VCloudTemplateOptions and VCloudTemplateOptions.Builder.* + * Tests possible uses of VCloudTemplateOptions and + * VCloudTemplateOptions.Builder.* * * @author Adrian Cole */ @@ -82,6 +84,19 @@ public class VCloudTemplateOptionsTest { assertEquals(options.getCustomizationScript(), "mykeypair"); } + @Test + public void testdescription() { + VCloudTemplateOptions options = new VCloudTemplateOptions(); + options.description("mykeypair"); + assertEquals(options.getDescription(), "mykeypair"); + } + + @Test + public void testdescriptionStatic() { + VCloudTemplateOptions options = description("mykeypair"); + assertEquals(options.getDescription(), "mykeypair"); + } + @Test(expectedExceptions = IllegalArgumentException.class) public void testcustomizationScriptNPE() { customizationScript(null); diff --git a/apis/vcloud/src/test/java/org/jclouds/vcloud/options/InstantiateVAppTemplateOptionsTest.java b/apis/vcloud/src/test/java/org/jclouds/vcloud/options/InstantiateVAppTemplateOptionsTest.java index 50bd524bb8..5c46e14de3 100644 --- a/apis/vcloud/src/test/java/org/jclouds/vcloud/options/InstantiateVAppTemplateOptionsTest.java +++ b/apis/vcloud/src/test/java/org/jclouds/vcloud/options/InstantiateVAppTemplateOptionsTest.java @@ -20,6 +20,7 @@ package org.jclouds.vcloud.options; import static org.jclouds.vcloud.options.InstantiateVAppTemplateOptions.Builder.addNetworkConfig; import static org.jclouds.vcloud.options.InstantiateVAppTemplateOptions.Builder.customizeOnInstantiate; +import static org.jclouds.vcloud.options.InstantiateVAppTemplateOptions.Builder.description; import static org.testng.Assert.assertEquals; import java.net.URI; @@ -74,4 +75,16 @@ public class InstantiateVAppTemplateOptionsTest { assertEquals(options.shouldCustomizeOnInstantiate(), new Boolean(true)); } + @Test + public void testDescription() { + InstantiateVAppTemplateOptions options = new InstantiateVAppTemplateOptions(); + options.description("foo"); + assertEquals(options.getDescription(), "foo"); + } + + @Test + public void testDescriptionStatic() { + InstantiateVAppTemplateOptions options = description("foo"); + assertEquals(options.getDescription(), "foo"); + } } diff --git a/apis/vcloud/src/test/java/org/jclouds/vcloud/xml/VAppHandlerTest.java b/apis/vcloud/src/test/java/org/jclouds/vcloud/xml/VAppHandlerTest.java index 9705f35792..842c4958a7 100644 --- a/apis/vcloud/src/test/java/org/jclouds/vcloud/xml/VAppHandlerTest.java +++ b/apis/vcloud/src/test/java/org/jclouds/vcloud/xml/VAppHandlerTest.java @@ -61,6 +61,7 @@ public class VAppHandlerTest { Factory factory = injector.getInstance(ParseSax.Factory.class); VApp result = factory.create(injector.getInstance(VAppHandler.class)).parse(is); assertEquals(result.getName(), "vApp_acole_2"); + assertEquals(result.getDescription(), "foo"); assertEquals(result.getHref(), URI.create("https://vcenterprise.bluelock.com/api/v1.0/vApp/vapp-607806320")); assertEquals(result.getType(), "application/vnd.vmware.vcloud.vApp+xml"); assertEquals(result.getStatus(), Status.OFF); @@ -68,7 +69,6 @@ public class VAppHandlerTest { result.getVDC(), new ReferenceTypeImpl(null, VCloudMediaType.VDC_XML, URI .create("https://vcenterprise.bluelock.com/api/v1.0/vdc/1014839439"))); - assertEquals(result.getDescription(), null); assertEquals(result.getTasks(), ImmutableList.of()); assert result.isOvfDescriptorUploaded(); Vm vm = Iterables.getOnlyElement(result.getChildren()); diff --git a/apis/vcloud/src/test/resources/instantiationparams-description.xml b/apis/vcloud/src/test/resources/instantiationparams-description.xml new file mode 100644 index 0000000000..672829018e --- /dev/null +++ b/apis/vcloud/src/test/resources/instantiationparams-description.xml @@ -0,0 +1 @@ +my fooConfiguration parameters for logical networksbridgedtrue \ No newline at end of file diff --git a/apis/vcloud/src/test/resources/vapp-rhel-off-static.xml b/apis/vcloud/src/test/resources/vapp-rhel-off-static.xml index 6da1abe907..7e29ffcbd2 100644 --- a/apis/vcloud/src/test/resources/vapp-rhel-off-static.xml +++ b/apis/vcloud/src/test/resources/vapp-rhel-off-static.xml @@ -23,7 +23,7 @@ href="https://vcenterprise.bluelock.com/api/v1.0/vApp/vapp-607806320" /> - + foo