Issue 280: added operating system and started parsing ip addresses from api

This commit is contained in:
Adrian Cole 2010-08-25 00:12:33 -07:00
parent 53db05540b
commit 0038733646
21 changed files with 461 additions and 209 deletions

View File

@ -669,7 +669,9 @@ public class TemplateBuilderImpl implements TemplateBuilder {
predicates.add(imageDescriptionPredicate);
}
Predicate<Image> imagePredicate = predicates.size() == 1 ? Iterables.get(predicates, 0) : and(predicates);
// looks verbose, but explicit <Image> type needed for this to compile properly
Predicate<Image> imagePredicate = predicates.size() == 1 ? Iterables.<Predicate<Image>> get(predicates, 0)
: Predicates.<Image> and(predicates);
return imagePredicate;
}

View File

@ -149,7 +149,7 @@ public class Utils {
return ImmutableSet.<T> of(in);
}
public static Object propagateOrNull(Exception from) {
public static <T> T propagateOrNull(Exception from) {
propagate(from);
assert false : "exception should have propogated";
return null;

View File

@ -19,24 +19,14 @@
package org.jclouds.vcloud.bluelock.compute.config;
import java.util.Set;
import org.jclouds.compute.domain.Size;
import org.jclouds.compute.strategy.PopulateDefaultLoginCredentialsForImageStrategy;
import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.vcloud.bluelock.compute.BlueLockVCloudDirectorComputeClient;
import org.jclouds.vcloud.bluelock.compute.config.suppliers.ParseSizeFromImageSupplier;
import org.jclouds.vcloud.bluelock.compute.functions.BlueLockVCloudImageForVAppTemplate;
import org.jclouds.vcloud.bluelock.compute.strategy.DefaultLoginCredentialsFromBlueLockFAQ;
import org.jclouds.vcloud.compute.VCloudComputeClient;
import org.jclouds.vcloud.compute.config.VCloudComputeServiceContextModule;
import org.jclouds.vcloud.compute.functions.ImageForVCloudExpressVAppTemplate;
import com.google.common.base.Supplier;
import com.google.inject.Injector;
/**
* Configures the {@link BlueLockVCloudComputeServiceContext}; requires
* {@link BlueLockVCloudComputeClient} bound.
* Configures the {@link ComputeServiceContext}; requires
* {@link BlueLockVCloudDirectorComputeClient} bound.
*
* @author Adrian Cole
*/
@ -45,13 +35,7 @@ public class BlueLockVCloudDirectorComputeServiceContextModule extends VCloudCom
@Override
protected void configure() {
super.configure();
bind(ImageForVCloudExpressVAppTemplate.class).to(BlueLockVCloudImageForVAppTemplate.class);
bind(VCloudComputeClient.class).to(BlueLockVCloudDirectorComputeClient.class);
bind(PopulateDefaultLoginCredentialsForImageStrategy.class).to(DefaultLoginCredentialsFromBlueLockFAQ.class);
}
@Override
protected Supplier<Set<? extends Size>> getSourceSizeSupplier(Injector injector) {
return injector.getInstance(ParseSizeFromImageSupplier.class);
}
}

View File

@ -36,14 +36,13 @@ import org.jclouds.vcloud.domain.VCloudExpressVAppTemplate;
*
*/
@Singleton
public class DefaultLoginCredentialsFromBlueLockFAQ implements
PopulateDefaultLoginCredentialsForImageStrategy {
public class DefaultLoginCredentialsFromBlueLockFAQ implements PopulateDefaultLoginCredentialsForImageStrategy {
@Override
public Credentials execute(Object resourceToAuthenticate) {
checkNotNull(resourceToAuthenticate);
checkArgument(resourceToAuthenticate instanceof VCloudExpressVAppTemplate,
"Resource must be an VAppTemplate (for Terremark)");
"Resource must be an VCloudExpressVAppTemplate (for Bluelock vCloud Express)");
VCloudExpressVAppTemplate template = (VCloudExpressVAppTemplate) resourceToAuthenticate;
if (template.getDescription().indexOf("Windows") >= 0) {
return new Credentials("expressuser", "ExpressPassword#1");

View File

@ -26,6 +26,7 @@ import org.jclouds.compute.domain.Image;
import org.jclouds.compute.domain.OsFamily;
import org.jclouds.compute.domain.Template;
import org.jclouds.compute.domain.TemplateBuilder;
import org.jclouds.compute.predicates.OperatingSystemPredicates;
import org.jclouds.vcloud.compute.VCloudComputeServiceLiveTest;
import org.testng.annotations.Test;
@ -53,8 +54,10 @@ public class BlueLockVCloudDirectorComputeServiceLiveTest extends VCloudComputeS
@Test
public void testTemplateBuilder() {
Template defaultTemplate = client.templateBuilder().build();
assertEquals(defaultTemplate.getImage().getOperatingSystem().is64Bit(), true);
assertEquals(defaultTemplate.getImage().getOperatingSystem().is64Bit(), false);
assert OperatingSystemPredicates.supportsApt().apply(defaultTemplate.getImage().getOperatingSystem());
assertEquals(defaultTemplate.getImage().getOperatingSystem().getFamily(), OsFamily.UBUNTU);
assertEquals(defaultTemplate.getImage().getOperatingSystem().getDescription(), "Ubuntu Template");
assert defaultTemplate.getLocation().getId() != null : defaultTemplate.getLocation();
assertEquals(defaultTemplate.getSize().getCores(), 1.0d);
}

View File

@ -19,7 +19,10 @@
package org.jclouds.vcloud.compute.functions;
import static com.google.common.collect.Iterables.filter;
import static com.google.common.collect.Iterables.find;
import static com.google.common.collect.Maps.newHashMap;
import static org.jclouds.vcloud.predicates.VCloudPredicates.resourceType;
import java.util.Map;
@ -28,9 +31,15 @@ import javax.inject.Singleton;
import org.jclouds.logging.Logger;
import org.jclouds.vcloud.compute.internal.VCloudExpressComputeClientImpl;
import org.jclouds.vcloud.domain.ResourceAllocation;
import org.jclouds.vcloud.domain.ResourceType;
import org.jclouds.vcloud.domain.VApp;
import org.jclouds.vcloud.domain.VCloudHardDisk;
import org.jclouds.vcloud.domain.VCloudNetworkAdapter;
import org.jclouds.vcloud.domain.Vm;
import com.google.common.base.Function;
import com.google.common.collect.Iterables;
/**
* Configures the {@link VCloudComputeServiceContext}; requires
@ -47,24 +56,30 @@ public class GetExtraFromVApp implements Function<VApp, Map<String, String>> {
public Map<String, String> apply(VApp vApp) {
Map<String, String> extra = newHashMap();
try {
// TODO
// extra.put("memory/mb", find(vApp.getResourceAllocations(),
// resourceType(ResourceType.MEMORY))
// .getVirtualQuantity()
// + "");
// extra.put("processor/count", find(vApp.getResourceAllocations(),
// resourceType(ResourceType.PROCESSOR))
// .getVirtualQuantity()
// + "");
// for (ResourceAllocation disk : filter(vApp.getResourceAllocations(),
// resourceType(ResourceType.DISK_DRIVE))) {
// extra.put(String.format("disk_drive/%s/kb", disk.getAddressOnParent()),
// disk.getVirtualQuantity() + "");
// }
//
// for (Entry<String, String> net : vApp.getNetworkToAddresses().entries()) {
// extra.put(String.format("network/%s/ip", net.getKey()), net.getValue());
// }
// TODO make this work with composite vApps
Vm vm = Iterables.get(vApp.getChildren(), 0);
extra.put("memory/mb", find(vm.getHardware().getResourceAllocations(), resourceType(ResourceType.MEMORY))
.getVirtualQuantity()
+ "");
extra.put("processor/count", find(vm.getHardware().getResourceAllocations(),
resourceType(ResourceType.PROCESSOR)).getVirtualQuantity()
+ "");
for (ResourceAllocation disk : filter(vm.getHardware().getResourceAllocations(),
resourceType(ResourceType.DISK_DRIVE))) {
if (disk instanceof VCloudHardDisk) {
VCloudHardDisk vDisk = VCloudHardDisk.class.cast(disk);
extra.put(String.format("disk_drive/%s/kb", disk.getAddressOnParent()), vDisk.getCapacity() + "");
} else {
extra.put(String.format("disk_drive/%s/kb", disk.getAddressOnParent()), disk.getVirtualQuantity() + "");
}
}
for (ResourceAllocation net : filter(vm.getHardware().getResourceAllocations(),
resourceType(ResourceType.ETHERNET_ADAPTER))) {
if (net instanceof VCloudNetworkAdapter) {
VCloudNetworkAdapter vNet = VCloudNetworkAdapter.class.cast(net);
extra.put(String.format("network/%s/ip", net.getAddressOnParent()), vNet.getIpAddress());
}
}
} catch (Exception e) {
logger.error(e, "error getting extra data for vApp: %s", vApp);
}

View File

@ -30,6 +30,7 @@ import org.jclouds.vcloud.domain.VAppTemplate;
import org.jclouds.vcloud.functions.AllCatalogItemsInOrg;
import com.google.common.base.Function;
import com.google.common.base.Predicates;
import com.google.common.collect.Iterables;
/**
@ -55,7 +56,7 @@ public class ImagesInOrg implements Function<Org, Iterable<? extends Image>> {
public Iterable<? extends Image> apply(Org from) {
Iterable<? extends CatalogItem> catalogs = allCatalogItemsInOrg.apply(from);
Iterable<? extends VAppTemplate> vAppTemplates = vAppTemplatesForCatalogItems.apply(catalogs);
return Iterables.transform(vAppTemplates, imageForVAppTemplateProvider.get().withParent(from));
return Iterables.transform(Iterables.filter(vAppTemplates, Predicates.notNull()), imageForVAppTemplateProvider.get().withParent(from));
}
}

View File

@ -1,58 +0,0 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed 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.inject.Inject;
import javax.inject.Singleton;
import org.jclouds.compute.domain.Image;
import org.jclouds.vcloud.domain.VAppTemplate;
import org.jclouds.vcloud.domain.VDC;
import org.jclouds.vcloud.functions.VAppTemplatesForResourceEntities;
import com.google.common.base.Function;
import com.google.common.collect.Iterables;
/**
* @author Adrian Cole
*/
@Singleton
public class ImagesInVDC implements Function<VDC, Iterable<? extends Image>> {
private final VAppTemplatesForResourceEntities vAppTemplatesForResourceEntities;
private final ImageForVAppTemplate imageForVAppTemplateProvider;
@Inject
public ImagesInVDC(VAppTemplatesForResourceEntities vAppTemplatesForResourceEntities,
ImageForVAppTemplate imageForVAppTemplateProvider) {
this.vAppTemplatesForResourceEntities = checkNotNull(vAppTemplatesForResourceEntities,
"vAppTemplatesForResourceEntities");
this.imageForVAppTemplateProvider = checkNotNull(imageForVAppTemplateProvider, "imageForVAppTemplateProvider");
}
@Override
public Iterable<? extends Image> apply(VDC from) {
Iterable<? extends VAppTemplate> vAppTemplates = vAppTemplatesForResourceEntities.apply(checkNotNull(from, "vdc")
.getResourceEntities().values());
return Iterables.transform(vAppTemplates, imageForVAppTemplateProvider.withParent(from));
}
}

View File

@ -20,6 +20,8 @@
package org.jclouds.vcloud.compute.internal;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.Iterables.filter;
import static org.jclouds.vcloud.predicates.VCloudPredicates.resourceType;
import java.net.URI;
import java.util.Map;
@ -31,16 +33,22 @@ import javax.inject.Singleton;
import org.jclouds.compute.domain.NodeState;
import org.jclouds.vcloud.VCloudClient;
import org.jclouds.vcloud.compute.VCloudComputeClient;
import org.jclouds.vcloud.domain.ResourceAllocation;
import org.jclouds.vcloud.domain.ResourceType;
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.domain.VCloudNetworkAdapter;
import org.jclouds.vcloud.domain.VDC;
import org.jclouds.vcloud.domain.Vm;
import org.jclouds.vcloud.options.InstantiateVAppTemplateOptions;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.inject.Inject;
import com.google.inject.internal.util.Sets;
/**
* @author Adrian Cole
@ -67,8 +75,8 @@ public class VCloudComputeClientImpl extends CommonVCloudComputeClientImpl<VAppT
VDC vdc = client.getVDC(VDC);
VAppTemplate template = VCloudClient.class.cast(client).getVAppTemplate(templateId);
VApp vAppResponse = VCloudClient.class.cast(client).instantiateVAppTemplateInVDC(vdc.getHref(), template.getHref(),
name, options);
VApp vAppResponse = VCloudClient.class.cast(client).instantiateVAppTemplateInVDC(vdc.getHref(),
template.getHref(), name, options);
logger.debug("<< instantiated VApp(%s)", vAppResponse.getName());
logger.debug(">> deploying vApp(%s)", vAppResponse.getName());
@ -91,10 +99,18 @@ public class VCloudComputeClientImpl extends CommonVCloudComputeClientImpl<VAppT
@Override
public Set<String> getPublicAddresses(URI id) {
return ImmutableSet.of();
// TODO
// VApp vApp = refreshVApp(id);
// return Sets.newHashSet(vApp.getNetworkToAddresses().values());
Set<String> ips = Sets.newLinkedHashSet();
VApp vApp = refreshVApp(id);
// TODO make this work with composite vApps
Vm vm = Iterables.get(vApp.getChildren(), 0);
for (ResourceAllocation net : filter(vm.getHardware().getResourceAllocations(),
resourceType(ResourceType.ETHERNET_ADAPTER))) {
if (net instanceof VCloudNetworkAdapter) {
VCloudNetworkAdapter vNet = VCloudNetworkAdapter.class.cast(net);
ips.add(vNet.getIpAddress());
}
}
return ips;
}
@Override

View File

@ -47,9 +47,11 @@ import org.jclouds.vcloud.compute.functions.FindLocationForResource;
import org.jclouds.vcloud.compute.functions.GetExtraFromVApp;
import org.jclouds.vcloud.domain.Status;
import org.jclouds.vcloud.domain.VApp;
import org.jclouds.vcloud.domain.Vm;
import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
/**
* @author Adrian Cole
@ -92,11 +94,11 @@ public class VCloudGetNodeMetadataStrategy implements GetNodeMetadataStrategy {
}
private OperatingSystem getOperatingSystemForVAppOrDefaultTo(VApp vApp, OperatingSystem operatingSystem) {
// TODO
return new CIMOperatingSystem(CIMOperatingSystem.OSType.UBUNTU_64, null, null, vApp.getDescription());
// return vApp.getOsType() != null ? new
// CIMOperatingSystem(CIMOperatingSystem.OSType.fromValue(vApp.getOsType()),
// null, null, vApp.getOperatingSystemDescription()) : operatingSystem;
// TODO we need to change the design so that it doesn't assume single-vms
Vm vm = Iterables.get(vApp.getChildren(), 0);
return vm.getOperatingSystem() != null && vm.getOperatingSystem().getId() != null ? new CIMOperatingSystem(
CIMOperatingSystem.OSType.fromValue(vm.getOperatingSystem().getId()), null, null, vm
.getOperatingSystem().getDescription()) : operatingSystem;
}
}

View File

@ -0,0 +1,149 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed 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.domain;
import static com.google.common.base.Preconditions.checkNotNull;
import java.net.URI;
import javax.annotation.Nullable;
import org.jclouds.vcloud.domain.internal.ReferenceTypeImpl;
/**
* A description of the operating system supported by a virtual machine.
*/
public class OperatingSystem extends ReferenceTypeImpl {
@Nullable
private final Integer id;
@Nullable
private final String info;
@Nullable
private final String vmwOsType;
@Nullable
private final String description;
private final ReferenceType edit;
public OperatingSystem(@Nullable String name, String type, URI href, @Nullable Integer id, @Nullable String info,
@Nullable String vmwOsType, @Nullable String description, ReferenceType edit) {
super(name, type, href);
this.id = id;
this.info = info;
this.vmwOsType = vmwOsType;
this.description = description;
this.edit = checkNotNull(edit, "edit");
}
/**
*
* @return ovf id
*/
public Integer getId() {
return id;
}
/**
*
* @return ovf info
*/
public String getInfo() {
return info;
}
/**
*
* @return VMware osType, if running on VMware
*/
public String getVmwOsType() {
return vmwOsType;
}
/**
*
* @return description or null
*/
public String getDescription() {
return description;
}
/**
*
* @return edit link
*/
public ReferenceType getEdit() {
return edit;
}
@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + ((description == null) ? 0 : description.hashCode());
result = prime * result + ((edit == null) ? 0 : edit.hashCode());
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((info == null) ? 0 : info.hashCode());
result = prime * result + ((vmwOsType == null) ? 0 : vmwOsType.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!super.equals(obj))
return false;
if (getClass() != obj.getClass())
return false;
OperatingSystem other = (OperatingSystem) obj;
if (description == null) {
if (other.description != null)
return false;
} else if (!description.equals(other.description))
return false;
if (edit == null) {
if (other.edit != null)
return false;
} else if (!edit.equals(other.edit))
return false;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (info == null) {
if (other.info != null)
return false;
} else if (!info.equals(other.info))
return false;
if (vmwOsType == null) {
if (other.vmwOsType != null)
return false;
} else if (!vmwOsType.equals(other.vmwOsType))
return false;
return true;
}
@Override
public String toString() {
return "[href=" + getHref() + ", name=" + getName() + ", type=" + getType() + ", id=" + getId() + ", vmwOsType="
+ getVmwOsType() + ", description=" + getDescription() + "]";
}
}

View File

@ -75,6 +75,14 @@ public interface Vm extends ReferenceType {
*/
@Nullable
VirtualHardware getHardware();
/**
* @return operating system on this VM, or null, if part of a vApp template
*
* @since vcloud api 1.0
*/
@Nullable
OperatingSystem getOperatingSystem();
/**
* read-only identifier created on import

View File

@ -26,6 +26,7 @@ import java.util.List;
import javax.annotation.Nullable;
import org.jclouds.vcloud.domain.OperatingSystem;
import org.jclouds.vcloud.domain.ReferenceType;
import org.jclouds.vcloud.domain.Status;
import org.jclouds.vcloud.domain.Task;
@ -52,16 +53,18 @@ public class VmImpl extends ReferenceTypeImpl implements Vm {
@Nullable
private final VirtualHardware hardware;
private final String vAppScopedLocalId;
private final OperatingSystem os;
public VmImpl(String name, String type, URI id, @Nullable Status status, ReferenceType vApp,
@Nullable String description, Iterable<Task> tasks, @Nullable VirtualHardware hardware,
@Nullable String vAppScopedLocalId) {
@Nullable OperatingSystem os, @Nullable String vAppScopedLocalId) {
super(name, type, id);
this.status = status;
this.vApp = vApp;// TODO: once <1.0 is killed check not null
this.description = description;
Iterables.addAll(this.tasks, checkNotNull(tasks, "tasks"));
this.hardware = hardware;
this.os = os;
this.vAppScopedLocalId = vAppScopedLocalId;
}
@ -106,6 +109,14 @@ public class VmImpl extends ReferenceTypeImpl implements Vm {
return hardware;
}
/**
* {@inheritDoc}
*/
@Override
public OperatingSystem getOperatingSystem() {
return os;
}
/**
* {@inheritDoc}
*/
@ -120,6 +131,7 @@ public class VmImpl extends ReferenceTypeImpl implements Vm {
int result = super.hashCode();
result = prime * result + ((description == null) ? 0 : description.hashCode());
result = prime * result + ((hardware == null) ? 0 : hardware.hashCode());
result = prime * result + ((os == null) ? 0 : os.hashCode());
result = prime * result + ((status == null) ? 0 : status.hashCode());
result = prime * result + ((tasks == null) ? 0 : tasks.hashCode());
result = prime * result + ((vApp == null) ? 0 : vApp.hashCode());
@ -146,6 +158,11 @@ public class VmImpl extends ReferenceTypeImpl implements Vm {
return false;
} else if (!hardware.equals(other.hardware))
return false;
if (os == null) {
if (other.os != null)
return false;
} else if (!os.equals(other.os))
return false;
if (status == null) {
if (other.status != null)
return false;
@ -172,7 +189,7 @@ public class VmImpl extends ReferenceTypeImpl implements Vm {
@Override
public String toString() {
return "[href=" + getHref() + ", name=" + getName() + ", type=" + getType() + ", description=" + description
+ ", status=" + status + ", tasks=" + tasks + ", vApp=" + vApp + ", hardware=" + hardware
+ ", status=" + status + ", tasks=" + tasks + ", vApp=" + vApp + ", hardware=" + hardware + ", os=" + os
+ ", vAppScopedLocalId=" + vAppScopedLocalId + "]";
}

View File

@ -21,6 +21,7 @@ package org.jclouds.vcloud.functions;
import static com.google.common.collect.Iterables.filter;
import static org.jclouds.concurrent.FutureIterables.transformParallel;
import static org.jclouds.util.Utils.propagateOrNull;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
@ -32,7 +33,10 @@ import javax.inject.Singleton;
import org.jclouds.Constants;
import org.jclouds.compute.reference.ComputeServiceConstants;
import org.jclouds.concurrent.ExceptionParsingListenableFuture;
import org.jclouds.concurrent.Futures;
import org.jclouds.logging.Logger;
import org.jclouds.rest.AuthorizationException;
import org.jclouds.vcloud.CommonVCloudAsyncClient;
import org.jclouds.vcloud.VCloudAsyncClient;
import org.jclouds.vcloud.VCloudMediaType;
@ -47,18 +51,32 @@ import com.google.common.base.Predicate;
*/
@Singleton
public class VAppTemplatesForCatalogItems implements
Function<Iterable<? extends CatalogItem>, Iterable<? extends VAppTemplate>> {
Function<Iterable<? extends CatalogItem>, Iterable<? extends VAppTemplate>> {
@Resource
@Named(ComputeServiceConstants.COMPUTE_LOGGER)
public Logger logger = Logger.NULL;
private final CommonVCloudAsyncClient aclient;
private final ExecutorService executor;
private final ReturnNullOnAuthorizationException returnNullOnAuthorizationException;
@Singleton
static class ReturnNullOnAuthorizationException implements Function<Exception, VAppTemplate> {
public VAppTemplate apply(Exception from) {
if (from instanceof AuthorizationException) {
return null;
}
return propagateOrNull(from);
}
}
@Inject
VAppTemplatesForCatalogItems(CommonVCloudAsyncClient aclient,
@Named(Constants.PROPERTY_USER_THREADS) ExecutorService executor) {
@Named(Constants.PROPERTY_USER_THREADS) ExecutorService executor,
ReturnNullOnAuthorizationException returnNullOnAuthorizationException) {
this.aclient = aclient;
this.executor = executor;
this.returnNullOnAuthorizationException = returnNullOnAuthorizationException;
}
@Override
@ -75,8 +93,9 @@ public class VAppTemplatesForCatalogItems implements
@SuppressWarnings("unchecked")
@Override
public Future<VAppTemplate> apply(CatalogItem from) {
return (Future<VAppTemplate>) VCloudAsyncClient.class.cast(aclient).getVAppTemplate(
from.getEntity().getHref());
return new ExceptionParsingListenableFuture<VAppTemplate>(Futures.makeListenable(
(Future<VAppTemplate>) VCloudAsyncClient.class.cast(aclient).getVAppTemplate(
from.getEntity().getHref()), executor), returnNullOnAuthorizationException);
}
}, executor, null, logger, "vappTemplates in");

View File

@ -1,84 +0,0 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed 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.functions;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.Iterables.filter;
import static org.jclouds.concurrent.FutureIterables.transformParallel;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import javax.annotation.Resource;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import org.jclouds.Constants;
import org.jclouds.compute.reference.ComputeServiceConstants;
import org.jclouds.logging.Logger;
import org.jclouds.vcloud.VCloudAsyncClient;
import org.jclouds.vcloud.VCloudMediaType;
import org.jclouds.vcloud.domain.ReferenceType;
import org.jclouds.vcloud.domain.VAppTemplate;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
/**
* @author Adrian Cole
*/
@Singleton
public class VAppTemplatesForResourceEntities implements
Function<Iterable<? extends ReferenceType>, Iterable<? extends VAppTemplate>> {
@Resource
@Named(ComputeServiceConstants.COMPUTE_LOGGER)
public Logger logger = Logger.NULL;
private final VCloudAsyncClient aclient;
private final ExecutorService executor;
@Inject
VAppTemplatesForResourceEntities(VCloudAsyncClient aclient,
@Named(Constants.PROPERTY_USER_THREADS) ExecutorService executor) {
this.aclient = aclient;
this.executor = executor;
}
@Override
public Iterable<? extends VAppTemplate> apply(Iterable<? extends ReferenceType> from) {
return transformParallel(filter(checkNotNull(from, "named resources"), new Predicate<ReferenceType>() {
@Override
public boolean apply(ReferenceType input) {
return input.getType().equals(VCloudMediaType.VAPPTEMPLATE_XML);
}
}), new Function<ReferenceType, Future<VAppTemplate>>() {
@SuppressWarnings("unchecked")
@Override
public Future<VAppTemplate> apply(ReferenceType from) {
return (Future<VAppTemplate>) aclient.getVAppTemplate(from.getHref());
}
}, executor, null, logger, "vappTemplates in");
}
}

View File

@ -0,0 +1,87 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed 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.xml;
import static org.jclouds.vcloud.util.Utils.newReferenceType;
import java.util.Map;
import org.jclouds.http.functions.ParseSax;
import org.jclouds.vcloud.domain.OperatingSystem;
import org.jclouds.vcloud.domain.ReferenceType;
import org.jclouds.vcloud.util.Utils;
import org.xml.sax.Attributes;
/**
* @author Adrian Cole
*/
public class OperatingSystemHandler extends ParseSax.HandlerWithResult<OperatingSystem> {
private StringBuilder currentText = new StringBuilder();
protected ReferenceType os;
protected Integer id;
protected String info;
protected String vmwOsType;
protected String description;
protected ReferenceType edit;
public OperatingSystem getResult() {
OperatingSystem system = new OperatingSystem(os.getName(), os.getType(), os.getHref(), id, info, vmwOsType,
description, edit);
os = null;
id = null;
info = null;
vmwOsType = null;
description = null;
edit = null;
return system;
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attrs) {
Map<String, String> attributes = Utils.cleanseAttributes(attrs);
if (qName.endsWith("Link")) {
this.edit = Utils.newReferenceType(attributes);
} else if (qName.endsWith("OperatingSystemSection")) {
if (attributes.containsKey("href") && attributes.get("href").endsWith("/")) {
String href = attributes.get("href");
attributes.put("href", href.substring(0, href.lastIndexOf('/')));
}
os = newReferenceType(attributes);
vmwOsType = attributes.get("osType");
if (attributes.containsKey("id"))
this.id = Integer.parseInt(attributes.get("id"));
}
}
@Override
public void endElement(String uri, String localName, String qName) {
if (qName.endsWith("Info")) {
this.info = currentText.toString().trim();
} else if (qName.endsWith("Description")) {
this.description = currentText.toString().trim();
}
currentText = new StringBuilder();
}
public void characters(char ch[], int start, int length) {
currentText.append(ch, start, length);
}
}

View File

@ -65,6 +65,10 @@ public class VirtualHardwareHandler extends ParseSax.HandlerWithResult<VirtualHa
public void startElement(String uri, String localName, String qName, Attributes attrs) {
Map<String, String> attributes = cleanseAttributes(attrs);
if (attributes.containsKey("href") && attributes.get("href").endsWith("/")) {
String href = attributes.get("href");
attributes.put("href", href.substring(0, href.lastIndexOf('/')));
}
if (qName.endsWith("System")) {
inSystem = true;
} else if (!inSystem && qName.endsWith("Item")) {
@ -75,10 +79,6 @@ public class VirtualHardwareHandler extends ParseSax.HandlerWithResult<VirtualHa
} else if (inItem) {
allocationHandler.startElement(uri, localName, qName, attrs);
} else if (qName.endsWith("VirtualHardwareSection")) {
if (attributes.containsKey("href") && attributes.get("href").endsWith("/")) {
String href = attributes.get("href");
attributes.put("href", href.substring(0, href.lastIndexOf('/')));
}
hardware = newReferenceType(attributes);
}

View File

@ -28,6 +28,7 @@ import java.util.Map;
import javax.inject.Inject;
import org.jclouds.http.functions.ParseSax;
import org.jclouds.vcloud.domain.OperatingSystem;
import org.jclouds.vcloud.domain.ReferenceType;
import org.jclouds.vcloud.domain.Status;
import org.jclouds.vcloud.domain.Task;
@ -46,11 +47,14 @@ public class VmHandler extends ParseSax.HandlerWithResult<Vm> {
protected final TaskHandler taskHandler;
protected final VirtualHardwareHandler virtualHardwareHandler;
protected final OperatingSystemHandler operatingSystemHandler;
@Inject
public VmHandler(TaskHandler taskHandler, VirtualHardwareHandler virtualHardwareHandler) {
public VmHandler(TaskHandler taskHandler, VirtualHardwareHandler virtualHardwareHandler,
OperatingSystemHandler operatingSystemHandler) {
this.taskHandler = taskHandler;
this.virtualHardwareHandler = virtualHardwareHandler;
this.operatingSystemHandler = operatingSystemHandler;
}
protected StringBuilder currentText = new StringBuilder();
@ -61,13 +65,15 @@ public class VmHandler extends ParseSax.HandlerWithResult<Vm> {
protected String description;
protected List<Task> tasks = Lists.newArrayList();
protected VirtualHardware hardware;
protected OperatingSystem os;
protected String vAppScopedLocalId;
private boolean inTasks;
private boolean inHardware;
private boolean inOs;
public Vm getResult() {
return new VmImpl(vm.getName(), vm.getType(), vm.getHref(), status, vdc, description, tasks, hardware,
return new VmImpl(vm.getName(), vm.getType(), vm.getHref(), status, vdc, description, tasks, hardware, os,
vAppScopedLocalId);
}
@ -76,11 +82,15 @@ public class VmHandler extends ParseSax.HandlerWithResult<Vm> {
Map<String, String> attributes = cleanseAttributes(attrs);
if (qName.endsWith("VirtualHardwareSection")) {
inHardware = true;
} else if (qName.endsWith("OperatingSystemSection")) {
inOs = true;
} else if (qName.endsWith("Tasks")) {
inTasks = true;
}
if (inHardware) {
virtualHardwareHandler.startElement(uri, localName, qName, attrs);
} else if (inOs) {
operatingSystemHandler.startElement(uri, localName, qName, attrs);
} else if (inTasks) {
taskHandler.startElement(uri, localName, qName, attrs);
} else if (qName.equals("Vm")) {
@ -97,12 +107,17 @@ public class VmHandler extends ParseSax.HandlerWithResult<Vm> {
if (qName.endsWith("VirtualHardwareSection")) {
inHardware = false;
this.hardware = virtualHardwareHandler.getResult();
} else if (qName.endsWith("OperatingSystemSection")) {
inOs = false;
os = operatingSystemHandler.getResult();
} else if (qName.endsWith("Tasks")) {
inTasks = false;
this.tasks.add(taskHandler.getResult());
}
if (inHardware) {
virtualHardwareHandler.endElement(uri, name, qName);
} else if (inOs) {
operatingSystemHandler.endElement(uri, name, qName);
} else if (inTasks) {
taskHandler.endElement(uri, name, qName);
} else if (qName.equals("Description")) {
@ -118,6 +133,8 @@ public class VmHandler extends ParseSax.HandlerWithResult<Vm> {
taskHandler.characters(ch, start, length);
if (inHardware)
virtualHardwareHandler.characters(ch, start, length);
if (inOs)
operatingSystemHandler.characters(ch, start, length);
currentText.append(ch, start, length);
}

View File

@ -0,0 +1,63 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed 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.xml;
import static org.testng.Assert.assertEquals;
import java.io.InputStream;
import java.net.URI;
import java.net.UnknownHostException;
import org.jclouds.http.functions.BaseHandlerTest;
import org.jclouds.vcloud.domain.OperatingSystem;
import org.jclouds.vcloud.domain.internal.ReferenceTypeImpl;
import org.testng.annotations.Test;
/**
* Tests behavior of {@code OperatingSystemHandler}
*
* @author Adrian Cole
*/
@Test(groups = "unit", testName = "vcloud.OperatingSystemHandlerTest")
public class OperatingSystemHandlerTest extends BaseHandlerTest {
public void testDefault() throws UnknownHostException {
InputStream is = getClass().getResourceAsStream("/os.xml");
OperatingSystem result = factory.create(injector.getInstance(OperatingSystemHandler.class)).parse(is);
checkOs(result);
}
static void checkOs(OperatingSystem result) {
assertEquals(result.getHref(), URI
.create("https://vcenterprise.bluelock.com/api/v1.0/vApp/vm-2087535248/operatingSystemSection"));
assertEquals(result.getDescription(), "Red Hat Enterprise Linux 5 (64-bit)");
assertEquals(result.getEdit(), new ReferenceTypeImpl(null,
"application/vnd.vmware.vcloud.operatingSystemSection+xml",
URI.create("https://vcenterprise.bluelock.com/api/v1.0/vApp/vm-2087535248/operatingSystemSection/")));
assertEquals(result.getId(), new Integer(80));
assertEquals(result.getVmwOsType(), "rhel5_64Guest");
assertEquals(result.getName(), null);
assertEquals(result.getType(), "application/vnd.vmware.vcloud.operatingSystemSection+xml");
assertEquals(result.getInfo(), "Specifies the operating system installed");
}
}

View File

@ -63,6 +63,7 @@ public class VmHandlerTest {
assertEquals(result.getTasks(), ImmutableList.of());
assertEquals(result.getVAppScopedLocalId(), "10_rhel_template");
VirtualHardwareHandlerTest.checkHardware(result.getHardware());
OperatingSystemHandlerTest.checkOs(result.getOperatingSystem());
}
}

View File

@ -0,0 +1,11 @@
<ovf:OperatingSystemSection xmlns:vcloud="http://www.vmware.com/vcloud/v1"
xmlns:vmw="http://www.vmware.com/schema/ovf" ovf:id="80"
vcloud:href="https://vcenterprise.bluelock.com/api/v1.0/vApp/vm-2087535248/operatingSystemSection/"
vcloud:type="application/vnd.vmware.vcloud.operatingSystemSection+xml"
vmw:osType="rhel5_64Guest">
<ovf:Info>Specifies the operating system installed</ovf:Info>
<ovf:Description>Red Hat Enterprise Linux 5 (64-bit)</ovf:Description>
<Link rel="edit"
type="application/vnd.vmware.vcloud.operatingSystemSection+xml"
href="https://vcenterprise.bluelock.com/api/v1.0/vApp/vm-2087535248/operatingSystemSection/" />
</ovf:OperatingSystemSection>