Set<? extends Image> images = getImages();
checkState(images.size() > 0, "no images present!");
Set<? extends Hardware> hardwaresToSearch = hardwares.get();
checkState(hardwaresToSearch.size() > 0, "no hardware profiles present!");
Image image = null;
if (imageId != null) {
image = findImageWithId(images);
if (currentLocationWiderThan(image.getLocation()))
this.location = image.getLocation();
}
Hardware hardware = null;
if (hardwareId != null) {
hardware = findHardwareWithId(hardwaresToSearch);
if (currentLocationWiderThan(hardware.getLocation()))
this.location = hardware.getLocation();
}
// if the user hasn't specified a location id, or an image or hardware
// with location, let's search scoped to the implicit one
if (location == null)
location = defaultLocation.get();
if (image == null) {
Iterable<? extends Image> supportedImages = findSupportedImages(images);
if (hardware == null)
hardware = resolveHardware(hardwaresToSearch, supportedImages);
image = resolveImage(hardware, supportedImages);
} else {
if (hardware == null)
hardware = resolveHardware(hardwaresToSearch, ImmutableSet.of(image));
}
logger.debug("<< matched image(%s) hardware(%s) location(%s)", image.getId(), hardware.getId(),
location.getId());
return new TemplateImpl(image, hardware, location, options);
}