JCLOUDS-570: Fallback to the GetImageStrategy

If the TemplateBuilderImpl is given an imageId but the image can not be
found in the image cache, fallback to the GetImageStrategy to perform a
call to the provider to try to get it.

We've seen that in some cases images are not returned in the image list
but they actually exist in the provider. This fix won't make them
available when filtering by other properties such as the operating system,
etc, but at least will make them available if their id is known.
This commit is contained in:
Ignasi Barrera 2014-06-05 19:13:04 +02:00
parent a7e342422c
commit 39f77ad3f8
8 changed files with 219 additions and 149 deletions

View File

@ -28,6 +28,7 @@ import org.jclouds.compute.domain.Image;
import org.jclouds.compute.domain.TemplateBuilder;
import org.jclouds.compute.domain.internal.TemplateBuilderImpl;
import org.jclouds.compute.options.TemplateOptions;
import org.jclouds.compute.strategy.GetImageStrategy;
import org.jclouds.domain.Location;
import com.google.common.base.Supplier;
@ -40,7 +41,7 @@ public class CloudSigmaTemplateBuilderImpl extends TemplateBuilderImpl {
public CloudSigmaTemplateBuilderImpl(@Memoized Supplier<Set<? extends Location>> locations,
@Memoized Supplier<Set<? extends Image>> images, @Memoized Supplier<Set<? extends Hardware>> hardwares,
Supplier<Location> defaultLocation2, @Named("DEFAULT") Provider<TemplateOptions> optionsProvider,
@Named("DEFAULT") Provider<TemplateBuilder> defaultTemplateProvider) {
super(locations, images, hardwares, defaultLocation2, optionsProvider, defaultTemplateProvider);
@Named("DEFAULT") Provider<TemplateBuilder> defaultTemplateProvider, GetImageStrategy getImageStrategy) {
super(locations, images, hardwares, defaultLocation2, optionsProvider, defaultTemplateProvider, getImageStrategy);
}
}

View File

@ -32,6 +32,7 @@ import org.jclouds.compute.domain.Image;
import org.jclouds.compute.domain.TemplateBuilder;
import org.jclouds.compute.domain.internal.TemplateBuilderImpl;
import org.jclouds.compute.options.TemplateOptions;
import org.jclouds.compute.strategy.GetImageStrategy;
import org.jclouds.domain.Location;
import org.jclouds.ec2.compute.domain.RegionAndName;
import org.jclouds.util.Throwables2;
@ -54,8 +55,9 @@ public class EC2TemplateBuilderImpl extends TemplateBuilderImpl {
protected EC2TemplateBuilderImpl(@Memoized Supplier<Set<? extends Location>> locations,
@Memoized Supplier<Set<? extends Image>> images, @Memoized Supplier<Set<? extends Hardware>> sizes,
Supplier<Location> defaultLocation, @Named("DEFAULT") Provider<TemplateOptions> optionsProvider,
@Named("DEFAULT") Provider<TemplateBuilder> defaultTemplateProvider, Supplier<LoadingCache<RegionAndName, ? extends Image>> imageMap) {
super(locations, images, sizes, defaultLocation, optionsProvider, defaultTemplateProvider);
@Named("DEFAULT") Provider<TemplateBuilder> defaultTemplateProvider, GetImageStrategy getImageStrategy,
Supplier<LoadingCache<RegionAndName, ? extends Image>> imageMap) {
super(locations, images, sizes, defaultLocation, optionsProvider, defaultTemplateProvider, getImageStrategy);
this.lazyImageCache = imageMap;
}

View File

@ -48,6 +48,7 @@ import org.jclouds.compute.domain.OsFamily;
import org.jclouds.compute.domain.Template;
import org.jclouds.compute.domain.TemplateBuilder;
import org.jclouds.compute.options.TemplateOptions;
import org.jclouds.compute.strategy.GetImageStrategy;
import org.jclouds.domain.Location;
import org.jclouds.domain.LocationBuilder;
import org.jclouds.domain.LocationScope;
@ -212,11 +213,13 @@ public class EC2TemplateBuilderTest {
Provider<TemplateOptions> optionsProvider = createMock(Provider.class);
Provider<TemplateBuilder> templateBuilderProvider = createMock(Provider.class);
TemplateOptions defaultOptions = createMock(TemplateOptions.class);
GetImageStrategy getImageStrategy = createMock(GetImageStrategy.class);
expect(optionsProvider.get()).andReturn(defaultOptions);
replay(optionsProvider);
replay(templateBuilderProvider);
replay(getImageStrategy);
Supplier<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet
.<Location> of(location));
Supplier<Set<? extends Hardware>> sizes = Suppliers.<Set<? extends Hardware>> ofInstance(ImmutableSet
@ -225,10 +228,10 @@ public class EC2TemplateBuilderTest {
m2_4xlarge().build(), g2_2xlarge().build(), CC1_4XLARGE));
return new EC2TemplateBuilderImpl(locations, images, sizes, Suppliers.ofInstance(location), optionsProvider,
templateBuilderProvider, imageCache) {
templateBuilderProvider, getImageStrategy, imageCache) {
};
}
Function<ComputeMetadata, String> indexer() {
return new Function<ComputeMetadata, String>() {
@Override

View File

@ -37,6 +37,7 @@ import org.jclouds.compute.domain.TemplateBuilder;
import org.jclouds.compute.domain.internal.TemplateBuilderImpl;
import org.jclouds.compute.domain.internal.TemplateBuilderImplTest;
import org.jclouds.compute.options.TemplateOptions;
import org.jclouds.compute.strategy.GetImageStrategy;
import org.jclouds.domain.Location;
import org.jclouds.ec2.compute.domain.RegionAndName;
import org.jclouds.ec2.compute.functions.ImagesToRegionAndIdMap;
@ -68,7 +69,7 @@ public class EC2TemplateBuilderImplTest extends TemplateBuilderImplTest {
protected EC2TemplateBuilderImpl createTemplateBuilder(final Image knownImage,
@Memoized Supplier<Set<? extends Location>> locations, @Memoized final Supplier<Set<? extends Image>> images,
@Memoized Supplier<Set<? extends Hardware>> sizes, Location defaultLocation,
Provider<TemplateOptions> optionsProvider, Provider<TemplateBuilder> templateBuilderProvider) {
Provider<TemplateOptions> optionsProvider, Provider<TemplateBuilder> templateBuilderProvider, GetImageStrategy getImageStrategy) {
LoadingCache<RegionAndName, ? extends Image> imageMap;
if (knownImage != null) {
@ -88,7 +89,7 @@ public class EC2TemplateBuilderImplTest extends TemplateBuilderImplTest {
}
return new EC2TemplateBuilderImpl(locations, images, sizes, Suppliers.ofInstance(defaultLocation),
optionsProvider, templateBuilderProvider, Suppliers.<LoadingCache<RegionAndName, ? extends Image>>ofInstance(imageMap));
optionsProvider, templateBuilderProvider, getImageStrategy, Suppliers.<LoadingCache<RegionAndName, ? extends Image>>ofInstance(imageMap));
}
@Override
@ -118,6 +119,7 @@ public class EC2TemplateBuilderImplTest extends TemplateBuilderImplTest {
TemplateOptions defaultOptions = createMock(TemplateOptions.class);
Image knownImage = createMock(Image.class);
OperatingSystem os = createMock(OperatingSystem.class);
GetImageStrategy getImageStrategy = createMock(GetImageStrategy.class);
expect(optionsProvider.get()).andReturn(defaultOptions);
@ -137,22 +139,14 @@ public class EC2TemplateBuilderImplTest extends TemplateBuilderImplTest {
expect(os.getArch()).andReturn("paravirtual").atLeastOnce();
expect(os.is64Bit()).andReturn(false).atLeastOnce();
replay(knownImage);
replay(os);
replay(defaultOptions);
replay(optionsProvider);
replay(templateBuilderProvider);
replay(knownImage, os, defaultOptions, optionsProvider, templateBuilderProvider, getImageStrategy);
TemplateBuilderImpl template = createTemplateBuilder(knownImage, locations, images, sizes, region,
optionsProvider, templateBuilderProvider);
optionsProvider, templateBuilderProvider, getImageStrategy);
assertEquals(template.imageId("us-east-1/ami").build().getImage(), knownImage);
verify(knownImage);
verify(os);
verify(defaultOptions);
verify(optionsProvider);
verify(templateBuilderProvider);
verify(knownImage, os, defaultOptions, optionsProvider, templateBuilderProvider, getImageStrategy);
}
@SuppressWarnings("unchecked")
@ -169,29 +163,25 @@ public class EC2TemplateBuilderImplTest extends TemplateBuilderImplTest {
Provider<TemplateBuilder> templateBuilderProvider = createMock(Provider.class);
TemplateOptions defaultOptions = createMock(TemplateOptions.class);
Image knownImage = createMock(Image.class);
GetImageStrategy getImageStrategy = createMock(GetImageStrategy.class);
expect(knownImage.getId()).andReturn("region/ami").anyTimes();
expect(knownImage.getProviderId()).andReturn("ami").anyTimes();
expect(knownImage.getLocation()).andReturn(region).anyTimes();
expect(optionsProvider.get()).andReturn(defaultOptions);
replay(knownImage);
replay(defaultOptions);
replay(optionsProvider);
replay(templateBuilderProvider);
replay(knownImage, defaultOptions, optionsProvider, templateBuilderProvider, getImageStrategy);
TemplateBuilderImpl template = createTemplateBuilder(knownImage, locations, images, sizes, region,
optionsProvider, templateBuilderProvider);
optionsProvider, templateBuilderProvider, getImageStrategy);
try {
template.imageId("ami").build();
fail("Expected IllegalArgumentException");
} catch (IllegalArgumentException e) {
}
verify(knownImage);
verify(defaultOptions);
verify(optionsProvider);
verify(templateBuilderProvider);
verify(knownImage, defaultOptions, optionsProvider, templateBuilderProvider, getImageStrategy);
}
@SuppressWarnings("unchecked")
@ -208,6 +198,7 @@ public class EC2TemplateBuilderImplTest extends TemplateBuilderImplTest {
Provider<TemplateOptions> optionsProvider = createMock(Provider.class);
Provider<TemplateBuilder> templateBuilderProvider = createMock(Provider.class);
TemplateOptions defaultOptions = createMock(TemplateOptions.class);
GetImageStrategy getImageStrategy = createMock(GetImageStrategy.class);
Image knownImage = createMock(Image.class);
expect(knownImage.getId()).andReturn("region/ami").anyTimes();
expect(knownImage.getProviderId()).andReturn("ami").anyTimes();
@ -216,22 +207,21 @@ public class EC2TemplateBuilderImplTest extends TemplateBuilderImplTest {
expect(defaultLocation.getId()).andReturn("region");
expect(optionsProvider.get()).andReturn(defaultOptions);
replay(knownImage);
replay(defaultOptions);
replay(defaultLocation);
replay(optionsProvider);
replay(templateBuilderProvider);
replay(knownImage, defaultOptions, defaultLocation, optionsProvider, templateBuilderProvider, getImageStrategy);
TemplateBuilderImpl template = createTemplateBuilder(knownImage, locations, images, sizes, defaultLocation,
optionsProvider, templateBuilderProvider);
optionsProvider, templateBuilderProvider, getImageStrategy);
assertEquals(template.imageId("region/bad").build().getImage(), knownImage);
verify(knownImage);
verify(defaultOptions);
verify(defaultLocation);
verify(optionsProvider);
verify(templateBuilderProvider);
verify(knownImage, defaultOptions, defaultLocation, optionsProvider, templateBuilderProvider, getImageStrategy);
}
// The EC2 provider already overrides the getImage method so this test is not useful for EC2
@Override
@Test(enabled = false)
public void testFindImageWithIdDefaultToGetImageStrategy() {
}
}

View File

@ -28,6 +28,7 @@ import org.jclouds.compute.domain.Image;
import org.jclouds.compute.domain.TemplateBuilder;
import org.jclouds.compute.domain.internal.TemplateBuilderImpl;
import org.jclouds.compute.options.TemplateOptions;
import org.jclouds.compute.strategy.GetImageStrategy;
import org.jclouds.domain.Location;
import com.google.common.base.Supplier;
@ -42,7 +43,7 @@ public class VCloudTemplateBuilderImpl extends TemplateBuilderImpl {
protected VCloudTemplateBuilderImpl(@Memoized Supplier<Set<? extends Location>> locations,
@Memoized Supplier<Set<? extends Image>> images, @Memoized Supplier<Set<? extends Hardware>> sizes,
Supplier<Location> defaultLocation, @Named("DEFAULT") Provider<TemplateOptions> optionsProvider,
@Named("DEFAULT") Provider<TemplateBuilder> defaultTemplateProvider) {
super(locations, images, sizes, defaultLocation, optionsProvider, defaultTemplateProvider);
@Named("DEFAULT") Provider<TemplateBuilder> defaultTemplateProvider, GetImageStrategy getImageStrategy) {
super(locations, images, sizes, defaultLocation, optionsProvider, defaultTemplateProvider, getImageStrategy);
}
}

View File

@ -53,6 +53,7 @@ import org.jclouds.compute.domain.TemplateBuilder;
import org.jclouds.compute.domain.TemplateBuilderSpec;
import org.jclouds.compute.options.TemplateOptions;
import org.jclouds.compute.reference.ComputeServiceConstants;
import org.jclouds.compute.strategy.GetImageStrategy;
import org.jclouds.domain.Location;
import org.jclouds.logging.Logger;
@ -60,6 +61,7 @@ import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Function;
import com.google.common.base.Objects;
import com.google.common.base.Objects.ToStringHelper;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.base.Supplier;
@ -85,6 +87,7 @@ public class TemplateBuilderImpl implements TemplateBuilder {
protected final Supplier<Location> defaultLocation;
protected final Provider<TemplateOptions> optionsProvider;
protected final Provider<TemplateBuilder> defaultTemplateProvider;
protected final GetImageStrategy getImageStrategy;
@VisibleForTesting
protected Location location;
@ -133,13 +136,14 @@ public class TemplateBuilderImpl implements TemplateBuilder {
protected TemplateBuilderImpl(@Memoized Supplier<Set<? extends Location>> locations,
@Memoized Supplier<Set<? extends Image>> images, @Memoized Supplier<Set<? extends Hardware>> hardwares,
Supplier<Location> defaultLocation2, @Named("DEFAULT") Provider<TemplateOptions> optionsProvider,
@Named("DEFAULT") Provider<TemplateBuilder> defaultTemplateProvider) {
this.locations = locations;
this.images = images;
this.hardwares = hardwares;
this.defaultLocation = defaultLocation2;
this.optionsProvider = optionsProvider;
this.defaultTemplateProvider = defaultTemplateProvider;
@Named("DEFAULT") Provider<TemplateBuilder> defaultTemplateProvider, GetImageStrategy getImageStrategy) {
this.locations = checkNotNull(locations, "locations");
this.images = checkNotNull(images, "locations");
this.hardwares = checkNotNull(hardwares, "hardwares");
this.defaultLocation = checkNotNull(defaultLocation2, "defaultLocation2");
this.optionsProvider = checkNotNull(optionsProvider, "optionsProvider");
this.defaultTemplateProvider = checkNotNull(defaultTemplateProvider, "defaultTemplateProvider");
this.getImageStrategy = checkNotNull(getImageStrategy, "getImageStrategy");
}
static Predicate<Hardware> supportsImagesPredicate(final Iterable<? extends Image> images) {
@ -727,12 +731,21 @@ public class TemplateBuilderImpl implements TemplateBuilder {
}
private Image findImageWithId(Set<? extends Image> images) {
Image image;
// TODO: switch to GetImageStrategy in version 1.5
image = tryFind(images, idPredicate).orNull();
if (image == null)
throwNoSuchElementExceptionAfterLoggingImageIds(format("%s not found", idPredicate), images);
return image;
// Try find the image in the cache and fallback to the GetImageStrategy
// see https://issues.apache.org/jira/browse/JCLOUDS-570
Optional<? extends Image> image = tryFind(images, idPredicate);
if (!image.isPresent()) {
logger.warn("Image %s not found in the image cache. Trying to get it directly...", imageId);
// Note that this might generate make a call to the provider instead of using a cache, but
// this will be executed rarely, only when an image is not present in the image list but
// it actually exists in the provider. It shouldn't be an expensive call so using a cache just for
// this corner case is overkill.
image = Optional.fromNullable(getImageStrategy.getImage(imageId));
if (!image.isPresent()) {
throwNoSuchElementExceptionAfterLoggingImageIds(format("%s not found", idPredicate), images);
}
}
return image.get();
}
private Hardware findHardwareWithId(Set<? extends Hardware> hardwaresToSearch) {

View File

@ -16,6 +16,7 @@
*/
package org.jclouds.compute.domain.internal;
import static org.easymock.EasyMock.anyObject;
import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.replay;
@ -30,6 +31,7 @@ import java.util.Set;
import javax.inject.Provider;
import org.easymock.EasyMock;
import org.jclouds.compute.domain.Hardware;
import org.jclouds.compute.domain.HardwareBuilder;
import org.jclouds.compute.domain.Image;
@ -43,6 +45,7 @@ import org.jclouds.compute.domain.TemplateBuilder;
import org.jclouds.compute.domain.Volume;
import org.jclouds.compute.options.TemplateOptions;
import org.jclouds.compute.predicates.ImagePredicates;
import org.jclouds.compute.strategy.GetImageStrategy;
import org.jclouds.domain.Location;
import org.jclouds.domain.LocationBuilder;
import org.jclouds.domain.LocationScope;
@ -135,15 +138,15 @@ public class TemplateBuilderImplTest {
Provider<TemplateOptions> optionsProvider = createMock(Provider.class);
Provider<TemplateBuilder> templateBuilderProvider = createMock(Provider.class);
TemplateBuilder defaultTemplate = createMock(TemplateBuilder.class);
GetImageStrategy getImageStrategy = createMock(GetImageStrategy.class);
replay(defaultTemplate, optionsProvider, templateBuilderProvider);
replay(defaultTemplate, optionsProvider, templateBuilderProvider, getImageStrategy);
TemplateBuilderImpl template = createTemplateBuilder(null, locations, images, hardwares, region,
optionsProvider, templateBuilderProvider);
optionsProvider, templateBuilderProvider, getImageStrategy);
assert template.locationPredicate.apply(hardware);
verify(defaultTemplate, optionsProvider, templateBuilderProvider);
verify(defaultTemplate, optionsProvider, templateBuilderProvider, getImageStrategy);
}
/**
@ -165,16 +168,17 @@ public class TemplateBuilderImplTest {
Provider<TemplateOptions> optionsProvider = createMock(Provider.class);
Provider<TemplateBuilder> templateBuilderProvider = createMock(Provider.class);
TemplateBuilder defaultTemplate = createMock(TemplateBuilder.class);
GetImageStrategy getImageStrategy = createMock(GetImageStrategy.class);
replay(defaultTemplate, optionsProvider, templateBuilderProvider);
replay(defaultTemplate, optionsProvider, templateBuilderProvider, getImageStrategy);
TemplateBuilderImpl template = createTemplateBuilder(null, locations, inputImages, hardwares, region,
optionsProvider, templateBuilderProvider);
optionsProvider, templateBuilderProvider, getImageStrategy);
template = templateBuilderPerTestCustomizer.apply(template);
assertEquals(template.resolveImage(hardware, inputImages.get()), expectedBest);
verify(defaultTemplate, optionsProvider, templateBuilderProvider);
verify(defaultTemplate, optionsProvider, templateBuilderProvider, getImageStrategy);
}
protected void doTestResolveImages(Supplier<Set<? extends Image>> inputImages, Image expectedBest) {
@ -293,17 +297,18 @@ public class TemplateBuilderImplTest {
Provider<TemplateOptions> optionsProvider = createMock(Provider.class);
Provider<TemplateBuilder> templateBuilderProvider = createMock(Provider.class);
TemplateBuilder defaultTemplate = createMock(TemplateBuilder.class);
GetImageStrategy getImageStrategy = createMock(GetImageStrategy.class);
expect(optionsProvider.get()).andReturn(new TemplateOptions());
replay(defaultTemplate, optionsProvider, templateBuilderProvider);
replay(defaultTemplate, optionsProvider, templateBuilderProvider, getImageStrategy);
TemplateBuilderImpl template = createTemplateBuilder(null, locations, images, hardwares, region,
optionsProvider, templateBuilderProvider);
optionsProvider, templateBuilderProvider, getImageStrategy);
assertEquals(template.smallest().osArchMatches("X86_32").build().getImage(), image);
verify(defaultTemplate, optionsProvider, templateBuilderProvider);
verify(defaultTemplate, optionsProvider, templateBuilderProvider, getImageStrategy);
}
@SuppressWarnings("unchecked")
@ -322,21 +327,18 @@ public class TemplateBuilderImplTest {
Provider<TemplateOptions> optionsProvider = createMock(Provider.class);
Provider<TemplateBuilder> templateBuilderProvider = createMock(Provider.class);
TemplateBuilder defaultTemplate = createMock(TemplateBuilder.class);
GetImageStrategy getImageStrategy = createMock(GetImageStrategy.class);
expect(optionsProvider.get()).andReturn(new TemplateOptions());
replay(defaultTemplate);
replay(optionsProvider);
replay(templateBuilderProvider);
replay(defaultTemplate, optionsProvider, templateBuilderProvider, getImageStrategy);
TemplateBuilderImpl template = createTemplateBuilder(null, locations, images, hardwares, region,
optionsProvider, templateBuilderProvider);
optionsProvider, templateBuilderProvider, getImageStrategy);
template.imageId(getProviderFormatId("imageId")).build();
verify(defaultTemplate);
verify(optionsProvider);
verify(templateBuilderProvider);
verify(defaultTemplate, optionsProvider, templateBuilderProvider, getImageStrategy);
}
@SuppressWarnings("unchecked")
@ -355,17 +357,18 @@ public class TemplateBuilderImplTest {
Provider<TemplateOptions> optionsProvider = createMock(Provider.class);
Provider<TemplateBuilder> templateBuilderProvider = createMock(Provider.class);
TemplateBuilder defaultTemplate = createMock(TemplateBuilder.class);
GetImageStrategy getImageStrategy = createMock(GetImageStrategy.class);
expect(optionsProvider.get()).andReturn(new TemplateOptions());
replay(defaultTemplate, optionsProvider, templateBuilderProvider);
replay(defaultTemplate, optionsProvider, templateBuilderProvider, getImageStrategy);
TemplateBuilderImpl template = createTemplateBuilder(null, locations, images, hardwares, region,
optionsProvider, templateBuilderProvider);
optionsProvider, templateBuilderProvider, getImageStrategy);
template.imageId(getProviderFormatId("imageId")).build();
verify(defaultTemplate, optionsProvider, templateBuilderProvider);
verify(defaultTemplate, optionsProvider, templateBuilderProvider, getImageStrategy);
}
@ -385,20 +388,21 @@ public class TemplateBuilderImplTest {
Provider<TemplateOptions> optionsProvider = createMock(Provider.class);
Provider<TemplateBuilder> templateBuilderProvider = createMock(Provider.class);
TemplateBuilder defaultTemplate = createMock(TemplateBuilder.class);
GetImageStrategy getImageStrategy = createMock(GetImageStrategy.class);
expect(optionsProvider.get()).andReturn(new TemplateOptions());
replay(defaultTemplate, optionsProvider, templateBuilderProvider);
replay(defaultTemplate, optionsProvider, templateBuilderProvider, getImageStrategy);
TemplateBuilderImpl template = createTemplateBuilder(image, locations, images, hardwares, region,
optionsProvider, templateBuilderProvider);
optionsProvider, templateBuilderProvider, getImageStrategy);
try {
template.imageId(getProviderFormatId("imageId")).build();
fail("Expected NoSuchElementException");
} catch (NoSuchElementException e) {
// make sure message is succinct
assertEquals(e.getMessage(), "no hardware profiles support images matching params: idEquals(differentImageId)");
verify(defaultTemplate, optionsProvider, templateBuilderProvider);
verify(defaultTemplate, optionsProvider, templateBuilderProvider, getImageStrategy);
}
}
@ -417,26 +421,21 @@ public class TemplateBuilderImplTest {
Provider<TemplateOptions> optionsProvider = createMock(Provider.class);
Provider<TemplateBuilder> templateBuilderProvider = createMock(Provider.class);
TemplateBuilder defaultTemplate = createMock(TemplateBuilder.class);
GetImageStrategy getImageStrategy = createMock(GetImageStrategy.class);
expect(templateBuilderProvider.get()).andReturn(defaultTemplate);
expect(defaultTemplate.options(from)).andReturn(defaultTemplate);
expect(defaultTemplate.build()).andReturn(null);
expect(optionsProvider.get()).andReturn(from).atLeastOnce();
replay(defaultTemplate);
replay(defaultLocation);
replay(optionsProvider);
replay(templateBuilderProvider);
replay(defaultTemplate, defaultLocation, optionsProvider, templateBuilderProvider, getImageStrategy);
TemplateBuilderImpl template = createTemplateBuilder(null, locations, images, hardwares, defaultLocation,
optionsProvider, templateBuilderProvider);
optionsProvider, templateBuilderProvider, getImageStrategy);
template.options(options).build();
verify(defaultTemplate);
verify(defaultLocation);
verify(optionsProvider);
verify(templateBuilderProvider);
verify(defaultTemplate, defaultLocation, optionsProvider, templateBuilderProvider, getImageStrategy);
}
@SuppressWarnings("unchecked")
@ -453,32 +452,27 @@ public class TemplateBuilderImplTest {
Provider<TemplateOptions> optionsProvider = createMock(Provider.class);
Provider<TemplateBuilder> templateBuilderProvider = createMock(Provider.class);
TemplateBuilder defaultTemplate = createMock(TemplateBuilder.class);
GetImageStrategy getImageStrategy = createMock(GetImageStrategy.class);
expect(templateBuilderProvider.get()).andReturn(defaultTemplate);
expect(defaultTemplate.build()).andReturn(null);
replay(defaultTemplate);
replay(defaultLocation);
replay(optionsProvider);
replay(templateBuilderProvider);
replay(defaultTemplate, defaultLocation, optionsProvider, templateBuilderProvider, getImageStrategy);
TemplateBuilderImpl template = createTemplateBuilder(null, locations, images, hardwares, defaultLocation,
optionsProvider, templateBuilderProvider);
optionsProvider, templateBuilderProvider, getImageStrategy);
template.build();
verify(defaultTemplate);
verify(defaultLocation);
verify(optionsProvider);
verify(templateBuilderProvider);
verify(defaultTemplate, defaultLocation, optionsProvider, templateBuilderProvider, getImageStrategy);
}
protected TemplateBuilderImpl createTemplateBuilder(Image knownImage, Supplier<Set<? extends Location>> locations,
Supplier<Set<? extends Image>> images, Supplier<Set<? extends Hardware>> hardwares,
Location defaultLocation, Provider<TemplateOptions> optionsProvider,
Provider<TemplateBuilder> templateBuilderProvider) {
Provider<TemplateBuilder> templateBuilderProvider, GetImageStrategy getImageStrategy) {
TemplateBuilderImpl template = new TemplateBuilderImpl(locations, images, hardwares, Suppliers
.ofInstance(defaultLocation), optionsProvider, templateBuilderProvider);
.ofInstance(defaultLocation), optionsProvider, templateBuilderProvider, getImageStrategy);
return template;
}
@ -499,16 +493,17 @@ public class TemplateBuilderImplTest {
Provider<TemplateOptions> optionsProvider = createMock(Provider.class);
Provider<TemplateBuilder> templateBuilderProvider = createMock(Provider.class);
TemplateOptions defaultOptions = createMock(TemplateOptions.class);
GetImageStrategy getImageStrategy = createMock(GetImageStrategy.class);
expect(optionsProvider.get()).andReturn(from).atLeastOnce();
replay(defaultOptions, optionsProvider, templateBuilderProvider);
replay(defaultOptions, optionsProvider, templateBuilderProvider, getImageStrategy);
TemplateBuilderImpl template = createTemplateBuilder(null, locations, images, hardwares, region,
optionsProvider, templateBuilderProvider);
optionsProvider, templateBuilderProvider, getImageStrategy);
assertEquals(template.imageId(image.getId()).locationId(provider.getId()).build().getLocation(), region);
verify(defaultOptions, optionsProvider, templateBuilderProvider);
verify(defaultOptions, optionsProvider, templateBuilderProvider, getImageStrategy);
}
@SuppressWarnings("unchecked")
@ -522,13 +517,12 @@ public class TemplateBuilderImplTest {
Provider<TemplateOptions> optionsProvider = createMock(Provider.class);
Provider<TemplateBuilder> templateBuilderProvider = createMock(Provider.class);
TemplateOptions defaultOptions = createMock(TemplateOptions.class);
GetImageStrategy getImageStrategy = createMock(GetImageStrategy.class);
replay(defaultOptions);
replay(optionsProvider);
replay(templateBuilderProvider);
replay(defaultOptions, optionsProvider, templateBuilderProvider, getImageStrategy);
TemplateBuilderImpl template = createTemplateBuilder(null, locations, images, hardwares, region,
optionsProvider, templateBuilderProvider);
optionsProvider, templateBuilderProvider, getImageStrategy);
try {
template.imageId("foo").locationId("location").build();
@ -537,9 +531,7 @@ public class TemplateBuilderImplTest {
}
verify(defaultOptions);
verify(optionsProvider);
verify(templateBuilderProvider);
verify(defaultOptions, optionsProvider, templateBuilderProvider, getImageStrategy);
}
@SuppressWarnings("unchecked")
@ -554,14 +546,14 @@ public class TemplateBuilderImplTest {
.<Hardware> of());
Provider<TemplateOptions> optionsProvider = createMock(Provider.class);
Provider<TemplateBuilder> templateBuilderProvider = createMock(Provider.class);
GetImageStrategy getImageStrategy = createMock(GetImageStrategy.class);
expect(optionsProvider.get()).andReturn(from).atLeastOnce();
replay(optionsProvider);
replay(templateBuilderProvider);
replay(optionsProvider, templateBuilderProvider, getImageStrategy);
TemplateBuilderImpl template = createTemplateBuilder(null, locations, images, hardwares, region,
optionsProvider, templateBuilderProvider);
optionsProvider, templateBuilderProvider, getImageStrategy);
try {
template.imageId("foo").options(provideTemplateOptions()).locationId("location").build();
@ -570,8 +562,7 @@ public class TemplateBuilderImplTest {
}
verify(optionsProvider);
verify(templateBuilderProvider);
verify(optionsProvider, templateBuilderProvider, getImageStrategy);
}
@SuppressWarnings("unchecked")
@ -584,13 +575,14 @@ public class TemplateBuilderImplTest {
Provider<TemplateOptions> optionsProvider = createMock(Provider.class);
Provider<TemplateBuilder> templateBuilderProvider = createMock(Provider.class);
TemplateOptions defaultOptions = createMock(TemplateOptions.class);
GetImageStrategy getImageStrategy = createMock(GetImageStrategy.class);
expect(optionsProvider.get()).andReturn(defaultOptions);
replay(defaultOptions, optionsProvider, templateBuilderProvider);
replay(defaultOptions, optionsProvider, templateBuilderProvider, getImageStrategy);
TemplateBuilderImpl template = createTemplateBuilder(null, locations, images, hardwares, region,
optionsProvider, templateBuilderProvider);
optionsProvider, templateBuilderProvider, getImageStrategy);
try {
template.os64Bit(true).build();
@ -599,7 +591,7 @@ public class TemplateBuilderImplTest {
assertEquals(e.getMessage(), "no images present!");
}
verify(defaultOptions, optionsProvider, templateBuilderProvider);
verify(defaultOptions, optionsProvider, templateBuilderProvider, getImageStrategy);
}
@SuppressWarnings("unchecked")
@ -612,13 +604,14 @@ public class TemplateBuilderImplTest {
Provider<TemplateOptions> optionsProvider = createMock(Provider.class);
Provider<TemplateBuilder> templateBuilderProvider = createMock(Provider.class);
TemplateOptions defaultOptions = createMock(TemplateOptions.class);
GetImageStrategy getImageStrategy = createMock(GetImageStrategy.class);
expect(optionsProvider.get()).andReturn(defaultOptions);
replay(defaultOptions, optionsProvider, templateBuilderProvider);
replay(defaultOptions, optionsProvider, templateBuilderProvider, getImageStrategy);
TemplateBuilderImpl template = createTemplateBuilder(null, locations, images, hardwares, region,
optionsProvider, templateBuilderProvider);
optionsProvider, templateBuilderProvider, getImageStrategy);
try {
template.os64Bit(true).build();
@ -627,7 +620,7 @@ public class TemplateBuilderImplTest {
assertEquals(e.getMessage(), "no hardware profiles present!");
}
verify(defaultOptions, optionsProvider, templateBuilderProvider);
verify(defaultOptions, optionsProvider, templateBuilderProvider, getImageStrategy);
}
@SuppressWarnings("unchecked")
@ -641,13 +634,15 @@ public class TemplateBuilderImplTest {
Provider<TemplateOptions> optionsProvider = createMock(Provider.class);
Provider<TemplateBuilder> templateBuilderProvider = createMock(Provider.class);
TemplateOptions defaultOptions = createMock(TemplateOptions.class);
GetImageStrategy getImageStrategy = createMock(GetImageStrategy.class);
expect(optionsProvider.get()).andReturn(defaultOptions);
expect(getImageStrategy.getImage(anyObject(String.class))).andReturn(null);
replay(defaultOptions, optionsProvider, templateBuilderProvider);
replay(defaultOptions, optionsProvider, templateBuilderProvider, getImageStrategy);
TemplateBuilderImpl template = createTemplateBuilder(null, locations, images, hardwares, region,
optionsProvider, templateBuilderProvider);
optionsProvider, templateBuilderProvider, getImageStrategy);
try {
template.imageId("region/imageId2").build();
@ -671,13 +666,14 @@ public class TemplateBuilderImplTest {
Provider<TemplateOptions> optionsProvider = createMock(Provider.class);
Provider<TemplateBuilder> templateBuilderProvider = createMock(Provider.class);
TemplateOptions defaultOptions = createMock(TemplateOptions.class);
GetImageStrategy getImageStrategy = createMock(GetImageStrategy.class);
expect(optionsProvider.get()).andReturn(defaultOptions);
replay(defaultOptions, optionsProvider, templateBuilderProvider);
replay(defaultOptions, optionsProvider, templateBuilderProvider, getImageStrategy);
TemplateBuilderImpl template = createTemplateBuilder(null, locations, images, hardwares, region,
optionsProvider, templateBuilderProvider);
optionsProvider, templateBuilderProvider, getImageStrategy);
try {
template.imageDescriptionMatches("notDescription").build();
@ -687,7 +683,7 @@ public class TemplateBuilderImplTest {
assertEquals(e.getMessage(), "no image matched predicate: Predicates.and(nullEqualToIsParentOrIsGrandparentOfCurrentLocation(),imageDescription(notDescription))");
}
verify(defaultOptions, optionsProvider, templateBuilderProvider);
verify(defaultOptions, optionsProvider, templateBuilderProvider, getImageStrategy);
}
protected TemplateOptions provideTemplateOptions() {
@ -706,13 +702,15 @@ public class TemplateBuilderImplTest {
Provider<TemplateOptions> optionsProvider = createMock(Provider.class);
TemplateOptions from = provideTemplateOptions();
Provider<TemplateBuilder> templateBuilderProvider = createMock(Provider.class);
GetImageStrategy getImageStrategy = createMock(GetImageStrategy.class);
expect(optionsProvider.get()).andReturn(from);
expect(getImageStrategy.getImage(anyObject(String.class))).andReturn(null);
replay(defaultLocation, optionsProvider, templateBuilderProvider);
replay(defaultLocation, optionsProvider, templateBuilderProvider, getImageStrategy);
TemplateBuilderImpl template = createTemplateBuilder(null, locations, images, hardwares, defaultLocation,
optionsProvider, templateBuilderProvider);
optionsProvider, templateBuilderProvider, getImageStrategy);
try {
template.imageId("region/ami").options(provideTemplateOptions()).build();
@ -735,13 +733,12 @@ public class TemplateBuilderImplTest {
Location defaultLocation = createMock(Location.class);
Provider<TemplateOptions> optionsProvider = createMock(Provider.class);
Provider<TemplateBuilder> templateBuilderProvider = createMock(Provider.class);
GetImageStrategy getImageStrategy = createMock(GetImageStrategy.class);
replay(defaultLocation);
replay(optionsProvider);
replay(templateBuilderProvider);
replay(defaultLocation, optionsProvider, templateBuilderProvider, getImageStrategy);
TemplateBuilderImpl template = createTemplateBuilder(null, locations, images, hardwares, defaultLocation,
optionsProvider, templateBuilderProvider);
optionsProvider, templateBuilderProvider, getImageStrategy);
template.imageDescriptionMatches("imageDescriptionMatches");
template.imageNameMatches("imageNameMatches");
@ -767,9 +764,69 @@ public class TemplateBuilderImplTest {
assertEquals(template.osFamily, null);
assertEquals(template.imageId, "myid");
verify(defaultLocation);
verify(optionsProvider);
verify(templateBuilderProvider);
verify(defaultLocation, optionsProvider, templateBuilderProvider, getImageStrategy);
}
@Test
public void testFindImageWithIdDefaultToGetImageStrategy() {
final Supplier<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet
.<Location> of(region));
final Supplier<Set<? extends Image>> images = Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet
.<Image> of(
new ImageBuilder()
.ids("Ubuntu 11.04 x64")
.name("Ubuntu 11.04 x64")
.description("Ubuntu 11.04 x64")
.location(region)
.status(Status.AVAILABLE)
.operatingSystem(
OperatingSystem.builder().name("Ubuntu 11.04 x64").description("Ubuntu 11.04 x64")
.is64Bit(true).version("11.04").family(OsFamily.UBUNTU).build()).build(),
new ImageBuilder()
.ids("Ubuntu 11.04 64-bit")
.name("Ubuntu 11.04 64-bit")
.description("Ubuntu 11.04 64-bit")
.location(region)
.status(Status.AVAILABLE)
.operatingSystem(
OperatingSystem.builder().name("Ubuntu 11.04 64-bit").description("Ubuntu 11.04 64-bit")
.is64Bit(true).version("11.04").family(OsFamily.UBUNTU).build()).build()));
final Supplier<Set<? extends Hardware>> hardwares = Suppliers.<Set<? extends Hardware>> ofInstance(ImmutableSet
.<Hardware> of(
new HardwareBuilder()
.ids(String.format("datacenter(%s)platform(%s)cpuCores(%d)memorySizeMB(%d)diskSizeGB(%d)",
"Falkenberg", "Xen", 1, 512, 5)).ram(512)
.processors(ImmutableList.of(new Processor(1, 1.0)))
.volumes(ImmutableList.<Volume> of(new VolumeImpl((float) 5, true, true))).hypervisor("Xen")
.location(region)
.supportsImage(ImagePredicates.idEquals(image.getId())).build()));
final Provider<TemplateOptions> optionsProvider = new Provider<TemplateOptions>() {
@Override
public TemplateOptions get() {
return new TemplateOptions();
}
};
final GetImageStrategy getImageStrategy = createMock(GetImageStrategy.class);
expect(getImageStrategy.getImage(image.getId())).andReturn(image);
replay(getImageStrategy);
Provider<TemplateBuilder> templateBuilderProvider = new Provider<TemplateBuilder>() {
@Override
public TemplateBuilder get() {
return createTemplateBuilder(null, locations, images, hardwares, region, optionsProvider, this, getImageStrategy);
}
};
// Note that the image provided is not in the image list, but it is the one returned by the GetImagestrategy
TemplateBuilder templateBuilder = templateBuilderProvider.get().imageId(image.getId());
Template template = templateBuilder.build();
assertEquals(template.getImage().getId(), image.getId());
verify(getImageStrategy);
}
@SuppressWarnings("unchecked")
@ -783,13 +840,12 @@ public class TemplateBuilderImplTest {
Location defaultLocation = createMock(Location.class);
Provider<TemplateOptions> optionsProvider = createMock(Provider.class);
Provider<TemplateBuilder> templateBuilderProvider = createMock(Provider.class);
GetImageStrategy getImageStrategy = createMock(GetImageStrategy.class);
replay(defaultLocation);
replay(optionsProvider);
replay(templateBuilderProvider);
replay(defaultLocation, optionsProvider, templateBuilderProvider, getImageStrategy);
TemplateBuilderImpl template = createTemplateBuilder(null, locations, images, hardwares, defaultLocation,
optionsProvider, templateBuilderProvider);
optionsProvider, templateBuilderProvider, getImageStrategy);
template.hypervisorMatches("OpenVZ");
@ -802,14 +858,11 @@ public class TemplateBuilderImplTest {
assertEquals(template.hypervisor, null);
verify(defaultLocation);
verify(optionsProvider);
verify(templateBuilderProvider);
verify(defaultLocation, optionsProvider, templateBuilderProvider, getImageStrategy);
}
@Test
public void testMatchesHardwareWithIdPredicate() {
final Supplier<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet
.<Location> of(region));
final Supplier<Set<? extends Image>> images = Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet
@ -858,11 +911,14 @@ public class TemplateBuilderImplTest {
}
};
final GetImageStrategy getImageStrategy = createMock(GetImageStrategy.class);
Provider<TemplateBuilder> templateBuilderProvider = new Provider<TemplateBuilder>() {
@Override
public TemplateBuilder get() {
return createTemplateBuilder(null, locations, images, hardwares, region, optionsProvider, this);
return createTemplateBuilder(null, locations, images, hardwares, region, optionsProvider, this, getImageStrategy);
}
};
@ -875,13 +931,11 @@ public class TemplateBuilderImplTest {
Template template = templateBuilder.build();
assertEquals(template.getHardware().getHypervisor(), "OpenVZ");
assertEquals(template.getImage().getId(), "Ubuntu 11.04 64-bit");
}
@Test
public void testImageLocationNonDefault() {
final Supplier<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet
.<Location> of(region));
final Supplier<Set<? extends Image>> images = Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet
@ -912,11 +966,14 @@ public class TemplateBuilderImplTest {
}
};
final GetImageStrategy getImageStrategy = createMock(GetImageStrategy.class);
Provider<TemplateBuilder> templateBuilderProvider = new Provider<TemplateBuilder>() {
@Override
public TemplateBuilder get() {
return createTemplateBuilder(null, locations, images, hardwares, region, optionsProvider, this);
return createTemplateBuilder(null, locations, images, hardwares, region, optionsProvider, this, getImageStrategy);
}
};
@ -927,12 +984,10 @@ public class TemplateBuilderImplTest {
Template template = templateBuilder.build();
assertEquals(template.getLocation().getId(), "us-east-2");
}
@Test
public void testFromSpecWithLoginUser() {
final Supplier<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet
.<Location> of(region));
final Supplier<Set<? extends Image>> images = Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet
@ -963,11 +1018,14 @@ public class TemplateBuilderImplTest {
}
};
final GetImageStrategy getImageStrategy = createMock(GetImageStrategy.class);
Provider<TemplateBuilder> templateBuilderProvider = new Provider<TemplateBuilder>() {
@Override
public TemplateBuilder get() {
return createTemplateBuilder(null, locations, images, hardwares, region, optionsProvider, this);
return createTemplateBuilder(null, locations, images, hardwares, region, optionsProvider, this, getImageStrategy);
}
};

View File

@ -27,6 +27,7 @@ import org.jclouds.compute.domain.Hardware;
import org.jclouds.compute.domain.Image;
import org.jclouds.compute.domain.TemplateBuilder;
import org.jclouds.compute.options.TemplateOptions;
import org.jclouds.compute.strategy.GetImageStrategy;
import org.jclouds.domain.Location;
import org.jclouds.ec2.compute.domain.RegionAndName;
import org.jclouds.ec2.compute.internal.EC2TemplateBuilderImpl;
@ -44,8 +45,9 @@ public class AWSEC2TemplateBuilderImpl extends EC2TemplateBuilderImpl {
protected AWSEC2TemplateBuilderImpl(@Memoized Supplier<Set<? extends Location>> locations,
@Memoized Supplier<Set<? extends Image>> images, @Memoized Supplier<Set<? extends Hardware>> sizes,
Supplier<Location> defaultLocation, @Named("DEFAULT") Provider<TemplateOptions> optionsProvider,
@Named("DEFAULT") Provider<TemplateBuilder> defaultTemplateProvider, Supplier<LoadingCache<RegionAndName, ? extends Image>> imageMap) {
super(locations, images, sizes, defaultLocation, optionsProvider, defaultTemplateProvider, imageMap);
@Named("DEFAULT") Provider<TemplateBuilder> defaultTemplateProvider, GetImageStrategy getImageStrategy,
Supplier<LoadingCache<RegionAndName, ? extends Image>> imageMap) {
super(locations, images, sizes, defaultLocation, optionsProvider, defaultTemplateProvider, getImageStrategy, imageMap);
}
}