@Test public void createImageRegistersInCacheAndAddsCredentials() { ImageCacheSupplier imageCache = createMock(ImageCacheSupplier.class); ImageExtension delegate = createMock(ImageExtension.class); AddDefaultCredentialsToImage credsToImage = createMock(AddDefaultCredentialsToImage.class); ImageTemplate template = new ImageTemplateImpl("test") { }; Image result = new ImageBuilder().id("test") .operatingSystem(OperatingSystem.builder().description("test").build()).status(Status.AVAILABLE).build(); LoginCredentials credentials = LoginCredentials.builder().user("jclouds").password("pass").build(); Image withCredentials = ImageBuilder.fromImage(result).defaultCredentials(credentials).build(); expect(delegate.createImage(template)).andReturn(immediateFuture(result)); expect(credsToImage.apply(result)).andReturn(withCredentials); imageCache.registerImage(withCredentials); expectLastCall(); replay(delegate, imageCache, credsToImage); new DelegatingImageExtension(imageCache, delegate, credsToImage, null).createImage(template); verify(delegate, imageCache, credsToImage); }
public ListenableFuture<Image> createImage(final ImageTemplate template) { ListenableFuture<Image> future = delegate.createImage(template); future = Futures.transform(future, new Function<Image, Image>() { @Override public Image apply(Image input) { if (input.getDefaultCredentials() != null) { return input; } if (template instanceof CloneImageTemplate) { final CloneImageTemplate cloneImageTemplate = (CloneImageTemplate) template; Credentials nodeCredentials = credentialStore.get("node#" + cloneImageTemplate.getSourceNodeId()); if (nodeCredentials != null) { logger.info(">> Adding node(%s) credentials to image(%s)...", cloneImageTemplate.getSourceNodeId(), cloneImageTemplate.getName()); return ImageBuilder.fromImage(input) .defaultCredentials(LoginCredentials.fromCredentials(nodeCredentials)).build(); } } logger.info(">> Adding default image credentials to image(%s)...", template.getName()); return addDefaultCredentialsToImage.apply(input); } }); Futures.addCallback(future, new FutureCallback<Image>() { @Override public void onSuccess(Image result) { imageCache.registerImage(result); } @Override public void onFailure(Throwable t) { } }); return future; }
DelegatingImageExtension implements ImageExtension { public ListenableFuture<Image> createImage(final ImageTemplate template) { ListenableFuture<Image> future = delegate.createImage(template); future = Futures.transform(future, new Function<Image, Image>() { @Override public Image apply(Image input) { if (input.getDefaultCredentials() != null) { return input; } if (template instanceof CloneImageTemplate) { final CloneImageTemplate cloneImageTemplate = (CloneImageTemplate) template; Credentials nodeCredentials = credentialStore.get("node#" + cloneImageTemplate.getSourceNodeId()); if (nodeCredentials != null) { logger.info(">> Adding node(%s) credentials to image(%s)...", cloneImageTemplate.getSourceNodeId(), cloneImageTemplate.getName()); return ImageBuilder.fromImage(input) .defaultCredentials(LoginCredentials.fromCredentials(nodeCredentials)).build(); } } logger.info(">> Adding default image credentials to image(%s)...", template.getName()); return addDefaultCredentialsToImage.apply(input); } }); Futures.addCallback(future, new FutureCallback<Image>() { @Override public void onSuccess(Image result) { imageCache.registerImage(result); } @Override public void onFailure(Throwable t) { } }); return future; } }
DelegatingImageExtension implements ImageExtension { public ListenableFuture<Image> createImage(final ImageTemplate template) { ListenableFuture<Image> future = delegate.createImage(template); future = Futures.transform(future, new Function<Image, Image>() { @Override public Image apply(Image input) { if (input.getDefaultCredentials() != null) { return input; } if (template instanceof CloneImageTemplate) { final CloneImageTemplate cloneImageTemplate = (CloneImageTemplate) template; Credentials nodeCredentials = credentialStore.get("node#" + cloneImageTemplate.getSourceNodeId()); if (nodeCredentials != null) { logger.info(">> Adding node(%s) credentials to image(%s)...", cloneImageTemplate.getSourceNodeId(), cloneImageTemplate.getName()); return ImageBuilder.fromImage(input) .defaultCredentials(LoginCredentials.fromCredentials(nodeCredentials)).build(); } } logger.info(">> Adding default image credentials to image(%s)...", template.getName()); return addDefaultCredentialsToImage.apply(input); } }); Futures.addCallback(future, new FutureCallback<Image>() { @Override public void onSuccess(Image result) { imageCache.registerImage(result); } @Override public void onFailure(Throwable t) { } }); return future; } @Inject  DelegatingImageExtension(@Assisted ImageCacheSupplier imageCache, @Assisted ImageExtension delegate,
         AddDefaultCredentialsToImage addDefaultCredentialsToImage, Map<String, Credentials> credentialStore); }
DelegatingImageExtension implements ImageExtension { public ListenableFuture<Image> createImage(final ImageTemplate template) { ListenableFuture<Image> future = delegate.createImage(template); future = Futures.transform(future, new Function<Image, Image>() { @Override public Image apply(Image input) { if (input.getDefaultCredentials() != null) { return input; } if (template instanceof CloneImageTemplate) { final CloneImageTemplate cloneImageTemplate = (CloneImageTemplate) template; Credentials nodeCredentials = credentialStore.get("node#" + cloneImageTemplate.getSourceNodeId()); if (nodeCredentials != null) { logger.info(">> Adding node(%s) credentials to image(%s)...", cloneImageTemplate.getSourceNodeId(), cloneImageTemplate.getName()); return ImageBuilder.fromImage(input) .defaultCredentials(LoginCredentials.fromCredentials(nodeCredentials)).build(); } } logger.info(">> Adding default image credentials to image(%s)...", template.getName()); return addDefaultCredentialsToImage.apply(input); } }); Futures.addCallback(future, new FutureCallback<Image>() { @Override public void onSuccess(Image result) { imageCache.registerImage(result); } @Override public void onFailure(Throwable t) { } }); return future; } @Inject  DelegatingImageExtension(@Assisted ImageCacheSupplier imageCache, @Assisted ImageExtension delegate,
         AddDefaultCredentialsToImage addDefaultCredentialsToImage, Map<String, Credentials> credentialStore); ImageTemplate buildImageTemplateFromNode(String name, String id); ListenableFuture<Image> createImage(final ImageTemplate template); boolean deleteImage(String id); }
DelegatingImageExtension implements ImageExtension { public ListenableFuture<Image> createImage(final ImageTemplate template) { ListenableFuture<Image> future = delegate.createImage(template); future = Futures.transform(future, new Function<Image, Image>() { @Override public Image apply(Image input) { if (input.getDefaultCredentials() != null) { return input; } if (template instanceof CloneImageTemplate) { final CloneImageTemplate cloneImageTemplate = (CloneImageTemplate) template; Credentials nodeCredentials = credentialStore.get("node#" + cloneImageTemplate.getSourceNodeId()); if (nodeCredentials != null) { logger.info(">> Adding node(%s) credentials to image(%s)...", cloneImageTemplate.getSourceNodeId(), cloneImageTemplate.getName()); return ImageBuilder.fromImage(input) .defaultCredentials(LoginCredentials.fromCredentials(nodeCredentials)).build(); } } logger.info(">> Adding default image credentials to image(%s)...", template.getName()); return addDefaultCredentialsToImage.apply(input); } }); Futures.addCallback(future, new FutureCallback<Image>() { @Override public void onSuccess(Image result) { imageCache.registerImage(result); } @Override public void onFailure(Throwable t) { } }); return future; } @Inject  DelegatingImageExtension(@Assisted ImageCacheSupplier imageCache, @Assisted ImageExtension delegate,
         AddDefaultCredentialsToImage addDefaultCredentialsToImage, Map<String, Credentials> credentialStore); ImageTemplate buildImageTemplateFromNode(String name, String id); ListenableFuture<Image> createImage(final ImageTemplate template); boolean deleteImage(String id);  }