refactored image from yaml creation

This commit is contained in:
David Ribeiro Alves 2012-02-28 19:11:37 +00:00
parent c015f9f699
commit eab826fd42
5 changed files with 64 additions and 50 deletions

View File

@ -66,11 +66,10 @@ public class VirtualBoxComputeServiceAdapter implements ComputeServiceAdapter<IM
@Inject
public VirtualBoxComputeServiceAdapter(Supplier<VirtualBoxManager> manager,
Function<String, Map<Image, YamlImage>> imagesMapper,
LoadingCache<Image, IMachine> mastersLoader, Supplier<String> imagesDescSupplier,
Supplier<Map<Image, YamlImage>> imagesMapper, LoadingCache<Image, IMachine> mastersLoader,
Function<IMachine, NodeAndInitialCredentials<IMachine>> cloneCreator) {
this.manager = checkNotNull(manager, "manager");
this.images = imagesMapper.apply(imagesDescSupplier.get());
this.images = imagesMapper.get();
this.mastersLoader = mastersLoader;
this.cloneCreator = cloneCreator;
}

View File

@ -115,7 +115,7 @@ public class VirtualBoxComputeServiceContextModule extends
bind(new TypeLiteral<Supplier<VirtualBoxManager>>() {
}).to((Class) StartVBoxIfNotAlreadyRunning.class);
// the yaml config to image mapper
bind(new TypeLiteral<Function<String, Map<Image, YamlImage>>>() {
bind(new TypeLiteral<Supplier<Map<Image, YamlImage>>>() {
}).to((Class) ImageFromYamlString.class);
// the yaml config provider
bind(new TypeLiteral<Supplier<String>>() {
@ -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");
}

View File

@ -73,8 +73,8 @@ public class MasterImages extends AbstractLoadingCache<Image, IMachine> {
@Inject
public MasterImages(@Named(Constants.PROPERTY_BUILD_VERSION) String version,
Function<MasterSpec, IMachine> masterLoader, ValueOfConfigurationKeyOrNull cfg, Supplier<String> yamlSupplier,
Function<String, Map<Image, YamlImage>> yamlMapper) {
Function<MasterSpec, IMachine> masterLoader, ValueOfConfigurationKeyOrNull cfg,
Supplier<Map<Image, YamlImage>> yamlMapper) {
checkNotNull(version, "version");
this.mastersLoader = masterLoader;
this.cfg = cfg;
@ -85,7 +85,7 @@ public class MasterImages extends AbstractLoadingCache<Image, IMachine> {
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));
}

View File

@ -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<String, Map<Image, YamlImage>> {
public class ImageFromYamlString implements Supplier<Map<Image, YamlImage>> {
/**
* Type-safe config class for YAML
*
*/
public static class Config {
public List<YamlImage> images;
}
private String yamlDescriptor;
@Override
public Map<Image, YamlImage> apply(String source) {
checkNotNull(source, "yaml descriptor");
checkState(!source.equals(""),"yaml descriptor is empty");
@Inject
public ImageFromYamlString(Supplier<String> 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<YamlImage> images;
}
TypeDescription imageDesc = new TypeDescription(YamlImage.class);
imageDesc.putListPropertyType("images", String.class);
constructor.addTypeDescription(imageDesc);
@Override
public Map<Image, YamlImage> 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<Image, YamlImage> 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<Image, YamlImage> backingMap = Maps.newLinkedHashMap();
for (YamlImage yamlImage : config.images) {
backingMap.put(YamlImage.toImage.apply(yamlImage), yamlImage);
}
return backingMap;
}
}

View File

@ -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<String>() {
@Override
public String get() {
return yamlFileLines.toString();
}
});
assertEquals(parser.get(), ImmutableMap.of(TEST1.getId(), TEST1));
}
}