|
| 1 | +using Microsoft.Extensions.DependencyInjection; |
| 2 | +using ModularityKit.Mutator.Abstractions.Context; |
| 3 | +using ModularityKit.Mutator.Abstractions.Intent; |
| 4 | +using ModularityKit.Mutator.Abstractions.Policies; |
| 5 | +using ModularityKit.Mutator.Governance.Abstractions.Lifecycle.Model; |
| 6 | +using ModularityKit.Mutator.Governance.Abstractions.Queries.Contracts; |
| 7 | +using ModularityKit.Mutator.Governance.Abstractions.Queries.Model; |
| 8 | +using ModularityKit.Mutator.Governance.Abstractions.Requests.Decisions; |
| 9 | +using ModularityKit.Mutator.Governance.Abstractions.Requests.Factory; |
| 10 | +using ModularityKit.Mutator.Governance.Abstractions.Requests.Model; |
| 11 | +using ModularityKit.Mutator.Governance.Abstractions.Storage; |
| 12 | +using ModularityKit.Mutator.Governance.Redis; |
| 13 | +using StackExchange.Redis; |
| 14 | + |
| 15 | +namespace RedisQueries.Scenarios; |
| 16 | + |
| 17 | +internal static class GovernanceRedisQueriesScenario |
| 18 | +{ |
| 19 | + public static async Task Run() |
| 20 | + { |
| 21 | + var redisConnectionString = BuildRedisConnectionString(); |
| 22 | + var keyPrefix = $"modularitykit:examples:governance:redis:{Guid.NewGuid():N}"; |
| 23 | + |
| 24 | + try |
| 25 | + { |
| 26 | + await using var multiplexer = await ConnectionMultiplexer.ConnectAsync(redisConnectionString); |
| 27 | + var services = new ServiceCollection(); |
| 28 | + |
| 29 | + services.AddRedisGovernanceStore( |
| 30 | + multiplexer, |
| 31 | + options => options.KeyPrefix = keyPrefix); |
| 32 | + |
| 33 | + await using var provider = services.BuildServiceProvider(); |
| 34 | + var requestStore = provider.GetRequiredService<IMutationRequestStore>(); |
| 35 | + var queryStore = provider.GetRequiredService<IMutationRequestQueryStore>(); |
| 36 | + |
| 37 | + await Seed(requestStore); |
| 38 | + |
| 39 | + Console.WriteLine($"Redis: {redisConnectionString}"); |
| 40 | + Console.WriteLine($"KeyPrefix: {keyPrefix}"); |
| 41 | + |
| 42 | + PrintSection("Pending Approval Queue"); |
| 43 | + PrintRequests(await queryStore.GetPendingApprovalQueueAsync()); |
| 44 | + |
| 45 | + PrintSection("Pending Approvals For security-lead"); |
| 46 | + PrintApprovals(await queryStore.GetPendingApprovalsAsync(new MutationApprovalQuery |
| 47 | + { |
| 48 | + ApproverIds = new HashSet<string> { "security-lead" } |
| 49 | + })); |
| 50 | + |
| 51 | + PrintSection("Recent Execution Outcomes"); |
| 52 | + PrintDecisions(await queryStore.GetRecentDecisionsAsync( |
| 53 | + MutationRequestDecisionQuery.RecentExecutionOutcomes(), |
| 54 | + take: 5)); |
| 55 | + } |
| 56 | + catch (RedisConnectionException exception) |
| 57 | + { |
| 58 | + Console.Error.WriteLine($"Could not connect to Redis at '{redisConnectionString}'."); |
| 59 | + Console.Error.WriteLine(exception.Message); |
| 60 | + Console.Error.WriteLine("Start Redis locally or set MODULARITYKIT_REDIS to a reachable endpoint."); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + private static string BuildRedisConnectionString() |
| 65 | + { |
| 66 | + var explicitConnectionString = Environment.GetEnvironmentVariable("MODULARITYKIT_REDIS"); |
| 67 | + if (!string.IsNullOrWhiteSpace(explicitConnectionString)) |
| 68 | + return explicitConnectionString; |
| 69 | + |
| 70 | + var host = Environment.GetEnvironmentVariable("MODULARITYKIT_REDIS_HOST") ?? "localhost"; |
| 71 | + var port = Environment.GetEnvironmentVariable("MODULARITYKIT_REDIS_PORT") ?? "6379"; |
| 72 | + var password = Environment.GetEnvironmentVariable("MODULARITYKIT_REDIS_PASSWORD"); |
| 73 | + |
| 74 | + return string.IsNullOrWhiteSpace(password) |
| 75 | + ? $"{host}:{port}" |
| 76 | + : $"{host}:{port},password={password}"; |
| 77 | + } |
| 78 | + |
| 79 | + private static async Task Seed(IMutationRequestStore requestStore) |
| 80 | + { |
| 81 | + await requestStore.Create(CreatePendingApprovalRequest( |
| 82 | + requestId: "redis-req-security-1", |
| 83 | + stateId: "tenant-42:roles", |
| 84 | + category: "Security", |
| 85 | + approverId: "security-lead", |
| 86 | + createdAt: new DateTimeOffset(2026, 6, 25, 8, 0, 0, TimeSpan.Zero))); |
| 87 | + |
| 88 | + await requestStore.Create(CreatePendingApprovalRequest( |
| 89 | + requestId: "redis-req-billing-1", |
| 90 | + stateId: "tenant-42:quota", |
| 91 | + category: "Billing", |
| 92 | + approverId: "billing-owner", |
| 93 | + createdAt: new DateTimeOffset(2026, 6, 25, 9, 0, 0, TimeSpan.Zero))); |
| 94 | + |
| 95 | + await requestStore.Create(CreateExecutedRequest( |
| 96 | + requestId: "redis-req-executed-1", |
| 97 | + stateId: "tenant-42:quota", |
| 98 | + category: "Billing", |
| 99 | + executedAt: new DateTimeOffset(2026, 6, 25, 12, 0, 0, TimeSpan.Zero))); |
| 100 | + } |
| 101 | + |
| 102 | + private static MutationRequest CreatePendingApprovalRequest( |
| 103 | + string requestId, |
| 104 | + string stateId, |
| 105 | + string category, |
| 106 | + string approverId, |
| 107 | + DateTimeOffset createdAt) |
| 108 | + => MutationRequestFactory.PendingApproval( |
| 109 | + stateId: stateId, |
| 110 | + stateType: "ExampleState", |
| 111 | + mutationType: "ExampleMutation", |
| 112 | + intent: new MutationIntent |
| 113 | + { |
| 114 | + OperationName = "ExampleOperation", |
| 115 | + Category = category, |
| 116 | + Description = $"Redis-backed governed request for {category.ToLowerInvariant()} flow" |
| 117 | + }, |
| 118 | + context: MutationContext.User("requester", "Requester", "Need governed change"), |
| 119 | + requirements: |
| 120 | + [ |
| 121 | + PolicyRequirement.Approval(approverId, $"Approval required from {approverId}") |
| 122 | + ]) |
| 123 | + with |
| 124 | + { |
| 125 | + RequestId = requestId, |
| 126 | + CreatedAt = createdAt, |
| 127 | + UpdatedAt = createdAt |
| 128 | + }; |
| 129 | + |
| 130 | + private static MutationRequest CreateExecutedRequest( |
| 131 | + string requestId, |
| 132 | + string stateId, |
| 133 | + string category, |
| 134 | + DateTimeOffset executedAt) |
| 135 | + => MutationRequestFactory.Approved( |
| 136 | + stateId: stateId, |
| 137 | + stateType: "ExampleState", |
| 138 | + mutationType: "ExampleMutation", |
| 139 | + intent: new MutationIntent |
| 140 | + { |
| 141 | + OperationName = "ExampleOperation", |
| 142 | + Category = category, |
| 143 | + Description = "Executed governed request" |
| 144 | + }, |
| 145 | + context: MutationContext.Service("governance-runtime", "Execute approved request")) |
| 146 | + with |
| 147 | + { |
| 148 | + RequestId = requestId, |
| 149 | + Status = MutationRequestStatus.Executed, |
| 150 | + CreatedAt = executedAt.AddMinutes(-15), |
| 151 | + UpdatedAt = executedAt, |
| 152 | + ExecutedAt = executedAt, |
| 153 | + Decisions = |
| 154 | + [ |
| 155 | + MutationRequestDecision.Create( |
| 156 | + MutationRequestDecisionType.Lifecycle(MutationRequestLifecycleDecisionType.Submitted), |
| 157 | + MutationContext.Service("governance-runtime", "Submitted")) |
| 158 | + with |
| 159 | + { |
| 160 | + Timestamp = executedAt.AddMinutes(-15) |
| 161 | + }, |
| 162 | + MutationRequestDecision.Create( |
| 163 | + MutationRequestDecisionType.Lifecycle(MutationRequestLifecycleDecisionType.Approved), |
| 164 | + MutationContext.Service("governance-runtime", "Approved")) |
| 165 | + with |
| 166 | + { |
| 167 | + Timestamp = executedAt.AddMinutes(-5) |
| 168 | + }, |
| 169 | + MutationRequestDecision.Create( |
| 170 | + MutationRequestDecisionType.Lifecycle(MutationRequestLifecycleDecisionType.Executed), |
| 171 | + MutationContext.Service("governance-runtime", "Executed")) |
| 172 | + with |
| 173 | + { |
| 174 | + Timestamp = executedAt |
| 175 | + } |
| 176 | + ] |
| 177 | + }; |
| 178 | + |
| 179 | + private static void PrintSection(string title) |
| 180 | + { |
| 181 | + Console.WriteLine(); |
| 182 | + Console.WriteLine($"=== {title} ==="); |
| 183 | + } |
| 184 | + |
| 185 | + private static void PrintRequests(IReadOnlyList<MutationRequest> requests) |
| 186 | + { |
| 187 | + foreach (var request in requests) |
| 188 | + { |
| 189 | + Console.WriteLine( |
| 190 | + $"- {request.RequestId} | {request.StateId} | {request.Intent.Category} | {request.Status} | pending: {request.PendingReason?.ToString() ?? "-"}"); |
| 191 | + } |
| 192 | + |
| 193 | + if (requests.Count == 0) |
| 194 | + Console.WriteLine("- none"); |
| 195 | + } |
| 196 | + |
| 197 | + private static void PrintApprovals(IReadOnlyList<MutationApprovalView> approvals) |
| 198 | + { |
| 199 | + foreach (var approval in approvals) |
| 200 | + { |
| 201 | + Console.WriteLine( |
| 202 | + $"- {approval.Request.RequestId} | {approval.Request.Intent.Category} | approver: {approval.Approval.ApproverId} | status: {approval.Approval.Status}"); |
| 203 | + } |
| 204 | + |
| 205 | + if (approvals.Count == 0) |
| 206 | + Console.WriteLine("- none"); |
| 207 | + } |
| 208 | + |
| 209 | + private static void PrintDecisions(IReadOnlyList<MutationRequestDecisionView> decisions) |
| 210 | + { |
| 211 | + foreach (var decision in decisions) |
| 212 | + { |
| 213 | + Console.WriteLine( |
| 214 | + $"- {decision.Request.RequestId} | {decision.Decision.Type.Category}:{decision.Decision.Type.Code} | {decision.Decision.Timestamp:O}"); |
| 215 | + } |
| 216 | + |
| 217 | + if (decisions.Count == 0) |
| 218 | + Console.WriteLine("- none"); |
| 219 | + } |
| 220 | +} |
0 commit comments