From c7f394a5984f87ef87b77d24d7b7aad51e8e55b6 Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Thu, 31 Jan 2013 11:04:15 -0800 Subject: [PATCH] fix issue #1264 parse smartos in joyent --- .../org/jclouds/compute/domain/OsFamily.java | 15 ++++--- .../functions/DatasetToOperatingSystem.java | 41 ++++++++----------- .../JoyentCloudTemplateBuilderLiveTest.java | 33 +++++++++++---- 3 files changed, 53 insertions(+), 36 deletions(-) diff --git a/compute/src/main/java/org/jclouds/compute/domain/OsFamily.java b/compute/src/main/java/org/jclouds/compute/domain/OsFamily.java index 3d855dd758..fbdea0cc6c 100644 --- a/compute/src/main/java/org/jclouds/compute/domain/OsFamily.java +++ b/compute/src/main/java/org/jclouds/compute/domain/OsFamily.java @@ -20,7 +20,7 @@ import static com.google.common.base.Preconditions.checkNotNull; -import com.google.common.base.CaseFormat; +import static com.google.common.base.CaseFormat.*; /** * Running Operating system @@ -29,7 +29,11 @@ * @author Adrian Cole */ public enum OsFamily { - UNRECOGNIZED, AIX, ARCH, CENTOS, DARWIN, DEBIAN, ESX, FEDORA, FREEBSD, GENTOO, HPUX, LINUX, + UNRECOGNIZED, AIX, ARCH, CENTOS, DARWIN, DEBIAN, ESX, FEDORA, FREEBSD, GENTOO, HPUX, LINUX, + /** + * @see SmartOS + */ + SMARTOS, /** * @see amazon linux ami */ @@ -46,8 +50,9 @@ public enum OsFamily { SIGAR, SLACKWARE, SOLARIS, SUSE, TURBOLINUX, CLOUD_LINUX, UBUNTU, WINDOWS; + public String value() { - return CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_HYPHEN, name()); + return UPPER_UNDERSCORE.to(LOWER_HYPHEN, name()); } @Override @@ -57,9 +62,9 @@ public String toString() { public static OsFamily fromValue(String osFamily) { try { - return valueOf(CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_UNDERSCORE, checkNotNull(osFamily, "osFamily"))); + return valueOf(LOWER_HYPHEN.to(UPPER_UNDERSCORE, checkNotNull(osFamily, "osFamily"))); } catch (IllegalArgumentException e) { return UNRECOGNIZED; } } -} \ No newline at end of file +} diff --git a/labs/joyent-cloudapi/src/main/java/org/jclouds/joyent/cloudapi/v6_5/compute/functions/DatasetToOperatingSystem.java b/labs/joyent-cloudapi/src/main/java/org/jclouds/joyent/cloudapi/v6_5/compute/functions/DatasetToOperatingSystem.java index 7bfdb5c4c2..7353f3c418 100644 --- a/labs/joyent-cloudapi/src/main/java/org/jclouds/joyent/cloudapi/v6_5/compute/functions/DatasetToOperatingSystem.java +++ b/labs/joyent-cloudapi/src/main/java/org/jclouds/joyent/cloudapi/v6_5/compute/functions/DatasetToOperatingSystem.java @@ -18,39 +18,30 @@ */ package org.jclouds.joyent.cloudapi.v6_5.compute.functions; +import static org.jclouds.compute.domain.OsFamily.UNRECOGNIZED; +import static org.jclouds.compute.domain.OsFamily.fromValue; +import static org.jclouds.compute.util.ComputeServiceUtils.parseVersionOrReturnEmptyString; + import java.util.List; import java.util.Map; -import java.util.regex.Pattern; import javax.inject.Inject; -import javax.inject.Named; import org.jclouds.compute.domain.OperatingSystem; import org.jclouds.compute.domain.OperatingSystem.Builder; import org.jclouds.compute.domain.OsFamily; -import org.jclouds.compute.reference.ComputeServiceConstants; -import org.jclouds.compute.util.ComputeServiceUtils; import org.jclouds.joyent.cloudapi.v6_5.domain.Dataset; -import org.jclouds.logging.Logger; import com.google.common.base.Function; import com.google.common.base.Splitter; import com.google.common.collect.ImmutableList; /** - * A function for transforming a cloudApi specific Dataset into a generic - * OperatingSystem object. + * A function for transforming a cloudApi specific Dataset into a generic OperatingSystem object. * * @author Adrian Cole */ public class DatasetToOperatingSystem implements Function { - public static final Pattern DEFAULT_PATTERN = Pattern.compile("(([^ ]*) ([0-9.]+) ?.*)"); - // Windows Machine 2008 R2 x64 - public static final Pattern WINDOWS_PATTERN = Pattern.compile("Windows (.*) (x[86][64])"); - - @javax.annotation.Resource - @Named(ComputeServiceConstants.COMPUTE_LOGGER) - protected Logger logger = Logger.NULL; private final Map> osVersionMap; @@ -63,20 +54,24 @@ public OperatingSystem apply(Dataset from) { Builder builder = OperatingSystem.builder(); builder.name(from.getName()); builder.description(from.getUrn()); - builder.is64Bit(true);//TODO: verify - + builder.is64Bit(true);// TODO: verify + OsFamily family = UNRECOGNIZED; + String version = ""; List pieces = ImmutableList.copyOf(Splitter.on(':').split(from.getUrn())); if (pieces.get(2).indexOf('-') != -1) { List osFamVersion = ImmutableList.copyOf(Splitter.on('-').split(pieces.get(2))); - OsFamily family = OsFamily.fromValue(osFamVersion.get(0)); - builder.family(family); - if (family != OsFamily.UNRECOGNIZED) - builder.version(ComputeServiceUtils.parseVersionOrReturnEmptyString(family, osFamVersion.get(1), - osVersionMap)); + family = fromValue(osFamVersion.get(0)); + if (family != UNRECOGNIZED) + version = osFamVersion.get(1); } else { - builder.family(OsFamily.fromValue(pieces.get(2))); + family = fromValue(pieces.get(2)); } + builder.family(family); + if (family != UNRECOGNIZED) + version = parseVersionOrReturnEmptyString(family, version, osVersionMap); + if ("".equals(version)) + version = from.getVersion(); + builder.version(version); return builder.build(); } - } diff --git a/labs/joyentcloud/src/test/java/org/jclouds/joyent/joyentcloud/JoyentCloudTemplateBuilderLiveTest.java b/labs/joyentcloud/src/test/java/org/jclouds/joyent/joyentcloud/JoyentCloudTemplateBuilderLiveTest.java index 69ddb0350c..53694d74fc 100644 --- a/labs/joyentcloud/src/test/java/org/jclouds/joyent/joyentcloud/JoyentCloudTemplateBuilderLiveTest.java +++ b/labs/joyentcloud/src/test/java/org/jclouds/joyent/joyentcloud/JoyentCloudTemplateBuilderLiveTest.java @@ -18,18 +18,20 @@ */ package org.jclouds.joyent.joyentcloud; +import static com.google.common.base.Predicates.not; +import static org.jclouds.compute.domain.OsFamily.SMARTOS; +import static org.jclouds.compute.domain.OsFamily.UBUNTU; import static org.testng.Assert.assertEquals; +import java.io.IOException; import java.util.Set; -import org.jclouds.compute.domain.OsFamily; import org.jclouds.compute.domain.OsFamilyVersion64Bit; import org.jclouds.compute.domain.Template; import org.jclouds.compute.internal.BaseTemplateBuilderLiveTest; import org.testng.annotations.Test; import com.google.common.base.Predicate; -import com.google.common.base.Predicates; import com.google.common.collect.ImmutableSet; /** @@ -45,13 +47,14 @@ public JoyentCloudTemplateBuilderLiveTest() { @Override protected Predicate defineUnsupportedOperatingSystems() { - return Predicates.not(new Predicate() { + return not(new Predicate() { @Override public boolean apply(OsFamilyVersion64Bit input) { switch (input.family) { case UBUNTU: - return (input.version.equals("") || input.version.equals("10.04")) && input.is64Bit; + return (input.version.equals("") || input.version.equals("10.04") || input.version.equals("12.04")) + && input.is64Bit; case DEBIAN: return input.is64Bit && !input.version.equals("5.0"); case CENTOS: @@ -66,12 +69,26 @@ public boolean apply(OsFamilyVersion64Bit input) { } @Test - public void testTemplateBuilder() { + public void testTemplateBuilderSmartOS() throws IOException { + Template smartTemplate = view.getComputeService().templateBuilder().osFamily(SMARTOS).build(); + assertEquals(smartTemplate.getImage().getOperatingSystem().is64Bit(), true); + assertEquals(smartTemplate.getImage().getOperatingSystem().getVersion(), "1.6.3"); + assertEquals(smartTemplate.getImage().getOperatingSystem().getFamily(), SMARTOS); + assertEquals(smartTemplate.getImage().getName(), "smartos"); + assertEquals(smartTemplate.getImage().getDefaultCredentials().getUser(), "root"); + assertEquals(smartTemplate.getLocation().getId(), "us-east-1"); + assertEquals(smartTemplate.getImage().getLocation().getId(), "us-east-1"); + assertEquals(smartTemplate.getHardware().getLocation().getId(), "us-east-1"); + } + + @Test + @Override + public void testDefaultTemplateBuilder() { Template defaultTemplate = this.view.getComputeService().templateBuilder().build(); assertEquals(defaultTemplate.getImage().getOperatingSystem().is64Bit(), true); - assertEquals(defaultTemplate.getImage().getOperatingSystem().getVersion(), "10.04"); - assertEquals(defaultTemplate.getImage().getOperatingSystem().getFamily(), OsFamily.UBUNTU); - assertEquals(defaultTemplate.getImage().getName(), "ubuntu-10.04"); + assertEquals(defaultTemplate.getImage().getOperatingSystem().getVersion(), "12.04"); + assertEquals(defaultTemplate.getImage().getOperatingSystem().getFamily(), UBUNTU); + assertEquals(defaultTemplate.getImage().getName(), "ubuntu-12.04"); assertEquals(defaultTemplate.getImage().getDefaultCredentials().getUser(), "root"); assertEquals(defaultTemplate.getLocation().getId(), "us-east-1"); assertEquals(defaultTemplate.getImage().getLocation().getId(), "us-east-1");