mock data
This commit is contained in:
@@ -0,0 +1,219 @@
|
||||
// Copyright (c) StellaOps. All rights reserved.
|
||||
// Licensed under BUSL-1.1. See LICENSE in the project root.
|
||||
// Description: Admin endpoint for seeding demo data into all module databases.
|
||||
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Routing;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using StellaOps.Infrastructure.Postgres.Migrations;
|
||||
using StellaOps.Authority.Persistence.Postgres;
|
||||
using StellaOps.Scheduler.Persistence.Postgres;
|
||||
using StellaOps.Concelier.Persistence.Postgres;
|
||||
using StellaOps.Policy.Persistence.Postgres;
|
||||
using StellaOps.Notify.Persistence.Postgres;
|
||||
using StellaOps.Excititor.Persistence.Postgres;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StellaOps.Platform.WebService.Endpoints;
|
||||
|
||||
/// <summary>
|
||||
/// Admin-only endpoint for seeding databases with demo data.
|
||||
/// Gated by STELLAOPS_ENABLE_DEMO_SEED environment variable.
|
||||
/// </summary>
|
||||
public static class SeedEndpoints
|
||||
{
|
||||
public static IEndpointRouteBuilder MapSeedEndpoints(this IEndpointRouteBuilder app)
|
||||
{
|
||||
var seed = app.MapGroup("/api/v1/admin")
|
||||
.WithTags("Admin - Demo Seed")
|
||||
.RequireAuthorization("admin");
|
||||
|
||||
seed.MapPost("/seed-demo", HandleSeedDemoAsync)
|
||||
.WithName("SeedDemo")
|
||||
.WithSummary("Seed all databases with demo data")
|
||||
.Produces<SeedDemoResponse>(StatusCodes.Status200OK)
|
||||
.Produces(StatusCodes.Status403Forbidden)
|
||||
.Produces(StatusCodes.Status503ServiceUnavailable);
|
||||
|
||||
return app;
|
||||
}
|
||||
|
||||
private static async Task<IResult> HandleSeedDemoAsync(
|
||||
SeedDemoRequest? request,
|
||||
IConfiguration configuration,
|
||||
ILoggerFactory loggerFactory,
|
||||
CancellationToken ct)
|
||||
{
|
||||
var enabled = configuration.GetValue<bool>("STELLAOPS_ENABLE_DEMO_SEED",
|
||||
bool.TryParse(Environment.GetEnvironmentVariable("STELLAOPS_ENABLE_DEMO_SEED"), out var envVal) && envVal);
|
||||
|
||||
if (!enabled)
|
||||
{
|
||||
return Results.Json(new { error = "Demo seeding is disabled. Set STELLAOPS_ENABLE_DEMO_SEED=true to enable." },
|
||||
statusCode: StatusCodes.Status503ServiceUnavailable);
|
||||
}
|
||||
|
||||
var modules = request?.Modules ?? ["all"];
|
||||
var dryRun = request?.DryRun ?? false;
|
||||
var logger = loggerFactory.CreateLogger("SeedEndpoints");
|
||||
|
||||
logger.LogInformation("Demo seed requested. Modules={Modules}, DryRun={DryRun}", string.Join(",", modules), dryRun);
|
||||
|
||||
// Resolve connection string
|
||||
var connectionString = ResolveConnectionString(configuration);
|
||||
if (string.IsNullOrEmpty(connectionString))
|
||||
{
|
||||
return Results.Json(new { error = "No PostgreSQL connection string configured." },
|
||||
statusCode: StatusCodes.Status503ServiceUnavailable);
|
||||
}
|
||||
|
||||
var results = new List<SeedModuleResult>();
|
||||
|
||||
// Get the module definitions matching MigrationModuleRegistry in the CLI
|
||||
var moduleInfos = GetSeedModules(modules);
|
||||
|
||||
foreach (var module in moduleInfos)
|
||||
{
|
||||
try
|
||||
{
|
||||
var runner = new MigrationRunner(
|
||||
connectionString,
|
||||
module.SchemaName,
|
||||
module.Name,
|
||||
loggerFactory.CreateLogger($"migration.seed.{module.Name}"));
|
||||
|
||||
var options = new MigrationRunOptions
|
||||
{
|
||||
CategoryFilter = MigrationCategory.Seed,
|
||||
DryRun = dryRun,
|
||||
TimeoutSeconds = 300,
|
||||
ValidateChecksums = true,
|
||||
FailOnChecksumMismatch = true,
|
||||
};
|
||||
|
||||
var result = await runner.RunFromAssemblyAsync(
|
||||
module.Assembly, module.ResourcePrefix, options, ct);
|
||||
|
||||
results.Add(new SeedModuleResult
|
||||
{
|
||||
Module = module.Name,
|
||||
Success = result.Success,
|
||||
Applied = result.AppliedCount,
|
||||
Skipped = result.SkippedCount,
|
||||
DurationMs = result.DurationMs,
|
||||
Error = result.ErrorMessage,
|
||||
});
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "Seed failed for module {Module}", module.Name);
|
||||
results.Add(new SeedModuleResult
|
||||
{
|
||||
Module = module.Name,
|
||||
Success = false,
|
||||
Error = ex.Message,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
var allSuccess = results.All(r => r.Success);
|
||||
var response = new SeedDemoResponse
|
||||
{
|
||||
Success = allSuccess,
|
||||
DryRun = dryRun,
|
||||
Modules = results,
|
||||
Message = allSuccess
|
||||
? (dryRun ? "Dry run complete. No data was modified." : "Demo data seeded successfully.")
|
||||
: "Some modules failed to seed. Check individual module results.",
|
||||
};
|
||||
|
||||
return Results.Ok(response);
|
||||
}
|
||||
|
||||
private static string? ResolveConnectionString(IConfiguration configuration)
|
||||
{
|
||||
// Check env vars first, then configuration
|
||||
var candidates = new[]
|
||||
{
|
||||
Environment.GetEnvironmentVariable("STELLAOPS_POSTGRES_CONNECTION"),
|
||||
Environment.GetEnvironmentVariable("STELLAOPS_DB_CONNECTION"),
|
||||
configuration["StellaOps:Postgres:ConnectionString"],
|
||||
configuration["Postgres:ConnectionString"],
|
||||
configuration["Database:ConnectionString"],
|
||||
};
|
||||
|
||||
return candidates.FirstOrDefault(c => !string.IsNullOrWhiteSpace(c));
|
||||
}
|
||||
|
||||
private static List<SeedModuleInfo> GetSeedModules(string[] moduleFilter)
|
||||
{
|
||||
var all = new List<SeedModuleInfo>
|
||||
{
|
||||
new("Authority", "authority",
|
||||
typeof(AuthorityDataSource).Assembly,
|
||||
"StellaOps.Authority.Persistence.Migrations"),
|
||||
new("Scheduler", "scheduler",
|
||||
typeof(SchedulerDataSource).Assembly,
|
||||
"StellaOps.Scheduler.Persistence.Migrations"),
|
||||
new("Concelier", "vuln",
|
||||
typeof(ConcelierDataSource).Assembly,
|
||||
"StellaOps.Concelier.Persistence.Migrations"),
|
||||
new("Policy", "policy",
|
||||
typeof(PolicyDataSource).Assembly,
|
||||
"StellaOps.Policy.Persistence.Migrations"),
|
||||
new("Notify", "notify",
|
||||
typeof(NotifyDataSource).Assembly,
|
||||
"StellaOps.Notify.Persistence.Migrations"),
|
||||
new("Excititor", "vex",
|
||||
typeof(ExcititorDataSource).Assembly,
|
||||
"StellaOps.Excititor.Persistence.Migrations"),
|
||||
};
|
||||
|
||||
if (moduleFilter.Length == 1 && moduleFilter[0].Equals("all", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return all;
|
||||
}
|
||||
|
||||
var filterSet = new HashSet<string>(moduleFilter, StringComparer.OrdinalIgnoreCase);
|
||||
return all.Where(m => filterSet.Contains(m.Name)).ToList();
|
||||
}
|
||||
|
||||
// ── DTOs ──────────────────────────────────────────────────────────────────
|
||||
|
||||
private sealed record SeedModuleInfo(
|
||||
string Name,
|
||||
string SchemaName,
|
||||
Assembly Assembly,
|
||||
string ResourcePrefix);
|
||||
|
||||
public sealed class SeedDemoRequest
|
||||
{
|
||||
public string[] Modules { get; set; } = ["all"];
|
||||
public bool DryRun { get; set; }
|
||||
}
|
||||
|
||||
public sealed class SeedDemoResponse
|
||||
{
|
||||
public bool Success { get; set; }
|
||||
public bool DryRun { get; set; }
|
||||
public string Message { get; set; } = "";
|
||||
public List<SeedModuleResult> Modules { get; set; } = [];
|
||||
}
|
||||
|
||||
public sealed class SeedModuleResult
|
||||
{
|
||||
public string Module { get; set; } = "";
|
||||
public bool Success { get; set; }
|
||||
public int Applied { get; set; }
|
||||
public int Skipped { get; set; }
|
||||
public long DurationMs { get; set; }
|
||||
public string? Error { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -284,6 +284,7 @@ app.MapLegacyAliasEndpoints();
|
||||
app.MapPackAdapterEndpoints();
|
||||
app.MapAdministrationTrustSigningMutationEndpoints();
|
||||
app.MapFederationTelemetryEndpoints();
|
||||
app.MapSeedEndpoints();
|
||||
|
||||
app.MapGet("/healthz", () => Results.Ok(new { status = "ok" }))
|
||||
.WithTags("Health")
|
||||
|
||||
@@ -26,6 +26,13 @@
|
||||
<ProjectReference Include="..\..\Scanner\__Libraries\StellaOps.Scanner.Reachability\StellaOps.Scanner.Reachability.csproj" />
|
||||
<ProjectReference Include="..\..\Signals\StellaOps.Signals\StellaOps.Signals.csproj" />
|
||||
<ProjectReference Include="..\..\Policy\__Libraries\StellaOps.Policy.Interop\StellaOps.Policy.Interop.csproj" />
|
||||
<!-- Persistence modules for demo data seeding (SeedEndpoints) -->
|
||||
<ProjectReference Include="..\..\Authority\__Libraries\StellaOps.Authority.Persistence\StellaOps.Authority.Persistence.csproj" />
|
||||
<ProjectReference Include="..\..\Scheduler\__Libraries\StellaOps.Scheduler.Persistence\StellaOps.Scheduler.Persistence.csproj" />
|
||||
<ProjectReference Include="..\..\Concelier\__Libraries\StellaOps.Concelier.Persistence\StellaOps.Concelier.Persistence.csproj" />
|
||||
<ProjectReference Include="..\..\Policy\__Libraries\StellaOps.Policy.Persistence\StellaOps.Policy.Persistence.csproj" />
|
||||
<ProjectReference Include="..\..\Notify\__Libraries\StellaOps.Notify.Persistence\StellaOps.Notify.Persistence.csproj" />
|
||||
<ProjectReference Include="..\..\Excititor\__Libraries\StellaOps.Excititor.Persistence\StellaOps.Excititor.Persistence.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
Reference in New Issue
Block a user