Issue 139: introduced default exception for 404 errors

git-svn-id: http://jclouds.googlecode.com/svn/trunk@2501 3d8758e0-26b5-11de-8745-db77d3ebf521
This commit is contained in:
adrian.f.cole 2009-12-22 00:05:16 +00:00
parent 1e6848f7c8
commit 9c4ff83f25
18 changed files with 394 additions and 254 deletions

View File

@ -58,6 +58,7 @@ import org.jclouds.http.functions.ReturnVoidIf2xx;
import org.jclouds.logging.Logger;
import org.jclouds.logging.Logger.LoggerFactory;
import org.jclouds.rest.config.RestModule;
import org.jclouds.rest.functions.ThrowResourceNotFoundOn404;
import org.jclouds.rest.internal.GeneratedHttpRequest;
import org.jclouds.rest.internal.RestAnnotationProcessor;
import org.jclouds.util.Jsr330;
@ -95,7 +96,7 @@ public class AzureBlobClientTest {
.singletonList("2009-07-17"));
assertEquals(processor.createResponseParser(method, httpMethod).getClass(), ParseSax.class);
// TODO check generic type of response parser
assertEquals(processor.createExceptionParserOrNullIfNotFound(method), null);
assertEquals(processor.createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(method).getClass(), ThrowResourceNotFoundOn404.class);
}
public void testListContainersOptions() throws SecurityException, NoSuchMethodException {
@ -116,7 +117,7 @@ public class AzureBlobClientTest {
.singletonList("2009-07-17"));
assertEquals(processor.createResponseParser(method, httpMethod).getClass(), ParseSax.class);
// TODO check generic type of response parser
assertEquals(processor.createExceptionParserOrNullIfNotFound(method), null);
assertEquals(processor.createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(method).getClass(), ThrowResourceNotFoundOn404.class);
}
public void testCreateContainer() throws SecurityException, NoSuchMethodException {
@ -136,7 +137,7 @@ public class AzureBlobClientTest {
assertEquals(processor.createResponseParser(method, httpMethod).getClass(),
ReturnTrueIf2xx.class);
// TODO check generic type of response parser
assertEquals(processor.createExceptionParserOrNullIfNotFound(method).getClass(),
assertEquals(processor.createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(method).getClass(),
ReturnTrueIfContainerAlreadyExists.class);
}
@ -155,7 +156,7 @@ public class AzureBlobClientTest {
assertEquals(processor.createResponseParser(method, httpMethod).getClass(),
ReturnVoidIf2xx.class);
// TODO check generic type of response parser
assertEquals(processor.createExceptionParserOrNullIfNotFound(method).getClass(),
assertEquals(processor.createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(method).getClass(),
ReturnVoidOnNotFoundOr404.class);
}
@ -180,7 +181,7 @@ public class AzureBlobClientTest {
assertEquals(processor.createResponseParser(method, httpMethod).getClass(),
ReturnTrueIf2xx.class);
// TODO check generic type of response parser
assertEquals(processor.createExceptionParserOrNullIfNotFound(method).getClass(),
assertEquals(processor.createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(method).getClass(),
ReturnTrueIfContainerAlreadyExists.class);
}
@ -201,7 +202,7 @@ public class AzureBlobClientTest {
assertEquals(processor.createResponseParser(method, httpMethod).getClass(),
ReturnTrueIf2xx.class);
// TODO check generic type of response parser
assertEquals(processor.createExceptionParserOrNullIfNotFound(method).getClass(),
assertEquals(processor.createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(method).getClass(),
ReturnTrueIfContainerAlreadyExists.class);
}
@ -220,7 +221,7 @@ public class AzureBlobClientTest {
assertEquals(processor.createResponseParser(method, httpMethod).getClass(),
ReturnTrueIf2xx.class);
// TODO check generic type of response parser
assertEquals(processor.createExceptionParserOrNullIfNotFound(method).getClass(),
assertEquals(processor.createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(method).getClass(),
ReturnTrueOn404.class);
}
@ -244,7 +245,7 @@ public class AzureBlobClientTest {
assertEquals(processor.createResponseParser(method, httpMethod).getClass(),
ReturnTrueIf2xx.class);
// TODO check generic type of response parser
assertEquals(processor.createExceptionParserOrNullIfNotFound(method).getClass(),
assertEquals(processor.createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(method).getClass(),
ReturnTrueIfContainerAlreadyExists.class);
}
@ -263,7 +264,7 @@ public class AzureBlobClientTest {
.singletonList("2009-07-17"));
assertEquals(processor.createResponseParser(method, httpMethod).getClass(), ParseSax.class);
// TODO check generic type of response parser
assertEquals(processor.createExceptionParserOrNullIfNotFound(method), null);
assertEquals(processor.createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(method).getClass(), ThrowResourceNotFoundOn404.class);
}
public void testListRootBlobs() throws SecurityException, NoSuchMethodException {
@ -281,7 +282,7 @@ public class AzureBlobClientTest {
.singletonList("2009-07-17"));
assertEquals(processor.createResponseParser(method, httpMethod).getClass(), ParseSax.class);
// TODO check generic type of response parser
assertEquals(processor.createExceptionParserOrNullIfNotFound(method), null);
assertEquals(processor.createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(method).getClass(), ThrowResourceNotFoundOn404.class);
}
public void testContainerProperties() throws SecurityException, NoSuchMethodException {
@ -298,7 +299,7 @@ public class AzureBlobClientTest {
.singletonList("2009-07-17"));
assertEquals(processor.createResponseParser(method, httpMethod).getClass(),
ParseContainerPropertiesFromHeaders.class);
assertEquals(processor.createExceptionParserOrNullIfNotFound(method), null);
assertEquals(processor.createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(method).getClass(), ThrowResourceNotFoundOn404.class);
}
public void testSetResourceMetadata() throws SecurityException, NoSuchMethodException {
@ -320,7 +321,7 @@ public class AzureBlobClientTest {
assertEquals(processor.createResponseParser(method, httpMethod).getClass(),
ReturnVoidIf2xx.class);
assertEquals(processor.createExceptionParserOrNullIfNotFound(method), null);
assertEquals(processor.createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(method).getClass(), ThrowResourceNotFoundOn404.class);
}
public void testSetBlobMetadata() throws SecurityException, NoSuchMethodException {
@ -341,7 +342,7 @@ public class AzureBlobClientTest {
assertEquals(processor.createResponseParser(method, httpMethod).getClass(),
ReturnVoidIf2xx.class);
assertEquals(processor.createExceptionParserOrNullIfNotFound(method), null);
assertEquals(processor.createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(method).getClass(), ThrowResourceNotFoundOn404.class);
}
@BeforeClass

View File

@ -47,6 +47,7 @@ import org.jclouds.http.functions.ReturnTrueIf2xx;
import org.jclouds.logging.Logger;
import org.jclouds.logging.Logger.LoggerFactory;
import org.jclouds.rest.config.RestModule;
import org.jclouds.rest.functions.ThrowResourceNotFoundOn404;
import org.jclouds.rest.internal.GeneratedHttpRequest;
import org.jclouds.rest.internal.RestAnnotationProcessor;
import org.jclouds.util.Jsr330;
@ -86,7 +87,7 @@ public class AzureQueueClientTest {
.singletonList("2009-07-17"));
assertEquals(processor.createResponseParser(method, httpMethod).getClass(), ParseSax.class);
// TODO check generic type of response parser
assertEquals(processor.createExceptionParserOrNullIfNotFound(method), null);
assertEquals(processor.createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(method).getClass(), ThrowResourceNotFoundOn404.class);
}
public void testListQueuesOptions() throws SecurityException, NoSuchMethodException {
@ -106,7 +107,7 @@ public class AzureQueueClientTest {
.singletonList("2009-07-17"));
assertEquals(processor.createResponseParser(method, httpMethod).getClass(), ParseSax.class);
// TODO check generic type of response parser
assertEquals(processor.createExceptionParserOrNullIfNotFound(method), null);
assertEquals(processor.createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(method).getClass(), ThrowResourceNotFoundOn404.class);
}
public void testCreateQueue() throws SecurityException, NoSuchMethodException {
@ -126,7 +127,7 @@ public class AzureQueueClientTest {
assertEquals(processor.createResponseParser(method, httpMethod).getClass(),
ReturnTrueIf2xx.class);
// TODO check generic type of response parser
assertEquals(processor.createExceptionParserOrNullIfNotFound(method), null);
assertEquals(processor.createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(method).getClass(), ThrowResourceNotFoundOn404.class);
}
public void testDeleteQueue() throws SecurityException, NoSuchMethodException {
@ -144,7 +145,7 @@ public class AzureQueueClientTest {
assertEquals(processor.createResponseParser(method, httpMethod).getClass(),
ReturnTrueIf2xx.class);
// TODO check generic type of response parser
assertEquals(processor.createExceptionParserOrNullIfNotFound(method), null);
assertEquals(processor.createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(method).getClass(), ThrowResourceNotFoundOn404.class);
}
public void testCreateQueueOptions() throws SecurityException, NoSuchMethodException {
@ -165,7 +166,7 @@ public class AzureQueueClientTest {
assertEquals(processor.createResponseParser(method, httpMethod).getClass(),
ReturnTrueIf2xx.class);
// TODO check generic type of response parser
assertEquals(processor.createExceptionParserOrNullIfNotFound(method), null);
assertEquals(processor.createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(method).getClass(), ThrowResourceNotFoundOn404.class);
}
@BeforeClass

View File

@ -23,12 +23,14 @@
*/
package org.jclouds.blobstore;
import org.jclouds.rest.ResourceNotFoundException;
/**
* Thrown when a blob cannot be located in the container.
*
* @author Adrian Cole
*/
public class KeyNotFoundException extends RuntimeException {
public class KeyNotFoundException extends ResourceNotFoundException {
private String container;
private String key;

View File

@ -0,0 +1,53 @@
/**
*
* Copyright (C) 2009 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* 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.rest;
/**
* Thrown when a resource cannot be located.
*
* @author Adrian Cole
*/
public class ResourceNotFoundException extends RuntimeException {
/** The serialVersionUID */
private static final long serialVersionUID = -2272965726680821281L;
public ResourceNotFoundException() {
super();
}
public ResourceNotFoundException(String arg0, Throwable arg1) {
super(arg0, arg1);
}
public ResourceNotFoundException(String arg0) {
super(arg0);
}
public ResourceNotFoundException(Throwable arg0) {
super(arg0);
}
}

View File

@ -0,0 +1,47 @@
/**
*
* Copyright (C) 2009 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* 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.rest.functions;
import org.jclouds.http.HttpResponseException;
import org.jclouds.rest.ResourceNotFoundException;
import com.google.common.base.Function;
/**
*
* @author Adrian Cole
*/
public class ThrowResourceNotFoundOn404 implements Function<Exception, Object> {
public Object apply(Exception from) {
if (from instanceof HttpResponseException) {
HttpResponseException responseException = (HttpResponseException) from;
if (responseException.getResponse().getStatusCode() == 404) {
throw new ResourceNotFoundException(from);
}
}
return null;
}
}

View File

@ -92,7 +92,7 @@ public class AsyncRestClientProxy<T> implements InvocationHandler {
method = util.getDelegateOrNull(method);
logger.trace("Converting %s.%s", declaring.getSimpleName(), method.getName());
Function<Exception, ?> exceptionParser = util
.createExceptionParserOrNullIfNotFound(method);
.createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(method);
// in case there is an exception creating the request, we should at least pass in args
if (exceptionParser instanceof InvocationContext) {
((InvocationContext) exceptionParser).setContext(null);

View File

@ -94,6 +94,7 @@ import org.jclouds.rest.annotations.ResponseParser;
import org.jclouds.rest.annotations.SkipEncoding;
import org.jclouds.rest.annotations.VirtualHost;
import org.jclouds.rest.annotations.XMLResponseParser;
import org.jclouds.rest.functions.ThrowResourceNotFoundOn404;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Function;
@ -211,12 +212,12 @@ public class RestAnnotationProcessor<T> {
}
@VisibleForTesting
public Function<Exception, ?> createExceptionParserOrNullIfNotFound(Method method) {
public Function<Exception, ?> createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(Method method) {
ExceptionParser annotation = method.getAnnotation(ExceptionParser.class);
if (annotation != null) {
return injector.getInstance(annotation.value());
}
return null;
return injector.getInstance(ThrowResourceNotFoundOn404.class);
}
@SuppressWarnings("unchecked")

View File

@ -34,6 +34,7 @@ import org.jclouds.concurrent.config.ExecutorServiceModule;
import org.jclouds.http.HttpUtils;
import org.jclouds.http.config.JavaUrlHttpCommandExecutorServiceModule;
import org.jclouds.rest.config.RestModule;
import org.jclouds.rest.functions.ThrowResourceNotFoundOn404;
import org.jclouds.rest.internal.GeneratedHttpRequest;
import org.jclouds.rest.internal.RestAnnotationProcessor;
import org.jclouds.util.Utils;
@ -86,9 +87,9 @@ public abstract class RestClientTest<T> {
protected void assertExceptionParserClassEquals(Method method, @Nullable Class<?> parserClass) {
if (parserClass == null)
assertEquals(processor.createExceptionParserOrNullIfNotFound(method), null);
assertEquals(processor.createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(method).getClass(), ThrowResourceNotFoundOn404.class);
else
assertEquals(processor.createExceptionParserOrNullIfNotFound(method).getClass(),
assertEquals(processor.createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(method).getClass(),
parserClass);
}

View File

@ -58,6 +58,7 @@ import org.jclouds.mezeo.pcs2.options.PutBlockOptions;
import org.jclouds.mezeo.pcs2.xml.ContainerHandler;
import org.jclouds.mezeo.pcs2.xml.FileHandler;
import org.jclouds.rest.RestClientTest;
import org.jclouds.rest.functions.ThrowResourceNotFoundOn404;
import org.jclouds.rest.internal.GeneratedHttpRequest;
import org.jclouds.rest.internal.RestAnnotationProcessor;
import org.testng.annotations.BeforeClass;
@ -123,7 +124,7 @@ public class PCSClientTest extends RestClientTest<PCSAsyncClient> {
assertEquals(RestAnnotationProcessor.getSaxResponseParserClassOrNull(method), null);
assertEquals(httpMethod.getFilters().size(), 1);
assertEquals(httpMethod.getFilters().get(0).getClass(), BasicAuthentication.class);
assertEquals(processor.createExceptionParserOrNullIfNotFound(method).getClass(),
assertEquals(processor.createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(method).getClass(),
ReturnVoidOnNotFoundOr404.class);
}
@ -139,7 +140,7 @@ public class PCSClientTest extends RestClientTest<PCSAsyncClient> {
ContainerHandler.class);
assertEquals(httpMethod.getFilters().size(), 1);
assertEquals(httpMethod.getFilters().get(0).getClass(), BasicAuthentication.class);
assertEquals(processor.createExceptionParserOrNullIfNotFound(method), null);
assertEquals(processor.createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(method).getClass(), ThrowResourceNotFoundOn404.class);
}
public void testGetFileInfo() throws SecurityException, NoSuchMethodException {
@ -154,7 +155,7 @@ public class PCSClientTest extends RestClientTest<PCSAsyncClient> {
FileHandler.class);
assertEquals(httpMethod.getFilters().size(), 1);
assertEquals(httpMethod.getFilters().get(0).getClass(), BasicAuthentication.class);
assertEquals(processor.createExceptionParserOrNullIfNotFound(method).getClass(),
assertEquals(processor.createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(method).getClass(),
ThrowKeyNotFoundOn404.class);
}
@ -224,7 +225,7 @@ public class PCSClientTest extends RestClientTest<PCSAsyncClient> {
assertEquals(RestAnnotationProcessor.getSaxResponseParserClassOrNull(method), null);
assertEquals(httpMethod.getFilters().size(), 1);
assertEquals(httpMethod.getFilters().get(0).getClass(), BasicAuthentication.class);
assertEquals(processor.createExceptionParserOrNullIfNotFound(method).getClass(),
assertEquals(processor.createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(method).getClass(),
ReturnVoidOnNotFoundOr404.class);
}
@ -242,7 +243,7 @@ public class PCSClientTest extends RestClientTest<PCSAsyncClient> {
assertEquals(httpMethod.getHeaders().get(HttpHeaders.CONTENT_TYPE), Collections
.singletonList("application/unknown"));
assertEquals("bar", httpMethod.getPayload().getRawContent());
assertEquals(processor.createExceptionParserOrNullIfNotFound(method), null);
assertEquals(processor.createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(method).getClass(), ThrowResourceNotFoundOn404.class);
assertEquals(processor.createResponseParser(method, httpMethod).getClass(),
ReturnVoidIf2xx.class);
}
@ -257,7 +258,7 @@ public class PCSClientTest extends RestClientTest<PCSAsyncClient> {
assertEquals(httpMethod.getRequestLine(), "GET http://localhost/pow/metadata/newkey HTTP/1.1");
assertEquals(httpMethod.getHeaders().size(), 0);
assertEquals(processor.createExceptionParserOrNullIfNotFound(method), null);
assertEquals(processor.createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(method).getClass(), ThrowResourceNotFoundOn404.class);
assertEquals(processor.createResponseParser(method, httpMethod).getClass(),
AddMetadataItemIntoMap.class);
}

View File

@ -41,6 +41,7 @@ import org.jclouds.logging.Logger;
import org.jclouds.logging.Logger.LoggerFactory;
import org.jclouds.mezeo.pcs2.xml.CloudXlinkHandler;
import org.jclouds.rest.config.RestModule;
import org.jclouds.rest.functions.ThrowResourceNotFoundOn404;
import org.jclouds.rest.internal.GeneratedHttpRequest;
import org.jclouds.rest.internal.RestAnnotationProcessor;
import org.testng.annotations.BeforeClass;
@ -71,7 +72,7 @@ public class PCSCloudTest {
CloudXlinkHandler.class);
assertEquals(httpMethod.getFilters().size(), 1);
assertEquals(httpMethod.getFilters().get(0).getClass(), BasicAuthentication.class);
assertEquals(processor.createExceptionParserOrNullIfNotFound(method), null);
assertEquals(processor.createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(method).getClass(), ThrowResourceNotFoundOn404.class);
}
private RestAnnotationProcessor<PCSCloud> processor;

View File

@ -77,6 +77,7 @@ import org.jclouds.rackspace.cloudservers.options.CreateSharedIpGroupOptions;
import org.jclouds.rackspace.cloudservers.options.ListOptions;
import org.jclouds.rackspace.cloudservers.options.RebuildServerOptions;
import org.jclouds.rest.config.RestModule;
import org.jclouds.rest.functions.ThrowResourceNotFoundOn404;
import org.jclouds.rest.internal.GeneratedHttpRequest;
import org.jclouds.rest.internal.RestAnnotationProcessor;
import org.testng.annotations.BeforeClass;
@ -182,7 +183,7 @@ public class CloudServersClientTest {
.singletonList(MediaType.APPLICATION_JSON));
assertEquals(processor.createResponseParser(method, httpMethod).getClass(),
ParseServerFromJsonResponse.class);
assertEquals(processor.createExceptionParserOrNullIfNotFound(method), null);
assertEquals(processor.createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(method).getClass(), ThrowResourceNotFoundOn404.class);
assertNotNull(processor.getMapPayloadBinderOrNull(method, new Object[] { "", 1, 2,
new CreateServerOptions[] { CreateServerOptions.Builder.withSharedIpGroup(1) } }));
}
@ -196,7 +197,7 @@ public class CloudServersClientTest {
assertEquals(httpMethod.getEndpoint().getPath(), "/images/2");
assertEquals(httpMethod.getMethod(), HttpMethod.DELETE);
assertEquals(httpMethod.getHeaders().size(), 0);
assertEquals(processor.createExceptionParserOrNullIfNotFound(method).getClass(),
assertEquals(processor.createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(method).getClass(),
ReturnFalseOn404.class);
assertEquals(processor.createResponseParser(method, httpMethod).getClass(),
ReturnTrueIf2xx.class);
@ -215,7 +216,7 @@ public class CloudServersClientTest {
assertEquals(httpMethod.getHeaders().size(), 0);
assertEquals(processor.createResponseParser(method, httpMethod).getClass(),
ParseServerListFromJsonResponse.class);
assertEquals(processor.createExceptionParserOrNullIfNotFound(method), null);
assertEquals(processor.createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(method).getClass(), ThrowResourceNotFoundOn404.class);
}
Date now = new Date();
@ -234,7 +235,7 @@ public class CloudServersClientTest {
assertEquals(httpMethod.getHeaders().size(), 0);
assertEquals(processor.createResponseParser(method, httpMethod).getClass(),
ParseServerListFromJsonResponse.class);
assertEquals(processor.createExceptionParserOrNullIfNotFound(method), null);
assertEquals(processor.createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(method).getClass(), ThrowResourceNotFoundOn404.class);
}
public void testListServersDetail() throws SecurityException, NoSuchMethodException {
@ -250,7 +251,7 @@ public class CloudServersClientTest {
assertEquals(httpMethod.getHeaders().size(), 0);
assertEquals(processor.createResponseParser(method, httpMethod).getClass(),
ParseServerListFromJsonResponse.class);
assertEquals(processor.createExceptionParserOrNullIfNotFound(method), null);
assertEquals(processor.createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(method).getClass(), ThrowResourceNotFoundOn404.class);
}
public void testGetServer() throws SecurityException, NoSuchMethodException {
@ -265,7 +266,7 @@ public class CloudServersClientTest {
assertEquals(httpMethod.getHeaders().size(), 0);
assertEquals(processor.createResponseParser(method, httpMethod).getClass(),
ParseServerFromJsonResponse.class);
assertEquals(processor.createExceptionParserOrNullIfNotFound(method).getClass(),
assertEquals(processor.createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(method).getClass(),
ReturnServerNotFoundOn404.class);
}
@ -282,7 +283,7 @@ public class CloudServersClientTest {
assertEquals(httpMethod.getHeaders().size(), 0);
assertEquals(processor.createResponseParser(method, httpMethod).getClass(),
ParseFlavorListFromJsonResponse.class);
assertEquals(processor.createExceptionParserOrNullIfNotFound(method), null);
assertEquals(processor.createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(method).getClass(), ThrowResourceNotFoundOn404.class);
}
public void testListFlavorsOptions() throws SecurityException, NoSuchMethodException {
@ -299,7 +300,7 @@ public class CloudServersClientTest {
assertEquals(httpMethod.getHeaders().size(), 0);
assertEquals(processor.createResponseParser(method, httpMethod).getClass(),
ParseFlavorListFromJsonResponse.class);
assertEquals(processor.createExceptionParserOrNullIfNotFound(method), null);
assertEquals(processor.createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(method).getClass(), ThrowResourceNotFoundOn404.class);
}
public void testListFlavorsDetail() throws SecurityException, NoSuchMethodException {
@ -315,7 +316,7 @@ public class CloudServersClientTest {
assertEquals(httpMethod.getHeaders().size(), 0);
assertEquals(processor.createResponseParser(method, httpMethod).getClass(),
ParseFlavorListFromJsonResponse.class);
assertEquals(processor.createExceptionParserOrNullIfNotFound(method), null);
assertEquals(processor.createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(method).getClass(), ThrowResourceNotFoundOn404.class);
}
public void testListFlavorsDetailOptions() throws SecurityException, NoSuchMethodException {
@ -332,7 +333,7 @@ public class CloudServersClientTest {
assertEquals(httpMethod.getHeaders().size(), 0);
assertEquals(processor.createResponseParser(method, httpMethod).getClass(),
ParseFlavorListFromJsonResponse.class);
assertEquals(processor.createExceptionParserOrNullIfNotFound(method), null);
assertEquals(processor.createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(method).getClass(), ThrowResourceNotFoundOn404.class);
}
public void testGetFlavor() throws SecurityException, NoSuchMethodException {
@ -347,7 +348,7 @@ public class CloudServersClientTest {
assertEquals(httpMethod.getHeaders().size(), 0);
assertEquals(processor.createResponseParser(method, httpMethod).getClass(),
ParseFlavorFromJsonResponse.class);
assertEquals(processor.createExceptionParserOrNullIfNotFound(method).getClass(),
assertEquals(processor.createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(method).getClass(),
ReturnFlavorNotFoundOn404.class);
}
@ -364,7 +365,7 @@ public class CloudServersClientTest {
assertEquals(httpMethod.getHeaders().size(), 0);
assertEquals(processor.createResponseParser(method, httpMethod).getClass(),
ParseImageListFromJsonResponse.class);
assertEquals(processor.createExceptionParserOrNullIfNotFound(method), null);
assertEquals(processor.createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(method).getClass(), ThrowResourceNotFoundOn404.class);
}
public void testListImagesDetail() throws SecurityException, NoSuchMethodException {
@ -380,7 +381,7 @@ public class CloudServersClientTest {
assertEquals(httpMethod.getHeaders().size(), 0);
assertEquals(processor.createResponseParser(method, httpMethod).getClass(),
ParseImageListFromJsonResponse.class);
assertEquals(processor.createExceptionParserOrNullIfNotFound(method), null);
assertEquals(processor.createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(method).getClass(), ThrowResourceNotFoundOn404.class);
}
public void testListImagesOptions() throws SecurityException, NoSuchMethodException {
@ -397,7 +398,7 @@ public class CloudServersClientTest {
assertEquals(httpMethod.getHeaders().size(), 0);
assertEquals(processor.createResponseParser(method, httpMethod).getClass(),
ParseImageListFromJsonResponse.class);
assertEquals(processor.createExceptionParserOrNullIfNotFound(method), null);
assertEquals(processor.createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(method).getClass(), ThrowResourceNotFoundOn404.class);
}
public void testListImagesDetailOptions() throws SecurityException, NoSuchMethodException {
@ -414,7 +415,7 @@ public class CloudServersClientTest {
assertEquals(httpMethod.getHeaders().size(), 0);
assertEquals(processor.createResponseParser(method, httpMethod).getClass(),
ParseImageListFromJsonResponse.class);
assertEquals(processor.createExceptionParserOrNullIfNotFound(method), null);
assertEquals(processor.createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(method).getClass(), ThrowResourceNotFoundOn404.class);
}
public void testGetImage() throws SecurityException, NoSuchMethodException {
@ -429,7 +430,7 @@ public class CloudServersClientTest {
assertEquals(httpMethod.getHeaders().size(), 0);
assertEquals(processor.createResponseParser(method, httpMethod).getClass(),
ParseImageFromJsonResponse.class);
assertEquals(processor.createExceptionParserOrNullIfNotFound(method).getClass(),
assertEquals(processor.createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(method).getClass(),
ReturnImageNotFoundOn404.class);
}
@ -442,7 +443,7 @@ public class CloudServersClientTest {
assertEquals(httpMethod.getEndpoint().getPath(), "/servers/2");
assertEquals(httpMethod.getMethod(), HttpMethod.DELETE);
assertEquals(httpMethod.getHeaders().size(), 0);
assertEquals(processor.createExceptionParserOrNullIfNotFound(method).getClass(),
assertEquals(processor.createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(method).getClass(),
ReturnFalseOn404.class);
assertEquals(processor.createResponseParser(method, httpMethod).getClass(),
ReturnTrueIf2xx.class);
@ -465,7 +466,7 @@ public class CloudServersClientTest {
assertEquals(httpMethod.getHeaders().get(HttpHeaders.CONTENT_TYPE), Collections
.singletonList(MediaType.APPLICATION_JSON));
assertEquals("{\"shareIp\":{\"sharedIpGroupId\":3}}", httpMethod.getPayload().getRawContent());
assertEquals(processor.createExceptionParserOrNullIfNotFound(method).getClass(),
assertEquals(processor.createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(method).getClass(),
ReturnFalseOn404.class);
assertEquals(processor.createResponseParser(method, httpMethod).getClass(),
ReturnTrueIf2xx.class);
@ -489,7 +490,7 @@ public class CloudServersClientTest {
.singletonList(MediaType.APPLICATION_JSON));
assertEquals("{\"shareIp\":{\"sharedIpGroupId\":3,\"configureServer\":true}}", httpMethod
.getPayload().getRawContent());
assertEquals(processor.createExceptionParserOrNullIfNotFound(method).getClass(),
assertEquals(processor.createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(method).getClass(),
ReturnFalseOn404.class);
assertEquals(processor.createResponseParser(method, httpMethod).getClass(),
ReturnTrueIf2xx.class);
@ -506,7 +507,7 @@ public class CloudServersClientTest {
assertEquals(httpMethod.getEndpoint().getPath(), "/servers/2/ips/public/127.0.0.1");
assertEquals(httpMethod.getMethod(), HttpMethod.DELETE);
assertEquals(httpMethod.getHeaders().size(), 0);
assertEquals(processor.createExceptionParserOrNullIfNotFound(method).getClass(),
assertEquals(processor.createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(method).getClass(),
ReturnFalseOn404.class);
assertEquals(processor.createResponseParser(method, httpMethod).getClass(),
ReturnTrueIf2xx.class);
@ -530,7 +531,7 @@ public class CloudServersClientTest {
.singletonList(MediaType.APPLICATION_JSON));
assertEquals(httpMethod.getPayload().getRawContent(),
"{\"backupSchedule\":{\"daily\":\"H_0800_1000\",\"enabled\":true,\"weekly\":\"MONDAY\"}}");
assertEquals(processor.createExceptionParserOrNullIfNotFound(method).getClass(),
assertEquals(processor.createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(method).getClass(),
ReturnFalseOn404.class);
assertEquals(processor.createResponseParser(method, httpMethod).getClass(),
ReturnTrueIf2xx.class);
@ -545,7 +546,7 @@ public class CloudServersClientTest {
assertEquals(httpMethod.getEndpoint().getPath(), "/servers/2/backup_schedule");
assertEquals(httpMethod.getMethod(), HttpMethod.DELETE);
assertEquals(httpMethod.getHeaders().size(), 0);
assertEquals(processor.createExceptionParserOrNullIfNotFound(method).getClass(),
assertEquals(processor.createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(method).getClass(),
ReturnFalseOn404.class);
assertEquals(processor.createResponseParser(method, httpMethod).getClass(),
ReturnTrueIf2xx.class);
@ -567,7 +568,7 @@ public class CloudServersClientTest {
assertEquals(httpMethod.getHeaders().get(HttpHeaders.CONTENT_TYPE), Collections
.singletonList(MediaType.APPLICATION_JSON));
assertEquals("{\"server\":{\"adminPass\":\"foo\"}}", httpMethod.getPayload().getRawContent());
assertEquals(processor.createExceptionParserOrNullIfNotFound(method).getClass(),
assertEquals(processor.createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(method).getClass(),
ReturnFalseOn404.class);
assertEquals(processor.createResponseParser(method, httpMethod).getClass(),
ReturnTrueIf2xx.class);
@ -589,7 +590,7 @@ public class CloudServersClientTest {
assertEquals(httpMethod.getHeaders().get(HttpHeaders.CONTENT_TYPE), Collections
.singletonList(MediaType.APPLICATION_JSON));
assertEquals("{\"server\":{\"name\":\"foo\"}}", httpMethod.getPayload().getRawContent());
assertEquals(processor.createExceptionParserOrNullIfNotFound(method).getClass(),
assertEquals(processor.createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(method).getClass(),
ReturnFalseOn404.class);
assertEquals(processor.createResponseParser(method, httpMethod).getClass(),
ReturnTrueIf2xx.class);
@ -608,7 +609,7 @@ public class CloudServersClientTest {
assertEquals(httpMethod.getHeaders().size(), 0);
assertEquals(processor.createResponseParser(method, httpMethod).getClass(),
ParseSharedIpGroupListFromJsonResponse.class);
assertEquals(processor.createExceptionParserOrNullIfNotFound(method), null);
assertEquals(processor.createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(method).getClass(), ThrowResourceNotFoundOn404.class);
}
public void testListSharedIpGroupsOptions() throws SecurityException, NoSuchMethodException {
@ -625,7 +626,7 @@ public class CloudServersClientTest {
assertEquals(httpMethod.getHeaders().size(), 0);
assertEquals(processor.createResponseParser(method, httpMethod).getClass(),
ParseSharedIpGroupListFromJsonResponse.class);
assertEquals(processor.createExceptionParserOrNullIfNotFound(method), null);
assertEquals(processor.createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(method).getClass(), ThrowResourceNotFoundOn404.class);
}
public void testListSharedIpGroupsDetail() throws SecurityException, NoSuchMethodException {
@ -641,7 +642,7 @@ public class CloudServersClientTest {
assertEquals(httpMethod.getHeaders().size(), 0);
assertEquals(processor.createResponseParser(method, httpMethod).getClass(),
ParseSharedIpGroupListFromJsonResponse.class);
assertEquals(processor.createExceptionParserOrNullIfNotFound(method), null);
assertEquals(processor.createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(method).getClass(), ThrowResourceNotFoundOn404.class);
}
public void testListSharedIpGroupsDetailOptions() throws SecurityException,
@ -659,7 +660,7 @@ public class CloudServersClientTest {
assertEquals(httpMethod.getHeaders().size(), 0);
assertEquals(processor.createResponseParser(method, httpMethod).getClass(),
ParseSharedIpGroupListFromJsonResponse.class);
assertEquals(processor.createExceptionParserOrNullIfNotFound(method), null);
assertEquals(processor.createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(method).getClass(), ThrowResourceNotFoundOn404.class);
}
public void testGetSharedIpGroup() throws SecurityException, NoSuchMethodException {
@ -674,7 +675,7 @@ public class CloudServersClientTest {
assertEquals(httpMethod.getHeaders().size(), 0);
assertEquals(processor.createResponseParser(method, httpMethod).getClass(),
ParseSharedIpGroupFromJsonResponse.class);
assertEquals(processor.createExceptionParserOrNullIfNotFound(method).getClass(),
assertEquals(processor.createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(method).getClass(),
ReturnSharedIpGroupNotFoundOn404.class);
}
@ -717,7 +718,7 @@ public class CloudServersClientTest {
.singletonList(MediaType.APPLICATION_JSON));
assertEquals(processor.createResponseParser(method, httpMethod).getClass(),
ParseSharedIpGroupFromJsonResponse.class);
assertEquals(processor.createExceptionParserOrNullIfNotFound(method), null);
assertEquals(processor.createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(method).getClass(), ThrowResourceNotFoundOn404.class);
assertNotNull(processor.getMapPayloadBinderOrNull(method, new Object[] { "",
new CreateSharedIpGroupOptions[] { withServer(2) } }));
}
@ -731,7 +732,7 @@ public class CloudServersClientTest {
assertEquals(httpMethod.getEndpoint().getPath(), "/shared_ip_groups/2");
assertEquals(httpMethod.getMethod(), HttpMethod.DELETE);
assertEquals(httpMethod.getHeaders().size(), 0);
assertEquals(processor.createExceptionParserOrNullIfNotFound(method).getClass(),
assertEquals(processor.createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(method).getClass(),
ReturnFalseOn404.class);
assertEquals(processor.createResponseParser(method, httpMethod).getClass(),
ReturnTrueIf2xx.class);
@ -813,7 +814,7 @@ public class CloudServersClientTest {
.singletonList(MediaType.APPLICATION_JSON));
assertEquals(processor.createResponseParser(method, httpMethod).getClass(),
ParseImageFromJsonResponse.class);
assertNotNull(processor.createExceptionParserOrNullIfNotFound(method));
assertNotNull(processor.createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(method));
assertNotNull(processor.getMapPayloadBinderOrNull(method, new Object[] { "", 2 }));
}
@ -852,7 +853,7 @@ public class CloudServersClientTest {
+ ""));
assertEquals(httpMethod.getHeaders().get(HttpHeaders.CONTENT_TYPE), Collections
.singletonList(MediaType.APPLICATION_JSON));
assertEquals(processor.createExceptionParserOrNullIfNotFound(method).getClass(),
assertEquals(processor.createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(method).getClass(),
ReturnFalseOn404.class);
assertEquals(processor.createResponseParser(method, httpMethod).getClass(),
ReturnTrueIf2xx.class);
@ -876,7 +877,7 @@ public class CloudServersClientTest {
assertEquals(httpMethod.getHeaders().get(HttpHeaders.CONTENT_TYPE), Collections
.singletonList(MediaType.APPLICATION_JSON));
assertEquals("{\"reboot\":{\"type\":\"HARD\"}}", httpMethod.getPayload().getRawContent());
assertEquals(processor.createExceptionParserOrNullIfNotFound(method).getClass(),
assertEquals(processor.createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(method).getClass(),
ReturnFalseOn404.class);
assertEquals(processor.createResponseParser(method, httpMethod).getClass(),
ReturnTrueIf2xx.class);
@ -897,7 +898,7 @@ public class CloudServersClientTest {
assertEquals(httpMethod.getHeaders().get(HttpHeaders.CONTENT_TYPE), Collections
.singletonList(MediaType.APPLICATION_JSON));
assertEquals("{\"resize\":{\"flavorId\":3}}", httpMethod.getPayload().getRawContent());
assertEquals(processor.createExceptionParserOrNullIfNotFound(method).getClass(),
assertEquals(processor.createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(method).getClass(),
ReturnFalseOn404.class);
assertEquals(processor.createResponseParser(method, httpMethod).getClass(),
ReturnTrueIf2xx.class);
@ -918,7 +919,7 @@ public class CloudServersClientTest {
assertEquals(httpMethod.getHeaders().get(HttpHeaders.CONTENT_TYPE), Collections
.singletonList(MediaType.APPLICATION_JSON));
assertEquals("{\"confirmResize\":null}", httpMethod.getPayload().getRawContent());
assertEquals(processor.createExceptionParserOrNullIfNotFound(method).getClass(),
assertEquals(processor.createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(method).getClass(),
ReturnFalseOn404.class);
assertEquals(processor.createResponseParser(method, httpMethod).getClass(),
ReturnTrueIf2xx.class);
@ -938,7 +939,7 @@ public class CloudServersClientTest {
assertEquals(httpMethod.getHeaders().get(HttpHeaders.CONTENT_TYPE), Collections
.singletonList(MediaType.APPLICATION_JSON));
assertEquals("{\"revertResize\":null}", httpMethod.getPayload().getRawContent());
assertEquals(processor.createExceptionParserOrNullIfNotFound(method).getClass(),
assertEquals(processor.createExceptionParserOrThrowResourceNotFoundOn404IfNoAnnotation(method).getClass(),
ReturnFalseOn404.class);
assertEquals(processor.createResponseParser(method, httpMethod).getClass(),
ReturnTrueIf2xx.class);

View File

@ -31,6 +31,8 @@ import static org.jclouds.vcloud.reference.VCloudConstants.PROPERTY_VCLOUD_USER;
import static org.jclouds.vcloud.reference.VCloudConstants.PROPERTY_VCLOUD_VERSION;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.URI;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
@ -44,6 +46,9 @@ import org.jclouds.concurrent.internal.SyncProxy;
import org.jclouds.encryption.EncryptionService;
import org.jclouds.http.RequiresHttp;
import org.jclouds.http.filters.BasicAuthentication;
import org.jclouds.predicates.AddressReachable;
import org.jclouds.predicates.RetryablePredicate;
import org.jclouds.predicates.SocketOpen;
import org.jclouds.rest.ConfiguresRestClient;
import org.jclouds.rest.RestClientFactory;
import org.jclouds.util.Utils;
@ -65,7 +70,9 @@ import org.jclouds.vcloud.endpoints.internal.VAppTemplateRoot;
import org.jclouds.vcloud.internal.VCloudLoginAsyncClient;
import org.jclouds.vcloud.internal.VCloudVersionsAsyncClient;
import org.jclouds.vcloud.internal.VCloudLoginAsyncClient.VCloudSession;
import org.jclouds.vcloud.predicates.TaskSuccess;
import com.google.common.base.Predicate;
import com.google.common.base.Supplier;
import com.google.common.collect.Iterables;
import com.google.inject.AbstractModule;
@ -80,6 +87,24 @@ import com.google.inject.Provides;
@ConfiguresRestClient
public class VCloudRestClientModule extends AbstractModule {
@Provides
@Singleton
protected Predicate<InetSocketAddress> socketTester(SocketOpen open) {
return new RetryablePredicate<InetSocketAddress>(open, 130, 10, TimeUnit.SECONDS);
}
@Provides
@Singleton
protected Predicate<InetAddress> addressTester(AddressReachable reachable) {
return new RetryablePredicate<InetAddress>(reachable, 60, 5, TimeUnit.SECONDS);
}
@Provides
@Singleton
protected Predicate<String> successTester(TaskSuccess success) {
return new RetryablePredicate<String>(success, 600, 10, TimeUnit.SECONDS);
}
@Override
protected void configure() {
}

View File

@ -29,7 +29,11 @@ import static com.google.common.base.Preconditions.checkNotNull;
* @author Adrian Cole
*/
public enum TaskStatus {
SUCCESS, FAILED, RUNNING, QUEUED, ERROR, CANCELLED;
SUCCESS, FAILED, RUNNING, QUEUED, ERROR, CANCELLED,
/**
* invalid status, temporarily in.
*/
COMPLETED;
public String value() {
return name().toLowerCase();
}

View File

@ -26,17 +26,13 @@ package org.jclouds.vcloud;
import static com.google.common.base.Preconditions.checkArgument;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.URI;
import java.util.Map;
import javax.annotation.Resource;
import javax.inject.Inject;
import org.jclouds.compute.domain.Image;
import org.jclouds.logging.Logger;
import org.jclouds.ssh.ExecResponse;
import org.jclouds.ssh.SshClient;
import org.jclouds.ssh.SshClient.Factory;
import org.jclouds.vcloud.domain.Task;
import org.jclouds.vcloud.domain.VApp;
import org.jclouds.vcloud.domain.VAppStatus;
@ -54,56 +50,47 @@ public class VCloudComputeClient {
@Resource
protected Logger logger = Logger.NULL;
private final Predicate<InetSocketAddress> socketTester;
private final Predicate<URI> taskTester;
private final Predicate<String> taskTester;
private final VCloudClient tmClient;
@Inject
public VCloudComputeClient(VCloudClient tmClient, Factory sshFactory,
Predicate<InetSocketAddress> socketTester, Predicate<URI> successTester) {
public VCloudComputeClient(VCloudClient tmClient,
Predicate<String> successTester) {
this.tmClient = tmClient;
this.sshFactory = sshFactory;
this.socketTester = socketTester;
this.taskTester = successTester;
}
private final Factory sshFactory;
public enum Image {
CENTOS_53, RHEL_53, UMBUNTU_90, UMBUNTU_JEOS
}
private Map<Image, String> imageCatalogIdMap = ImmutableMap.<Image, String> builder().put(
Image.CENTOS_53, "3").put(Image.RHEL_53, "8").put(Image.UMBUNTU_90, "10").put(
Image.UMBUNTU_JEOS, "11").build();
private String username;
private String password;
public String start(String name, int minCores, int minMegs, Image image) {
public Map<String, String> start(String name, Image image, int minCores, int minMegs,
long diskSize, Map<String, String> properties) {
checkArgument(imageCatalogIdMap.containsKey(image), "image not configured: " + image);
String templateId = imageCatalogIdMap.get(image);
String vDCId = tmClient.getDefaultVDC().getId();
logger.debug(">> instantiating vApp name(%s) minCores(%d) minMegs(%d) template(%s) vDC(%s)",
name, minCores, minMegs, templateId, vDCId);
VApp vAppResponse = tmClient.instantiateVAppTemplateInVDC(name, templateId, vDCId,
InstantiateVAppTemplateOptions.Builder.processorCount(minCores).memory(minMegs));
tmClient.getVApp(vAppResponse.getId());
logger
.debug(
">> instantiating vApp vDC(%s) name(%s) template(%s) minCores(%d) minMegs(%d) diskSize(%d) properties(%s) ",
vDCId, name, templateId, minCores, minMegs, diskSize, properties);
VApp vAppResponse = tmClient.instantiateVAppTemplateInVDC(vDCId, name,
templateId, InstantiateVAppTemplateOptions.Builder.processorCount(minCores)
.memory(minMegs).disk(diskSize).productProperties(properties));
logger.debug("<< instantiated VApp(%s)", vAppResponse.getId());
logger.debug(">> deploying vApp(%s)", vAppResponse.getId());
VApp vApp = blockUntilVAppStatusOrThrowException(vAppResponse, tmClient
.deployVApp(vAppResponse.getId()), "deploy", VAppStatus.OFF);
logger.debug("<< deployed vApp(%s)", vApp.getId());
logger.debug(">> powering vApp(%s)", vApp.getId());
vApp = blockUntilVAppStatusOrThrowException(vApp, tmClient.powerOnVApp(vApp.getId()),
"powerOn", VAppStatus.ON);
.deployVApp(vAppResponse.getId()), "deploy", VAppStatus.ON);// TODO, I'm not sure
// this should be on
// already
// logger.debug("<< deployed vApp(%s)", vApp.getId());
//
// logger.debug(">> powering vApp(%s)", vApp.getId());
// vApp = blockUntilVAppStatusOrThrowException(vApp, tmClient.powerOnVApp(vApp.getId()),
// "powerOn", VAppStatus.ON);
logger.debug("<< on vApp(%s)", vApp.getId());
return vApp.getId();
return ImmutableMap.<String, String> of("id", vApp.getId(), "username", null, "password", null);
}
/**
@ -116,14 +103,6 @@ public class VCloudComputeClient {
return Iterables.getLast(vApp.getNetworkToAddresses().values());
}
public ExecResponse exec(InetAddress address, String command) {
InetSocketAddress sshSocket = new InetSocketAddress(address, 22);
logger.debug(">> exec ssh://%s@%s/%s", username, sshSocket, command);
ExecResponse exec = exec(sshSocket, username, password, command);
logger.debug("<< output(%s) error(%s)", exec.getOutput(), exec.getError());
return exec;
}
public void reboot(String id) {
VApp vApp = tmClient.getVApp(id);
logger.debug(">> rebooting vApp(%s)", vApp.getId());
@ -135,7 +114,8 @@ public class VCloudComputeClient {
public void stop(String id) {
VApp vApp = tmClient.getVApp(id);
if (vApp.getStatus() != VAppStatus.OFF) {
logger.debug(">> powering off vApp(%s)", vApp.getId());
logger.debug(">> powering off vApp(%s), current status: %s", vApp.getId(), vApp
.getStatus());
blockUntilVAppStatusOrThrowException(vApp, tmClient.powerOffVApp(vApp.getId()),
"powerOff", VAppStatus.OFF);
logger.debug("<< off vApp(%s)", vApp.getId());
@ -145,24 +125,9 @@ public class VCloudComputeClient {
logger.debug("<< deleted vApp(%s)", vApp.getId());
}
private ExecResponse exec(InetSocketAddress socket, String username, String password,
String command) {
if (!socketTester.apply(socket)) {
throw new SocketNotOpenException(socket);
}
SshClient connection = sshFactory.create(socket, username, password);
try {
connection.connect();
return connection.exec(command);
} finally {
if (connection != null)
connection.disconnect();
}
}
private VApp blockUntilVAppStatusOrThrowException(VApp vApp, Task deployTask, String taskType,
VAppStatus expectedStatus) {
if (!taskTester.apply(deployTask.getLocation())) {
if (!taskTester.apply(deployTask.getId())) {
throw new TaskException(taskType, vApp, deployTask);
}
@ -192,23 +157,6 @@ public class VCloudComputeClient {
}
public static class SocketNotOpenException extends RuntimeException {
private final InetSocketAddress socket;
/** The serialVersionUID */
private static final long serialVersionUID = 251801929573211256L;
public SocketNotOpenException(InetSocketAddress socket) {
super("socket not open: " + socket);
this.socket = socket;
}
public InetSocketAddress getSocket() {
return socket;
}
}
public static class VAppException extends RuntimeException {
private final VApp vApp;

View File

@ -28,41 +28,34 @@ import static org.testng.Assert.assertEquals;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.URI;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.jclouds.compute.domain.Image;
import org.jclouds.logging.log4j.config.Log4JLoggingModule;
import org.jclouds.predicates.AddressReachable;
import org.jclouds.predicates.RetryablePredicate;
import org.jclouds.predicates.SocketOpen;
import org.jclouds.ssh.jsch.config.JschSshClientModule;
import org.jclouds.vcloud.VCloudComputeClient.Image;
import org.jclouds.vcloud.domain.ResourceType;
import org.jclouds.vcloud.domain.VApp;
import org.jclouds.vcloud.domain.VAppStatus;
import org.jclouds.vcloud.predicates.TaskSuccess;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.Test;
import com.google.common.base.CaseFormat;
import com.google.common.base.Predicate;
import com.google.inject.AbstractModule;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.Provides;
import com.google.inject.TypeLiteral;
import com.google.inject.internal.ImmutableMap;
/**
* Tests behavior of {@code VCloudClient}
* Tests behavior of {@code HostingDotComVCloudClient}
*
* @author Adrian Cole
*/
@Test(groups = "live", sequential = true, testName = "vcloud.VCloudClientLiveTest")
@Test(groups = "live", sequential = true, testName = "vcloud.HostingDotComVCloudClientLiveTest")
public class VCloudComputeClientLiveTest {
VCloudComputeClient client;
VCloudClient tmClient;
@ -84,9 +77,9 @@ public class VCloudComputeClientLiveTest {
private Map<Image, Expectation> expectationMap = ImmutableMap.<Image, Expectation> builder()
.put(Image.CENTOS_53,
new Expectation(4194304 / 4 * 10, "Red Hat Enterprise Linux 5 (64-bit)")).put(
new Expectation(4194304 / 2 * 10, "Red Hat Enterprise Linux 5 (64-bit)")).put(
Image.RHEL_53,
new Expectation(4194304 / 4 * 10, "Red Hat Enterprise Linux 5 (64-bit)")).put(
new Expectation(4194304 / 2 * 10, "Red Hat Enterprise Linux 5 (64-bit)")).put(
Image.UMBUNTU_90, new Expectation(4194304, "Ubuntu Linux (64-bit)")).put(
Image.UMBUNTU_JEOS, new Expectation(4194304, "Ubuntu Linux (32-bit)")).build();
@ -100,8 +93,10 @@ public class VCloudComputeClientLiveTest {
String serverName = getCompatibleServerName(toTest);
int processorCount = 1;
int memory = 512;
long disk = 10 * 1025 * 1024;
Map<String, String> properties = ImmutableMap.of("foo", "bar");
id = client.start(serverName, processorCount, memory, toTest);
id = client.start(serverName, toTest, processorCount, memory, disk, properties).get("id");
Expectation expectation = expectationMap.get(toTest);
VApp vApp = tmClient.getVApp(id);
@ -111,7 +106,8 @@ public class VCloudComputeClientLiveTest {
}
private String getCompatibleServerName(Image toTest) {
String serverName = toTest.toString().toLowerCase().replaceAll("_", "-").substring(0,
String serverName = CaseFormat.UPPER_UNDERSCORE
.to(CaseFormat.LOWER_HYPHEN, toTest.toString()).substring(0,
toTest.toString().length() <= 15 ? toTest.toString().length() : 14);
return serverName;
}
@ -122,16 +118,10 @@ public class VCloudComputeClientLiveTest {
assert !addressTester.apply(privateAddress);
}
@Test(dependsOnMethods = "testGetAnyPrivateAddress")
public void testSshLoadBalanceIp() {
// assert addressTester.apply(publicIp);
client.exec(privateAddress, "uname -a");
}
private void verifyConfigurationOfVApp(VApp vApp, String serverName, String expectedOs,
int processorCount, int memory, long hardDisk) {
assertEquals(vApp.getName(), serverName);
assertEquals(vApp.getOperatingSystemDescription(), expectedOs);
// assertEquals(vApp.getName(), serverName);
// assertEquals(vApp.getOperatingSystemDescription(), expectedOs);
assertEquals(vApp.getResourceAllocationByType().get(ResourceType.PROCESSOR)
.getVirtualQuantity(), processorCount);
assertEquals(vApp.getResourceAllocationByType().get(ResourceType.SCSI_CONTROLLER)
@ -151,39 +141,12 @@ public class VCloudComputeClientLiveTest {
@BeforeGroups(groups = { "live" })
public void setupClient() {
String endpoint = checkNotNull(System.getProperty("jclouds.test.endpoint"),
"jclouds.test.endpoint");
String endpoint = checkNotNull(System.getProperty("jclouds.test.endpoint"), "jclouds.test.endpoint");
String account = checkNotNull(System.getProperty("jclouds.test.user"), "jclouds.test.user");
String key = checkNotNull(System.getProperty("jclouds.test.key"), "jclouds.test.key");
Injector injector = new VCloudContextBuilder(new VCloudPropertiesBuilder(
URI.create(endpoint), account, key).relaxSSLHostname().build()).withModules(
new Log4JLoggingModule(), new JschSshClientModule(), new AbstractModule() {
@Override
protected void configure() {
}
@SuppressWarnings("unused")
@Provides
private Predicate<InetSocketAddress> socketTester(SocketOpen open) {
return new RetryablePredicate<InetSocketAddress>(open, 130, 10,
TimeUnit.SECONDS);// make it longer then
// default internet
}
@SuppressWarnings("unused")
@Provides
private Predicate<InetAddress> addressTester(AddressReachable reachable) {
return new RetryablePredicate<InetAddress>(reachable, 60, 5, TimeUnit.SECONDS);
}
@SuppressWarnings("unused")
@Provides
private Predicate<String> successTester(TaskSuccess success) {
return new RetryablePredicate<String>(success, 300, 10, TimeUnit.SECONDS);
}
}).buildInjector();
Injector injector = new VCloudContextBuilder(
new VCloudPropertiesBuilder(URI.create(endpoint), account, key).relaxSSLHostname().build())
.withModules(new Log4JLoggingModule(), new JschSshClientModule()).buildInjector();
client = injector.getInstance(VCloudComputeClient.class);
tmClient = injector.getInstance(VCloudClient.class);
addressTester = injector.getInstance(Key.get(new TypeLiteral<Predicate<InetAddress>>() {

View File

@ -0,0 +1,144 @@
<?xml version="1.0" encoding="UTF-8"?>
<VApp href="http://10.150.4.49/api/v0.8/vApp/5" type="application/vnd.vmware.vcloud.vApp+xml"
name="SQL" status="4"
xmlns="http://www.vmware.com/vcloud/v0.8" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Link rel="up"
href="http://10.150.4.49/api/v0.8/vdc/3"
type="application/vnd.vmware.vcloud.vdc+xml"/>
<OperatingSystemSection ovf:id="36" xmlns="http://schemas.dmtf.org/ovf/envelope/1" xmlns:ovf="http://schemas.dmtf.org/ovf/envelope/1">
<Info>The kind of installed guest operating system</Info>
<Description>Other Linux (32-bit)</Description>
</OperatingSystemSection>
<VirtualHardwareSection xmlns="http://schemas.dmtf.org/ovf/envelope/1"
xmlns:vssd="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_VirtualSystemSettingData"
xmlns:rasd="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData">
<Info>Virtual hardware</Info>
<Link rel="edit" href="http://10.150.4.49/api/v0.8/vApp/5" type="application/vnd.vmware.ovf.virtualHardwareSection+xml"/>
<System>
<rasd:AutomaticRecoveryAction xsi:nil="true" />
<rasd:AutomaticShutdownAction xsi:nil="true" />
<rasd:AutomaticStartupAction xsi:nil="true" />
<rasd:AutomaticStartupActionDelay xsi:nil="true" />
<rasd:AutomaticStartupActionSequenceNumber xsi:nil="true" />
<rasd:Caption xsi:nil="true" />
<rasd:ConfigurationDataRoot xsi:nil="true" />
<rasd:ConfigurationFile xsi:nil="true" />
<rasd:ConfigurationID xsi:nil="true" />
<rasd:CreationTime xsi:nil="true" />
<rasd:Description xsi:nil="true" />
<rasd:ElementName>Virtual Hardware Family</rasd:ElementName>
<rasd:InstanceID>0</rasd:InstanceID>
<rasd:LogDataRoot xsi:nil="true" />
<rasd:RecoveryFile xsi:nil="true" />
<rasd:SnapshotDataRoot xsi:nil="true" />
<rasd:SuspendDataRoot xsi:nil="true" />
<rasd:SwapFileDataRoot xsi:nil="true" />
<rasd:VirtualSystemIdentifier>SQL</rasd:VirtualSystemIdentifier>
<rasd:VirtualSystemType>vmx-07</rasd:VirtualSystemType>
</System>
<Item>
<rasd:Address xsi:nil="true" />
<rasd:AddressOnParent xsi:nil="true" />
<rasd:AllocationUnits>hertz * 10^6</rasd:AllocationUnits>
<rasd:AutomaticAllocation xsi:nil="true" />
<rasd:AutomaticDeallocation xsi:nil="true" />
<rasd:Caption xsi:nil="true" />
<rasd:ConsumerVisibility xsi:nil="true" />
<rasd:Description>Number of Virtual CPUs</rasd:Description>
<rasd:ElementName>1 virtual CPU(s)</rasd:ElementName>
<rasd:InstanceID>1</rasd:InstanceID>
<rasd:Limit xsi:nil="true" />
<rasd:MappingBehavior xsi:nil="true" />
<rasd:OtherResourceType xsi:nil="true" />
<rasd:Parent xsi:nil="true" />
<rasd:PoolID xsi:nil="true" />
<rasd:Reservation xsi:nil="true" />
<rasd:ResourceSubType xsi:nil="true" />
<rasd:ResourceType>3</rasd:ResourceType>
<rasd:VirtualQuantity>1</rasd:VirtualQuantity>
<rasd:VirtualQuantityUnits>count</rasd:VirtualQuantityUnits>
<rasd:Weight xsi:nil="true" />
</Item>
<Item>
<rasd:Address xsi:nil="true" />
<rasd:AddressOnParent xsi:nil="true" />
<rasd:AllocationUnits>byte * 2^20</rasd:AllocationUnits>
<rasd:AutomaticAllocation xsi:nil="true" />
<rasd:AutomaticDeallocation xsi:nil="true" />
<rasd:Caption xsi:nil="true" />
<rasd:ConsumerVisibility xsi:nil="true" />
<rasd:Description>Memory Size</rasd:Description>
<rasd:ElementName>16MB of memory</rasd:ElementName>
<rasd:InstanceID>2</rasd:InstanceID>
<rasd:Limit xsi:nil="true" />
<rasd:MappingBehavior xsi:nil="true" />
<rasd:OtherResourceType xsi:nil="true" />
<rasd:Parent xsi:nil="true" />
<rasd:PoolID xsi:nil="true" />
<rasd:Reservation xsi:nil="true" />
<rasd:ResourceSubType xsi:nil="true" />
<rasd:ResourceType>4</rasd:ResourceType>
<rasd:VirtualQuantity>16</rasd:VirtualQuantity>
<rasd:VirtualQuantityUnits>byte * 2^20</rasd:VirtualQuantityUnits>
<rasd:Weight xsi:nil="true" />
</Item>
<Item>
<rasd:Address>0</rasd:Address>
<rasd:AddressOnParent xsi:nil="true" />
<rasd:AllocationUnits xsi:nil="true" />
<rasd:AutomaticAllocation xsi:nil="true" />
<rasd:AutomaticDeallocation xsi:nil="true" />
<rasd:Caption xsi:nil="true" />
<rasd:ConsumerVisibility xsi:nil="true" />
<rasd:Description>SCSI Controller</rasd:Description>
<rasd:ElementName>SCSI Controller 0</rasd:ElementName>
<rasd:InstanceID>3</rasd:InstanceID>
<rasd:Limit xsi:nil="true" />
<rasd:MappingBehavior xsi:nil="true" />
<rasd:OtherResourceType xsi:nil="true" />
<rasd:Parent xsi:nil="true" />
<rasd:PoolID xsi:nil="true" />
<rasd:Reservation xsi:nil="true" />
<rasd:ResourceSubType>lsilogic</rasd:ResourceSubType>
<rasd:ResourceType>6</rasd:ResourceType>
<rasd:VirtualQuantity xsi:nil="true" />
<rasd:VirtualQuantityUnits xsi:nil="true" />
<rasd:Weight xsi:nil="true" />
</Item>
<Item>
<rasd:AddressOnParent>7</rasd:AddressOnParent>
<rasd:AutomaticAllocation>true</rasd:AutomaticAllocation>
<rasd:Connection connected="true">Internal</rasd:Connection>
<rasd:Description>PCNet32 ethernet adapter on "Internal" network</rasd:Description>
<rasd:ElementName>Network Adapter 1</rasd:ElementName>
<rasd:InstanceID>8</rasd:InstanceID>
<rasd:ResourceSubType>PCNet32</rasd:ResourceSubType>
<rasd:ResourceType>10</rasd:ResourceType>
</Item>
<Item>
<rasd:Address xsi:nil="true" />
<rasd:AddressOnParent>0</rasd:AddressOnParent>
<rasd:AllocationUnits xsi:nil="true" />
<rasd:AutomaticAllocation xsi:nil="true" />
<rasd:AutomaticDeallocation xsi:nil="true" />
<rasd:Caption xsi:nil="true" />
<rasd:ConsumerVisibility xsi:nil="true" />
<rasd:Description xsi:nil="true" />
<rasd:ElementName>Hard Disk 1</rasd:ElementName>
<rasd:HostResource>104857</rasd:HostResource>
<rasd:InstanceID>9</rasd:InstanceID>
<rasd:Limit xsi:nil="true" />
<rasd:MappingBehavior xsi:nil="true" />
<rasd:OtherResourceType xsi:nil="true" />
<rasd:Parent>3</rasd:Parent>
<rasd:PoolID xsi:nil="true" />
<rasd:Reservation xsi:nil="true" />
<rasd:ResourceSubType xsi:nil="true" />
<rasd:ResourceType>17</rasd:ResourceType>
<rasd:VirtualQuantity>104857</rasd:VirtualQuantity>
<rasd:VirtualQuantityUnits xsi:nil="true" />
<rasd:Weight xsi:nil="true" />
</Item>
</VirtualHardwareSection>
</VApp>

View File

@ -23,18 +23,12 @@
*/
package org.jclouds.vcloud.hostingdotcom.config;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.URI;
import java.util.concurrent.TimeUnit;
import javax.inject.Singleton;
import org.jclouds.concurrent.internal.SyncProxy;
import org.jclouds.http.RequiresHttp;
import org.jclouds.predicates.AddressReachable;
import org.jclouds.predicates.RetryablePredicate;
import org.jclouds.predicates.SocketOpen;
import org.jclouds.rest.ConfiguresRestClient;
import org.jclouds.rest.RestClientFactory;
import org.jclouds.vcloud.VCloudAsyncClient;
@ -42,9 +36,7 @@ import org.jclouds.vcloud.VCloudClient;
import org.jclouds.vcloud.config.VCloudRestClientModule;
import org.jclouds.vcloud.hostingdotcom.HostingDotComVCloudAsyncClient;
import org.jclouds.vcloud.hostingdotcom.HostingDotComVCloudClient;
import org.jclouds.vcloud.predicates.TaskSuccess;
import com.google.common.base.Predicate;
import com.google.inject.Provides;
/**
@ -55,23 +47,6 @@ import com.google.inject.Provides;
@RequiresHttp
@ConfiguresRestClient
public class HostingDotComVCloudRestClientModule extends VCloudRestClientModule {
@Provides
@Singleton
protected Predicate<InetSocketAddress> socketTester(SocketOpen open) {
return new RetryablePredicate<InetSocketAddress>(open, 130, 10, TimeUnit.SECONDS);
}
@Provides
@Singleton
protected Predicate<InetAddress> addressTester(AddressReachable reachable) {
return new RetryablePredicate<InetAddress>(reachable, 60, 5, TimeUnit.SECONDS);
}
@Provides
@Singleton
protected Predicate<String> successTester(TaskSuccess success) {
return new RetryablePredicate<String>(success, 600, 10, TimeUnit.SECONDS);
}
@Provides
@Singleton

View File

@ -24,29 +24,21 @@
package org.jclouds.vcloud.terremark.config;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.concurrent.TimeUnit;
import javax.inject.Named;
import javax.inject.Singleton;
import org.jclouds.concurrent.internal.SyncProxy;
import org.jclouds.http.RequiresHttp;
import org.jclouds.predicates.AddressReachable;
import org.jclouds.predicates.RetryablePredicate;
import org.jclouds.predicates.SocketOpen;
import org.jclouds.rest.ConfiguresRestClient;
import org.jclouds.rest.RestClientFactory;
import org.jclouds.util.Utils;
import org.jclouds.vcloud.VCloudAsyncClient;
import org.jclouds.vcloud.VCloudClient;
import org.jclouds.vcloud.config.VCloudRestClientModule;
import org.jclouds.vcloud.predicates.TaskSuccess;
import org.jclouds.vcloud.terremark.TerremarkVCloudAsyncClient;
import org.jclouds.vcloud.terremark.TerremarkVCloudClient;
import com.google.common.base.Predicate;
import com.google.inject.Provides;
/**
@ -57,26 +49,6 @@ import com.google.inject.Provides;
@RequiresHttp
@ConfiguresRestClient
public class TerremarkVCloudRestClientModule extends VCloudRestClientModule {
@Provides
@Singleton
protected Predicate<InetSocketAddress> socketTester(SocketOpen open) {
return new RetryablePredicate<InetSocketAddress>(open, 130, 10, TimeUnit.SECONDS);// make it
// longer
// then
// default internet
}
@Provides
@Singleton
protected Predicate<InetAddress> addressTester(AddressReachable reachable) {
return new RetryablePredicate<InetAddress>(reachable, 60, 5, TimeUnit.SECONDS);
}
@Provides
@Singleton
protected Predicate<String> successTester(TaskSuccess success) {
return new RetryablePredicate<String>(success, 600, 10, TimeUnit.SECONDS);
}
@Provides
@Singleton