JCLOUDS-1351: improve OS Family parsing

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.
This commit is contained in:
Richard Downer 2017-11-02 20:46:01 +00:00 committed by Ignasi Barrera
parent 79aa09dd14
commit 4dbba815ca
2 changed files with 46 additions and 2 deletions

View File

@ -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 <a href="http://smartos.org">SmartOS</a>
*/
@ -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<OsFamily> predicateOnShouldPrioritise(final boolean prioritise) {
return new Predicate<OsFamily>() {
@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);
}
}

View File

@ -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;
}