Files
ScadaBridge/src/ZB.MOM.WW.ScadaBridge.DeploymentManager/StateTransitionValidator.cs
T
Joseph Doherty c1043569f6 docs(deployment): reconcile delete-from-NotDeployed — spec matrix now matches deliberate code (#31, M2.17)
git blame shows commit 1d5465f3 deliberately added NotDeployed to CanDelete so an
undeployed instance can have its orphan record fully removed. Code + tests already
permit it; the spec matrix said 'No'. Per M2.17, reconcile doc→code (not the reverse):
matrix now reads 'Delete from Not deployed = Yes (removes the orphan record)' with a
note, and CanDelete carries a remark citing the rationale + origin commit.
2026-06-16 07:24:57 -04:00

73 lines
3.7 KiB
C#

using ZB.MOM.WW.ScadaBridge.Commons.Types.Enums;
namespace ZB.MOM.WW.ScadaBridge.DeploymentManager;
/// <summary>
/// WP-4: State transition matrix for instance lifecycle.
///
/// State | Deploy | Disable | Enable | Delete
/// ----------|--------|---------|--------|-------
/// NotDeploy | OK | NO | NO | OK
/// Enabled | OK | OK | NO | OK
/// Disabled | OK* | NO | OK | OK
///
/// * Deploy on a Disabled instance also enables it.
/// Delete removes the instance record entirely; it is valid from any state.
/// </summary>
public static class StateTransitionValidator
{
/// <summary>Returns true when a deploy operation is allowed from the given state.</summary>
/// <param name="currentState">The current instance state.</param>
/// <returns><see langword="true"/> if deploy is permitted; otherwise <see langword="false"/>.</returns>
public static bool CanDeploy(InstanceState currentState) =>
currentState is InstanceState.NotDeployed or InstanceState.Enabled or InstanceState.Disabled;
/// <summary>Returns true when a disable operation is allowed from the given state.</summary>
/// <param name="currentState">The current instance state.</param>
/// <returns><see langword="true"/> if disable is permitted; otherwise <see langword="false"/>.</returns>
public static bool CanDisable(InstanceState currentState) =>
currentState == InstanceState.Enabled;
/// <summary>Returns true when an enable operation is allowed from the given state.</summary>
/// <param name="currentState">The current instance state.</param>
/// <returns><see langword="true"/> if enable is permitted; otherwise <see langword="false"/>.</returns>
public static bool CanEnable(InstanceState currentState) =>
currentState == InstanceState.Disabled;
/// <summary>Returns true when a delete operation is allowed from the given state.</summary>
/// <param name="currentState">The current instance state.</param>
/// <returns><see langword="true"/> if delete is permitted; otherwise <see langword="false"/>.</returns>
/// <remarks>
/// Delete is allowed from <see cref="InstanceState.NotDeployed"/> by design: an
/// undeployed instance would otherwise linger as an unremovable orphan record.
/// Delete from <c>NotDeployed</c> is a central-side record cleanup (no live site
/// config to tear down). This matches the state-transition matrix in
/// Component-DeploymentManager.md ("Delete from Not deployed = Yes") — reconciled
/// in M2.17 (#31); the deliberate behaviour was introduced in commit 1d5465f3.
/// </remarks>
public static bool CanDelete(InstanceState currentState) =>
currentState is InstanceState.NotDeployed or InstanceState.Enabled or InstanceState.Disabled;
/// <summary>
/// Returns a human-readable error message if the transition is invalid, or null if valid.
/// </summary>
/// <param name="currentState">The current instance state.</param>
/// <param name="operation">The operation name to validate (e.g. "deploy", "disable", "enable", "delete").</param>
/// <returns>An error message string if the transition is invalid; null if it is permitted.</returns>
public static string? ValidateTransition(InstanceState currentState, string operation)
{
var allowed = operation.ToLowerInvariant() switch
{
"deploy" => CanDeploy(currentState),
"disable" => CanDisable(currentState),
"enable" => CanEnable(currentState),
"delete" => CanDelete(currentState),
_ => false
};
if (allowed) return null;
return $"Operation '{operation}' is not allowed when instance is in state '{currentState}'.";
}
}