From 529a0ad0750e220a624a8f9664030ad826a4e643 Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Fri, 12 Mar 2010 16:00:41 -0800 Subject: [PATCH] added better error handling for vcloud --- .../compute/domain/internal/ImageImpl.java | 5 ++- .../BlueLockVCloudRestClientModule.java | 14 +++++--- .../VCloudComputeServiceContextModule.java | 3 +- .../vcloud/config/VCloudRestClientModule.java | 33 ++++++++++++++----- .../HostingDotComVCloudRestClientModule.java | 4 +-- 5 files changed, 39 insertions(+), 20 deletions(-) diff --git a/compute/src/main/java/org/jclouds/compute/domain/internal/ImageImpl.java b/compute/src/main/java/org/jclouds/compute/domain/internal/ImageImpl.java index 64f06b1f67..ad32a7dfe7 100644 --- a/compute/src/main/java/org/jclouds/compute/domain/internal/ImageImpl.java +++ b/compute/src/main/java/org/jclouds/compute/domain/internal/ImageImpl.java @@ -103,9 +103,8 @@ public class ImageImpl extends ComputeMetadataImpl implements Image { @Override public String toString() { return "[id=" + getId() + ", name=" + getName() + ", locationId=" + getLocationId() - + ", architecture=" + architecture + ", description=" + description - + ", osDescription=" + osDescription + ", osFamily=" + osFamily + ", version=" - + version + "]"; + + ", architecture=" + architecture + ", osDescription=" + osDescription + + ", osFamily=" + osFamily + ", version=" + version + "]"; } @Override diff --git a/vcloud/bluelock/src/main/java/org/jclouds/vcloud/bluelock/config/BlueLockVCloudRestClientModule.java b/vcloud/bluelock/src/main/java/org/jclouds/vcloud/bluelock/config/BlueLockVCloudRestClientModule.java index c941702ddb..3f9dba6c91 100644 --- a/vcloud/bluelock/src/main/java/org/jclouds/vcloud/bluelock/config/BlueLockVCloudRestClientModule.java +++ b/vcloud/bluelock/src/main/java/org/jclouds/vcloud/bluelock/config/BlueLockVCloudRestClientModule.java @@ -18,18 +18,19 @@ */ package org.jclouds.vcloud.bluelock.config; +import static com.google.common.base.Preconditions.checkState; import static org.jclouds.vcloud.reference.VCloudConstants.PROPERTY_VCLOUD_USER; import java.net.URI; +import java.util.Map; import java.util.concurrent.ExecutionException; -import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import javax.inject.Named; import org.jclouds.http.RequiresHttp; import org.jclouds.rest.ConfiguresRestClient; -import org.jclouds.vcloud.VCloudAsyncClient; +import org.jclouds.vcloud.VCloudClient; import org.jclouds.vcloud.config.VCloudRestClientModule; import org.jclouds.vcloud.domain.NamedResource; import org.jclouds.vcloud.domain.Organization; @@ -47,11 +48,13 @@ import com.google.common.collect.Iterables; public class BlueLockVCloudRestClientModule extends VCloudRestClientModule { @Override - protected URI provideDefaultNetwork(VCloudAsyncClient client) throws InterruptedException, + protected URI provideDefaultNetwork(VCloudClient client) throws InterruptedException, ExecutionException, TimeoutException { + org.jclouds.vcloud.domain.VDC vDC = client.getDefaultVDC(); + Map networks = vDC.getAvailableNetworks(); + checkState(networks.size() > 0, "No networks present in vDC: " + vDC.getName()); return Iterables.getOnlyElement( - Iterables.filter(client.getDefaultVDC().get(180, TimeUnit.SECONDS) - .getAvailableNetworks().values(), new Predicate() { + Iterables.filter(networks.values(), new Predicate() { @Override public boolean apply(NamedResource input) { @@ -63,6 +66,7 @@ public class BlueLockVCloudRestClientModule extends VCloudRestClientModule { @Override protected URI provideCatalog(Organization org, @Named(PROPERTY_VCLOUD_USER) final String user) { + checkState(org.getCatalogs().size() > 0, "No catalogs present in org: " + org.getName()); return Iterables.getOnlyElement( Iterables.filter(org.getCatalogs().values(), new Predicate() { diff --git a/vcloud/core/src/main/java/org/jclouds/vcloud/compute/config/VCloudComputeServiceContextModule.java b/vcloud/core/src/main/java/org/jclouds/vcloud/compute/config/VCloudComputeServiceContextModule.java index 4c71b7d759..330f72542d 100755 --- a/vcloud/core/src/main/java/org/jclouds/vcloud/compute/config/VCloudComputeServiceContextModule.java +++ b/vcloud/core/src/main/java/org/jclouds/vcloud/compute/config/VCloudComputeServiceContextModule.java @@ -230,7 +230,8 @@ public class VCloudComputeServiceContextModule extends VCloudContextModule { void addVAppToSetRetryingIfNotYetPresent(Set nodes, NamedResource vdc, NamedResource resource) { NodeMetadata node = null; - while (node == null) { + int i = 0; + while (node == null && i++ < 3) { try { node = getNodeMetadataByIdInVDC(vdc.getId(), resource.getId()); } catch (NullPointerException e) { diff --git a/vcloud/core/src/main/java/org/jclouds/vcloud/config/VCloudRestClientModule.java b/vcloud/core/src/main/java/org/jclouds/vcloud/config/VCloudRestClientModule.java index 70694f0ea1..608ea34216 100644 --- a/vcloud/core/src/main/java/org/jclouds/vcloud/config/VCloudRestClientModule.java +++ b/vcloud/core/src/main/java/org/jclouds/vcloud/config/VCloudRestClientModule.java @@ -18,6 +18,8 @@ */ package org.jclouds.vcloud.config; +import static com.google.common.base.Preconditions.checkNotNull; +import static com.google.common.base.Preconditions.checkState; import static org.jclouds.vcloud.reference.VCloudConstants.PROPERTY_VCLOUD_DEFAULT_NETWORK; import static org.jclouds.vcloud.reference.VCloudConstants.PROPERTY_VCLOUD_ENDPOINT; import static org.jclouds.vcloud.reference.VCloudConstants.PROPERTY_VCLOUD_KEY; @@ -29,6 +31,8 @@ import java.io.UnsupportedEncodingException; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.URI; +import java.util.Map; +import java.util.SortedMap; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; @@ -51,6 +55,7 @@ import org.jclouds.rest.RestClientFactory; import org.jclouds.vcloud.VCloudAsyncClient; import org.jclouds.vcloud.VCloudClient; import org.jclouds.vcloud.VCloudToken; +import org.jclouds.vcloud.domain.NamedResource; import org.jclouds.vcloud.domain.Organization; import org.jclouds.vcloud.domain.VApp; import org.jclouds.vcloud.endpoints.Catalog; @@ -121,14 +126,16 @@ public class VCloudRestClientModule extends AbstractModule { @VCloudToken @Provides String provideVCloudToken(Supplier cache) { - return cache.get().getVCloudToken(); + return checkNotNull(cache.get().getVCloudToken(), "No token present in session"); } @Provides @Org @Singleton protected URI provideOrg(Supplier cache, @Named(PROPERTY_VCLOUD_USER) String user) { - return Iterables.getLast(cache.get().getOrgs().values()).getLocation(); + VCloudSession discovery = cache.get(); + checkState(discovery.getOrgs().size() > 0, "No orgs present for user: " + user); + return Iterables.getLast(discovery.getOrgs().values()).getLocation(); } @Provides @@ -170,7 +177,12 @@ public class VCloudRestClientModule extends AbstractModule { protected URI provideAuthenticationURI(VCloudVersionsAsyncClient versionService, @Named(PROPERTY_VCLOUD_VERSION) String version) throws InterruptedException, ExecutionException, TimeoutException { - return versionService.getSupportedVersions().get(180, TimeUnit.SECONDS).get(version); + SortedMap versions = versionService.getSupportedVersions().get(180, + TimeUnit.SECONDS); + checkState(versions.size() > 0, "No versions present"); + checkState(versions.containsKey(version), "version " + version + " not present in: " + + versions); + return versions.get(version); } @Provides @@ -238,6 +250,7 @@ public class VCloudRestClientModule extends AbstractModule { @VDC @Singleton protected URI provideDefaultVDC(Organization org) { + checkState(org.getVDCs().size() > 0, "No vdcs present in org: " + org.getName()); return Iterables.get(org.getVDCs().values(), 0).getLocation(); } @@ -245,18 +258,19 @@ public class VCloudRestClientModule extends AbstractModule { @Catalog @Singleton protected URI provideCatalog(Organization org, @Named(PROPERTY_VCLOUD_USER) String user) { + checkState(org.getCatalogs().size() > 0, "No catalogs present in org: " + org.getName()); return Iterables.get(org.getCatalogs().values(), 0).getLocation(); } @Provides @Network @Singleton - protected URI provideDefaultNetwork(VCloudAsyncClient client) throws InterruptedException, + protected URI provideDefaultNetwork(VCloudClient client) throws InterruptedException, ExecutionException, TimeoutException { - return Iterables - .get( - client.getDefaultVDC().get(180, TimeUnit.SECONDS).getAvailableNetworks() - .values(), 0).getLocation(); + org.jclouds.vcloud.domain.VDC vDC = client.getDefaultVDC(); + Map networks = vDC.getAvailableNetworks(); + checkState(networks.size() > 0, "No networks present in vDC: " + vDC.getName()); + return Iterables.get(networks.values(), 0).getLocation(); } @Provides @@ -270,6 +284,7 @@ public class VCloudRestClientModule extends AbstractModule { @TasksList @Singleton protected URI provideDefaultTasksList(Organization org) { - return org.getTasksLists().values().iterator().next().getLocation(); + checkState(org.getTasksLists().size() > 0, "No tasks lists present in org: " + org.getName()); + return Iterables.get(org.getTasksLists().values(), 0).getLocation(); } } diff --git a/vcloud/hostingdotcom/src/main/java/org/jclouds/vcloud/hostingdotcom/config/HostingDotComVCloudRestClientModule.java b/vcloud/hostingdotcom/src/main/java/org/jclouds/vcloud/hostingdotcom/config/HostingDotComVCloudRestClientModule.java index 60b288a410..255e19258a 100644 --- a/vcloud/hostingdotcom/src/main/java/org/jclouds/vcloud/hostingdotcom/config/HostingDotComVCloudRestClientModule.java +++ b/vcloud/hostingdotcom/src/main/java/org/jclouds/vcloud/hostingdotcom/config/HostingDotComVCloudRestClientModule.java @@ -72,10 +72,10 @@ public class HostingDotComVCloudRestClientModule extends VCloudRestClientModule } @Override - protected URI provideDefaultNetwork(VCloudAsyncClient client) { + protected URI provideDefaultNetwork(VCloudClient client) { return URI.create("https://vcloud.safesecureweb.com/network/1990"); } - + @Override protected Predicate successTester(TaskSuccess success) { return new RetryablePredicate(success, 45, 10, TimeUnit.MINUTES);