yaml desc reader added to compute adapter

This commit is contained in:
David Ribeiro Alves 2012-02-27 15:38:35 +00:00
parent 2fc8e372ea
commit 80f6b65d4b
11 changed files with 91 additions and 35 deletions

View File

@ -102,6 +102,17 @@
<version>0.9.29</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.3.1</version>
<exclusions>
<exclusion>
<artifactId>snakeyaml</artifactId>
<groupId>org.yaml</groupId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<profiles>

View File

@ -71,7 +71,12 @@ public class VirtualBoxPropertiesBuilder extends PropertiesBuilder {
+ "initrd=/install/initrd.gz -- <Enter>");
properties.put(VIRTUALBOX_WORKINGDIR, System.getProperty("user.home") + File.separator
+ System.getProperty("test.virtualbox.workingDir", "jclouds-virtualbox-test"));
+ System.getProperty("test.virtualbox.workingDir", ".jclouds-virtualbox"));
properties.put(VIRTUALBOX_IMAGES_DESCRIPTOR,
System.getProperty("user.home") + File.separator
+ System.getProperty("test.virtualbox.image.descriptor.yaml", "images.yaml"));
properties.put(VIRTUALBOX_PRECONFIGURATION_URL, "http://10.0.2.2:8080/src/test/resources/preseed.cfg");

View File

@ -21,9 +21,10 @@ package org.jclouds.virtualbox.compute;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.Iterables.filter;
import static com.google.common.collect.Iterables.transform;
import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_IMAGE_PREFIX;
import java.io.InputStream;
import javax.inject.Inject;
import org.jclouds.compute.ComputeServiceAdapter;
@ -42,6 +43,7 @@ import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.base.Supplier;
import com.google.common.base.Throwables;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.inject.Singleton;
@ -58,12 +60,18 @@ public class VirtualBoxComputeServiceAdapter implements ComputeServiceAdapter<IM
private final Supplier<VirtualBoxManager> manager;
private final Function<IMachine, Image> iMachineToImage;
private final Function<String, LoadingCache<String, Image>> images;
private final Supplier<String> imagesDescSupplier;
@Inject
public VirtualBoxComputeServiceAdapter(Supplier<VirtualBoxManager> manager,
Function<IMachine, Image> iMachineToImage) {
Function<IMachine, Image> iMachineToImage,
Function<String, LoadingCache<String, Image>> images,
Supplier<String> imagesStreamSupplier) {
this.iMachineToImage = iMachineToImage;
this.manager = checkNotNull(manager, "manager");
this.images = images;
this.imagesDescSupplier = imagesStreamSupplier;
}
@Override
@ -89,10 +97,10 @@ public class VirtualBoxComputeServiceAdapter implements ComputeServiceAdapter<IM
return imageMachines();
}
@Override
public Iterable<Image> listImages() {
return transform(imageMachines(), iMachineToImage);
}
@Override
public Iterable<Image> listImages() {
return images.apply(imagesDescSupplier.get()).asMap().values();
}
private Iterable<IMachine> imageMachines() {
final Predicate<? super IMachine> imagePredicate = new Predicate<IMachine>() {

View File

@ -0,0 +1,5 @@
package org.jclouds.virtualbox.config;
public class LocalFileYamlStreamSupplier {
}

View File

@ -54,6 +54,7 @@ import org.jclouds.virtualbox.functions.IMachineToHardware;
import org.jclouds.virtualbox.functions.IMachineToImage;
import org.jclouds.virtualbox.functions.IMachineToNodeMetadata;
import org.jclouds.virtualbox.functions.IMachineToSshClient;
import org.jclouds.virtualbox.functions.admin.ImageFromYamlString;
import org.jclouds.virtualbox.functions.admin.StartJettyIfNotAlreadyRunning;
import org.jclouds.virtualbox.functions.admin.StartVBoxIfNotAlreadyRunning;
import org.jclouds.virtualbox.predicates.SshResponds;
@ -106,6 +107,8 @@ public class VirtualBoxComputeServiceContextModule extends
}).to((Class) StartJettyIfNotAlreadyRunning.class);
bind(new TypeLiteral<Supplier<VirtualBoxManager>>() {
}).to((Class) StartVBoxIfNotAlreadyRunning.class);
bind(new TypeLiteral<Function<InputStream, LoadingCache<String, Image>>>() {
}).to((Class) ImageFromYamlString.class);
// for byon
bind(new TypeLiteral<Function<URI, InputStream>>() {
}).to(SupplyFromProviderURIOrNodesProperty.class);

View File

@ -34,6 +34,8 @@ public interface VirtualBoxConstants {
public static final String VIRTUALBOX_INSTALLATION_KEY_SEQUENCE = "jclouds.virtualbox.installationkeysequence";
public static final String VIRTUALBOX_WORKINGDIR = "jclouds.virtualbox.workingdir";
public static final String VIRTUALBOX_IMAGES_DESCRIPTOR = "jclouds.virtualbox.image.descriptor.yaml";
public static final String VIRTUALBOX_ISO_URL = "jclouds.virtualbox.isourl";

View File

@ -21,7 +21,6 @@ package org.jclouds.virtualbox.functions.admin;
import static com.google.common.base.Preconditions.checkState;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
@ -29,7 +28,6 @@ import javax.inject.Singleton;
import org.jclouds.compute.domain.Image;
import org.jclouds.virtualbox.domain.YamlImage;
import org.yaml.snakeyaml.Loader;
import org.yaml.snakeyaml.TypeDescription;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;
@ -47,7 +45,7 @@ import com.google.common.collect.Maps;
* @author Andrea Turli
*/
@Singleton
public class ImageFromYamlStream implements Function<InputStream, LoadingCache<String, Image>> {
public class ImageFromYamlString implements Function<String, LoadingCache<String, Image>> {
/**
* Type-safe config class for YAML
@ -57,13 +55,8 @@ public class ImageFromYamlStream implements Function<InputStream, LoadingCache<S
public List<YamlImage> images;
}
private Object construct(String data) {
Yaml yaml = new Yaml();
return yaml.load(data);
}
@Override
public LoadingCache<String, Image> apply(InputStream source) {
public LoadingCache<String, Image> apply(String source) {
Constructor constructor = new Constructor(Config.class);
@ -71,10 +64,11 @@ public class ImageFromYamlStream implements Function<InputStream, LoadingCache<S
imageDesc.putListPropertyType("images", String.class);
constructor.addTypeDescription(imageDesc);
Yaml yaml = new Yaml(new Loader(constructor));
Yaml yaml = new Yaml(constructor);
Config config = (Config) yaml.load(source);
checkState(config != null, "missing config: class");
checkState(config.images != null, "missing images: collection");
Map<String, Image> backingMap = Maps.uniqueIndex(Iterables.transform(config.images, YamlImage.toImage),
new Function<Image, String>() {

View File

@ -96,7 +96,7 @@ public class BaseVirtualBoxClientLiveTest extends BaseVersionedServiceLiveTest {
@BeforeClass(groups = "live")
public void setupClient() {
setupCredentials();
Properties overrides = setupProperties();
Properties overrides = new VirtualBoxPropertiesBuilder(setupProperties()).build();
CacheNodeStoreModule hostModule = new CacheNodeStoreModule(
ImmutableMap.of(

View File

@ -38,6 +38,7 @@ import org.jclouds.compute.reference.ComputeServiceConstants;
import org.jclouds.json.Json;
import org.jclouds.json.config.GsonModule;
import org.jclouds.virtualbox.functions.IMachineToImage;
import org.jclouds.virtualbox.functions.admin.ImageFromYamlString;
import org.testng.annotations.Test;
import org.virtualbox_4_1.IGuestOSType;
import org.virtualbox_4_1.IMachine;
@ -45,6 +46,7 @@ import org.virtualbox_4_1.IVirtualBox;
import org.virtualbox_4_1.VirtualBoxManager;
import com.google.common.base.Function;
import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import com.google.common.collect.Iterators;
import com.google.inject.Guice;
@ -83,11 +85,25 @@ public class VirtualBoxComputeServiceAdapterTest {
replay(manager, vBox, clonedMachine, imageMachine, osType);
Function<IMachine, Image> iMachineToImage = new IMachineToImage(Suppliers.ofInstance(manager), osMap);
VirtualBoxComputeServiceAdapter adapter = new VirtualBoxComputeServiceAdapter(Suppliers.ofInstance(manager), iMachineToImage);
VirtualBoxComputeServiceAdapter adapter = new VirtualBoxComputeServiceAdapter(Suppliers.ofInstance(manager), iMachineToImage, new ImageFromYamlString(), new Supplier<String>() {
@Override
public String get() {
return "images:\n" +
" - id: myTestId\n" +
" name: ubuntu-11.04-server-i386\n" +
" description: ubuntu 11.04 server (i386)\n" +
" os_arch: x86\n" +
" os_family: ubuntu\n" +
" os_description: ubuntu\n" +
" os_version: 11.04\n" +
" iso: http://releases.ubuntu.com/11.04/ubuntu-11.04-server-i386.iso";
}
});
Iterator<Image> iterator = adapter.listImages().iterator();
Image image = Iterators.getOnlyElement(iterator);
assertEquals(image.getDescription(), VIRTUALBOX_IMAGE_PREFIX + "ubuntu-10.04");
assertEquals(image.getDescription(), "ubuntu 11.04 server (i386)");
}
}

View File

@ -22,7 +22,9 @@ package org.jclouds.virtualbox.functions.admin;
import static org.testng.Assert.assertEquals;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.commons.io.IOUtils;
import org.jclouds.compute.domain.Image;
import org.jclouds.compute.domain.ImageBuilder;
import org.jclouds.compute.domain.OperatingSystem;
@ -37,20 +39,23 @@ import com.google.common.collect.ImmutableMap;
@Test(groups = "unit")
public class ImageFromYamlStreamTest {
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 {
InputStream is = getClass().getResourceAsStream("/testImages.yaml");
ImageFromYamlStream parser = new ImageFromYamlStream();
assertEquals(parser.apply(is).asMap(), ImmutableMap.of(TEST1.getId(), TEST1));
is.close();
}
@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");
}
ImageFromYamlString parser = new ImageFromYamlString();
assertEquals(parser.apply(yamlFileLines.toString()).asMap(), ImmutableMap.of(TEST1.getId(), TEST1));
}
}

View File

@ -31,9 +31,16 @@
<Pattern>%d %-5p [%c] (%t) %m%n</Pattern>
</layout>
</appender>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<Pattern>%-4r [%thread] %-5level - %msg%n</Pattern>
</encoder>
</appender>
<root>
<level value="warn" />
<level value="debug" />
<appender-ref ref="CONSOLE" />
</root>
<logger name="org.jclouds">