From eab826fd429230f130491dab5f1da45008341bb4 Mon Sep 17 00:00:00 2001 From: David Ribeiro Alves Date: Tue, 28 Feb 2012 19:11:37 +0000 Subject: [PATCH] refactored image from yaml creation --- .../VirtualBoxComputeServiceAdapter.java | 5 +- ...VirtualBoxComputeServiceContextModule.java | 3 +- .../virtualbox/functions/MasterImages.java | 6 +- .../functions/admin/ImageFromYamlString.java | 63 ++++++++++--------- .../admin/ImageFromYamlStringTest.java | 37 ++++++----- 5 files changed, 64 insertions(+), 50 deletions(-) diff --git a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/compute/VirtualBoxComputeServiceAdapter.java b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/compute/VirtualBoxComputeServiceAdapter.java index dbaf730129..36ecd137a2 100644 --- a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/compute/VirtualBoxComputeServiceAdapter.java +++ b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/compute/VirtualBoxComputeServiceAdapter.java @@ -66,11 +66,10 @@ public class VirtualBoxComputeServiceAdapter implements ComputeServiceAdapter manager, - Function> imagesMapper, - LoadingCache mastersLoader, Supplier imagesDescSupplier, + Supplier> imagesMapper, LoadingCache mastersLoader, Function> cloneCreator) { this.manager = checkNotNull(manager, "manager"); - this.images = imagesMapper.apply(imagesDescSupplier.get()); + this.images = imagesMapper.get(); this.mastersLoader = mastersLoader; this.cloneCreator = cloneCreator; } diff --git a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/config/VirtualBoxComputeServiceContextModule.java b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/config/VirtualBoxComputeServiceContextModule.java index 9efce73093..e62440f41a 100644 --- a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/config/VirtualBoxComputeServiceContextModule.java +++ b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/config/VirtualBoxComputeServiceContextModule.java @@ -115,7 +115,7 @@ public class VirtualBoxComputeServiceContextModule extends bind(new TypeLiteral>() { }).to((Class) StartVBoxIfNotAlreadyRunning.class); // the yaml config to image mapper - bind(new TypeLiteral>>() { + bind(new TypeLiteral>>() { }).to((Class) ImageFromYamlString.class); // the yaml config provider bind(new TypeLiteral>() { @@ -188,6 +188,7 @@ public class VirtualBoxComputeServiceContextModule extends @Override protected TemplateBuilder provideTemplate(Injector injector, TemplateBuilder template) { + injector.getInstance(Supplier.class); return template.osFamily(OsFamily.UBUNTU).osVersionMatches("11.04"); } diff --git a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/MasterImages.java b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/MasterImages.java index eaad59d69e..b3da6a5419 100644 --- a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/MasterImages.java +++ b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/MasterImages.java @@ -73,8 +73,8 @@ public class MasterImages extends AbstractLoadingCache { @Inject public MasterImages(@Named(Constants.PROPERTY_BUILD_VERSION) String version, - Function masterLoader, ValueOfConfigurationKeyOrNull cfg, Supplier yamlSupplier, - Function> yamlMapper) { + Function masterLoader, ValueOfConfigurationKeyOrNull cfg, + Supplier> yamlMapper) { checkNotNull(version, "version"); this.mastersLoader = masterLoader; this.cfg = cfg; @@ -85,7 +85,7 @@ public class MasterImages extends AbstractLoadingCache { wdFile.mkdirs(); } this.adminDisk = workingDir + "/testadmin.vdi"; - this.imageMapping = yamlMapper.apply(yamlSupplier.get()); + this.imageMapping = yamlMapper.get(); this.guestAdditionsIso = String.format("%s/VBoxGuestAdditions_%s.iso", workingDir, Iterables.get(Splitter.on('r').split(version), 0)); } diff --git a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/admin/ImageFromYamlString.java b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/admin/ImageFromYamlString.java index 480adde699..baff6ee779 100644 --- a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/admin/ImageFromYamlString.java +++ b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/admin/ImageFromYamlString.java @@ -25,6 +25,7 @@ import static com.google.common.base.Preconditions.checkState; import java.util.List; import java.util.Map; +import javax.inject.Inject; import javax.inject.Singleton; import org.jclouds.compute.domain.Image; @@ -33,45 +34,51 @@ import org.yaml.snakeyaml.TypeDescription; import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.constructor.Constructor; -import com.google.common.base.Function; +import com.google.common.base.Supplier; import com.google.common.collect.Maps; /** * @author Andrea Turli */ @Singleton -public class ImageFromYamlString implements Function> { +public class ImageFromYamlString implements Supplier> { - /** - * Type-safe config class for YAML - * - */ - public static class Config { - public List images; - } + private String yamlDescriptor; - @Override - public Map apply(String source) { - - checkNotNull(source, "yaml descriptor"); - checkState(!source.equals(""),"yaml descriptor is empty"); + @Inject + public ImageFromYamlString(Supplier yamlDescriptorSupplier) { + this.yamlDescriptor = yamlDescriptorSupplier.get(); + checkNotNull(yamlDescriptor, "yaml descriptor"); + checkState(!yamlDescriptor.equals(""), "yaml descriptor is empty"); + } - Constructor constructor = new Constructor(Config.class); + /** + * Type-safe config class for YAML + * + */ + public static class Config { + public List images; + } - TypeDescription imageDesc = new TypeDescription(YamlImage.class); - imageDesc.putListPropertyType("images", String.class); - constructor.addTypeDescription(imageDesc); + @Override + public Map get() { - Yaml yaml = new Yaml(constructor); - Config config = (Config) yaml.load(source); - checkState(config != null, "missing config: class"); - checkState(config.images != null, "missing images: collection"); + Constructor constructor = new Constructor(Config.class); - Map backingMap = Maps.newLinkedHashMap(); - for (YamlImage yamlImage : config.images) { - backingMap.put(YamlImage.toImage.apply(yamlImage), yamlImage); - } - return backingMap; - } + TypeDescription imageDesc = new TypeDescription(YamlImage.class); + imageDesc.putListPropertyType("images", String.class); + constructor.addTypeDescription(imageDesc); + + Yaml yaml = new Yaml(constructor); + Config config = (Config) yaml.load(yamlDescriptor); + checkState(config != null, "missing config: class"); + checkState(config.images != null, "missing images: collection"); + + Map backingMap = Maps.newLinkedHashMap(); + for (YamlImage yamlImage : config.images) { + backingMap.put(YamlImage.toImage.apply(yamlImage), yamlImage); + } + return backingMap; + } } \ No newline at end of file diff --git a/labs/virtualbox/src/test/java/org/jclouds/virtualbox/functions/admin/ImageFromYamlStringTest.java b/labs/virtualbox/src/test/java/org/jclouds/virtualbox/functions/admin/ImageFromYamlStringTest.java index 8d2504c55c..431c86111d 100644 --- a/labs/virtualbox/src/test/java/org/jclouds/virtualbox/functions/admin/ImageFromYamlStringTest.java +++ b/labs/virtualbox/src/test/java/org/jclouds/virtualbox/functions/admin/ImageFromYamlStringTest.java @@ -30,6 +30,7 @@ import org.jclouds.compute.domain.OperatingSystem; import org.jclouds.compute.domain.OsFamily; import org.testng.annotations.Test; +import com.google.common.base.Supplier; import com.google.common.collect.ImmutableMap; /** @@ -38,23 +39,29 @@ import com.google.common.collect.ImmutableMap; @Test(groups = "unit") public class ImageFromYamlStringTest { - public static final Image TEST1 = new ImageBuilder() - .id("myTestId") - .name("ubuntu-11.04-server-i386") - .description("ubuntu 11.04 server (i386)") - .operatingSystem(OperatingSystem.builder().description("ubuntu").family(OsFamily.UBUNTU) - .version("11.04").build()).build(); + public static final Image TEST1 = new ImageBuilder() + .id("myTestId") + .name("ubuntu-11.04-server-i386") + .description("ubuntu 11.04 server (i386)") + .operatingSystem( + OperatingSystem.builder().description("ubuntu").family(OsFamily.UBUNTU) + .version("11.04").build()).build(); - @Test - public void testNodesParse() throws Exception { + @Test + public void testNodesParse() throws Exception { - StringBuilder yamlFileLines = new StringBuilder(); - for (Object line : IOUtils.readLines(new InputStreamReader(getClass().getResourceAsStream("/testImages.yaml")))) { - yamlFileLines.append(line).append("\n"); - } + final StringBuilder yamlFileLines = new StringBuilder(); + for (Object line : IOUtils.readLines(new InputStreamReader(getClass().getResourceAsStream("/testImages.yaml")))) { + yamlFileLines.append(line).append("\n"); + } - ImageFromYamlString parser = new ImageFromYamlString(); - assertEquals(parser.apply(yamlFileLines.toString()), ImmutableMap.of(TEST1.getId(), TEST1)); - } + ImageFromYamlString parser = new ImageFromYamlString(new Supplier() { + @Override + public String get() { + return yamlFileLines.toString(); + } + }); + assertEquals(parser.get(), ImmutableMap.of(TEST1.getId(), TEST1)); + } } \ No newline at end of file