|
| 1 | +using ModularityKit.Mutator.Abstractions.Context; |
| 2 | +using ModularityKit.Mutator.Abstractions.Intent; |
| 3 | +using ModularityKit.Mutator.Governance.Abstractions.Requests; |
| 4 | +using ModularityKit.Mutator.Governance.Abstractions.Resolution; |
| 5 | +using ModularityKit.Mutator.Governance.Runtime.Resolution; |
| 6 | + |
| 7 | +namespace VersionedResolution.Scenarios; |
| 8 | + |
| 9 | +internal static class GovernanceVersionedResolutionScenario |
| 10 | +{ |
| 11 | + public static void Run() |
| 12 | + { |
| 13 | + var resolver = new MutationRequestVersionResolver(); |
| 14 | + |
| 15 | + PrintSection("Current Version Matches Expected Version"); |
| 16 | + PrintResolution( |
| 17 | + resolver.Resolve( |
| 18 | + CreateApprovedRequest("v10"), |
| 19 | + currentStateVersion: "v10", |
| 20 | + resolutionContext: MutationContext.User("approver-1", "Approver One", "Current version verified"), |
| 21 | + strategy: VersionedRequestResolutionStrategy.RejectStale)); |
| 22 | + |
| 23 | + PrintSection("Reject Stale"); |
| 24 | + PrintResolution( |
| 25 | + resolver.Resolve( |
| 26 | + CreateApprovedRequest("v10"), |
| 27 | + currentStateVersion: "v15", |
| 28 | + resolutionContext: MutationContext.User("approver-2", "Approver Two", "Reject stale request"), |
| 29 | + strategy: VersionedRequestResolutionStrategy.RejectStale)); |
| 30 | + |
| 31 | + PrintSection("Require Renewed Approval"); |
| 32 | + PrintResolution( |
| 33 | + resolver.Resolve( |
| 34 | + CreateApprovedRequest("v10"), |
| 35 | + currentStateVersion: "v15", |
| 36 | + resolutionContext: MutationContext.User("approver-3", "Approver Three", "Request renewed approval"), |
| 37 | + strategy: VersionedRequestResolutionStrategy.RequireRenewedApproval)); |
| 38 | + |
| 39 | + PrintSection("Revalidate On Latest State"); |
| 40 | + PrintResolution( |
| 41 | + resolver.Resolve( |
| 42 | + CreateApprovedRequest("v10"), |
| 43 | + currentStateVersion: "v15", |
| 44 | + resolutionContext: MutationContext.User("approver-4", "Approver Four", "Revalidate on the latest state"), |
| 45 | + strategy: VersionedRequestResolutionStrategy.RevalidateOnLatestState)); |
| 46 | + } |
| 47 | + |
| 48 | + private static MutationRequest CreateApprovedRequest(string expectedStateVersion) |
| 49 | + { |
| 50 | + return MutationRequest.Approved( |
| 51 | + stateId: "tenant-42:roles", |
| 52 | + stateType: "IamRoleState", |
| 53 | + mutationType: "GrantRoleMutation", |
| 54 | + intent: new MutationIntent |
| 55 | + { |
| 56 | + OperationName = "GrantRole", |
| 57 | + Category = "Security", |
| 58 | + Description = "Grant elevated role to tenant operator" |
| 59 | + }, |
| 60 | + context: MutationContext.User("requester-1", "Requester One", "Need elevated access for incident"), |
| 61 | + expectedStateVersion: expectedStateVersion); |
| 62 | + } |
| 63 | + |
| 64 | + private static void PrintSection(string title) |
| 65 | + { |
| 66 | + Console.WriteLine(); |
| 67 | + Console.WriteLine($"=== {title} ==="); |
| 68 | + } |
| 69 | + |
| 70 | + private static void PrintResolution(MutationRequestVersionResolution resolution) |
| 71 | + { |
| 72 | + var decision = resolution.Request.Decisions[^1]; |
| 73 | + |
| 74 | + Console.WriteLine($"Outcome: {resolution.Outcome}"); |
| 75 | + Console.WriteLine($"Was stale: {resolution.IsStale}"); |
| 76 | + Console.WriteLine($"Expected version: {resolution.ExpectedStateVersion ?? "-"}"); |
| 77 | + Console.WriteLine($"Current version: {resolution.CurrentStateVersion}"); |
| 78 | + Console.WriteLine($"Request status: {resolution.Request.Status}"); |
| 79 | + Console.WriteLine($"Next expected version: {resolution.Request.ExpectedStateVersion ?? "-"}"); |
| 80 | + Console.WriteLine($"Last decision: {decision.Type}"); |
| 81 | + Console.WriteLine($"Decision reason: {decision.Reason}"); |
| 82 | + } |
| 83 | +} |
0 commit comments