if (nothingChangedExceptOptions()) { TemplateBuilder defaultTemplate = defaultTemplateProvider.get(); if (options != null) options = optionsProvider.get(); logger.debug(">> searching params(%s)", this); Set<? extends Image> images = getImages(); checkState(images.size() > 0, "no images present!"); Set<? extends Hardware> hardwaresToSearch = hardwares.get(); image = findImageWithId(images); if (currentLocationWiderThan(image.getLocation())) this.location = image.getLocation(); hardware = findHardwareWithId(hardwaresToSearch); if (currentLocationWiderThan(hardware.getLocation())) this.location = hardware.getLocation(); 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));
@Override public Image apply(Iterable<? extends Image> input) { List<? extends Image> maxImages = multiMax(ordering, input); if (logger.isTraceEnabled()) logger.trace("<< best images(%s)", transform(maxImages, imageToId)); return maxImages.get(maxImages.size() - 1); } };
/** * {@inheritDoc} */ @Override public TemplateBuilder fromTemplate(Template template) { location = template.getLocation(); fromHardware(template.getHardware()); fromImage(template.getImage()); options(template.getOptions()); return this; }
private Iterable<? extends Image> findSupportedImages(Set<? extends Image> images) { Predicate<Image> imagePredicate = buildImagePredicate(); Iterable<? extends Image> supportedImages = filter(images, imagePredicate); if (size(supportedImages) == 0) { throw throwNoSuchElementExceptionAfterLoggingImageIds( format("no image matched predicate: %s", imagePredicate), images); } return supportedImages; }
protected Hardware resolveHardware(Set<? extends Hardware> hardwarel, final Iterable<? extends Image> images) { Ordering<Hardware> hardwareOrdering = hardwareSorter(); Iterable<Predicate<Image>> supportsImagePredicates = Iterables.transform(hardwarel, new Function<Hardware, Predicate<Image>>() { @Override public Predicate<Image> apply(Hardware input) { return input.supportsImage(); } }); Predicate<Image> supportsImagePredicate = Iterables.size(supportsImagePredicates) == 1 ? Iterables .getOnlyElement(supportsImagePredicates) : Predicates.<Image>or(supportsImagePredicates); if (!Iterables.any(images, supportsImagePredicate)) { String message = format("no hardware profiles support images matching params: %s", supportsImagePredicate); throw throwNoSuchElementExceptionAfterLoggingHardwareIds(message, hardwarel); } Iterable<? extends Hardware> hardwareCompatibleWithOurImages = filter(hardwarel, supportsImagesPredicate(images)); Predicate<Hardware> hardwarePredicate = buildHardwarePredicate(); Hardware hardware; try { hardware = hardwareOrdering.max(filter(hardwareCompatibleWithOurImages, hardwarePredicate)); } catch (NoSuchElementException exception) { String message = format("no hardware profiles match params: %s", hardwarePredicate); throw throwNoSuchElementExceptionAfterLoggingHardwareIds(message, hardwareCompatibleWithOurImages); } logger.trace("<< matched hardware(%s)", hardware.getId()); return hardware; }
if (nothingChangedExceptOptions()) { TemplateBuilder defaultTemplate = defaultTemplateProvider.get(); if (options != null) options = optionsProvider.get(); logger.debug(">> searching params(%s)", this); Set<? extends Image> images = getImages(); checkState(!images.isEmpty(), "no images present!"); Set<? extends Hardware> hardwaresToSearch = hardwares.get(); image = loadImageWithId(images); if (currentLocationWiderThan(image.getLocation())) this.location = image.getLocation(); hardware = findHardwareWithId(hardwaresToSearch); if (currentLocationWiderThan(hardware.getLocation())) this.location = hardware.getLocation(); 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));
/** * @throws NoSuchElementException * if the image is not found */ @Override protected Image resolveImage(Hardware size, Iterable<? extends Image> supportedImages) { try { return super.resolveImage(size, supportedImages); } catch (NoSuchElementException e) { Image returnVal = lazyImageProvider.get(); if (returnVal != null) return returnVal; throw e; } }
optionsProvider, templateBuilderProvider); template.imageDescriptionMatches("imageDescriptionMatches"); template.imageNameMatches("imageNameMatches"); template.imageVersionMatches("imageVersionMatches"); template.osDescriptionMatches("osDescriptionMatches"); template.osFamily(OsFamily.CENTOS); template.osArchMatches("osArchMatches"); assertEquals(template.imageId, null); template.imageId("myid"); assertEquals(template.osArch, null); assertEquals(template.imageDescription, null);
@SuppressWarnings("unchecked") @Test public void testDefaultLocationWithNoOptionsNoSuchElement() { Supplier<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet .<Location> of(region)); Supplier<Set<? extends Image>> images = Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet.<Image> of(image)); Supplier<Set<? extends Hardware>> hardwares = Suppliers.<Set<? extends Hardware>> ofInstance(ImmutableSet .<Hardware> of(createMock(Hardware.class))); Provider<TemplateOptions> optionsProvider = createMock(Provider.class); Provider<TemplateBuilder> templateBuilderProvider = createMock(Provider.class); TemplateOptions defaultOptions = createMock(TemplateOptions.class); expect(optionsProvider.get()).andReturn(defaultOptions); replay(defaultOptions, optionsProvider, templateBuilderProvider); TemplateBuilderImpl template = createTemplateBuilder(null, locations, images, hardwares, region, optionsProvider, templateBuilderProvider); try { template.imageId("region/imageId2").build(); fail("Expected NoSuchElementException"); } catch (NoSuchElementException e) { // make sure big data is not in the exception message assertEquals(e.getMessage(), "imageId(region/imageId2) not found"); } verify(defaultOptions, optionsProvider, templateBuilderProvider); }
@Test public void testExistingHardwareProfileMatchHardwareProfileWithMinCoresMinRam() { Supplier<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet .of(region)); Supplier<Set<? extends Image>> images = Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet.of(image)); Supplier<Set<? extends Hardware>> hardwares = Suppliers.<Set<? extends Hardware>> ofInstance(ImmutableSet .<Hardware> of(hardware)); 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()); expect(getImageStrategy.getImage(anyObject(String.class))).andReturn(null); replay(defaultTemplate, optionsProvider, templateBuilderProvider, getImageStrategy); TemplateBuilderImpl templateBuilder = new ArbitraryCpuRamTemplateBuilderImpl(locations, new ImageCacheSupplier(images, 60, Atomics.<AuthorizationException>newReference(), Providers.of(getImageStrategy)), hardwares, Suppliers.ofInstance(region), optionsProvider, templateBuilderProvider); templateBuilder.minCores(2); templateBuilder.minRam(1024); Template template = templateBuilder.build(); Hardware hardware = template.getHardware(); assertThat(hardware.getRam()).isEqualTo(2048); assertThat(hardware.getProcessors()).extracting("cores").containsExactly(2.0); assertThat(hardware.getId()).isEqualTo("hardwareId"); }
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = errorMessage) public void testAutoGeneratedHardwareWithMinCoresMinRamAndInvalidMinDisk() { Supplier<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>>ofInstance(ImmutableSet.of(region)); Supplier<Set<? extends Image>> images = Suppliers.<Set<? extends Image>>ofInstance(ImmutableSet.of(image)); Supplier<Set<? extends Hardware>> hardwares = Suppliers.<Set<? extends Hardware>>ofInstance(ImmutableSet .<Hardware>of(hardware)); 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()); expect(getImageStrategy.getImage(anyObject(String.class))).andReturn(null); replay(defaultTemplate, optionsProvider, templateBuilderProvider, getImageStrategy); TemplateBuilderImpl templateBuilder = new ArbitraryCpuRamTemplateBuilderImpl(locations, new ImageCacheSupplier(images, 60, Atomics.<AuthorizationException>newReference(), Providers.of(getImageStrategy)), hardwares, Suppliers.ofInstance(region), optionsProvider, templateBuilderProvider); templateBuilder.minCores(2); templateBuilder.minRam(4096); templateBuilder.minDisk(-100f); Template template = templateBuilder.build(); Hardware hardware = template.getHardware(); assertThat(hardware.getId()).isEqualTo("automatic:cores=2.0;ram=4096;disk=-100"); }
if (logger.isTraceEnabled()) logger.trace("<< matched images(%s)", transform(matchingImages, imageToId)); List<? extends Image> maxImages = multiMax(DEFAULT_IMAGE_ORDERING, matchingImages); if (logger.isTraceEnabled()) logger.trace("<< best images(%s)", transform(maxImages, imageToId)); return maxImages.get(maxImages.size() - 1); } catch (NoSuchElementException exception) { throwNoSuchElementExceptionAfterLoggingImageIds(format("no image matched params: %s", toString()), supportedImages); assert false;
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = errorMessage) public void testOnlyRam() { Supplier<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet.of(region)); Supplier<Set<? extends Image>> images = Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet.of(image)); Supplier<Set<? extends Hardware>> hardwares = Suppliers.<Set<? extends Hardware>> ofInstance(ImmutableSet .<Hardware> of(hardware)); 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()); expect(getImageStrategy.getImage(anyObject(String.class))).andReturn(null); replay(defaultTemplate, optionsProvider, templateBuilderProvider, getImageStrategy); TemplateBuilderImpl templateBuilder = new ArbitraryCpuRamTemplateBuilderImpl(locations, new ImageCacheSupplier(images, 60, Atomics.<AuthorizationException>newReference(), Providers.of(getImageStrategy)), hardwares, Suppliers.ofInstance(region), optionsProvider, templateBuilderProvider); templateBuilder.minRam(4096); templateBuilder.build(); }
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = errorMessage) public void testOnlyCores() { Supplier<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet .of(region)); Supplier<Set<? extends Image>> images = Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet.of(image)); Supplier<Set<? extends Hardware>> hardwares = Suppliers.<Set<? extends Hardware>> ofInstance(ImmutableSet .<Hardware> of(hardware)); 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()); expect(getImageStrategy.getImage(anyObject(String.class))).andReturn(null); replay(defaultTemplate, optionsProvider, templateBuilderProvider, getImageStrategy); TemplateBuilderImpl templateBuilder = new ArbitraryCpuRamTemplateBuilderImpl(locations, new ImageCacheSupplier(images, 60, Atomics.<AuthorizationException>newReference(), Providers.of(getImageStrategy)), hardwares, Suppliers.ofInstance(region), optionsProvider, templateBuilderProvider); templateBuilder.minCores(4); templateBuilder.build(); }
/** * {@inheritDoc} */ @Override public TemplateBuilder fromHardware(Hardware hardware) { if (currentLocationWiderThan(hardware.getLocation())) this.location = hardware.getLocation(); this.minCores = getCores(hardware); this.minRam = hardware.getRam(); this.minDisk = getSpace(hardware); this.hypervisor = hardware.getHypervisor(); return this; }
@SuppressWarnings("unchecked") @Test public void testOptionsUsesDefaultTemplateBuilder() { TemplateOptions options = provideTemplateOptions(); TemplateOptions from = provideTemplateOptions(); Supplier<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet .<Location> of()); Supplier<Set<? extends Image>> images = Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet.<Image> of()); Supplier<Set<? extends Hardware>> hardwares = Suppliers.<Set<? extends Hardware>> ofInstance(ImmutableSet .<Hardware> of()); Location defaultLocation = createMock(Location.class); Provider<TemplateOptions> optionsProvider = createMock(Provider.class); Provider<TemplateBuilder> templateBuilderProvider = createMock(Provider.class); TemplateBuilder defaultTemplate = createMock(TemplateBuilder.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); TemplateBuilderImpl template = createTemplateBuilder(null, locations, images, hardwares, defaultLocation, optionsProvider, templateBuilderProvider); template.options(options).build(); verify(defaultTemplate); verify(defaultLocation); verify(optionsProvider); verify(templateBuilderProvider); }
@SuppressWarnings("unchecked") @Test public void testNothingUsesDefaultTemplateBuilder() { Supplier<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet .<Location> of()); Supplier<Set<? extends Image>> images = Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet.<Image> of()); Supplier<Set<? extends Hardware>> hardwares = Suppliers.<Set<? extends Hardware>> ofInstance(ImmutableSet .<Hardware> of()); Location defaultLocation = createMock(Location.class); 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, defaultLocation, optionsProvider, templateBuilderProvider, getImageStrategy); TemplateBuilderImpl template = createTemplateBuilder(null, locations, images, hardwares, defaultLocation, optionsProvider, templateBuilderProvider, getImageStrategy); template.build(); verify(defaultTemplate, defaultLocation, optionsProvider, templateBuilderProvider, getImageStrategy); }
protected Hardware findHardwareWithId(Set<? extends Hardware> hardwaresToSearch) { try { return super.findHardwareWithId(hardwaresToSearch); } catch (NoSuchElementException ex) { if (isAutomaticId(hardwareId)) { AutomaticHardwareIdSpec spec = parseId(hardwareId); return automaticHardware(spec.getCores(), spec.getRam(), spec.getDisk()); } else { throw ex; } } }
if (nothingChangedExceptOptions()) { TemplateBuilder defaultTemplate = defaultTemplateProvider.get(); if (options != null) options = optionsProvider.get(); logger.debug(">> searching params(%s)", this); Set<? extends Image> images = getImages(); checkState(!images.isEmpty(), "no images present!"); Set<? extends Hardware> hardwaresToSearch = hardwares.get(); image = loadImageWithId(images); if (currentLocationWiderThan(image.getLocation())) this.location = image.getLocation(); hardware = findHardwareWithId(hardwaresToSearch); if (currentLocationWiderThan(hardware.getLocation())) this.location = hardware.getLocation(); 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));
/** * @throws NoSuchElementException * if the image is not found */ @Override protected Image resolveImage(Hardware size, Iterable<? extends Image> supportedImages) { try { return super.resolveImage(size, supportedImages); } catch (NoSuchElementException e) { Image returnVal = lazyImageProvider.get(); if (returnVal != null) return returnVal; throw e; } }