mirror of https://github.com/apache/jclouds.git
Issue 406: absorb any problem looking up image implicitly
This commit is contained in:
parent
a8c24236ab
commit
2921dbb157
|
@ -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;
|
||||||
|
@ -55,11 +54,10 @@ public final class RegionAndIdToImage implements Function<RegionAndName, Image>
|
||||||
public Image apply(RegionAndName key) {
|
public Image apply(RegionAndName key) {
|
||||||
try {
|
try {
|
||||||
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 + "]"));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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() {
|
||||||
|
|
||||||
|
@ -54,10 +55,10 @@ public class RegionAndIdToImageTest {
|
||||||
org.jclouds.aws.ec2.domain.Image ec2Image = createMock(org.jclouds.aws.ec2.domain.Image.class);
|
org.jclouds.aws.ec2.domain.Image ec2Image = createMock(org.jclouds.aws.ec2.domain.Image.class);
|
||||||
Image image = createNiceMock(Image.class);
|
Image image = createNiceMock(Image.class);
|
||||||
Set<? extends org.jclouds.aws.ec2.domain.Image> images = ImmutableSet
|
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(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() {
|
||||||
|
|
||||||
|
@ -86,10 +87,10 @@ public class RegionAndIdToImageTest {
|
||||||
org.jclouds.aws.ec2.domain.Image ec2Image = createMock(org.jclouds.aws.ec2.domain.Image.class);
|
org.jclouds.aws.ec2.domain.Image ec2Image = createMock(org.jclouds.aws.ec2.domain.Image.class);
|
||||||
Image image = createNiceMock(Image.class);
|
Image image = createNiceMock(Image.class);
|
||||||
Set<? extends org.jclouds.aws.ec2.domain.Image> images = ImmutableSet
|
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(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);
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue