Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3x improvements #69

Merged
merged 11 commits into from
Feb 14, 2024
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
## [3.4.2]

### Added

- Added link to docs in exceptions
- Added some logging in subscription usage for CI builds

## [3.4.1]

### Changed

- Should not read subscription usage while in a CI build

## [3.4.0]

## Added
Expand Down
2 changes: 1 addition & 1 deletion build-push-local.ps1
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

param (
[string]$targetFrameworks = "net6.0"
[string]$targetFrameworks = "all"
)


Expand Down
16 changes: 16 additions & 0 deletions src/FakeXrmEasy.Core/CommercialLicense/EnvironmentReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,29 @@ namespace FakeXrmEasy.Core.CommercialLicense
internal interface IEnvironmentReader
{
string GetEnvironmentVariable(string variableName);
bool IsRunningInContinuousIntegration();
}

internal class EnvironmentReader: IEnvironmentReader
{
/// <summary>
/// Gets the value of environment variable with name variableName
/// </summary>
/// <param name="variableName"></param>
/// <returns></returns>
public string GetEnvironmentVariable(string variableName)
{
return Environment.GetEnvironmentVariable(variableName);
}

/// <summary>
/// Checks if the test run is running in a CI environment
/// </summary>
/// <returns></returns>
public bool IsRunningInContinuousIntegration()
{
return "1".Equals(GetEnvironmentVariable("FAKE_XRM_EASY_CI"))
|| "True".Equals(GetEnvironmentVariable("TF_BUILD"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ namespace FakeXrmEasy.Core.CommercialLicense.Exceptions
/// </summary>
public class ConsiderUpgradingPlanException: Exception
{
private const string _url =
CommercialLicenseTroubleshootingLinks.BaseUrl + "/consider-upgrading-exception/";

/// <summary>
/// Default constructor
/// </summary>
/// <param name="currentNumberOfUsers"></param>
/// <param name="allowedNumberOfUsers"></param>
public ConsiderUpgradingPlanException(long currentNumberOfUsers, long allowedNumberOfUsers) :
base($"Your current subscription allows up to {allowedNumberOfUsers.ToString()}, however, {currentNumberOfUsers.ToString()} are currently using it. Please consider upgrading your current plan.")
base($"Your current subscription allows up to {allowedNumberOfUsers.ToString()} users, however, {currentNumberOfUsers.ToString()} users are currently using it. Please consider upgrading your current plan. More info at {_url}.")
{

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ namespace FakeXrmEasy.Core.CommercialLicense.Exceptions
/// </summary>
public class InvalidLicenseKeyException: Exception
{
private const string _url =
CommercialLicenseTroubleshootingLinks.BaseUrl + "/invalid-license-key-exception/";

/// <summary>
/// Default constructor
/// </summary>
public InvalidLicenseKeyException() : base("The license key is invalid")
public InvalidLicenseKeyException() : base($"The license key is invalid. More info at {_url}.")
{

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ namespace FakeXrmEasy.Core.CommercialLicense.Exceptions
/// </summary>
public class NoSubscriptionPlanInfoException: Exception
{
private const string _url =
CommercialLicenseTroubleshootingLinks.BaseUrl + "/no-subscription-plan-info-exception/";

/// <summary>
/// Default constructor
/// </summary>
public NoSubscriptionPlanInfoException() : base("The current subscription info is unknown")
public NoSubscriptionPlanInfoException() : base($"The current subscription info is unknown. More info at {_url}.")
{

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ namespace FakeXrmEasy.Core.CommercialLicense.Exceptions
/// </summary>
public class NoUsageFoundException: Exception
{
private const string _url =
CommercialLicenseTroubleshootingLinks.BaseUrl + "/no-usage-found-exception/";

/// <summary>
/// Default constructor
/// </summary>
public NoUsageFoundException() : base("No info about your current usage of FakeXrmEasy was found")
public NoUsageFoundException() : base($"No info about your current usage of FakeXrmEasy was found. More info at {_url}.")
{

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ namespace FakeXrmEasy.Core.CommercialLicense.Exceptions
/// </summary>
public class RenewalRequestExpiredException: Exception
{
private const string _url =
CommercialLicenseTroubleshootingLinks.BaseUrl + "/renewal-request-expired-exception/";

/// <summary>
/// Throws an exception where the current subscription expired
/// </summary>
/// <param name="expiredOn"></param>
public RenewalRequestExpiredException(DateTime expiredOn) : base($"The current subscription expired on '{expiredOn.ToLongDateString()}' and a renewal license was not applied on time. Please request a new subscription license.")
public RenewalRequestExpiredException(DateTime expiredOn) : base($"The current subscription expired on '{expiredOn.ToLongDateString()}' and a renewal license was not applied on time. Please request a new subscription license. More info at {_url}.")
{

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ namespace FakeXrmEasy.Core.CommercialLicense.Exceptions
/// </summary>
public class SubscriptionExpiredException: Exception
{
private const string _url =
CommercialLicenseTroubleshootingLinks.BaseUrl + "/subscription-expired-exception/";

/// <summary>
/// Throws an exception where the current subscription expired
/// </summary>
/// <param name="expiredOn"></param>
public SubscriptionExpiredException(DateTime expiredOn) : base($"The current subscription expired on {expiredOn.ToLongDateString()}")
public SubscriptionExpiredException(DateTime expiredOn) : base($"The current subscription expired on {expiredOn.ToLongDateString()}. More info at {_url}.")
{

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@
namespace FakeXrmEasy.Core.CommercialLicense.Exceptions
{
/// <summary>
/// Exception raised when the grace period for requesting an upgrade has expired
/// Exception raised when the grace period for requesting an upgrade has expired: https://dynamicsvalue.github.io/fake-xrm-easy-docs/licensing/commercial-license/troubleshooting/upgrade-request-expired-exception/
/// </summary>
public class UpgradeRequestExpiredException: Exception
{
private const string _url =
CommercialLicenseTroubleshootingLinks.BaseUrl + "/upgrade-request-expired-exception/";

/// <summary>
/// Default constructor
/// </summary>
/// <param name="firstRequested"></param>
public UpgradeRequestExpiredException(DateTime firstRequested) :
base($"You requested a subscription upgrade on '{firstRequested.ToShortDateString()}', however, the new subscription details or upgrade progress has not been completed within the allowed upgrade window. Please contact your line manager and raise a support ticket")
base($"You requested a subscription upgrade on '{firstRequested.ToShortDateString()}', however, the new subscription details or upgrade progress has not been completed within the allowed upgrade window. More info at {_url}.")
{

}
Expand Down
99 changes: 77 additions & 22 deletions src/FakeXrmEasy.Core/CommercialLicense/SubscriptionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,101 @@

namespace FakeXrmEasy.Core.CommercialLicense
{
internal static class SubscriptionManager
internal class SubscriptionManager
{
internal static ISubscriptionInfo _subscriptionInfo;
internal static readonly object _subscriptionInfoLock = new object();

internal static ISubscriptionUsage _subscriptionUsage;
internal static readonly object _subscriptionUsageLock = new object();
internal ISubscriptionInfo _subscriptionInfo;
internal ISubscriptionUsage _subscriptionUsage;
internal readonly SubscriptionUsageManager _subscriptionUsageManager;

private bool _renewalRequested = false;

internal static bool _renewalRequested = false;
internal static bool _upgradeRequested = false;
private static readonly object _subscriptionManagerLock = new object();
private static SubscriptionManager _instance = null;
private readonly IEnvironmentReader _environmentReader;
internal SubscriptionManager(IEnvironmentReader environmentReader,
ISubscriptionInfo subscriptionInfo,
ISubscriptionUsage subscriptionUsage,
SubscriptionUsageManager subscriptionUsageManager)
{
_environmentReader = environmentReader;
_subscriptionInfo = subscriptionInfo;
_subscriptionUsage = subscriptionUsage;
_subscriptionUsageManager = subscriptionUsageManager;
}

private static void SetLicenseKey(string licenseKey)
internal static SubscriptionManager Instance
{
lock (_subscriptionInfoLock)
get
{
if (_subscriptionInfo == null)
lock (_subscriptionManagerLock)
{
var subscriptionPlanManager = new SubscriptionPlanManager();
_subscriptionInfo = subscriptionPlanManager.GetSubscriptionInfoFromKey(licenseKey);
if (_instance == null)
{
_instance = new SubscriptionManager(
new EnvironmentReader(),
null,
null,
new SubscriptionUsageManager());
}
}

return _instance;
}
}

/// <summary>
/// For testing
/// </summary>
/// <param name="instance"></param>
internal static void SetFakeInstance(SubscriptionManager instance)
{
_instance = instance;
}

internal ISubscriptionInfo SubscriptionInfo => _subscriptionInfo;
internal ISubscriptionUsage SubscriptionUsage => _subscriptionUsage;
internal bool RenewalRequested => _renewalRequested;

internal void SetLicenseKey(string licenseKey)
{
if (_subscriptionInfo == null)
{
var subscriptionPlanManager = new SubscriptionPlanManager();
_subscriptionInfo = subscriptionPlanManager.GetSubscriptionInfoFromKey(licenseKey);
}
}

internal static void SetSubscriptionStorageProvider(ISubscriptionStorageProvider subscriptionStorageProvider,
internal void SetSubscriptionStorageProvider_Internal(ISubscriptionStorageProvider subscriptionStorageProvider,
IUserReader userReader,
bool upgradeRequested,
bool renewalRequested)

{
SetLicenseKey(subscriptionStorageProvider.GetLicenseKey());

lock (_subscriptionUsageLock)
if (_subscriptionUsage == null)
{
if (_subscriptionUsage == null)
if (_environmentReader.IsRunningInContinuousIntegration())
{
_upgradeRequested = upgradeRequested;
_renewalRequested = renewalRequested;

var usageManager = new SubscriptionUsageManager();
_subscriptionUsage = usageManager.ReadAndUpdateUsage(_subscriptionInfo, subscriptionStorageProvider, userReader, _upgradeRequested);
Console.WriteLine("Running in CI... skipping usage.");
_subscriptionUsage = new SubscriptionUsage();
return;
}

_renewalRequested = renewalRequested;

_subscriptionUsage = _subscriptionUsageManager.ReadAndUpdateUsage(_subscriptionInfo, subscriptionStorageProvider, userReader, upgradeRequested);
}
}
internal void SetSubscriptionStorageProvider(ISubscriptionStorageProvider subscriptionStorageProvider,
IUserReader userReader,
bool upgradeRequested,
bool renewalRequested)
{
lock (_subscriptionManagerLock)
{
SetLicenseKey(subscriptionStorageProvider.GetLicenseKey());
SetSubscriptionStorageProvider_Internal(subscriptionStorageProvider, userReader, upgradeRequested,
renewalRequested);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ internal bool IsSubscriptionPlanValid()

internal bool IsUsageValid()
{
if (IsRunningInContinuousIntegration())
if (_environmentReader.IsRunningInContinuousIntegration())
{
return true;
}
Expand Down Expand Up @@ -118,11 +118,5 @@ internal bool IsUsageValid()
}
return true;
}

private bool IsRunningInContinuousIntegration()
{
return "1".Equals(_environmentReader.GetEnvironmentVariable("FAKE_XRM_EASY_CI"))
|| "True".Equals(_environmentReader.GetEnvironmentVariable("TF_BUILD"));
}
}
}
12 changes: 12 additions & 0 deletions src/FakeXrmEasy.Core/DocsLinks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace FakeXrmEasy
{
internal static class DocLinks
{
internal const string BaseUrl = "https://dynamicsvalue.github.io/fake-xrm-easy-docs";
}

internal static class CommercialLicenseTroubleshootingLinks
{
internal const string BaseUrl = DocLinks.BaseUrl + "/licensing/commercial-license/troubleshooting";
}
}
2 changes: 1 addition & 1 deletion src/FakeXrmEasy.Core/FakeXrmEasy.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<TargetFrameworks Condition="'$(Configuration)'=='FAKE_XRM_EASY_2013'">net6.0</TargetFrameworks>
<TargetFrameworks Condition="'$(Configuration)'=='FAKE_XRM_EASY'">net6.0</TargetFrameworks>
<PackageId>FakeXrmEasy.Core</PackageId>
<VersionPrefix>3.4.0</VersionPrefix>
<VersionPrefix>3.4.2</VersionPrefix>
<Authors>Jordi Montaña</Authors>
<Company>Dynamics Value</Company>
<Title>FakeXrmEasy Core</Title>
Expand Down
15 changes: 8 additions & 7 deletions src/FakeXrmEasy.Core/Middleware/MiddlewareBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,14 @@ public IXrmFakedContext Build()

if (_context.LicenseContext == FakeXrmEasyLicense.Commercial)
{
var subscriptionInfo = SubscriptionManager._subscriptionInfo;
if (subscriptionInfo != null)
var subscriptionManager = SubscriptionManager.Instance;
if (subscriptionManager.SubscriptionInfo != null)
{
var subscriptionValidator = new SubscriptionValidator(
new EnvironmentReader(),
SubscriptionManager._subscriptionInfo,
SubscriptionManager._subscriptionUsage,
SubscriptionManager._renewalRequested);
subscriptionManager.SubscriptionInfo,
subscriptionManager.SubscriptionUsage,
subscriptionManager.RenewalRequested);

subscriptionValidator.IsValid();
}
Expand Down Expand Up @@ -156,8 +156,9 @@ public IMiddlewareBuilder SetSubscriptionStorageProvider(ISubscriptionStoragePro
var userReader = new UserReader();
Console.WriteLine($"Setting Subscription Storage Provider...");
Console.WriteLine($" -> Running as '{userReader.GetCurrentUserName()}' ...");

SubscriptionManager.SetSubscriptionStorageProvider(storageProvider, userReader, upgradeRequested, renewalRequested);

var subscriptionManagerInstance = SubscriptionManager.Instance;
subscriptionManagerInstance.SetSubscriptionStorageProvider(storageProvider, userReader, upgradeRequested, renewalRequested);

Console.WriteLine($"Setting Subscription Storage Provider ok.");
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ public string GetEnvironmentVariable(string variableName)
return variableValue;
}

public bool IsRunningInContinuousIntegration()
{
return "1".Equals(GetEnvironmentVariable("FAKE_XRM_EASY_CI"))
|| "True".Equals(GetEnvironmentVariable("TF_BUILD"));
}

public void SetEnvironmentVariable(string variableName, string variableValue)
{
_variables.AddOrUpdate(variableName, variableValue, (key, oldValue) => variableValue);
Expand Down
Loading
Loading