From 3bf6a88f9c982a9fddbae832b4b8a2f5fdc1c1f1 Mon Sep 17 00:00:00 2001 From: Danilo Del Busso Date: Tue, 27 Jun 2023 15:36:29 +0100 Subject: [PATCH] CP-43000: Add `Host` extension methods to check if upselling messages are needed Signed-off-by: Danilo Del Busso --- XenModel/Utils/Util.cs | 2 +- XenModel/XenAPI-Extensions/Host.cs | 63 +++++++++++++++++++++++++++++- 2 files changed, 62 insertions(+), 3 deletions(-) diff --git a/XenModel/Utils/Util.cs b/XenModel/Utils/Util.cs index 5d403a44f..df9f962b6 100644 --- a/XenModel/Utils/Util.cs +++ b/XenModel/Utils/Util.cs @@ -350,7 +350,7 @@ namespace XenAdmin public const long TicksBefore1970 = 621355968000000000; public static readonly string[] Iso8601DateFormats = {"yyyyMMddTHH:mm:ssZ", "yyyy-MM-ddTHH:mm:ssZ"}; - public static readonly string[] NonIso8601DateFormats = { "yyyy-MM-dd" }; + public static readonly string[] NonIso8601DateFormats = { "yyyy-MM-dd", "yyyy.mmdd" }; public static DateTime GetUnixMinDateTime() { diff --git a/XenModel/XenAPI-Extensions/Host.cs b/XenModel/XenAPI-Extensions/Host.cs index 25fda9efe..063e74ca3 100644 --- a/XenModel/XenAPI-Extensions/Host.cs +++ b/XenModel/XenAPI-Extensions/Host.cs @@ -44,7 +44,7 @@ namespace XenAPI { public partial class Host : IComparable, IEquatable { - private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); + private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod()?.DeclaringType); public enum Edition { @@ -143,6 +143,7 @@ namespace XenAPI case Edition.Standard: return "standard"; default: + // CP-43000: For some hosts "trial" works, too. However, "express" is valid from Naples onwards return Helpers.NaplesOrGreater(this) ? "express" : "free"; } } @@ -187,6 +188,7 @@ namespace XenAPI /// /// The expiry date of this host's license in UTC. + /// Defaults to 2030-01-01 if not found. /// public virtual DateTime LicenseExpiryUTC() { @@ -196,6 +198,25 @@ namespace XenAPI return new DateTime(2030, 1, 1); } + /// + /// The CSS expiry date of this host's license. + /// The time component is always set to midnight. + /// Returns null if the value doesn't exist. + /// + public virtual DateTime? LicenseCssExpiry() + { + if(license_params != null && + license_params.TryGetValue("css_expiry", out var cssExpiryValue) && + !string.IsNullOrEmpty(cssExpiryValue) && + Util.TryParseNonIso8601DateTime(cssExpiryValue, out var result)) + { + // css_expiry is not a datetime object + return result.Date; + } + + return null; + } + public static bool RestrictRBAC(Host h) { return BoolKeyPreferTrue(h.license_params, "restrict_rbac"); @@ -293,7 +314,7 @@ namespace XenAPI public virtual bool IsFreeLicense() { - return edition == "free" || edition == "express"; + return edition == "free" || edition == "express" || edition == "trial"; } public virtual bool IsFreeLicenseOrExpired() @@ -303,6 +324,44 @@ namespace XenAPI return true; } + /// + /// True if host qualifies for showing an upselling message based on its license and version. + ///
+ /// Used to decide whether or not to show the upselling message from trial or express edition. + ///
+ /// See CP-43000 for more info. + ///
+ public virtual bool CanShowTrialEditionUpsell() + { + if (IsFreeLicense() && + software_version.TryGetValue("is_preview_release", out var isPreviewReleaseString) && + bool.TryParse(isPreviewReleaseString, out var isPreviewRelease) && !isPreviewRelease) + { + return true; + } + + return false; + } + + /// + /// Returns true if the CSS license has expired, regardless of what edition is shown. + ///
+ /// Do not rely on this method for enforcing restrictions as the user can circumvent this method + /// by updating the system date. + ///
+ public virtual bool CssLicenseHasExpired() + { + var cssExpiry = LicenseCssExpiry(); + + if (cssExpiry != null) + { + // User can circumvent this by changing system date + return DateTime.Now > cssExpiry; + } + + return false; + } + public static bool RestrictHA(Host h) { return !BoolKey(h.license_params, "enable_xha");