From 4dbba815caa77ad44d7e8d75fde1692a6810d862 Mon Sep 17 00:00:00 2001 From: Richard Downer Date: Thu, 2 Nov 2017 20:46:01 +0000 Subject: [PATCH] JCLOUDS-1351: improve OS Family parsing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Modifies OsFamily to have two tiers of known OSes, so that generic OS names such as “Linux” cannot end up taking priority over more specific OS names. This fixes the case where “CentOS Linux” was detected as LINUX and not CENTOS. --- .../org/jclouds/compute/domain/OsFamily.java | 38 ++++++++++++++++++- .../compute/util/ComputeServiceUtils.java | 10 ++++- 2 files changed, 46 insertions(+), 2 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 e411580ec3..b525c29ef2 100644 --- a/compute/src/main/java/org/jclouds/compute/domain/OsFamily.java +++ b/compute/src/main/java/org/jclouds/compute/domain/OsFamily.java @@ -21,11 +21,16 @@ import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.CaseFormat.LOWER_HYPHEN; import static com.google.common.base.CaseFormat.UPPER_UNDERSCORE; +import java.util.Arrays; + +import com.google.common.base.Predicate; +import com.google.common.collect.FluentIterable; + /** * Running Operating system */ public enum OsFamily { - UNRECOGNIZED, AIX, ALPINE, ARCH, CENTOS, DARWIN, DEBIAN, ESX, FEDORA, FREEBSD, GENTOO, HPUX, LINUX, COREOS, + UNRECOGNIZED(false), AIX, ALPINE, ARCH, CENTOS, DARWIN, DEBIAN, ESX, FEDORA, FREEBSD, GENTOO, HPUX, LINUX(false), COREOS, /** * @see SmartOS */ @@ -48,6 +53,16 @@ public enum OsFamily { */ GCEL, SIGAR, SLACKWARE, SOLARIS, SUSE, TURBOLINUX, CLOUD_LINUX, UBUNTU, WINDOWS; + private final boolean prioritise; + + OsFamily() { + this.prioritise = true; + } + + OsFamily(boolean prioritise) { + this.prioritise = prioritise; + } + public String value() { return UPPER_UNDERSCORE.to(LOWER_HYPHEN, name()); } @@ -57,6 +72,10 @@ public enum OsFamily { return value(); } + public boolean shouldPrioritise() { + return prioritise; + } + public static OsFamily fromValue(String osFamily) { try { return valueOf(LOWER_HYPHEN.to(UPPER_UNDERSCORE, checkNotNull(osFamily, "osFamily"))); @@ -64,4 +83,21 @@ public enum OsFamily { return UNRECOGNIZED; } } + + private static Predicate predicateOnShouldPrioritise(final boolean prioritise) { + return new Predicate() { + @Override + public boolean apply(OsFamily osFamily) { + return osFamily.shouldPrioritise() == prioritise; + } + }; + } + + public static OsFamily[] proritisedValues() { + return FluentIterable.from(Arrays.asList(values())).filter(predicateOnShouldPrioritise(true)).toArray(OsFamily.class); + } + + public static OsFamily[] nonProritisedValues() { + return FluentIterable.from(Arrays.asList(values())).filter(predicateOnShouldPrioritise(false)).toArray(OsFamily.class); + } } diff --git a/compute/src/main/java/org/jclouds/compute/util/ComputeServiceUtils.java b/compute/src/main/java/org/jclouds/compute/util/ComputeServiceUtils.java index b9333a597d..1d1cf95f2f 100644 --- a/compute/src/main/java/org/jclouds/compute/util/ComputeServiceUtils.java +++ b/compute/src/main/java/org/jclouds/compute/util/ComputeServiceUtils.java @@ -140,7 +140,15 @@ public class ComputeServiceUtils { public static org.jclouds.compute.domain.OsFamily parseOsFamilyOrUnrecognized(String in) { org.jclouds.compute.domain.OsFamily myOs = null; - for (org.jclouds.compute.domain.OsFamily os : org.jclouds.compute.domain.OsFamily.values()) { + for (org.jclouds.compute.domain.OsFamily os : org.jclouds.compute.domain.OsFamily.proritisedValues()) { + if (in.toLowerCase().replaceAll("\\s", "").indexOf(os.toString()) != -1) { + myOs = os; + } + } + if (myOs != null) { + return myOs; + } + for (org.jclouds.compute.domain.OsFamily os : org.jclouds.compute.domain.OsFamily.nonProritisedValues()) { if (in.toLowerCase().replaceAll("\\s", "").indexOf(os.toString()) != -1) { myOs = os; }