mirror of
https://github.com/apache/jclouds.git
synced 2025-02-16 06:56:45 +00:00
worked around case where softlayer virtual guests cannot always obtain the corresponding product package
This commit is contained in:
parent
616b3c0549
commit
27dcb1dedc
@ -143,6 +143,8 @@ public class VirtualGuestToNodeMetadata implements Function<VirtualGuest, NodeMe
|
||||
if (guest.getStartCpus() < 1)
|
||||
return null;
|
||||
ProductOrder order = client.getVirtualGuestClient().getOrderTemplate(guest.getId());
|
||||
if (order == null)
|
||||
return null;
|
||||
Iterable<ProductItem> items = Iterables.transform(order.getPrices(), ProductItems.item());
|
||||
return productItemsToHardware.apply(items);
|
||||
}
|
||||
@ -163,6 +165,8 @@ public class VirtualGuestToNodeMetadata implements Function<VirtualGuest, NodeMe
|
||||
if (guest.getStartCpus() < 1)
|
||||
return null;
|
||||
ProductOrder order = client.getVirtualGuestClient().getOrderTemplate(guest.getId());
|
||||
if (order == null)
|
||||
return null;
|
||||
Iterable<ProductItem> items = Iterables.transform(order.getPrices(), ProductItems.item());
|
||||
ProductItem os = Iterables.find(items, ProductItemPredicates.categoryCode("os"));
|
||||
return new ProductItemToImage().apply(os);
|
||||
|
@ -118,7 +118,7 @@ public interface VirtualGuestClient {
|
||||
* @param id
|
||||
* The id of the existing Virtual Guest
|
||||
* @return
|
||||
* The ProductOrder used to create the VirtualGust
|
||||
* The ProductOrder used to create the VirtualGust or null if not available
|
||||
*/
|
||||
ProductOrder getOrderTemplate(long id);
|
||||
}
|
||||
|
@ -20,14 +20,12 @@ package org.jclouds.softlayer.handlers;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.inject.Singleton;
|
||||
|
||||
import org.jclouds.http.HttpCommand;
|
||||
import org.jclouds.http.HttpErrorHandler;
|
||||
import org.jclouds.http.HttpResponse;
|
||||
import org.jclouds.http.HttpResponseException;
|
||||
import org.jclouds.logging.Logger;
|
||||
import org.jclouds.rest.AuthorizationException;
|
||||
import org.jclouds.rest.ResourceNotFoundException;
|
||||
import org.jclouds.util.Strings2;
|
||||
@ -43,27 +41,29 @@ import com.google.common.io.Closeables;
|
||||
*/
|
||||
@Singleton
|
||||
public class SoftLayerErrorHandler implements HttpErrorHandler {
|
||||
@Resource
|
||||
protected Logger logger = Logger.NULL;
|
||||
|
||||
public void handleError(HttpCommand command, HttpResponse response) {
|
||||
// it is important to always read fully and close streams
|
||||
String message = parseMessage(response);
|
||||
Exception exception = message != null ? new HttpResponseException(command, response, message)
|
||||
: new HttpResponseException(command, response);
|
||||
: new HttpResponseException(command, response);
|
||||
try {
|
||||
message = message != null ? message : String.format("%s -> %s", command.getCurrentRequest().getRequestLine(),
|
||||
response.getStatusLine());
|
||||
response.getStatusLine());
|
||||
switch (response.getStatusCode()) {
|
||||
case 401:
|
||||
case 403:
|
||||
exception = new AuthorizationException(message, exception);
|
||||
break;
|
||||
case 404:
|
||||
if (!command.getCurrentRequest().getMethod().equals("DELETE")) {
|
||||
exception = new ResourceNotFoundException(message, exception);
|
||||
}
|
||||
break;
|
||||
case 401:
|
||||
case 403:
|
||||
exception = new AuthorizationException(message, exception);
|
||||
break;
|
||||
case 404:
|
||||
if (!command.getCurrentRequest().getMethod().equals("DELETE")) {
|
||||
exception = new ResourceNotFoundException(message, exception);
|
||||
}
|
||||
break;
|
||||
case 500:
|
||||
if (message != null && message.indexOf("Unable to determine package for") != -1) {
|
||||
exception = new ResourceNotFoundException(message, exception);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
if (response.getPayload() != null)
|
||||
|
@ -0,0 +1,117 @@
|
||||
/**
|
||||
* Licensed to jclouds, Inc. (jclouds) under one or more
|
||||
* contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. jclouds 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.softlayer;
|
||||
|
||||
import static org.easymock.EasyMock.expect;
|
||||
import static org.easymock.EasyMock.reportMatcher;
|
||||
import static org.easymock.classextension.EasyMock.createMock;
|
||||
import static org.easymock.classextension.EasyMock.replay;
|
||||
import static org.easymock.classextension.EasyMock.verify;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import org.easymock.IArgumentMatcher;
|
||||
import org.jclouds.http.HttpCommand;
|
||||
import org.jclouds.http.HttpRequest;
|
||||
import org.jclouds.http.HttpResponse;
|
||||
import org.jclouds.io.Payloads;
|
||||
import org.jclouds.rest.AuthorizationException;
|
||||
import org.jclouds.rest.ResourceNotFoundException;
|
||||
import org.jclouds.softlayer.handlers.SoftLayerErrorHandler;
|
||||
import org.jclouds.util.Strings2;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import com.google.inject.Guice;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Adrian Cole
|
||||
*/
|
||||
@Test(groups = { "unit" })
|
||||
public class SoftLayerErrorHandlerTest {
|
||||
|
||||
@Test
|
||||
public void test500MakesResourceNotFoundExceptionOnUnableToDeterminePackage() {
|
||||
assertCodeMakes(
|
||||
"GET",
|
||||
URI.create("https://api.softlayer.com/foo"),
|
||||
500,
|
||||
"",
|
||||
"{\"error\":\"Unable to determine package for 'node2102835255.me.org'.\"}",
|
||||
ResourceNotFoundException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test401MakesAuthorizationException() {
|
||||
assertCodeMakes("GET", URI.create("https://api.softlayer.com/foo"), 401, "", "Unauthorized",
|
||||
AuthorizationException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test404MakesResourceNotFoundException() {
|
||||
assertCodeMakes("GET", URI.create("https://api.softlayer.com/foo"), 404, "", "Not Found",
|
||||
ResourceNotFoundException.class);
|
||||
}
|
||||
|
||||
private void assertCodeMakes(String method, URI uri, int statusCode, String message, String content,
|
||||
Class<? extends Exception> expected) {
|
||||
assertCodeMakes(method, uri, statusCode, message, "text/xml", content, expected);
|
||||
}
|
||||
|
||||
private void assertCodeMakes(String method, URI uri, int statusCode, String message, String contentType,
|
||||
String content, Class<? extends Exception> expected) {
|
||||
|
||||
SoftLayerErrorHandler function = Guice.createInjector().getInstance(SoftLayerErrorHandler.class);
|
||||
|
||||
HttpCommand command = createMock(HttpCommand.class);
|
||||
HttpRequest request = new HttpRequest(method, uri);
|
||||
HttpResponse response = new HttpResponse(statusCode, message, Payloads.newInputStreamPayload(Strings2
|
||||
.toInputStream(content)));
|
||||
response.getPayload().getContentMetadata().setContentType(contentType);
|
||||
|
||||
expect(command.getCurrentRequest()).andReturn(request).atLeastOnce();
|
||||
command.setException(classEq(expected));
|
||||
|
||||
replay(command);
|
||||
|
||||
function.handleError(command, response);
|
||||
|
||||
verify(command);
|
||||
}
|
||||
|
||||
public static Exception classEq(final Class<? extends Exception> in) {
|
||||
reportMatcher(new IArgumentMatcher() {
|
||||
|
||||
@Override
|
||||
public void appendTo(StringBuffer buffer) {
|
||||
buffer.append("classEq(");
|
||||
buffer.append(in);
|
||||
buffer.append(")");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matches(Object arg) {
|
||||
return arg.getClass() == in;
|
||||
}
|
||||
|
||||
});
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user