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.compute.domain.Image;
import org.jclouds.logging.Logger;
import org.jclouds.rest.ResourceNotFoundException;
import com.google.common.base.Function;
import com.google.common.collect.Iterables;
@ -55,11 +54,10 @@ public final class RegionAndIdToImage implements Function<RegionAndName, Image>
public Image apply(RegionAndName key) {
try {
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);
} catch (ResourceNotFoundException e) {
logger.warn(e, "no image found for %s/%s: %s", key.getRegion(), key.getName(), e
.getMessage());
} catch (Exception e) {
logger.warn(e, "could not find image %s/%s: %s", key.getRegion(), key.getName(), e.getMessage());
return null;
}
}

View File

@ -35,10 +35,14 @@ import com.google.common.collect.Sets;
*/
public class BaseEC2RequestOptions extends BaseHttpRequestOptions {
@Override
public String toString() {
return "[formParameters=" + formParameters + "]";
}
protected void indexFormValuesWithPrefix(String prefix, String... values) {
for (int i = 0; i < values.length; i++) {
formParameters.put(prefix + "." + (i + 1), checkNotNull(values[i], prefix.toLowerCase()
+ "s[" + i + "]"));
formParameters.put(prefix + "." + (i + 1), checkNotNull(values[i], prefix.toLowerCase() + "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.testng.Assert.assertEquals;
import java.util.NoSuchElementException;
import java.util.Set;
import org.jclouds.aws.ec2.EC2Client;
@ -44,7 +45,7 @@ import com.google.common.collect.ImmutableSet;
@Test(groups = "unit", testName = "ec2.RegionAndIdToImageTest")
public class RegionAndIdToImageTest {
@SuppressWarnings({ "unchecked"})
@SuppressWarnings({ "unchecked" })
@Test
public void testApply() {
@ -54,10 +55,10 @@ public class RegionAndIdToImageTest {
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);
.<org.jclouds.aws.ec2.domain.Image> of(ec2Image);
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);
replay(caller);
@ -76,7 +77,7 @@ public class RegionAndIdToImageTest {
}
@SuppressWarnings({ "unchecked"})
@SuppressWarnings({ "unchecked" })
@Test
public void testApplyNotFound() {
@ -86,10 +87,10 @@ public class RegionAndIdToImageTest {
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);
.<org.jclouds.aws.ec2.domain.Image> of(ec2Image);
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());
replay(caller);
@ -107,4 +108,36 @@ public class RegionAndIdToImageTest {
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);
}
}