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,14 +34,23 @@ 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>> {
private String yamlDescriptor;
@Inject
public ImageFromYamlString(Supplier<String> yamlDescriptorSupplier) {
this.yamlDescriptor = yamlDescriptorSupplier.get();
checkNotNull(yamlDescriptor, "yaml descriptor");
checkState(!yamlDescriptor.equals(""), "yaml descriptor is empty");
}
/**
* Type-safe config class for YAML
@ -51,10 +61,7 @@ public class ImageFromYamlString implements Function<String, Map<Image, YamlImag
}
@Override
public Map<Image, YamlImage> apply(String source) {
checkNotNull(source, "yaml descriptor");
checkState(!source.equals(""),"yaml descriptor is empty");
public Map<Image, YamlImage> get() {
Constructor constructor = new Constructor(Config.class);
@ -63,7 +70,7 @@ public class ImageFromYamlString implements Function<String, Map<Image, YamlImag
constructor.addTypeDescription(imageDesc);
Yaml yaml = new Yaml(constructor);
Config config = (Config) yaml.load(source);
Config config = (Config) yaml.load(yamlDescriptor);
checkState(config != null, "missing config: class");
checkState(config.images != null, "missing images: collection");

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;
/**
@ -42,19 +43,25 @@ public class ImageFromYamlStringTest {
.id("myTestId")
.name("ubuntu-11.04-server-i386")
.description("ubuntu 11.04 server (i386)")
.operatingSystem(OperatingSystem.builder().description("ubuntu").family(OsFamily.UBUNTU)
.operatingSystem(
OperatingSystem.builder().description("ubuntu").family(OsFamily.UBUNTU)
.version("11.04").build()).build();
@Test
public void testNodesParse() throws Exception {
StringBuilder yamlFileLines = new StringBuilder();
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));
}
}