Issue 406: absorb any problem looking up image implicitly

This commit is contained in:
Adrian Cole 2010-11-16 23:00:35 +01:00
parent a8c24236ab
commit 2921dbb157
3 changed files with 48 additions and 13 deletions

View File

@ -29,7 +29,6 @@ import org.jclouds.aws.ec2.EC2Client;
import org.jclouds.aws.ec2.compute.domain.RegionAndName; import org.jclouds.aws.ec2.compute.domain.RegionAndName;
import org.jclouds.compute.domain.Image; import org.jclouds.compute.domain.Image;
import org.jclouds.logging.Logger; import org.jclouds.logging.Logger;
import org.jclouds.rest.ResourceNotFoundException;
import com.google.common.base.Function; import com.google.common.base.Function;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
@ -57,9 +56,8 @@ public final class RegionAndIdToImage implements Function<RegionAndName, Image>
org.jclouds.aws.ec2.domain.Image image = Iterables.getOnlyElement(sync.getAMIServices() org.jclouds.aws.ec2.domain.Image image = Iterables.getOnlyElement(sync.getAMIServices()
.describeImagesInRegion(key.getRegion(), imageIds(key.getName()))); .describeImagesInRegion(key.getRegion(), imageIds(key.getName())));
return parser.apply(image); return parser.apply(image);
} catch (ResourceNotFoundException e) { } catch (Exception e) {
logger.warn(e, "no image found for %s/%s: %s", key.getRegion(), key.getName(), e logger.warn(e, "could not find image %s/%s: %s", key.getRegion(), key.getName(), e.getMessage());
.getMessage());
return null; return null;
} }
} }

View File

@ -35,10 +35,14 @@ import com.google.common.collect.Sets;
*/ */
public class BaseEC2RequestOptions extends BaseHttpRequestOptions { public class BaseEC2RequestOptions extends BaseHttpRequestOptions {
@Override
public String toString() {
return "[formParameters=" + formParameters + "]";
}
protected void indexFormValuesWithPrefix(String prefix, String... values) { protected void indexFormValuesWithPrefix(String prefix, String... values) {
for (int i = 0; i < values.length; i++) { for (int i = 0; i < values.length; i++) {
formParameters.put(prefix + "." + (i + 1), checkNotNull(values[i], prefix.toLowerCase() formParameters.put(prefix + "." + (i + 1), checkNotNull(values[i], prefix.toLowerCase() + "s[" + i + "]"));
+ "s[" + i + "]"));
} }
} }

View File

@ -27,6 +27,7 @@ import static org.easymock.classextension.EasyMock.verify;
import static org.jclouds.aws.ec2.options.DescribeImagesOptions.Builder.imageIds; import static org.jclouds.aws.ec2.options.DescribeImagesOptions.Builder.imageIds;
import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertEquals;
import java.util.NoSuchElementException;
import java.util.Set; import java.util.Set;
import org.jclouds.aws.ec2.EC2Client; import org.jclouds.aws.ec2.EC2Client;
@ -44,7 +45,7 @@ import com.google.common.collect.ImmutableSet;
@Test(groups = "unit", testName = "ec2.RegionAndIdToImageTest") @Test(groups = "unit", testName = "ec2.RegionAndIdToImageTest")
public class RegionAndIdToImageTest { public class RegionAndIdToImageTest {
@SuppressWarnings({ "unchecked"}) @SuppressWarnings({ "unchecked" })
@Test @Test
public void testApply() { public void testApply() {
@ -57,7 +58,7 @@ public class RegionAndIdToImageTest {
.<org.jclouds.aws.ec2.domain.Image> of(ec2Image); .<org.jclouds.aws.ec2.domain.Image> of(ec2Image);
expect(caller.getAMIServices()).andReturn(client).atLeastOnce(); expect(caller.getAMIServices()).andReturn(client).atLeastOnce();
expect(client.describeImagesInRegion("region", imageIds("ami"))).andReturn((Set)images); expect(client.describeImagesInRegion("region", imageIds("ami"))).andReturn((Set) images);
expect(parser.apply(ec2Image)).andReturn(image); expect(parser.apply(ec2Image)).andReturn(image);
replay(caller); replay(caller);
@ -76,7 +77,7 @@ public class RegionAndIdToImageTest {
} }
@SuppressWarnings({ "unchecked"}) @SuppressWarnings({ "unchecked" })
@Test @Test
public void testApplyNotFound() { public void testApplyNotFound() {
@ -89,7 +90,7 @@ public class RegionAndIdToImageTest {
.<org.jclouds.aws.ec2.domain.Image> of(ec2Image); .<org.jclouds.aws.ec2.domain.Image> of(ec2Image);
expect(caller.getAMIServices()).andReturn(client).atLeastOnce(); expect(caller.getAMIServices()).andReturn(client).atLeastOnce();
expect(client.describeImagesInRegion("region", imageIds("ami"))).andReturn((Set)images); expect(client.describeImagesInRegion("region", imageIds("ami"))).andReturn((Set) images);
expect(parser.apply(ec2Image)).andThrow(new ResourceNotFoundException()); expect(parser.apply(ec2Image)).andThrow(new ResourceNotFoundException());
replay(caller); replay(caller);
@ -107,4 +108,36 @@ public class RegionAndIdToImageTest {
verify(client); verify(client);
} }
@SuppressWarnings({ "unchecked" })
@Test
public void testApplyNoSuchElementException() {
ImageParser parser = createMock(ImageParser.class);
EC2Client caller = createMock(EC2Client.class);
AMIClient client = createMock(AMIClient.class);
org.jclouds.aws.ec2.domain.Image ec2Image = createMock(org.jclouds.aws.ec2.domain.Image.class);
Image image = createNiceMock(Image.class);
Set<? extends org.jclouds.aws.ec2.domain.Image> images = ImmutableSet
.<org.jclouds.aws.ec2.domain.Image> of(ec2Image);
expect(caller.getAMIServices()).andReturn(client).atLeastOnce();
expect(client.describeImagesInRegion("region", imageIds("ami"))).andReturn((Set) images);
expect(parser.apply(ec2Image)).andThrow(new NoSuchElementException());
replay(caller);
replay(image);
replay(parser);
replay(client);
RegionAndIdToImage function = new RegionAndIdToImage(parser, caller);
assertEquals(function.apply(new RegionAndName("region", "ami")), null);
verify(caller);
verify(image);
verify(parser);
verify(client);
}
} }