You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@stratos.apache.org by la...@apache.org on 2014/03/31 09:13:43 UTC
[30/52] [partial] Moving jclouds dependencies to accurate parent
directories
http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/19c8cdaf/dependencies/jclouds/apis/vcloud/1.7.1-stratos/src/main/java/org/jclouds/vcloud/binders/OrgNameVDCNameResourceNameToEndpoint.java
----------------------------------------------------------------------
diff --git a/dependencies/jclouds/apis/vcloud/1.7.1-stratos/src/main/java/org/jclouds/vcloud/binders/OrgNameVDCNameResourceNameToEndpoint.java b/dependencies/jclouds/apis/vcloud/1.7.1-stratos/src/main/java/org/jclouds/vcloud/binders/OrgNameVDCNameResourceNameToEndpoint.java
new file mode 100755
index 0000000..79d990e
--- /dev/null
+++ b/dependencies/jclouds/apis/vcloud/1.7.1-stratos/src/main/java/org/jclouds/vcloud/binders/OrgNameVDCNameResourceNameToEndpoint.java
@@ -0,0 +1,81 @@
+/*
+ * 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.binders;
+
+import static com.google.common.base.Preconditions.checkState;
+
+import java.net.URI;
+import java.util.Map;
+import java.util.NoSuchElementException;
+
+import javax.inject.Inject;
+
+import org.jclouds.http.HttpRequest;
+import org.jclouds.rest.MapBinder;
+import org.jclouds.vcloud.domain.ReferenceType;
+import org.jclouds.vcloud.domain.VDC;
+import org.jclouds.vcloud.endpoints.Org;
+
+import com.google.common.base.Supplier;
+/**
+ *
+ * @author Adrian Cole
+ */
+public abstract class OrgNameVDCNameResourceNameToEndpoint implements MapBinder {
+
+ protected final Supplier<Map<String, Map<String, org.jclouds.vcloud.domain.VDC>>> orgVDCMap;
+ protected final Supplier<ReferenceType> defaultOrg;
+ protected final Supplier<ReferenceType> defaultVDC;
+
+ @Inject
+ public OrgNameVDCNameResourceNameToEndpoint(
+ Supplier<Map<String, Map<String, org.jclouds.vcloud.domain.VDC>>> orgVDCMap,
+ @Org Supplier<ReferenceType> defaultOrg, @org.jclouds.vcloud.endpoints.VDC Supplier<ReferenceType> defaultVDC) {
+ this.orgVDCMap = orgVDCMap;
+ this.defaultOrg = defaultOrg;
+ this.defaultVDC = defaultVDC;
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public <R extends HttpRequest> R bindToRequest(R request, Map<String, Object> postParams) {
+ Object org = postParams.get("orgName");
+ Object vDC = postParams.get("vdcName");
+ Object resource = postParams.get("resourceName");
+ if (org == null)
+ org = defaultOrg.get().getName();
+ if (vDC == null)
+ vDC = defaultVDC.get().getName();
+ Map<String, Map<String, org.jclouds.vcloud.domain.VDC>> orgToVDCs = orgVDCMap.get();
+ checkState(orgToVDCs != null, "could not get map of org name to vdcs!");
+ Map<String, org.jclouds.vcloud.domain.VDC> vDCs = orgToVDCs.get(org);
+ if (vDCs == null)
+ throw new NoSuchElementException("org " + org + " not found in " + orgToVDCs.keySet());
+ org.jclouds.vcloud.domain.VDC vDCObject = vDCs.get(vDC);
+ if (vDCObject == null)
+ throw new NoSuchElementException("vdc " + vDC + " in org " + org + " not found in " + vDCs.keySet());
+ URI endpoint = getEndpointOfResourceInVDC(org, vDC, resource, vDCObject);
+ return (R) request.toBuilder().endpoint(endpoint).build();
+ }
+
+ protected abstract URI getEndpointOfResourceInVDC(Object org, Object vDC, Object resource, VDC vDCObject);
+
+ @Override
+ public <R extends HttpRequest> R bindToRequest(R request, Object input) {
+ throw new IllegalStateException(getClass() + " needs parameters");
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/19c8cdaf/dependencies/jclouds/apis/vcloud/1.7.1-stratos/src/main/java/org/jclouds/vcloud/compute/config/VCloudComputeServiceContextModule.java
----------------------------------------------------------------------
diff --git a/dependencies/jclouds/apis/vcloud/1.7.1-stratos/src/main/java/org/jclouds/vcloud/compute/config/VCloudComputeServiceContextModule.java b/dependencies/jclouds/apis/vcloud/1.7.1-stratos/src/main/java/org/jclouds/vcloud/compute/config/VCloudComputeServiceContextModule.java
new file mode 100755
index 0000000..2c994af
--- /dev/null
+++ b/dependencies/jclouds/apis/vcloud/1.7.1-stratos/src/main/java/org/jclouds/vcloud/compute/config/VCloudComputeServiceContextModule.java
@@ -0,0 +1,39 @@
+/*
+ * 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.compute.config;
+
+import org.jclouds.compute.config.ComputeServiceAdapterContextModule;
+import org.jclouds.domain.Location;
+import org.jclouds.vcloud.domain.VApp;
+import org.jclouds.vcloud.domain.VAppTemplate;
+
+/**
+ * Configures the {@link VCloudComputeServiceContext}; requires {@link VCloudComputeClientImpl}
+ * bound.
+ *
+ * @author Adrian Cole
+ */
+public class VCloudComputeServiceContextModule extends
+ ComputeServiceAdapterContextModule<VApp, VAppTemplate, VAppTemplate, Location> {
+
+ @Override
+ protected void configure() {
+ super.configure();
+ install(new VCloudComputeServiceDependenciesModule());
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/19c8cdaf/dependencies/jclouds/apis/vcloud/1.7.1-stratos/src/main/java/org/jclouds/vcloud/compute/config/VCloudComputeServiceDependenciesModule.java
----------------------------------------------------------------------
diff --git a/dependencies/jclouds/apis/vcloud/1.7.1-stratos/src/main/java/org/jclouds/vcloud/compute/config/VCloudComputeServiceDependenciesModule.java b/dependencies/jclouds/apis/vcloud/1.7.1-stratos/src/main/java/org/jclouds/vcloud/compute/config/VCloudComputeServiceDependenciesModule.java
new file mode 100755
index 0000000..b975b96
--- /dev/null
+++ b/dependencies/jclouds/apis/vcloud/1.7.1-stratos/src/main/java/org/jclouds/vcloud/compute/config/VCloudComputeServiceDependenciesModule.java
@@ -0,0 +1,154 @@
+/*
+ * 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.compute.config;
+
+
+import java.util.Map;
+
+import javax.inject.Singleton;
+
+import org.jclouds.compute.ComputeServiceAdapter;
+import org.jclouds.compute.domain.Hardware;
+import org.jclouds.compute.domain.Image;
+import org.jclouds.compute.domain.NodeMetadata;
+import org.jclouds.compute.domain.TemplateBuilder;
+import org.jclouds.compute.options.TemplateOptions;
+import org.jclouds.domain.Location;
+import org.jclouds.functions.IdentityFunction;
+import org.jclouds.vcloud.compute.functions.HardwareForVApp;
+import org.jclouds.vcloud.compute.functions.HardwareForVAppTemplate;
+import org.jclouds.vcloud.compute.functions.ImageForVAppTemplate;
+import org.jclouds.vcloud.compute.functions.VAppToNodeMetadata;
+import org.jclouds.vcloud.compute.internal.VCloudTemplateBuilderImpl;
+import org.jclouds.vcloud.compute.options.VCloudTemplateOptions;
+import org.jclouds.vcloud.compute.strategy.VCloudComputeServiceAdapter;
+import org.jclouds.vcloud.domain.Org;
+import org.jclouds.vcloud.domain.ReferenceType;
+import org.jclouds.vcloud.domain.Status;
+import org.jclouds.vcloud.domain.VApp;
+import org.jclouds.vcloud.domain.VAppTemplate;
+import org.jclouds.vcloud.domain.network.FenceMode;
+import org.jclouds.vcloud.domain.network.NetworkConfig;
+import org.jclouds.vcloud.endpoints.Network;
+import org.jclouds.vcloud.functions.VAppTemplatesInOrg;
+
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Function;
+import com.google.common.base.Supplier;
+import com.google.common.base.Suppliers;
+import com.google.common.collect.ImmutableMap;
+import com.google.inject.AbstractModule;
+import com.google.inject.Provides;
+import com.google.inject.TypeLiteral;
+
+/**
+ *
+ * @author Adrian Cole
+ */
+public class VCloudComputeServiceDependenciesModule extends AbstractModule {
+
+ @VisibleForTesting
+ public static final Map<Status, NodeMetadata.Status> toPortableNodeStatus = ImmutableMap
+ .<Status, NodeMetadata.Status> builder()
+ .put(Status.OFF, NodeMetadata.Status.SUSPENDED)
+ .put(Status.ON, NodeMetadata.Status.RUNNING)
+ .put(Status.RESOLVED, NodeMetadata.Status.PENDING)
+ .put(Status.MIXED, NodeMetadata.Status.PENDING)
+ .put(Status.UNKNOWN, NodeMetadata.Status.UNRECOGNIZED)
+ .put(Status.UNRECOGNIZED, NodeMetadata.Status.UNRECOGNIZED)
+ .put(Status.DEPLOYED, NodeMetadata.Status.PENDING)
+ .put(Status.SUSPENDED, NodeMetadata.Status.SUSPENDED)
+ .put(Status.WAITING_FOR_INPUT, NodeMetadata.Status.PENDING)
+ .put(Status.INCONSISTENT, NodeMetadata.Status.PENDING)
+ .put(Status.ERROR, NodeMetadata.Status.ERROR)
+ .put(Status.UNRESOLVED, NodeMetadata.Status.PENDING).build();
+
+ @Singleton
+ @Provides
+ protected Map<Status, NodeMetadata.Status> toPortableNodeStatus() {
+ return toPortableNodeStatus;
+ }
+
+ @VisibleForTesting
+ public static final Map<Status, Image.Status> toPortableImageStatus = ImmutableMap
+ .<Status, Image.Status> builder()
+ .put(Status.RESOLVED, Image.Status.AVAILABLE)
+ .put(Status.OFF, Image.Status.AVAILABLE)
+ .put(Status.MIXED, Image.Status.PENDING)
+ .put(Status.UNKNOWN, Image.Status.UNRECOGNIZED)
+ .put(Status.UNRECOGNIZED, Image.Status.UNRECOGNIZED)
+ .put(Status.DEPLOYED, Image.Status.PENDING)
+ .put(Status.PENDING_DESCRIPTOR, Image.Status.PENDING)
+ .put(Status.COPYING, Image.Status.PENDING)
+ .put(Status.PENDING_CONTENTS, Image.Status.PENDING)
+ .put(Status.QUARANTINED, Image.Status.PENDING)
+ .put(Status.QUARANTINE_EXPIRED, Image.Status.ERROR)
+ .put(Status.REJECTED, Image.Status.ERROR)
+ .put(Status.TRANSFER_TIMEOUT, Image.Status.ERROR)
+ .put(Status.ERROR, Image.Status.ERROR)
+ .put(Status.UNRESOLVED, Image.Status.PENDING).build();
+
+ @Singleton
+ @Provides
+ protected Map<Status, Image.Status> toPortableImageStatus() {
+ return toPortableImageStatus;
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ protected void configure() {
+ bind(new TypeLiteral<ComputeServiceAdapter<VApp, VAppTemplate, VAppTemplate, Location>>() {
+ }).to(VCloudComputeServiceAdapter.class);
+
+ bind(new TypeLiteral<Function<VApp, NodeMetadata>>() {
+ }).to(VAppToNodeMetadata.class);
+
+ bind(TemplateOptions.class).to(VCloudTemplateOptions.class);
+ bind(TemplateBuilder.class).to(VCloudTemplateBuilderImpl.class);
+
+ bind(new TypeLiteral<Function<VApp, Hardware>>() {
+ }).to(new TypeLiteral<HardwareForVApp>() {
+ });
+
+ bind(new TypeLiteral<Function<Org, Iterable<VAppTemplate>>>() {
+ }).to(VAppTemplatesInOrg.class);
+ bind(new TypeLiteral<Function<VAppTemplate, Image>>() {
+ }).to(ImageForVAppTemplate.class);
+ bind(new TypeLiteral<Function<VAppTemplate, Hardware>>() {
+ }).to(HardwareForVAppTemplate.class);
+
+ // we aren't converting from a provider-specific type
+ bind(new TypeLiteral<Function<Location, Location>>() {
+ }).to(Class.class.cast(IdentityFunction.class));
+ }
+
+
+ @Provides
+ @Singleton
+ public Supplier<NetworkConfig> networkConfig(@Network Supplier<ReferenceType> network,
+ final FenceMode defaultFenceMode) {
+ return Suppliers.compose(new Function<ReferenceType, NetworkConfig>() {
+
+ @Override
+ public NetworkConfig apply(ReferenceType input) {
+ return new NetworkConfig(input.getName(), input.getHref(), defaultFenceMode);
+ }
+
+ }, network);
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/19c8cdaf/dependencies/jclouds/apis/vcloud/1.7.1-stratos/src/main/java/org/jclouds/vcloud/compute/functions/FindLocationForResource.java
----------------------------------------------------------------------
diff --git a/dependencies/jclouds/apis/vcloud/1.7.1-stratos/src/main/java/org/jclouds/vcloud/compute/functions/FindLocationForResource.java b/dependencies/jclouds/apis/vcloud/1.7.1-stratos/src/main/java/org/jclouds/vcloud/compute/functions/FindLocationForResource.java
new file mode 100755
index 0000000..346016a
--- /dev/null
+++ b/dependencies/jclouds/apis/vcloud/1.7.1-stratos/src/main/java/org/jclouds/vcloud/compute/functions/FindLocationForResource.java
@@ -0,0 +1,69 @@
+/*
+ * 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.compute.functions;
+
+import java.net.URI;
+import java.util.NoSuchElementException;
+import java.util.Set;
+
+import javax.annotation.Resource;
+import javax.inject.Inject;
+import javax.inject.Singleton;
+
+import org.jclouds.collect.Memoized;
+import org.jclouds.domain.Location;
+import org.jclouds.logging.Logger;
+import org.jclouds.vcloud.domain.ReferenceType;
+
+import com.google.common.base.Function;
+import com.google.common.base.Supplier;
+
+/**
+ * @author Adrian Cole
+ */
+@Singleton
+public class FindLocationForResource implements Function<ReferenceType, Location> {
+
+ @Resource
+ protected Logger logger = Logger.NULL;
+
+ final Supplier<Set<? extends Location>> locations;
+
+ @Inject
+ public FindLocationForResource(@Memoized Supplier<Set<? extends Location>> locations) {
+ this.locations = locations;
+ }
+
+ /**
+ * searches for a location associated with this resource.
+ *
+ * @throws NoSuchElementException
+ * if not found
+ */
+ public Location apply(ReferenceType resource) {
+ for (Location input : locations.get()) {
+ do {
+ // The "name" isn't always present, ex inside a vApp we have a rel
+ // link that only includes href and type.
+ if (URI.create(input.getId()).equals(resource.getHref()))
+ return input;
+ } while ((input = input.getParent()) != null);
+ }
+ throw new NoSuchElementException(String.format("resource: %s not found in locations: %s", resource,
+ locations.get()));
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/19c8cdaf/dependencies/jclouds/apis/vcloud/1.7.1-stratos/src/main/java/org/jclouds/vcloud/compute/functions/HardwareForVApp.java
----------------------------------------------------------------------
diff --git a/dependencies/jclouds/apis/vcloud/1.7.1-stratos/src/main/java/org/jclouds/vcloud/compute/functions/HardwareForVApp.java b/dependencies/jclouds/apis/vcloud/1.7.1-stratos/src/main/java/org/jclouds/vcloud/compute/functions/HardwareForVApp.java
new file mode 100755
index 0000000..558c78a
--- /dev/null
+++ b/dependencies/jclouds/apis/vcloud/1.7.1-stratos/src/main/java/org/jclouds/vcloud/compute/functions/HardwareForVApp.java
@@ -0,0 +1,71 @@
+/*
+ * 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.compute.functions;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+import javax.annotation.Resource;
+import javax.inject.Inject;
+
+import org.jclouds.compute.domain.Hardware;
+import org.jclouds.compute.domain.HardwareBuilder;
+import org.jclouds.compute.predicates.ImagePredicates;
+import org.jclouds.domain.Location;
+import org.jclouds.logging.Logger;
+import org.jclouds.ovf.VirtualHardwareSection;
+import org.jclouds.vcloud.domain.ReferenceType;
+import org.jclouds.vcloud.domain.VApp;
+import org.jclouds.vcloud.domain.Vm;
+
+import com.google.common.base.Function;
+import com.google.common.collect.Iterables;
+
+/**
+ * @author Adrian Cole
+ */
+public class HardwareForVApp implements Function<VApp, Hardware> {
+
+ @Resource
+ protected Logger logger = Logger.NULL;
+
+ private final Function<ReferenceType, Location> findLocationForResource;
+ private final VCloudHardwareBuilderFromResourceAllocations rasdToHardwareBuilder;
+
+ @Inject
+ protected HardwareForVApp(Function<ReferenceType, Location> findLocationForResource,
+ VCloudHardwareBuilderFromResourceAllocations rasdToHardwareBuilder) {
+ this.findLocationForResource = checkNotNull(findLocationForResource, "findLocationForResource");
+ this.rasdToHardwareBuilder = checkNotNull(rasdToHardwareBuilder, "rasdToHardwareBuilder");
+ }
+
+ @Override
+ public Hardware apply(VApp from) {
+ checkNotNull(from, "VApp");
+ // TODO make this work with composite vApps
+ Vm vm = from.getChildren().size() == 0 ? null : Iterables.get(from.getChildren(), 0);
+ if (vm == null)
+ return null;
+
+ VirtualHardwareSection hardware = vm.getVirtualHardwareSection();
+ HardwareBuilder builder = rasdToHardwareBuilder.apply(hardware.getItems());
+ builder.location(findLocationForResource.apply(checkNotNull(from, "from").getVDC()));
+ builder.ids(from.getHref().toASCIIString()).name(from.getName()).supportsImage(
+ ImagePredicates.idEquals(from.getHref().toASCIIString()));
+ builder.hypervisor("VMware");
+ return builder.build();
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/19c8cdaf/dependencies/jclouds/apis/vcloud/1.7.1-stratos/src/main/java/org/jclouds/vcloud/compute/functions/HardwareForVAppTemplate.java
----------------------------------------------------------------------
diff --git a/dependencies/jclouds/apis/vcloud/1.7.1-stratos/src/main/java/org/jclouds/vcloud/compute/functions/HardwareForVAppTemplate.java b/dependencies/jclouds/apis/vcloud/1.7.1-stratos/src/main/java/org/jclouds/vcloud/compute/functions/HardwareForVAppTemplate.java
new file mode 100755
index 0000000..2671c26
--- /dev/null
+++ b/dependencies/jclouds/apis/vcloud/1.7.1-stratos/src/main/java/org/jclouds/vcloud/compute/functions/HardwareForVAppTemplate.java
@@ -0,0 +1,84 @@
+/*
+ * 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.compute.functions;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+import javax.annotation.Resource;
+import javax.inject.Inject;
+import javax.inject.Singleton;
+
+import org.jclouds.compute.domain.Hardware;
+import org.jclouds.compute.domain.HardwareBuilder;
+import org.jclouds.compute.predicates.ImagePredicates;
+import org.jclouds.logging.Logger;
+import org.jclouds.ovf.Envelope;
+import org.jclouds.ovf.VirtualHardwareSection;
+import org.jclouds.vcloud.domain.VAppTemplate;
+
+import com.google.common.base.Function;
+import com.google.common.collect.Iterables;
+
+/**
+ * @author Adrian Cole
+ */
+@Singleton
+public class HardwareForVAppTemplate implements Function<VAppTemplate, Hardware> {
+
+ @Resource
+ protected Logger logger = Logger.NULL;
+
+ private final Function<VAppTemplate, Envelope> templateToEnvelope;
+ private final FindLocationForResource findLocationForResource;
+ private final VCloudHardwareBuilderFromResourceAllocations rasdToHardwareBuilder;
+
+ @Inject
+ protected HardwareForVAppTemplate(Function<VAppTemplate, Envelope> templateToEnvelope,
+ FindLocationForResource findLocationForResource,
+ VCloudHardwareBuilderFromResourceAllocations rasdToHardwareBuilder) {
+ this.templateToEnvelope = checkNotNull(templateToEnvelope, "templateToEnvelope");
+ this.findLocationForResource = checkNotNull(findLocationForResource, "findLocationForResource");
+ this.rasdToHardwareBuilder = checkNotNull(rasdToHardwareBuilder, "rasdToHardwareBuilder");
+ }
+
+ @Override
+ public Hardware apply(VAppTemplate from) {
+ checkNotNull(from, "VAppTemplate");
+
+ Envelope ovf = templateToEnvelope.apply(from);
+
+ if (ovf.getVirtualSystem().getVirtualHardwareSections().size() > 1) {
+ logger.warn("multiple hardware choices found. using first", ovf);
+ }
+ VirtualHardwareSection hardware = Iterables.get(ovf.getVirtualSystem().getVirtualHardwareSections(), 0);
+ HardwareBuilder builder = rasdToHardwareBuilder.apply(hardware.getItems());
+ if (from.getVDC() != null) {
+ builder.location(findLocationForResource.apply(from.getVDC()));
+ } else {
+ // otherwise, it could be in a public catalog, which is not assigned to a VDC
+ }
+ builder.ids(from.getHref().toASCIIString()).name(from.getName()).supportsImage(
+ ImagePredicates.idEquals(from.getHref().toASCIIString()));
+ builder.hypervisor("VMware");
+ return builder.build();
+
+ }
+
+ protected String getName(String name) {
+ return name;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/19c8cdaf/dependencies/jclouds/apis/vcloud/1.7.1-stratos/src/main/java/org/jclouds/vcloud/compute/functions/ImageForVAppTemplate.java
----------------------------------------------------------------------
diff --git a/dependencies/jclouds/apis/vcloud/1.7.1-stratos/src/main/java/org/jclouds/vcloud/compute/functions/ImageForVAppTemplate.java b/dependencies/jclouds/apis/vcloud/1.7.1-stratos/src/main/java/org/jclouds/vcloud/compute/functions/ImageForVAppTemplate.java
new file mode 100755
index 0000000..9afc714
--- /dev/null
+++ b/dependencies/jclouds/apis/vcloud/1.7.1-stratos/src/main/java/org/jclouds/vcloud/compute/functions/ImageForVAppTemplate.java
@@ -0,0 +1,82 @@
+/*
+ * 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.compute.functions;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+import java.util.Map;
+
+import javax.annotation.Resource;
+import javax.inject.Inject;
+import javax.inject.Named;
+import javax.inject.Singleton;
+
+import org.jclouds.compute.domain.CIMOperatingSystem;
+import org.jclouds.compute.domain.Image;
+import org.jclouds.compute.domain.ImageBuilder;
+import org.jclouds.compute.reference.ComputeServiceConstants;
+import org.jclouds.logging.Logger;
+import org.jclouds.ovf.Envelope;
+import org.jclouds.vcloud.domain.Status;
+import org.jclouds.vcloud.domain.VAppTemplate;
+
+import com.google.common.base.Function;
+
+/**
+ * @author Adrian Cole
+ */
+@Singleton
+public class ImageForVAppTemplate implements Function<VAppTemplate, Image> {
+
+ @Resource
+ @Named(ComputeServiceConstants.COMPUTE_LOGGER)
+ public Logger logger = Logger.NULL;
+
+ private final Map<Status, Image.Status> toPortableImageStatus;
+ private final Function<VAppTemplate, Envelope> templateToEnvelope;
+ private final FindLocationForResource findLocationForResource;
+
+
+ @Inject
+ protected ImageForVAppTemplate(Map<Status, Image.Status> toPortableImageStatus, Function<VAppTemplate, Envelope> templateToEnvelope,
+ FindLocationForResource findLocationForResource) {
+ this.toPortableImageStatus = checkNotNull(toPortableImageStatus, "toPortableImageStatus");
+ this.templateToEnvelope = checkNotNull(templateToEnvelope, "templateToEnvelope");
+ this.findLocationForResource = checkNotNull(findLocationForResource, "findLocationForResource");
+ }
+
+ @Override
+ public Image apply(VAppTemplate from) {
+ checkNotNull(from, "VAppTemplate");
+ Envelope ovf = templateToEnvelope.apply(from);
+
+ ImageBuilder builder = new ImageBuilder();
+ builder.ids(from.getHref().toASCIIString());
+ builder.uri(from.getHref());
+ builder.name(from.getName());
+ if (from.getVDC() != null) {
+ builder.location(findLocationForResource.apply(from.getVDC()));
+ } else {
+ // otherwise, it could be in a public catalog, which is not assigned to a VDC
+ }
+ builder.description(from.getDescription() != null ? from.getDescription() : from.getName());
+ builder.operatingSystem(CIMOperatingSystem.toComputeOs(ovf));
+ builder.status(toPortableImageStatus.get(from.getStatus()));
+ return builder.build();
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/19c8cdaf/dependencies/jclouds/apis/vcloud/1.7.1-stratos/src/main/java/org/jclouds/vcloud/compute/functions/VAppToNodeMetadata.java
----------------------------------------------------------------------
diff --git a/dependencies/jclouds/apis/vcloud/1.7.1-stratos/src/main/java/org/jclouds/vcloud/compute/functions/VAppToNodeMetadata.java b/dependencies/jclouds/apis/vcloud/1.7.1-stratos/src/main/java/org/jclouds/vcloud/compute/functions/VAppToNodeMetadata.java
new file mode 100755
index 0000000..bebbbce
--- /dev/null
+++ b/dependencies/jclouds/apis/vcloud/1.7.1-stratos/src/main/java/org/jclouds/vcloud/compute/functions/VAppToNodeMetadata.java
@@ -0,0 +1,112 @@
+/*
+ * 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.compute.functions;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static com.google.common.base.Predicates.not;
+import static com.google.common.base.Strings.isNullOrEmpty;
+import static com.google.common.collect.Iterables.filter;
+import static org.jclouds.compute.util.ComputeServiceUtils.addMetadataAndParseTagsFromCommaDelimitedValue;
+import static org.jclouds.compute.util.ComputeServiceUtils.groupFromMapOrName;
+import static org.jclouds.vcloud.compute.util.VCloudComputeUtils.getCredentialsFrom;
+import static org.jclouds.vcloud.compute.util.VCloudComputeUtils.getIpsFromVApp;
+import static org.jclouds.vcloud.compute.util.VCloudComputeUtils.toComputeOs;
+
+import java.util.Map;
+import java.util.Set;
+
+import javax.annotation.Resource;
+import javax.inject.Inject;
+import javax.inject.Singleton;
+
+import org.jclouds.compute.domain.Hardware;
+import org.jclouds.compute.domain.NodeMetadata;
+import org.jclouds.compute.domain.NodeMetadataBuilder;
+import org.jclouds.compute.functions.GroupNamingConvention;
+import org.jclouds.domain.Credentials;
+import org.jclouds.logging.Logger;
+import org.jclouds.util.InetAddresses2.IsPrivateIPAddress;
+import org.jclouds.vcloud.domain.Status;
+import org.jclouds.vcloud.domain.VApp;
+
+import com.google.common.base.Function;
+import com.google.common.base.Splitter;
+import com.google.common.collect.ImmutableMap;
+
+/** @author Adrian Cole */
+@Singleton
+public class VAppToNodeMetadata implements Function<VApp, NodeMetadata> {
+ @Resource
+ protected static Logger logger = Logger.NULL;
+
+ protected final FindLocationForResource findLocationForResourceInVDC;
+ protected final Function<VApp, Hardware> hardwareForVApp;
+ protected final Map<Status, NodeMetadata.Status> vAppStatusToNodeStatus;
+ protected final Map<String, Credentials> credentialStore;
+ protected final GroupNamingConvention nodeNamingConvention;
+
+ @Inject
+ protected VAppToNodeMetadata(Map<Status, NodeMetadata.Status> vAppStatusToNodeStatus, Map<String, Credentials> credentialStore,
+ FindLocationForResource findLocationForResourceInVDC, Function<VApp, Hardware> hardwareForVApp,
+ GroupNamingConvention.Factory namingConvention) {
+ this.nodeNamingConvention = checkNotNull(namingConvention, "namingConvention").createWithoutPrefix();
+ this.hardwareForVApp = checkNotNull(hardwareForVApp, "hardwareForVApp");
+ this.findLocationForResourceInVDC = checkNotNull(findLocationForResourceInVDC, "findLocationForResourceInVDC");
+ this.credentialStore = checkNotNull(credentialStore, "credentialStore");
+ this.vAppStatusToNodeStatus = checkNotNull(vAppStatusToNodeStatus, "vAppStatusToNodeStatus");
+ }
+
+ public NodeMetadata apply(VApp from) {
+ NodeMetadataBuilder builder = new NodeMetadataBuilder();
+ builder.ids(from.getHref().toASCIIString());
+ builder.uri(from.getHref());
+ builder.name(from.getName());
+ String groupName = "";
+ Map<String, String> metadataMap;
+
+ if (!isNullOrEmpty(from.getDescription())
+ && from.getDescription().indexOf('=') != -1
+ && from.getDescription().indexOf('\n') != -1) {
+ try {
+ metadataMap = Splitter.on('\n').withKeyValueSeparator("=").split(from.getDescription());
+
+ addMetadataAndParseTagsFromCommaDelimitedValue(builder, metadataMap);
+ } catch (IllegalArgumentException iae) {
+ // no op
+ metadataMap = ImmutableMap.of();
+ }
+ } else {
+ metadataMap = ImmutableMap.of();
+ }
+ builder.hostname(from.getName());
+ builder.location(findLocationForResourceInVDC.apply(from.getVDC()));
+ builder.group(groupFromMapOrName(metadataMap, from.getName(), nodeNamingConvention));
+ builder.operatingSystem(toComputeOs(from, null));
+ builder.hardware(hardwareForVApp.apply(from));
+ builder.status(vAppStatusToNodeStatus.get(from.getStatus()));
+ Set<String> addresses = getIpsFromVApp(from);
+ builder.publicAddresses(filter(addresses, not(IsPrivateIPAddress.INSTANCE)));
+ builder.privateAddresses(filter(addresses, IsPrivateIPAddress.INSTANCE));
+
+ // normally, we don't affect the credential store when reading vApps.
+ // However, login user, etc, is actually in the metadata, so lets see
+ Credentials fromApi = getCredentialsFrom(from);
+ if (fromApi != null && !credentialStore.containsKey("node#" + from.getHref().toASCIIString()))
+ credentialStore.put("node#" + from.getHref().toASCIIString(), fromApi);
+ return builder.build();
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/19c8cdaf/dependencies/jclouds/apis/vcloud/1.7.1-stratos/src/main/java/org/jclouds/vcloud/compute/functions/VCloudHardwareBuilderFromResourceAllocations.java
----------------------------------------------------------------------
diff --git a/dependencies/jclouds/apis/vcloud/1.7.1-stratos/src/main/java/org/jclouds/vcloud/compute/functions/VCloudHardwareBuilderFromResourceAllocations.java b/dependencies/jclouds/apis/vcloud/1.7.1-stratos/src/main/java/org/jclouds/vcloud/compute/functions/VCloudHardwareBuilderFromResourceAllocations.java
new file mode 100755
index 0000000..302ff47
--- /dev/null
+++ b/dependencies/jclouds/apis/vcloud/1.7.1-stratos/src/main/java/org/jclouds/vcloud/compute/functions/VCloudHardwareBuilderFromResourceAllocations.java
@@ -0,0 +1,43 @@
+/*
+ * 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.compute.functions;
+
+import javax.inject.Singleton;
+
+import org.jclouds.cim.ResourceAllocationSettingData;
+import org.jclouds.cim.functions.HardwareBuilderFromResourceAllocations;
+import org.jclouds.compute.domain.Volume;
+import org.jclouds.compute.domain.internal.VolumeImpl;
+import org.jclouds.vcloud.domain.ovf.VCloudHardDisk;
+
+/**
+ * @author Adrian Cole
+ */
+@Singleton
+public class VCloudHardwareBuilderFromResourceAllocations extends HardwareBuilderFromResourceAllocations {
+ @Override
+ public Volume apply(ResourceAllocationSettingData from) {
+ if (from instanceof VCloudHardDisk) {
+ VCloudHardDisk vDisk = VCloudHardDisk.class.cast(from);
+ return new VolumeImpl(from.getAddressOnParent() + "", Volume.Type.LOCAL, vDisk.getCapacity() / 1024f, null,
+ "0".equals(from.getAddressOnParent()), true);
+ } else {
+ return super.apply(from);
+ }
+
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/19c8cdaf/dependencies/jclouds/apis/vcloud/1.7.1-stratos/src/main/java/org/jclouds/vcloud/compute/functions/ValidateVAppTemplateAndReturnEnvelopeOrThrowIllegalArgumentException.java
----------------------------------------------------------------------
diff --git a/dependencies/jclouds/apis/vcloud/1.7.1-stratos/src/main/java/org/jclouds/vcloud/compute/functions/ValidateVAppTemplateAndReturnEnvelopeOrThrowIllegalArgumentException.java b/dependencies/jclouds/apis/vcloud/1.7.1-stratos/src/main/java/org/jclouds/vcloud/compute/functions/ValidateVAppTemplateAndReturnEnvelopeOrThrowIllegalArgumentException.java
new file mode 100755
index 0000000..cecd812
--- /dev/null
+++ b/dependencies/jclouds/apis/vcloud/1.7.1-stratos/src/main/java/org/jclouds/vcloud/compute/functions/ValidateVAppTemplateAndReturnEnvelopeOrThrowIllegalArgumentException.java
@@ -0,0 +1,77 @@
+/*
+ * 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.compute.functions;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkNotNull;
+
+import java.net.URI;
+import java.util.concurrent.ExecutionException;
+
+import javax.annotation.Resource;
+import javax.inject.Inject;
+import javax.inject.Singleton;
+
+import org.jclouds.logging.Logger;
+import org.jclouds.ovf.Envelope;
+import org.jclouds.vcloud.domain.VAppTemplate;
+
+import com.google.common.base.Function;
+import com.google.common.cache.LoadingCache;
+
+/**
+ * @author Adrian Cole
+ */
+@Singleton
+public class ValidateVAppTemplateAndReturnEnvelopeOrThrowIllegalArgumentException implements
+ Function<VAppTemplate, Envelope> {
+
+ @Resource
+ protected Logger logger = Logger.NULL;
+
+ private final LoadingCache<URI, Envelope> envelopes;
+
+ @Inject
+ protected ValidateVAppTemplateAndReturnEnvelopeOrThrowIllegalArgumentException(LoadingCache<URI, Envelope> envelopes) {
+ this.envelopes = checkNotNull(envelopes, "envelopes");
+ }
+
+ @Override
+ public Envelope apply(VAppTemplate from) {
+ checkArgument(from.getChildren().size() == 1, "multiple vms are not supported: %s", from);
+
+ checkArgument(from.getNetworkSection().getNetworks().size() == 1,
+ "multiple network connections are not supported: %s", from);
+
+ checkArgument(from.isOvfDescriptorUploaded(), "ovf descriptor is not uploaded: %s", from);
+ Envelope ovf = getOVFForVAppTemplateAndValidate(from);
+ return ovf;
+ }
+
+ private Envelope getOVFForVAppTemplateAndValidate(VAppTemplate from) throws IllegalArgumentException {
+ Envelope ovf;
+ try {
+ ovf = envelopes.get(from.getHref());
+ checkArgument(ovf.getVirtualSystem().getVirtualHardwareSections().size() > 0,
+ "no hardware sections exist in ovf %s", ovf);
+ } catch (ExecutionException e) {
+ throw new IllegalArgumentException("no ovf envelope found for: " + from, e);
+ }
+ return ovf;
+ }
+}
+
http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/19c8cdaf/dependencies/jclouds/apis/vcloud/1.7.1-stratos/src/main/java/org/jclouds/vcloud/compute/internal/VCloudTemplateBuilderImpl.java
----------------------------------------------------------------------
diff --git a/dependencies/jclouds/apis/vcloud/1.7.1-stratos/src/main/java/org/jclouds/vcloud/compute/internal/VCloudTemplateBuilderImpl.java b/dependencies/jclouds/apis/vcloud/1.7.1-stratos/src/main/java/org/jclouds/vcloud/compute/internal/VCloudTemplateBuilderImpl.java
new file mode 100755
index 0000000..49a1f78
--- /dev/null
+++ b/dependencies/jclouds/apis/vcloud/1.7.1-stratos/src/main/java/org/jclouds/vcloud/compute/internal/VCloudTemplateBuilderImpl.java
@@ -0,0 +1,48 @@
+/*
+ * 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.compute.internal;
+
+import java.util.Set;
+
+import javax.inject.Inject;
+import javax.inject.Named;
+import javax.inject.Provider;
+
+import org.jclouds.collect.Memoized;
+import org.jclouds.compute.domain.Hardware;
+import org.jclouds.compute.domain.Image;
+import org.jclouds.compute.domain.TemplateBuilder;
+import org.jclouds.compute.domain.internal.TemplateBuilderImpl;
+import org.jclouds.compute.options.TemplateOptions;
+import org.jclouds.domain.Location;
+
+import com.google.common.base.Supplier;
+
+/**
+ *
+ * @author Adrian Cole
+ */
+public class VCloudTemplateBuilderImpl extends TemplateBuilderImpl {
+
+ @Inject
+ protected VCloudTemplateBuilderImpl(@Memoized Supplier<Set<? extends Location>> locations,
+ @Memoized Supplier<Set<? extends Image>> images, @Memoized Supplier<Set<? extends Hardware>> sizes,
+ Supplier<Location> defaultLocation, @Named("DEFAULT") Provider<TemplateOptions> optionsProvider,
+ @Named("DEFAULT") Provider<TemplateBuilder> defaultTemplateProvider) {
+ super(locations, images, sizes, defaultLocation, optionsProvider, defaultTemplateProvider);
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/19c8cdaf/dependencies/jclouds/apis/vcloud/1.7.1-stratos/src/main/java/org/jclouds/vcloud/compute/options/VCloudTemplateOptions.java
----------------------------------------------------------------------
diff --git a/dependencies/jclouds/apis/vcloud/1.7.1-stratos/src/main/java/org/jclouds/vcloud/compute/options/VCloudTemplateOptions.java b/dependencies/jclouds/apis/vcloud/1.7.1-stratos/src/main/java/org/jclouds/vcloud/compute/options/VCloudTemplateOptions.java
new file mode 100755
index 0000000..f60aae2
--- /dev/null
+++ b/dependencies/jclouds/apis/vcloud/1.7.1-stratos/src/main/java/org/jclouds/vcloud/compute/options/VCloudTemplateOptions.java
@@ -0,0 +1,350 @@
+/*
+ * 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.compute.options;
+
+import static com.google.common.base.Objects.equal;
+import static com.google.common.base.Preconditions.checkNotNull;
+import static com.google.common.base.Strings.emptyToNull;
+
+import java.net.URI;
+import java.util.Map;
+
+import org.jclouds.compute.options.TemplateOptions;
+import org.jclouds.vcloud.domain.network.FenceMode;
+import org.jclouds.vcloud.domain.network.IpAddressAllocationMode;
+
+import com.google.common.base.Objects;
+import com.google.common.base.Objects.ToStringHelper;
+
+/**
+ * Contains options supported in the {@code ComputeService#runNode} operation on
+ * the "vcloud" provider. <h2>
+ * Usage</h2> 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):
+ * <p/>
+ * <code>
+ * import static org.jclouds.compute.options.VCloudTemplateOptions.Builder.*;
+ * <p/>
+ * ComputeService client = // get connection
+ * templateBuilder.options(inboundPorts(22, 80, 8080, 443));
+ * Set<NodeMetadata> set = client.createNodesInGroup(tag, 2, templateBuilder.build());
+ * <code>
+ *
+ * @author Adrian Cole
+ */
+public class VCloudTemplateOptions extends TemplateOptions implements Cloneable {
+ @Override
+ public VCloudTemplateOptions clone() {
+ VCloudTemplateOptions options = new VCloudTemplateOptions();
+ copyTo(options);
+ return options;
+ }
+
+ @Override
+ public void copyTo(TemplateOptions to) {
+ super.copyTo(to);
+ if (to instanceof VCloudTemplateOptions) {
+ VCloudTemplateOptions eTo = VCloudTemplateOptions.class.cast(to);
+ if (getCustomizationScript() != null)
+ eTo.customizationScript(getCustomizationScript());
+ if (getDescription() != null)
+ eTo.description(getDescription());
+ if (getIpAddressAllocationMode() != null)
+ eTo.ipAddressAllocationMode(getIpAddressAllocationMode());
+ if (getIpAddressAllocationMode() != null)
+ eTo.ipAddressAllocationMode(getIpAddressAllocationMode());
+ if (getParentNetwork() != null)
+ eTo.parentNetwork(getParentNetwork());
+ if (getFenceMode() != null)
+ eTo.fenceMode(getFenceMode());
+ }
+ }
+
+ private String description = null;
+ private String customizationScript = null;
+ private IpAddressAllocationMode ipAddressAllocationMode = null;
+ private URI parentNetwork = null;
+ private FenceMode fenceMode = null;
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o)
+ return true;
+ if (o == null || getClass() != o.getClass())
+ return false;
+ VCloudTemplateOptions that = VCloudTemplateOptions.class.cast(o);
+ return super.equals(that) && equal(this.description, that.description)
+ && equal(this.customizationScript, that.customizationScript)
+ && equal(this.ipAddressAllocationMode, that.ipAddressAllocationMode)
+ && equal(this.parentNetwork, that.parentNetwork);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hashCode(super.hashCode(), description, customizationScript, ipAddressAllocationMode,
+ parentNetwork);
+ }
+
+ @Override
+ public ToStringHelper string() {
+ return super.string().add("description", description).add("customizationScript", customizationScript)
+ .add("ipAddressAllocationMode", ipAddressAllocationMode).add("parentNetwork", parentNetwork);
+ }
+
+ /**
+ * 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
+ */
+ public VCloudTemplateOptions customizationScript(String customizationScript) {
+ this.customizationScript = checkNotNull(emptyToNull(customizationScript), "customizationScript must be defined");
+ return this;
+ }
+
+ /**
+ * Specifies the ipAddressAllocationMode used to for network interfaces on
+ * the VMs
+ */
+ public VCloudTemplateOptions ipAddressAllocationMode(IpAddressAllocationMode ipAddressAllocationMode) {
+ this.ipAddressAllocationMode = ipAddressAllocationMode;
+ return this;
+ }
+
+ /**
+ * Specifies the parentNetwork to connect the the network interfaces on the
+ * VMs to.
+ *
+ * @see InstantiateVAppTemplateOptions#addNetworkConfig
+ */
+ public VCloudTemplateOptions parentNetwork(URI parentNetwork) {
+ this.parentNetwork = parentNetwork;
+ return this;
+ }
+
+ /**
+ * How to connect to the parent network
+ *
+ * @see InstantiateVAppTemplateOptions#addNetworkConfig
+ */
+ public VCloudTemplateOptions fenceMode(FenceMode fenceMode) {
+ this.fenceMode = fenceMode;
+ return this;
+ }
+
+ public static class Builder {
+ /**
+ * @see VCloudTemplateOptions#description
+ */
+ public static VCloudTemplateOptions description(String description) {
+ return new VCloudTemplateOptions().description(description);
+ }
+
+ /**
+ * @see VCloudTemplateOptions#customizationScript
+ */
+ public static VCloudTemplateOptions customizationScript(String customizationScript) {
+ return new VCloudTemplateOptions().customizationScript(customizationScript);
+ }
+
+ /**
+ * @see VCloudTemplateOptions#ipAddressAllocationMode
+ */
+ public static VCloudTemplateOptions ipAddressAllocationMode(IpAddressAllocationMode ipAddressAllocationMode) {
+ return new VCloudTemplateOptions().ipAddressAllocationMode(ipAddressAllocationMode);
+ }
+
+ /**
+ * @see VCloudTemplateOptions#parentNetwork(URI parentNetwork)
+ */
+ public static VCloudTemplateOptions parentNetwork(URI parentNetwork) {
+ return new VCloudTemplateOptions().parentNetwork(parentNetwork);
+ }
+
+ /**
+ * @see VCloudTemplateOptions#fenceMode(FenceMode)
+ */
+ public static VCloudTemplateOptions fenceMode(FenceMode fenceMode) {
+ return new VCloudTemplateOptions().fenceMode(fenceMode);
+ }
+
+ // methods that only facilitate returning the correct object type
+ /**
+ * @see TemplateOptions#inboundPorts
+ */
+ public static VCloudTemplateOptions inboundPorts(int... ports) {
+ VCloudTemplateOptions options = new VCloudTemplateOptions();
+ return VCloudTemplateOptions.class.cast(options.inboundPorts(ports));
+ }
+
+ /**
+ * @see TemplateOptions#port
+ */
+ public static VCloudTemplateOptions blockOnPort(int port, int seconds) {
+ VCloudTemplateOptions options = new VCloudTemplateOptions();
+ return VCloudTemplateOptions.class.cast(options.blockOnPort(port, seconds));
+ }
+
+ /**
+ * @see TemplateOptions#userMetadata(Map)
+ */
+ public static VCloudTemplateOptions userMetadata(Map<String, String> userMetadata) {
+ VCloudTemplateOptions options = new VCloudTemplateOptions();
+ return VCloudTemplateOptions.class.cast(options.userMetadata(userMetadata));
+ }
+
+ /**
+ * @see TemplateOptions#userMetadata(String, String)
+ */
+ public static VCloudTemplateOptions userMetadata(String key, String value) {
+ VCloudTemplateOptions options = new VCloudTemplateOptions();
+ return VCloudTemplateOptions.class.cast(options.userMetadata(key, value));
+ }
+
+ /**
+ * @see TemplateOptions#nodeNames(Iterable)
+ */
+ public static VCloudTemplateOptions nodeNames(Iterable<String> nodeNames) {
+ VCloudTemplateOptions options = new VCloudTemplateOptions();
+ return VCloudTemplateOptions.class.cast(options.nodeNames(nodeNames));
+ }
+
+ /**
+ * @see TemplateOptions#networks(Iterable)
+ */
+ public static VCloudTemplateOptions networks(Iterable<String> networks) {
+ VCloudTemplateOptions options = new VCloudTemplateOptions();
+ return VCloudTemplateOptions.class.cast(options.networks(networks));
+ }
+
+ }
+
+ /**
+ * @return description of the vApp
+ */
+ public String getDescription() {
+ return description;
+ }
+
+ /**
+ * @return customizationScript on the vms
+ */
+ public String getCustomizationScript() {
+ return customizationScript;
+ }
+
+ /**
+ * @return ipAddressAllocationMode on the vms
+ */
+ public IpAddressAllocationMode getIpAddressAllocationMode() {
+ return ipAddressAllocationMode;
+ }
+
+ /**
+ * @return parentNetwork to connect to the vms
+ */
+ public URI getParentNetwork() {
+ return parentNetwork;
+ }
+
+ /**
+ * @return FenceMode to connect the parent network with
+ */
+ public FenceMode getFenceMode() {
+ return fenceMode;
+ }
+
+ // methods that only facilitate returning the correct object type
+
+ /**
+ * @see TemplateOptions#blockOnPort
+ */
+ @Override
+ public VCloudTemplateOptions blockOnPort(int port, int seconds) {
+ return VCloudTemplateOptions.class.cast(super.blockOnPort(port, seconds));
+ }
+
+ /**
+ *
+ * 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
+ */
+ @Override
+ public VCloudTemplateOptions inboundPorts(int... ports) {
+ return VCloudTemplateOptions.class.cast(super.inboundPorts(ports));
+ }
+
+ /**
+ * @see TemplateOptions#authorizePublicKey(String)
+ */
+ @Override
+ public VCloudTemplateOptions authorizePublicKey(String publicKey) {
+ return VCloudTemplateOptions.class.cast(super.authorizePublicKey(publicKey));
+ }
+
+ /**
+ * @see TemplateOptions#installPrivateKey(String)
+ */
+ @Override
+ public VCloudTemplateOptions installPrivateKey(String privateKey) {
+ return VCloudTemplateOptions.class.cast(super.installPrivateKey(privateKey));
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public VCloudTemplateOptions userMetadata(Map<String, String> userMetadata) {
+ return VCloudTemplateOptions.class.cast(super.userMetadata(userMetadata));
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public VCloudTemplateOptions userMetadata(String key, String value) {
+ return VCloudTemplateOptions.class.cast(super.userMetadata(key, value));
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public VCloudTemplateOptions nodeNames(Iterable<String> nodeNames) {
+ return VCloudTemplateOptions.class.cast(super.nodeNames(nodeNames));
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public VCloudTemplateOptions networks(Iterable<String> networks) {
+ return VCloudTemplateOptions.class.cast(super.networks(networks));
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/19c8cdaf/dependencies/jclouds/apis/vcloud/1.7.1-stratos/src/main/java/org/jclouds/vcloud/compute/strategy/InstantiateVAppTemplateWithGroupEncodedIntoNameThenCustomizeDeployAndPowerOn.java
----------------------------------------------------------------------
diff --git a/dependencies/jclouds/apis/vcloud/1.7.1-stratos/src/main/java/org/jclouds/vcloud/compute/strategy/InstantiateVAppTemplateWithGroupEncodedIntoNameThenCustomizeDeployAndPowerOn.java b/dependencies/jclouds/apis/vcloud/1.7.1-stratos/src/main/java/org/jclouds/vcloud/compute/strategy/InstantiateVAppTemplateWithGroupEncodedIntoNameThenCustomizeDeployAndPowerOn.java
new file mode 100755
index 0000000..caecde6
--- /dev/null
+++ b/dependencies/jclouds/apis/vcloud/1.7.1-stratos/src/main/java/org/jclouds/vcloud/compute/strategy/InstantiateVAppTemplateWithGroupEncodedIntoNameThenCustomizeDeployAndPowerOn.java
@@ -0,0 +1,302 @@
+/*
+ * 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.compute.strategy;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Predicates.not;
+import static com.google.common.collect.Iterables.find;
+import static com.google.common.collect.Iterables.get;
+import static org.jclouds.compute.util.ComputeServiceUtils.getCores;
+import static org.jclouds.compute.util.ComputeServiceUtils.metadataAndTagsAsCommaDelimitedValue;
+import static org.jclouds.vcloud.compute.util.VCloudComputeUtils.getCredentialsFrom;
+import static org.jclouds.vcloud.options.InstantiateVAppTemplateOptions.Builder.addNetworkConfig;
+
+import java.net.URI;
+import java.util.Map;
+
+import javax.annotation.Resource;
+import javax.inject.Inject;
+import javax.inject.Named;
+import javax.inject.Singleton;
+
+import org.jclouds.compute.ComputeServiceAdapter.NodeAndInitialCredentials;
+import org.jclouds.compute.domain.Template;
+import org.jclouds.compute.reference.ComputeServiceConstants;
+import org.jclouds.javax.annotation.Nullable;
+import org.jclouds.logging.Logger;
+import org.jclouds.ovf.Network;
+import org.jclouds.predicates.validators.DnsNameValidator;
+import org.jclouds.rest.annotations.BuildVersion;
+import org.jclouds.vcloud.TaskStillRunningException;
+import org.jclouds.vcloud.VCloudApi;
+import org.jclouds.vcloud.compute.options.VCloudTemplateOptions;
+import org.jclouds.vcloud.domain.GuestCustomizationSection;
+import org.jclouds.vcloud.domain.NetworkConnection;
+import org.jclouds.vcloud.domain.NetworkConnectionSection;
+import org.jclouds.vcloud.domain.NetworkConnectionSection.Builder;
+import org.jclouds.vcloud.domain.Task;
+import org.jclouds.vcloud.domain.VApp;
+import org.jclouds.vcloud.domain.VAppTemplate;
+import org.jclouds.vcloud.domain.Vm;
+import org.jclouds.vcloud.domain.network.IpAddressAllocationMode;
+import org.jclouds.vcloud.domain.network.NetworkConfig;
+import org.jclouds.vcloud.options.InstantiateVAppTemplateOptions;
+
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Joiner;
+import com.google.common.base.Predicate;
+import com.google.common.cache.LoadingCache;
+import com.google.common.collect.ImmutableSet;
+
+/**
+ * @author Adrian Cole
+ */
+@Singleton
+public class InstantiateVAppTemplateWithGroupEncodedIntoNameThenCustomizeDeployAndPowerOn {
+ @Resource
+ @Named(ComputeServiceConstants.COMPUTE_LOGGER)
+ protected Logger logger = Logger.NULL;
+
+ protected final VCloudApi client;
+ protected final Predicate<URI> successTester;
+ protected final LoadingCache<URI, VAppTemplate> vAppTemplates;
+ protected final NetworkConfigurationForNetworkAndOptions networkConfigurationForNetworkAndOptions;
+ protected final String buildVersion;
+
+
+ @Inject
+ protected InstantiateVAppTemplateWithGroupEncodedIntoNameThenCustomizeDeployAndPowerOn(VCloudApi client,
+ Predicate<URI> successTester, LoadingCache<URI, VAppTemplate> vAppTemplates, NetworkConfigurationForNetworkAndOptions networkConfigurationForNetworkAndOptions,
+ @BuildVersion String buildVersion) {
+ this.client = client;
+ this.successTester = successTester;
+ this.vAppTemplates = vAppTemplates;
+ this.networkConfigurationForNetworkAndOptions = networkConfigurationForNetworkAndOptions;
+ this.buildVersion = buildVersion;
+ }
+
+ /**
+ * per john ellis at bluelock, vCloud Director 1.5 is more strict than earlier versions.
+ * <p/>
+ * It appears to be 15 characters to match Windows' hostname limitation. Must be alphanumeric, at
+ * least one non-number character and hyphens and underscores are the only non-alpha character
+ * allowed.
+ */
+ public static enum ComputerNameValidator {
+ INSTANCE;
+
+ private DnsNameValidator validator;
+
+ ComputerNameValidator() {
+ this.validator = new DnsNameValidator(3, 15);
+ }
+
+ public void validate(@Nullable String t) throws IllegalArgumentException {
+ this.validator.validate(t);
+ }
+
+ }
+
+ public NodeAndInitialCredentials<VApp> createNodeWithGroupEncodedIntoName(String group, String name, Template template) {
+ // no sense waiting until failures occur later
+ ComputerNameValidator.INSTANCE.validate(name);
+ VApp vAppResponse = instantiateVAppFromTemplate(name, template);
+ waitForTask(vAppResponse.getTasks().get(0));
+ logger.debug("<< instantiated VApp(%s)", vAppResponse.getName());
+
+ // vm data is available after instantiate completes
+ vAppResponse = client.getVAppApi().getVApp(vAppResponse.getHref());
+
+ // per above check, we know there is only a single VM
+ Vm vm = get(vAppResponse.getChildren(), 0);
+
+ template.getOptions().userMetadata(ComputeServiceConstants.NODE_GROUP_KEY, group);
+ VCloudTemplateOptions vOptions = VCloudTemplateOptions.class.cast(template.getOptions());
+
+ // note we cannot do tasks in parallel or VCD will throw "is busy" errors
+
+ // note we must do this before any other customizations as there is a dependency on
+ // valid naming conventions before you can perform commands such as updateCPUCount
+ logger.trace(">> updating customization vm(%s) name->(%s)", vm.getName(), name);
+ waitForTask(updateVmWithNameAndCustomizationScript(vm, name, vOptions.getCustomizationScript()));
+ logger.trace("<< updated customization vm(%s)", name);
+
+ ensureVmHasAllocationModeOrPooled(vAppResponse, vOptions.getIpAddressAllocationMode());
+
+ int cpuCount = (int) getCores(template.getHardware());
+ logger.trace(">> updating cpuCount(%d) vm(%s)", cpuCount, vm.getName());
+ waitForTask(updateCPUCountOfVm(vm, cpuCount));
+ logger.trace("<< updated cpuCount vm(%s)", vm.getName());
+ int memoryMB = template.getHardware().getRam();
+ logger.trace(">> updating memoryMB(%d) vm(%s)", memoryMB, vm.getName());
+ waitForTask(updateMemoryMBOfVm(vm, memoryMB));
+ logger.trace("<< updated memoryMB vm(%s)", vm.getName());
+ logger.trace(">> deploying vApp(%s)", vAppResponse.getName());
+ waitForTask(client.getVAppApi().deployVApp(vAppResponse.getHref()));
+ logger.trace("<< deployed vApp(%s)", vAppResponse.getName());
+
+ // only after deploy is the password valid
+ vAppResponse = client.getVAppApi().getVApp(vAppResponse.getHref());
+
+ logger.trace(">> powering on vApp(%s)", vAppResponse.getName());
+ client.getVAppApi().powerOnVApp(vAppResponse.getHref());
+
+ return new NodeAndInitialCredentials<VApp>(vAppResponse, vAppResponse.getHref().toASCIIString(),
+ getCredentialsFrom(vAppResponse));
+
+ }
+
+ @VisibleForTesting
+ protected VApp instantiateVAppFromTemplate(String name, Template template) {
+ VCloudTemplateOptions vOptions = VCloudTemplateOptions.class.cast(template.getOptions());
+
+ URI templateId = URI.create(template.getImage().getId());
+
+ VAppTemplate vAppTemplate = vAppTemplates.getUnchecked(templateId);
+
+ if (vAppTemplate.getChildren().size() > 1)
+ throw new UnsupportedOperationException("we currently do not support multiple vms in a vAppTemplate "
+ + vAppTemplate);
+
+ if (vAppTemplate.getNetworkSection().getNetworks().size() > 1)
+ throw new UnsupportedOperationException(
+ "we currently do not support multiple network connections in a vAppTemplate " + vAppTemplate);
+
+ Network networkToConnect = get(vAppTemplate.getNetworkSection().getNetworks(), 0);
+
+
+ NetworkConfig config = networkConfigurationForNetworkAndOptions.apply(networkToConnect, vOptions);
+
+ // note that in VCD 1.5, the network name after instantiation will be the same as the parent
+ InstantiateVAppTemplateOptions options = addNetworkConfig(config);
+
+ // TODO make disk size specifiable
+ // disk((long) ((template.getHardware().getVolumes().get(0).getSize()) *
+ // 1024 * 1024l));
+
+
+
+ String description = VCloudTemplateOptions.class.cast(template.getOptions()).getDescription();
+ if (description == null) {
+ Map<String, String> md = metadataAndTagsAsCommaDelimitedValue(template.getOptions());
+ description = Joiner.on('\n').withKeyValueSeparator("=").join(md);
+ }
+
+ options.description(description);
+ options.deploy(false);
+ options.powerOn(false);
+
+ URI VDC = URI.create(template.getLocation().getId());
+
+ logger.debug(">> instantiating vApp vDC(%s) template(%s) name(%s) options(%s) ", VDC, templateId, name, options);
+
+ VApp vAppResponse = client.getVAppTemplateApi().createVAppInVDCByInstantiatingTemplate(name, VDC, templateId,
+ options);
+ return vAppResponse;
+ }
+
+ // TODO: filtering on "none" is a hack until we can filter on
+ // vAppTemplate.getNetworkConfigSection().getNetworkConfigs() where
+ // name = getChildren().NetworkConnectionSection.connection where ipallocationmode == none
+ static Predicate<Network> networkWithNoIpAllocation = new Predicate<Network>() {
+
+ @Override
+ public boolean apply(Network input) {
+ return "none".equals(input.getName());
+ }
+
+ };
+
+ public void waitForTask(Task task) {
+ if (!successTester.apply(task.getHref())) {
+ throw new TaskStillRunningException(task);
+ }
+ }
+ /**
+ * Naming constraints modifying a VM on a VApp in vCloud Director (at least v1.5) can be more
+ * strict than those in a vAppTemplate. For example, while it is possible to instantiate a
+ * vAppTemplate with a VM named (incorrectly) {@code Ubuntu_10.04}, you must change the name to a
+ * valid (alphanumeric underscore) name before you can update it.
+ */
+ public Task updateVmWithNameAndCustomizationScript(Vm vm, String name, @Nullable String customizationScript) {
+ GuestCustomizationSection guestConfiguration = vm.getGuestCustomizationSection();
+ guestConfiguration.setComputerName(name);
+ if (customizationScript != null) {
+ // In version 1.0.0, the api returns a script that loses newlines, so we cannot append to a
+ // customization script.
+ // TODO: parameterize whether to overwrite or append existing customization
+ if (!buildVersion.startsWith("1.0.0") && !"".endsWith(buildVersion)
+ && guestConfiguration.getCustomizationScript() != null)
+ customizationScript = guestConfiguration.getCustomizationScript() + "\n" + customizationScript;
+
+ guestConfiguration.setCustomizationScript(customizationScript);
+ }
+ return client.getVmApi().updateGuestCustomizationOfVm(guestConfiguration, vm.getHref());
+ }
+
+ public void ensureVmHasAllocationModeOrPooled(VApp vApp, @Nullable IpAddressAllocationMode ipAllocationMode) {
+ Network networkToConnect = find(vApp.getNetworkSection().getNetworks(), not(networkWithNoIpAllocation));
+
+ Vm vm = get(vApp.getChildren(), 0);
+
+ NetworkConnectionSection net = vm.getNetworkConnectionSection();
+ checkArgument(net.getConnections().size() > 0, "no connections on vm %s", vm);
+
+ NetworkConnection toConnect = findWithPoolAllocationOrFirst(net);
+
+ if (ipAllocationMode == null)
+ ipAllocationMode = toConnect.getIpAddressAllocationMode();
+
+ // make sure that we are in fact allocating ips
+ if (ipAllocationMode == IpAddressAllocationMode.NONE)
+ ipAllocationMode = IpAddressAllocationMode.POOL;
+
+ if (toConnect.isConnected() && toConnect.getIpAddressAllocationMode() == ipAllocationMode
+ && toConnect.getNetwork().equals(networkToConnect.getName())) {
+ // then we don't need to change the network settings, and can save a call
+ } else {
+ Builder builder = net.toBuilder();
+ builder.connections(ImmutableSet.of(toConnect.toBuilder().network(networkToConnect.getName()).connected(true)
+ .ipAddressAllocationMode(ipAllocationMode).build()));
+ logger.trace(">> updating networkConnection vm(%s)", vm.getName());
+
+ waitForTask(client.getVmApi().updateNetworkConnectionOfVm(builder.build(), vm.getHref()));
+ logger.trace("<< updated networkConnection vm(%s)", vm.getName());
+
+ }
+
+ }
+
+ private NetworkConnection findWithPoolAllocationOrFirst(NetworkConnectionSection net) {
+ return find(net.getConnections(), new Predicate<NetworkConnection>() {
+
+ @Override
+ public boolean apply(NetworkConnection input) {
+ return input.getIpAddressAllocationMode() == IpAddressAllocationMode.POOL;
+ }
+
+ }, get(net.getConnections(), 0));
+ }
+
+ public Task updateCPUCountOfVm(Vm vm, int cpuCount) {
+ return client.getVmApi().updateCPUCountOfVm(cpuCount, vm.getHref());
+ }
+
+ public Task updateMemoryMBOfVm(Vm vm, int memoryInMB) {
+ return client.getVmApi().updateMemoryMBOfVm(memoryInMB, vm.getHref());
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/19c8cdaf/dependencies/jclouds/apis/vcloud/1.7.1-stratos/src/main/java/org/jclouds/vcloud/compute/strategy/NetworkConfigurationForNetworkAndOptions.java
----------------------------------------------------------------------
diff --git a/dependencies/jclouds/apis/vcloud/1.7.1-stratos/src/main/java/org/jclouds/vcloud/compute/strategy/NetworkConfigurationForNetworkAndOptions.java b/dependencies/jclouds/apis/vcloud/1.7.1-stratos/src/main/java/org/jclouds/vcloud/compute/strategy/NetworkConfigurationForNetworkAndOptions.java
new file mode 100755
index 0000000..7719efb
--- /dev/null
+++ b/dependencies/jclouds/apis/vcloud/1.7.1-stratos/src/main/java/org/jclouds/vcloud/compute/strategy/NetworkConfigurationForNetworkAndOptions.java
@@ -0,0 +1,80 @@
+/*
+ * 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.compute.strategy;
+
+import java.net.URI;
+
+import javax.inject.Inject;
+
+import org.jclouds.ovf.Network;
+import org.jclouds.vcloud.compute.options.VCloudTemplateOptions;
+import org.jclouds.vcloud.domain.network.FenceMode;
+import org.jclouds.vcloud.domain.network.NetworkConfig;
+
+import com.google.common.annotations.Beta;
+import com.google.common.base.Supplier;
+
+/**
+ *
+ * @author Adrian Cole
+ */
+@Beta
+public class NetworkConfigurationForNetworkAndOptions {
+ protected final Supplier<NetworkConfig> defaultNetworkConfig;
+ protected final FenceMode defaultFenceMode;
+
+ @Inject
+ protected NetworkConfigurationForNetworkAndOptions(Supplier<NetworkConfig> defaultNetworkConfig,
+ FenceMode defaultFenceMode) {
+ this.defaultNetworkConfig = defaultNetworkConfig;
+ this.defaultFenceMode = defaultFenceMode;
+ }
+
+ /**
+ *
+ * returns a {@link NetworkConfig} used to instantiate a vAppTemplate to
+ * either the default parent (org) network, or one specified by options.
+ *
+ * @param networkToConnect
+ * network defined in the VAppTemplate you wish to connect to
+ * @param vOptions
+ * options to override defaults with
+ * @return
+ */
+ public NetworkConfig apply(Network networkToConnect, VCloudTemplateOptions vOptions) {
+ NetworkConfig config;
+ URI userDefinedParentNetwork = vOptions.getParentNetwork();
+ FenceMode fenceMode = vOptions.getFenceMode() != null ? vOptions.getFenceMode() : defaultFenceMode;
+ if (userDefinedParentNetwork != null) {
+ config = NetworkConfig.builder().networkName("jclouds").fenceMode(fenceMode)
+ .parentNetwork(userDefinedParentNetwork).build();
+ } else {
+ config = defaultNetworkConfig.get().toBuilder().fenceMode(fenceMode).build();
+ }
+
+ // if we only have a disconnected network, we are adding a new section
+ // for the upstream
+ if (InstantiateVAppTemplateWithGroupEncodedIntoNameThenCustomizeDeployAndPowerOn.networkWithNoIpAllocation
+ .apply(networkToConnect)) {
+ // TODO: remove the disconnected entry
+ } else {
+ config = config.toBuilder().networkName(networkToConnect.getName()).build();
+ }
+ return config;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/19c8cdaf/dependencies/jclouds/apis/vcloud/1.7.1-stratos/src/main/java/org/jclouds/vcloud/compute/strategy/VCloudComputeServiceAdapter.java
----------------------------------------------------------------------
diff --git a/dependencies/jclouds/apis/vcloud/1.7.1-stratos/src/main/java/org/jclouds/vcloud/compute/strategy/VCloudComputeServiceAdapter.java b/dependencies/jclouds/apis/vcloud/1.7.1-stratos/src/main/java/org/jclouds/vcloud/compute/strategy/VCloudComputeServiceAdapter.java
new file mode 100755
index 0000000..377c8f9
--- /dev/null
+++ b/dependencies/jclouds/apis/vcloud/1.7.1-stratos/src/main/java/org/jclouds/vcloud/compute/strategy/VCloudComputeServiceAdapter.java
@@ -0,0 +1,263 @@
+/*
+ * 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.compute.strategy;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static com.google.common.collect.Iterables.contains;
+import static com.google.common.collect.Iterables.filter;
+
+import java.net.URI;
+import java.util.Map;
+import java.util.Set;
+
+import javax.annotation.Resource;
+import javax.inject.Inject;
+import javax.inject.Named;
+import javax.inject.Singleton;
+
+import org.jclouds.compute.ComputeServiceAdapter;
+import org.jclouds.compute.domain.Template;
+import org.jclouds.compute.reference.ComputeServiceConstants;
+import org.jclouds.domain.Location;
+import org.jclouds.logging.Logger;
+import org.jclouds.ovf.Envelope;
+import org.jclouds.util.Throwables2;
+import org.jclouds.vcloud.TaskInErrorStateException;
+import org.jclouds.vcloud.TaskStillRunningException;
+import org.jclouds.vcloud.VCloudApi;
+import org.jclouds.vcloud.VCloudMediaType;
+import org.jclouds.vcloud.domain.Org;
+import org.jclouds.vcloud.domain.ReferenceType;
+import org.jclouds.vcloud.domain.Status;
+import org.jclouds.vcloud.domain.Task;
+import org.jclouds.vcloud.domain.VApp;
+import org.jclouds.vcloud.domain.VAppTemplate;
+import org.jclouds.vcloud.suppliers.VAppTemplatesSupplier;
+
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Function;
+import com.google.common.base.Predicate;
+import com.google.common.base.Supplier;
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.ImmutableSet.Builder;
+
+/**
+ * defines the connection between the {@link org.jclouds.vcloud.VCloudApi} implementation and the jclouds
+ * {@link ComputeService}
+ *
+ */
+@Singleton
+public class VCloudComputeServiceAdapter implements ComputeServiceAdapter<VApp, VAppTemplate, VAppTemplate, Location> {
+
+ @Resource
+ @Named(ComputeServiceConstants.COMPUTE_LOGGER)
+ protected Logger logger = Logger.NULL;
+
+ protected final VCloudApi client;
+ protected final Predicate<URI> successTester;
+ protected final InstantiateVAppTemplateWithGroupEncodedIntoNameThenCustomizeDeployAndPowerOn booter;
+ protected final Supplier<Map<String, Org>> nameToOrg;
+ protected final Supplier<Set<VAppTemplate>> templates;
+ protected final Function<VAppTemplate, Envelope> templateToEnvelope;
+
+ @Inject
+ protected VCloudComputeServiceAdapter(VCloudApi client, Predicate<URI> successTester,
+ InstantiateVAppTemplateWithGroupEncodedIntoNameThenCustomizeDeployAndPowerOn booter,
+ Supplier<Map<String, Org>> nameToOrg, VAppTemplatesSupplier templates,
+ Function<VAppTemplate, Envelope> templateToEnvelope) {
+ this.client = checkNotNull(client, "client");
+ this.successTester = checkNotNull(successTester, "successTester");
+ this.booter = checkNotNull(booter, "booter");
+ this.nameToOrg = checkNotNull(nameToOrg, "nameToOrg");
+ this.templates = checkNotNull(templates, "templates");
+ this.templateToEnvelope = checkNotNull(templateToEnvelope, "templateToEnvelope");
+ }
+
+ @Override
+ public NodeAndInitialCredentials<VApp> createNodeWithGroupEncodedIntoName(String group, String name,
+ Template template) {
+ return booter.createNodeWithGroupEncodedIntoName(group, name, template);
+ }
+
+ @Override
+ public Iterable<VAppTemplate> listHardwareProfiles() {
+ return supportedTemplates();
+ }
+
+ private Iterable<VAppTemplate> supportedTemplates() {
+ return filter(templates.get(), new Predicate<VAppTemplate>() {
+
+ @Override
+ public boolean apply(VAppTemplate from) {
+ try {
+ templateToEnvelope.apply(from);
+ } catch (IllegalArgumentException e) {
+ logger.warn("Unsupported: " + e.getMessage());
+ return false;
+ } catch (RuntimeException e) {
+ IllegalArgumentException e2 = Throwables2.getFirstThrowableOfType(e, IllegalArgumentException.class);
+ if (e2 != null) {
+ logger.warn("Unsupported: " + e2.getMessage());
+ return false;
+ } else {
+ throw e;
+ }
+ }
+ return true;
+ }
+
+ });
+ }
+
+ @Override
+ public Iterable<VAppTemplate> listImages() {
+ return supportedTemplates();
+ }
+
+ @Override
+ public Iterable<VApp> listNodes() {
+ // TODO: parallel or cache
+ Builder<VApp> nodes = ImmutableSet.builder();
+ for (Org org : nameToOrg.get().values()) {
+ for (ReferenceType vdc : org.getVDCs().values()) {
+ for (ReferenceType resource : client.getVDCApi().getVDC(vdc.getHref()).getResourceEntities().values()) {
+ if (resource.getType().equals(VCloudMediaType.VAPP_XML)) {
+ addVAppToSetRetryingIfNotYetPresent(nodes, vdc, resource);
+ }
+ }
+ }
+ }
+ return nodes.build();
+ }
+
+ @Override
+ public Iterable<VApp> listNodesByIds(final Iterable<String> ids) {
+ return filter(listNodes(), new Predicate<VApp>() {
+
+ @Override
+ public boolean apply(VApp vm) {
+ return contains(ids, vm.getHref().toASCIIString());
+ }
+ });
+ }
+
+ @VisibleForTesting
+ void addVAppToSetRetryingIfNotYetPresent(Builder<VApp> nodes, ReferenceType vdc, ReferenceType resource) {
+ VApp node = null;
+ int i = 0;
+ while (node == null && i++ < 3) {
+ try {
+ node = client.getVAppApi().getVApp(resource.getHref());
+ nodes.add(node);
+ } catch (NullPointerException e) {
+ logger.warn("vApp %s not yet present in vdc %s", resource.getName(), vdc.getName());
+ }
+ }
+ }
+
+ @Override
+ public Iterable<Location> listLocations() {
+ // Not using the adapter to determine locations
+ return ImmutableSet.<Location>of();
+ }
+
+ @Override
+ public VApp getNode(String in) {
+ URI id = URI.create(in);
+ return client.getVAppApi().getVApp(id);
+ }
+
+ @Override
+ public VAppTemplate getImage(String in) {
+ URI id = URI.create(in);
+ return client.getVAppTemplateApi().getVAppTemplate(id);
+ }
+
+ @Override
+ public void destroyNode(String id) {
+ URI vappId = URI.create(checkNotNull(id, "node.id"));
+ VApp vApp = cancelAnyRunningTasks(vappId);
+ if (vApp.getStatus() != Status.OFF) {
+ logger.debug(">> powering off VApp vApp(%s), current status: %s", vApp.getName(), vApp.getStatus());
+ try {
+ waitForTask(client.getVAppApi().powerOffVApp(vApp.getHref()));
+ vApp = client.getVAppApi().getVApp(vApp.getHref());
+ logger.debug("<< %s vApp(%s)", vApp.getStatus(), vApp.getName());
+ } catch (IllegalStateException e) {
+ logger.warn(e, "<< %s vApp(%s)", vApp.getStatus(), vApp.getName());
+ }
+ logger.debug(">> undeploying vApp(%s), current status: %s", vApp.getName(), vApp.getStatus());
+ try {
+ waitForTask(client.getVAppApi().undeployVApp(vApp.getHref()));
+ vApp = client.getVAppApi().getVApp(vApp.getHref());
+ logger.debug("<< %s vApp(%s)", vApp.getStatus(), vApp.getName());
+ } catch (IllegalStateException e) {
+ logger.warn(e, "<< %s vApp(%s)", vApp.getStatus(), vApp.getName());
+ }
+ }
+ logger.debug(">> deleting vApp(%s)", vApp.getHref());
+ waitForTask(client.getVAppApi().deleteVApp(vApp.getHref()));
+ logger.debug("<< deleted vApp(%s)", vApp.getHref());
+ }
+
+ VApp waitForPendingTasksToComplete(URI vappId) {
+ VApp vApp = client.getVAppApi().getVApp(vappId);
+ if (vApp.getTasks().size() == 0)
+ return vApp;
+ for (Task task : vApp.getTasks())
+ waitForTask(task);
+ return client.getVAppApi().getVApp(vappId);
+ }
+
+ VApp cancelAnyRunningTasks(URI vappId) {
+ VApp vApp = client.getVAppApi().getVApp(vappId);
+ if (vApp.getTasks().size() == 0)
+ return vApp;
+ for (Task task : vApp.getTasks()) {
+ try {
+ client.getTaskApi().cancelTask(task.getHref());
+ waitForTask(task);
+ } catch (TaskInErrorStateException e) {
+ }
+ }
+ return client.getVAppApi().getVApp(vappId);
+
+ }
+
+ public void waitForTask(Task task) {
+ if (!successTester.apply(task.getHref()))
+ throw new TaskStillRunningException(task);
+ }
+
+ @Override
+ public void rebootNode(String in) {
+ URI id = URI.create(checkNotNull(in, "node.id"));
+ waitForTask(client.getVAppApi().resetVApp(id));
+ }
+
+ @Override
+ public void resumeNode(String in) {
+ URI id = URI.create(checkNotNull(in, "node.id"));
+ waitForTask(client.getVAppApi().powerOnVApp(id));
+ }
+
+ @Override
+ public void suspendNode(String in) {
+ URI id = URI.create(checkNotNull(in, "node.id"));
+ waitForTask(client.getVAppApi().powerOffVApp(id));
+ }
+}