refactor: rename ScadaLink → ZB.MOM.WW.ScadaBridge (code + projects + namespaces)

Solution + 23 src projects + 26 test projects renamed; folders, csproj,
namespaces, and ScadaLinkDbContext/ScadaBridgeDbContext class updated.
ActorSystem "scadalink" → "scadabridge", Akka seed-node URLs migrated.
SQL roles/logins, LDAP domains, CLI command name, and CLI config dir
(~/.scadalink → ~/.scadabridge) also renamed.

Build green; 5 Host.Tests fail awaiting SQL login rename in next commit.
Pre-existing StaleTagMonitor timing flakes unchanged.

Rename script committed at tools/rename-to-scadabridge.sh.
This commit is contained in:
Joseph Doherty
2026-05-28 09:37:45 -04:00
parent 6d87ee3c3b
commit 7b0b9c7365
1531 changed files with 11180 additions and 11054 deletions
@@ -0,0 +1,170 @@
using System.Security.Cryptography;
using Microsoft.Extensions.Options;
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Services;
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Transport;
using ZB.MOM.WW.ScadaBridge.Commons.Types.Transport;
using ZB.MOM.WW.ScadaBridge.ConfigurationDatabase;
using ZB.MOM.WW.ScadaBridge.Transport.Encryption;
using ZB.MOM.WW.ScadaBridge.Transport.Serialization;
namespace ZB.MOM.WW.ScadaBridge.Transport.Export;
/// <summary>
/// Ties together the resolver, serializer, encryptor, manifest builder, and audit
/// service into a single export pipeline. Produces a ZIP-formatted bundle stream
/// and emits one append-only audit row per export — distinguishing encrypted vs
/// unencrypted runs via the <c>Action</c> column. The returned stream is positioned
/// at <c>0</c> so callers can immediately copy to a response/file.
/// </summary>
public sealed class BundleExporter : IBundleExporter
{
private readonly DependencyResolver _resolver;
private readonly EntitySerializer _entitySerializer;
private readonly ManifestBuilder _manifestBuilder;
private readonly BundleSecretEncryptor _encryptor;
private readonly BundleSerializer _bundleSerializer;
private readonly IAuditService _auditService;
private readonly ScadaBridgeDbContext _dbContext;
private readonly IOptions<TransportOptions> _options;
/// <summary>Initializes the exporter with all pipeline collaborators.</summary>
/// <param name="resolver">Resolves template dependency graphs and transitive entities.</param>
/// <param name="entitySerializer">Converts EF entities to wire-shaped bundle DTOs.</param>
/// <param name="manifestBuilder">Builds the bundle manifest including hash and summary.</param>
/// <param name="encryptor">Encrypts bundle secrets when a passphrase is provided.</param>
/// <param name="bundleSerializer">Serializes and compresses the bundle into a ZIP stream.</param>
/// <param name="auditService">Records one audit row per export operation.</param>
/// <param name="dbContext">Database context for reading export-time entity state.</param>
/// <param name="options">Transport options (e.g., format version).</param>
public BundleExporter(
DependencyResolver resolver,
EntitySerializer entitySerializer,
ManifestBuilder manifestBuilder,
BundleSecretEncryptor encryptor,
BundleSerializer bundleSerializer,
IAuditService auditService,
ScadaBridgeDbContext dbContext,
IOptions<TransportOptions> options)
{
_resolver = resolver ?? throw new ArgumentNullException(nameof(resolver));
_entitySerializer = entitySerializer ?? throw new ArgumentNullException(nameof(entitySerializer));
_manifestBuilder = manifestBuilder ?? throw new ArgumentNullException(nameof(manifestBuilder));
_encryptor = encryptor ?? throw new ArgumentNullException(nameof(encryptor));
_bundleSerializer = bundleSerializer ?? throw new ArgumentNullException(nameof(bundleSerializer));
_auditService = auditService ?? throw new ArgumentNullException(nameof(auditService));
_dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
_options = options ?? throw new ArgumentNullException(nameof(options));
}
/// <inheritdoc />
public async Task<Stream> ExportAsync(
ExportSelection selection,
string user,
string sourceEnvironment,
string? passphrase,
CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(selection);
ArgumentNullException.ThrowIfNull(user);
ArgumentNullException.ThrowIfNull(sourceEnvironment);
// 1. Resolve dependencies → ResolvedExport (topologically sorted templates,
// folder chain, transitive shared-scripts/external-systems if requested).
var resolved = await _resolver.ResolveAsync(selection, cancellationToken).ConfigureAwait(false);
// 2. Convert to the wire-shaped DTO (strips EF identity, refs-by-name).
var aggregate = new EntityAggregate(
TemplateFolders: resolved.TemplateFolders,
Templates: resolved.Templates,
SharedScripts: resolved.SharedScripts,
ExternalSystems: resolved.ExternalSystems,
ExternalSystemMethods: resolved.ExternalSystemMethods,
DatabaseConnections: resolved.DatabaseConnections,
NotificationLists: resolved.NotificationLists,
SmtpConfigurations: resolved.SmtpConfigs,
ApiKeys: resolved.ApiKeys,
ApiMethods: resolved.ApiMethods);
var contentDto = _entitySerializer.ToBundleContent(aggregate);
// 3. Per-category summary counts — used for fast humanly-readable preview at import.
var summary = new BundleSummary(
Templates: resolved.Templates.Count,
TemplateFolders: resolved.TemplateFolders.Count,
SharedScripts: resolved.SharedScripts.Count,
ExternalSystems: resolved.ExternalSystems.Count,
DbConnections: resolved.DatabaseConnections.Count,
NotificationLists: resolved.NotificationLists.Count,
SmtpConfigs: resolved.SmtpConfigs.Count,
ApiKeys: resolved.ApiKeys.Count,
ApiMethods: resolved.ApiMethods.Count);
// 4. Build a TEMPLATE manifest. BundleSerializer.Pack re-stamps both
// ContentHash and EncryptionMetadata against the bytes it actually
// writes, so we only need to seed EncryptionMetadata.Iterations when
// a passphrase is supplied — Pack rebuilds salt/iv/hash internally.
var assemblyVersion = typeof(BundleExporter).Assembly.GetName().Version?.ToString() ?? "0.0.0";
EncryptionMetadata? encryptionSeed = passphrase is null
? null
: new EncryptionMetadata(
Algorithm: "AES-256-GCM",
Kdf: "PBKDF2-SHA256",
Iterations: _options.Value.Pbkdf2Iterations,
SaltB64: string.Empty,
IvB64: string.Empty);
var templateManifest = _manifestBuilder.Build(
sourceEnvironment: sourceEnvironment,
exportedBy: user,
scadaLinkVersion: assemblyVersion,
encryption: encryptionSeed,
summary: summary,
contents: resolved.ContentManifest,
// Pack re-stamps ContentHash against the bytes it actually writes
// (either fresh ciphertext or the JSON it just serialized), so the
// upstream hash here only matters for the plaintext path — Pack
// trusts the supplied hash and the serializer is deterministic.
contentBytes: _bundleSerializer.SerializeContentBytes(contentDto));
// 5. Pack the zip; with a passphrase, Pack re-encrypts and re-stamps
// EncryptionMetadata (fresh salt/iv) + ContentHash.
var zipStream = _bundleSerializer.Pack(contentDto, templateManifest, passphrase, _encryptor);
// 6. Audit — entityId is the bundle's overall SHA-256 over the zip bytes
// (opaque to caller but uniquely identifies this export). Action
// discriminates encrypted vs unencrypted runs.
var bundleHash = ComputeStreamSha256(zipStream);
await _auditService.LogAsync(
user: user,
action: passphrase is null ? "UnencryptedBundleExport" : "BundleExported",
entityType: "Bundle",
entityId: bundleHash,
entityName: sourceEnvironment,
afterState: new
{
SourceEnvironment = sourceEnvironment,
Encrypted = passphrase is not null,
ArtifactCount = resolved.ContentManifest.Count,
Summary = summary,
},
cancellationToken: cancellationToken).ConfigureAwait(false);
await _dbContext.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
zipStream.Position = 0;
return zipStream;
}
/// <summary>
/// Hashes the zip stream's bytes (resets position before + after) so the audit
/// row's <c>EntityId</c> uniquely identifies this exact bundle without leaking
/// any of the manifest's internal hashes (which describe content, not the zip).
/// </summary>
private static string ComputeStreamSha256(Stream stream)
{
stream.Position = 0;
using var sha = SHA256.Create();
var hash = sha.ComputeHash(stream);
stream.Position = 0;
return "sha256:" + Convert.ToHexString(hash).ToLowerInvariant();
}
}
@@ -0,0 +1,433 @@
using ZB.MOM.WW.ScadaBridge.Commons.Entities.ExternalSystems;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.InboundApi;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Notifications;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Scripts;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Templates;
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories;
using ZB.MOM.WW.ScadaBridge.Commons.Types.Transport;
namespace ZB.MOM.WW.ScadaBridge.Transport.Export;
/// <summary>
/// Expands an <see cref="ExportSelection"/> into the full set of entities that
/// must be in the bundle for the selection to be self-consistent. Walks the
/// dependency edges documented in §6.1 of the Transport design doc:
/// <list type="bullet">
/// <item>Template composes Template (via TemplateComposition.ComposedTemplateId).</item>
/// <item>Template references SharedScript (by name, scanned in script bodies).</item>
/// <item>Template references ExternalSystem (by name, scanned in script bodies + attribute DataSourceReference).</item>
/// <item>ApiMethod references SharedScript (by name, scanned in ApiMethod.Script).</item>
/// <item>Selected Template's folder chain (ancestor folders included).</item>
/// <item>ExternalSystemDefinition pulls its ExternalSystemMethods.</item>
/// </list>
/// Templates are returned topologically sorted (base-before-derived) via Kahn's
/// algorithm so importers can apply them in order.
/// </summary>
public sealed class DependencyResolver
{
private readonly ITemplateEngineRepository _templates;
private readonly IExternalSystemRepository _externalSystems;
private readonly INotificationRepository _notifications;
private readonly IInboundApiRepository _inboundApi;
/// <summary>Initializes a new instance of <see cref="DependencyResolver"/>.</summary>
/// <param name="templates">Repository for template and shared script access.</param>
/// <param name="externalSystems">Repository for external system definitions and methods.</param>
/// <param name="notifications">Repository for notification lists and SMTP configurations.</param>
/// <param name="inboundApi">Repository for inbound API keys and methods.</param>
public DependencyResolver(
ITemplateEngineRepository templates,
IExternalSystemRepository externalSystems,
INotificationRepository notifications,
IInboundApiRepository inboundApi)
{
_templates = templates ?? throw new ArgumentNullException(nameof(templates));
_externalSystems = externalSystems ?? throw new ArgumentNullException(nameof(externalSystems));
_notifications = notifications ?? throw new ArgumentNullException(nameof(notifications));
_inboundApi = inboundApi ?? throw new ArgumentNullException(nameof(inboundApi));
}
/// <summary>Expands the selection into a fully self-consistent set of entities for bundling.</summary>
/// <param name="selection">The user's export selection specifying which entities to include.</param>
/// <param name="ct">Cancellation token.</param>
/// <returns>A <see cref="ResolvedExport"/> with all transitively required entities.</returns>
public async Task<ResolvedExport> ResolveAsync(ExportSelection selection, CancellationToken ct)
{
ArgumentNullException.ThrowIfNull(selection);
// ---- Seed: fetch the directly-selected entities ----
var templates = new Dictionary<int, Template>();
foreach (var id in selection.TemplateIds.Distinct())
{
var t = await _templates.GetTemplateWithChildrenAsync(id, ct).ConfigureAwait(false);
if (t is not null) templates[t.Id] = t;
}
var sharedScripts = new Dictionary<int, SharedScript>();
foreach (var id in selection.SharedScriptIds.Distinct())
{
var s = await _templates.GetSharedScriptByIdAsync(id, ct).ConfigureAwait(false);
if (s is not null) sharedScripts[s.Id] = s;
}
var externalSystems = new Dictionary<int, ExternalSystemDefinition>();
foreach (var id in selection.ExternalSystemIds.Distinct())
{
var es = await _externalSystems.GetExternalSystemByIdAsync(id, ct).ConfigureAwait(false);
if (es is not null) externalSystems[es.Id] = es;
}
var dbConnections = new Dictionary<int, DatabaseConnectionDefinition>();
foreach (var id in selection.DatabaseConnectionIds.Distinct())
{
var db = await _externalSystems.GetDatabaseConnectionByIdAsync(id, ct).ConfigureAwait(false);
if (db is not null) dbConnections[db.Id] = db;
}
var notificationLists = new Dictionary<int, NotificationList>();
foreach (var id in selection.NotificationListIds.Distinct())
{
var nl = await _notifications.GetNotificationListByIdAsync(id, ct).ConfigureAwait(false);
if (nl is not null) notificationLists[nl.Id] = nl;
}
var smtpConfigs = new Dictionary<int, SmtpConfiguration>();
foreach (var id in selection.SmtpConfigurationIds.Distinct())
{
var sm = await _notifications.GetSmtpConfigurationByIdAsync(id, ct).ConfigureAwait(false);
if (sm is not null) smtpConfigs[sm.Id] = sm;
}
var apiKeys = new Dictionary<int, ApiKey>();
foreach (var id in selection.ApiKeyIds.Distinct())
{
var k = await _inboundApi.GetApiKeyByIdAsync(id, ct).ConfigureAwait(false);
if (k is not null) apiKeys[k.Id] = k;
}
var apiMethods = new Dictionary<int, ApiMethod>();
foreach (var id in selection.ApiMethodIds.Distinct())
{
var m = await _inboundApi.GetApiMethodByIdAsync(id, ct).ConfigureAwait(false);
if (m is not null) apiMethods[m.Id] = m;
}
// ---- Dependency expansion ----
if (selection.IncludeDependencies)
{
await ExpandTemplateClosureAsync(templates, ct).ConfigureAwait(false);
await ExpandSharedScriptsFromTemplatesAsync(templates, sharedScripts, ct).ConfigureAwait(false);
await ExpandSharedScriptsFromApiMethodsAsync(apiMethods, sharedScripts, ct).ConfigureAwait(false);
await ExpandExternalSystemsFromTemplatesAsync(templates, externalSystems, ct).ConfigureAwait(false);
}
// ExternalSystemMethods always travel with their parent ExternalSystem.
var externalSystemMethods = new List<ExternalSystemMethod>();
foreach (var es in externalSystems.Values.OrderBy(x => x.Name, StringComparer.Ordinal))
{
var methods = await _externalSystems
.GetMethodsByExternalSystemIdAsync(es.Id, ct)
.ConfigureAwait(false);
externalSystemMethods.AddRange(methods);
}
// Folder ancestor chain — always pull, regardless of IncludeDependencies,
// so the imported tree never references missing parents.
var folders = await ResolveFolderChainAsync(templates.Values, ct).ConfigureAwait(false);
// ---- Topological sort of templates (base-before-derived) ----
var orderedTemplates = TopologicallySortTemplates(templates.Values);
// ---- Build deterministic content manifest ----
var manifest = BuildContentManifest(
folders,
orderedTemplates,
sharedScripts.Values,
externalSystems.Values,
externalSystemMethods,
dbConnections.Values,
notificationLists.Values,
smtpConfigs.Values,
apiKeys.Values,
apiMethods.Values);
return new ResolvedExport(
TemplateFolders: folders,
Templates: orderedTemplates,
SharedScripts: sharedScripts.Values.OrderBy(s => s.Name, StringComparer.Ordinal).ToList(),
ExternalSystems: externalSystems.Values.OrderBy(e => e.Name, StringComparer.Ordinal).ToList(),
ExternalSystemMethods: externalSystemMethods,
DatabaseConnections: dbConnections.Values.OrderBy(d => d.Name, StringComparer.Ordinal).ToList(),
NotificationLists: notificationLists.Values.OrderBy(n => n.Name, StringComparer.Ordinal).ToList(),
SmtpConfigs: smtpConfigs.Values.OrderBy(s => s.Host, StringComparer.Ordinal).ToList(),
ApiKeys: apiKeys.Values.OrderBy(a => a.Name, StringComparer.Ordinal).ToList(),
ApiMethods: apiMethods.Values.OrderBy(a => a.Name, StringComparer.Ordinal).ToList(),
ContentManifest: manifest);
}
// ---- Template composition closure ----
private async Task ExpandTemplateClosureAsync(Dictionary<int, Template> templates, CancellationToken ct)
{
var queue = new Queue<Template>(templates.Values);
while (queue.Count > 0)
{
var current = queue.Dequeue();
foreach (var comp in current.Compositions)
{
if (templates.ContainsKey(comp.ComposedTemplateId)) continue;
var composed = await _templates
.GetTemplateWithChildrenAsync(comp.ComposedTemplateId, ct)
.ConfigureAwait(false);
if (composed is null) continue;
templates[composed.Id] = composed;
queue.Enqueue(composed);
}
}
}
// ---- SharedScript pulls from template script bodies + attributes ----
private async Task ExpandSharedScriptsFromTemplatesAsync(
Dictionary<int, Template> templates,
Dictionary<int, SharedScript> sharedScripts,
CancellationToken ct)
{
var allShared = await _templates.GetAllSharedScriptsAsync(ct).ConfigureAwait(false);
foreach (var shared in allShared)
{
if (sharedScripts.ContainsKey(shared.Id)) continue;
if (TemplatesReferenceName(templates.Values, shared.Name))
{
sharedScripts[shared.Id] = shared;
}
}
}
// ---- SharedScript pulls from ApiMethod.Script bodies ----
private async Task ExpandSharedScriptsFromApiMethodsAsync(
Dictionary<int, ApiMethod> apiMethods,
Dictionary<int, SharedScript> sharedScripts,
CancellationToken ct)
{
if (apiMethods.Count == 0) return;
var allShared = await _templates.GetAllSharedScriptsAsync(ct).ConfigureAwait(false);
foreach (var shared in allShared)
{
if (sharedScripts.ContainsKey(shared.Id)) continue;
if (apiMethods.Values.Any(m => ContainsIdentifier(m.Script, shared.Name)))
{
sharedScripts[shared.Id] = shared;
}
}
}
// ---- ExternalSystem pulls from template script/attribute bodies ----
private async Task ExpandExternalSystemsFromTemplatesAsync(
Dictionary<int, Template> templates,
Dictionary<int, ExternalSystemDefinition> externalSystems,
CancellationToken ct)
{
var allSystems = await _externalSystems.GetAllExternalSystemsAsync(ct).ConfigureAwait(false);
foreach (var es in allSystems)
{
if (externalSystems.ContainsKey(es.Id)) continue;
if (TemplatesReferenceName(templates.Values, es.Name))
{
externalSystems[es.Id] = es;
}
}
}
private static bool TemplatesReferenceName(IEnumerable<Template> templates, string name)
{
foreach (var t in templates)
{
foreach (var s in t.Scripts)
{
if (ContainsIdentifier(s.Code, name)) return true;
}
foreach (var a in t.Attributes)
{
if (a.DataSourceReference is not null && ContainsIdentifier(a.DataSourceReference, name)) return true;
if (a.Value is not null && ContainsIdentifier(a.Value, name)) return true;
}
}
return false;
}
/// <summary>
/// Substring scan with word-boundary guarding: the name must not be a partial
/// match of a longer identifier. Cheap, dependency-free, sufficient for the
/// v1 export-time scan (callers can always over-include with explicit ids).
/// </summary>
private static bool ContainsIdentifier(string? haystack, string name)
{
if (string.IsNullOrEmpty(haystack) || string.IsNullOrEmpty(name)) return false;
var idx = 0;
while (true)
{
var hit = haystack.IndexOf(name, idx, StringComparison.Ordinal);
if (hit < 0) return false;
var before = hit == 0 ? '\0' : haystack[hit - 1];
var afterIdx = hit + name.Length;
var after = afterIdx >= haystack.Length ? '\0' : haystack[afterIdx];
if (!IsIdentifierChar(before) && !IsIdentifierChar(after)) return true;
idx = hit + 1;
}
}
private static bool IsIdentifierChar(char c) => c == '_' || char.IsLetterOrDigit(c);
// ---- Folder ancestor chain ----
private async Task<IReadOnlyList<TemplateFolder>> ResolveFolderChainAsync(
IEnumerable<Template> templates,
CancellationToken ct)
{
var allFolders = await _templates.GetAllFoldersAsync(ct).ConfigureAwait(false);
var byId = allFolders.ToDictionary(f => f.Id);
var needed = new Dictionary<int, TemplateFolder>();
foreach (var t in templates)
{
var fid = t.FolderId;
while (fid is not null && byId.TryGetValue(fid.Value, out var folder))
{
if (!needed.TryAdd(folder.Id, folder)) break; // already walked this chain
fid = folder.ParentFolderId;
}
}
// Root-first ordering so importers create parents before children.
return needed.Values
.OrderBy(f => DepthOf(f, byId))
.ThenBy(f => f.SortOrder)
.ThenBy(f => f.Name, StringComparer.Ordinal)
.ToList();
}
private static int DepthOf(TemplateFolder folder, IReadOnlyDictionary<int, TemplateFolder> byId)
{
var depth = 0;
var current = folder;
while (current.ParentFolderId is int pid && byId.TryGetValue(pid, out var parent))
{
depth++;
current = parent;
if (depth > 1024) break; // defensive: folder graph is acyclic by schema
}
return depth;
}
// ---- Kahn's algorithm: order templates base-before-derived ----
/// <summary>Sorts templates in base-before-derived order using Kahn's algorithm.</summary>
/// <param name="templates">The templates to sort; must form an acyclic composition graph.</param>
/// <returns>Templates sorted so composed (base) templates appear before their composing (derived) templates.</returns>
internal static List<Template> TopologicallySortTemplates(IEnumerable<Template> templates)
{
// Edge: composed --> composing (composed must appear first).
var nodes = templates.ToDictionary(t => t.Id);
var inDegree = nodes.ToDictionary(kv => kv.Key, _ => 0);
var outEdges = nodes.ToDictionary(kv => kv.Key, _ => new List<int>());
foreach (var t in nodes.Values)
{
foreach (var comp in t.Compositions)
{
if (!nodes.ContainsKey(comp.ComposedTemplateId)) continue;
outEdges[comp.ComposedTemplateId].Add(t.Id);
inDegree[t.Id]++;
}
}
var queue = new Queue<int>(
inDegree.Where(kv => kv.Value == 0)
.OrderBy(kv => nodes[kv.Key].Name, StringComparer.Ordinal)
.Select(kv => kv.Key));
var result = new List<Template>(nodes.Count);
while (queue.Count > 0)
{
var id = queue.Dequeue();
result.Add(nodes[id]);
foreach (var next in outEdges[id].OrderBy(x => nodes[x].Name, StringComparer.Ordinal))
{
if (--inDegree[next] == 0) queue.Enqueue(next);
}
}
if (result.Count != nodes.Count)
throw new InvalidOperationException("Template composition graph is cyclic");
return result;
}
private static IReadOnlyList<ManifestContentEntry> BuildContentManifest(
IReadOnlyList<TemplateFolder> folders,
IReadOnlyList<Template> templates,
IEnumerable<SharedScript> sharedScripts,
IEnumerable<ExternalSystemDefinition> externalSystems,
IEnumerable<ExternalSystemMethod> externalSystemMethods,
IEnumerable<DatabaseConnectionDefinition> dbConnections,
IEnumerable<NotificationList> notificationLists,
IEnumerable<SmtpConfiguration> smtpConfigs,
IEnumerable<ApiKey> apiKeys,
IEnumerable<ApiMethod> apiMethods)
{
var entries = new List<ManifestContentEntry>();
foreach (var f in folders)
{
entries.Add(new ManifestContentEntry("TemplateFolder", f.Name, 1, Array.Empty<string>()));
}
foreach (var t in templates)
{
var deps = t.Compositions
.Select(c => $"Template:{(templates.FirstOrDefault(x => x.Id == c.ComposedTemplateId)?.Name ?? c.ComposedTemplateId.ToString())}")
.OrderBy(x => x, StringComparer.Ordinal)
.ToList();
entries.Add(new ManifestContentEntry("Template", t.Name, 1, deps));
}
foreach (var s in sharedScripts.OrderBy(x => x.Name, StringComparer.Ordinal))
{
entries.Add(new ManifestContentEntry("SharedScript", s.Name, 1, Array.Empty<string>()));
}
foreach (var es in externalSystems.OrderBy(x => x.Name, StringComparer.Ordinal))
{
entries.Add(new ManifestContentEntry("ExternalSystem", es.Name, 1, Array.Empty<string>()));
}
var methodsBySystem = externalSystemMethods.GroupBy(m => m.ExternalSystemDefinitionId);
var systemById = externalSystems.ToDictionary(x => x.Id);
foreach (var grp in methodsBySystem.OrderBy(g => systemById.TryGetValue(g.Key, out var es) ? es.Name : string.Empty, StringComparer.Ordinal))
{
var systemName = systemById.TryGetValue(grp.Key, out var es) ? es.Name : grp.Key.ToString();
foreach (var m in grp.OrderBy(x => x.Name, StringComparer.Ordinal))
{
entries.Add(new ManifestContentEntry(
"ExternalSystemMethod",
$"{systemName}.{m.Name}",
1,
new[] { $"ExternalSystem:{systemName}" }));
}
}
foreach (var d in dbConnections.OrderBy(x => x.Name, StringComparer.Ordinal))
{
entries.Add(new ManifestContentEntry("DatabaseConnection", d.Name, 1, Array.Empty<string>()));
}
foreach (var n in notificationLists.OrderBy(x => x.Name, StringComparer.Ordinal))
{
entries.Add(new ManifestContentEntry("NotificationList", n.Name, 1, Array.Empty<string>()));
}
foreach (var s in smtpConfigs.OrderBy(x => x.Host, StringComparer.Ordinal))
{
entries.Add(new ManifestContentEntry("SmtpConfiguration", s.Host, 1, Array.Empty<string>()));
}
foreach (var k in apiKeys.OrderBy(x => x.Name, StringComparer.Ordinal))
{
entries.Add(new ManifestContentEntry("ApiKey", k.Name, 1, Array.Empty<string>()));
}
foreach (var m in apiMethods.OrderBy(x => x.Name, StringComparer.Ordinal))
{
entries.Add(new ManifestContentEntry("ApiMethod", m.Name, 1, Array.Empty<string>()));
}
return entries;
}
}
@@ -0,0 +1,28 @@
using ZB.MOM.WW.ScadaBridge.Commons.Entities.ExternalSystems;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.InboundApi;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Notifications;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Scripts;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Templates;
using ZB.MOM.WW.ScadaBridge.Commons.Types.Transport;
namespace ZB.MOM.WW.ScadaBridge.Transport.Export;
/// <summary>
/// Output of <see cref="DependencyResolver.ResolveAsync"/> — the full closure of
/// entities that need to land in a bundle for the given <see cref="ExportSelection"/>,
/// along with a stable, manifest-ready <c>ContentManifest</c>. Templates are
/// topologically ordered (base before derived) so the importer can apply them
/// in-order without further sorting.
/// </summary>
public sealed record ResolvedExport(
IReadOnlyList<TemplateFolder> TemplateFolders,
IReadOnlyList<Template> Templates,
IReadOnlyList<SharedScript> SharedScripts,
IReadOnlyList<ExternalSystemDefinition> ExternalSystems,
IReadOnlyList<ExternalSystemMethod> ExternalSystemMethods,
IReadOnlyList<DatabaseConnectionDefinition> DatabaseConnections,
IReadOnlyList<NotificationList> NotificationLists,
IReadOnlyList<SmtpConfiguration> SmtpConfigs,
IReadOnlyList<ApiKey> ApiKeys,
IReadOnlyList<ApiMethod> ApiMethods,
IReadOnlyList<ManifestContentEntry> ContentManifest);