diff --git a/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/IMachineToHardware.java b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/IMachineToHardware.java index c145242679..09e592ac4c 100644 --- a/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/IMachineToHardware.java +++ b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/IMachineToHardware.java @@ -51,6 +51,6 @@ public class IMachineToHardware implements Function { hardwareBuilder.ids(vm.getId()); vm.getSessionPid(); hardwareBuilder.is64Bit(is64Bit); - return null; + return hardwareBuilder.build(); } } diff --git a/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/IMachineToImage.java b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/IMachineToImage.java index 54eb8936e9..a8d7be3fba 100644 --- a/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/IMachineToImage.java +++ b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/IMachineToImage.java @@ -21,19 +21,89 @@ package org.jclouds.virtualbox.functions; -import com.google.common.base.Function; +import java.util.NoSuchElementException; + +import javax.inject.Inject; + import org.jclouds.compute.domain.Image; import org.jclouds.compute.domain.ImageBuilder; +import org.jclouds.compute.domain.OperatingSystem; +import org.jclouds.compute.domain.OsFamily; import org.jclouds.javax.annotation.Nullable; +import org.jclouds.virtualbox.VirtualBox; +import org.virtualbox_4_1.IGuestOSType; import org.virtualbox_4_1.IMachine; +import org.virtualbox_4_1.VirtualBoxManager; + +import com.google.common.base.Function; public class IMachineToImage implements Function { - @Override - public Image apply(@Nullable IMachine vm) { - ImageBuilder imageBuilder = new ImageBuilder(); - imageBuilder.ids(vm.getId()); - return null; - } + private static final String UBUNTU = "Ubuntu Linux"; + + private VirtualBoxManager virtualboxManager; + + @Inject + public IMachineToImage(VirtualBoxManager virtualboxManager) { + this.virtualboxManager = virtualboxManager; + } + + @Override + public Image apply(@Nullable IMachine from) { + + Boolean is64Bit = virtualboxManager.getVBox().getGuestOSType(from.getOSTypeId()).getIs64Bit(); + + //Somehow this method gets called with the correct product item. + OsFamily family = osFamily().apply(from); + OperatingSystem os = OperatingSystem.builder() + .description(from.getDescription()) + .family(family) + .version(osVersion().apply(from)) + .is64Bit(is64Bit) + .build(); + + return new ImageBuilder() + .id("" + from.getId()) + .description(from.getDescription()) + .operatingSystem(os) + .build(); + } + + /** + * Parses the item description to determine the OSFamily + * @return the @see OsFamily or OsFamily.UNRECOGNIZED + */ + public static Function osFamily() { + return new Function() { + @Override + public OsFamily apply(IMachine iMachine) { + final String description = iMachine.getDescription(); + if ( description.startsWith(UBUNTU) ) return OsFamily.UBUNTU; + return OsFamily.UNRECOGNIZED; + } + }; + } + + /** + * Parses the item description to determine the os version + * @return the version + * @throws java.util.NoSuchElementException if the version cannot be determined + */ + public static Function osVersion() { + return new Function() { + @Override + public String apply(IMachine iMachine) { + final String description = iMachine.getDescription(); + OsFamily family = osFamily().apply(iMachine); + if(family.equals(OsFamily.UBUNTU)) return parseVersion(description, UBUNTU); + else throw new NoSuchElementException("No os parseVersion for item:" + iMachine); + } + }; + } + + private static String parseVersion(String description, String os) { + String noOsName = description.replaceFirst(os,"").trim(); + return noOsName.split(" ")[0]; + } }