@using ZB.MOM.WW.ScadaBridge.CentralUI.Auth @inject Microsoft.AspNetCore.Components.Authorization.AuthenticationStateProvider MutationAuth @using ZB.MOM.WW.ScadaBridge.CentralUI.Services @using ZB.MOM.WW.ScadaBridge.Commons.Messages.Management @inject IServiceProvider MutationServices @page "/design/api-methods/create" @page "/design/api-methods/{Id:int}/edit" @using ZB.MOM.WW.ScadaBridge.Security @using ZB.MOM.WW.ScadaBridge.Commons.Entities.InboundApi @using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories @using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Security @using ZB.MOM.WW.ScadaBridge.Commons.Types.InboundApi @using ZB.MOM.WW.ScadaBridge.ManagementService @using Microsoft.Extensions.DependencyInjection @using ScriptAnalysis = ZB.MOM.WW.ScadaBridge.CentralUI.ScriptAnalysis @attribute [Authorize(Policy = AuthorizationPolicies.RequireDesign)] @inject IInboundApiRepository InboundApiRepository @inject IInboundApiKeyAdmin ApiKeyAdmin @inject ScriptAnalysis.ScriptAnalysisService AnalysisService @inject NavigationManager NavigationManager @implements IDisposable

@(Id.HasValue ? "Edit API Method" : "Add API Method")

@if (_loading) { } else {
@* The body used to stack six unrelated groups with nothing but a label between them. Fieldset + legend gives each stack a real boundary — same idiom as TransportExport. *@
Method
Every call to this method records its routed site steps as audit rows the Flow Debugger can replay. Adds 7–10 audit rows per call; for low-volume integration methods.
Approved API Keys
@if (_allKeys.Count == 0) {
No API keys configured. Create one to authorize callers for this method.
} else {
@foreach (var key in _allKeys) { var checkboxId = $"approved-key-{key.KeyId}";
}
Callers must present a checked key in the X-API-Key header to invoke this method.
}
Contract
Parameters
Return value
Script
@if (_existing != null) { @* ScadaBridge#75: the data plane answers a broken method with a generic "Script compilation failed for this method"; operators read the real diagnostics here (same operations as CLI api-method get/validate/recompile). *@
Compilation (saved script)
@if (_compileStatus is null) { Unavailable on this node } else if (_compileStatus.IsKnownBad) { Compile failed } else if (_compileStatus.IsCompiled) { Compiled } else { Not compiled yet } @if (_compileStatus?.Node is { } node) { on node @node }
@if (_compileStatus?.LastCompileError is { } lastError) {
@lastError
} @if (_compileResult != null) {
@_compileAction: @(_compileResult.Success ? "the saved script compiles." : "the saved script does not compile.")
@if (_compileResult.Errors.Count > 0) {
    @foreach (var error in _compileResult.Errors) {
  • @error
  • }
} }
Checks the script as saved, not unsaved edits. Recompile re-registers it on this central node, clearing a stale compile failure.
} @if (_formError != null) {
@_formError
}
@if (_showTestRun) {
Test Run
@* Lives inside the body, not between header and body, where it rendered as a third band. Wording unchanged — pinned by TestRunWarningTests. *@
Heads up: runs the script as typed (unsaved edits included) against the supplied Parameters. Route calls throw — cross-site routing needs a deployed site reachable over the cluster transport.
Parameter values
@if (_runResult != null) { @_runResult.DurationMs ms }
@if (_runResult != null) { @if (_runResult.Success) {
Return value @_runResult.ReturnTypeName
@_runResult.ReturnValueJson
} else {
@ErrorKindLabel(_runResult.ErrorKind)
@_runResult.Error
@if (_runResult.Markers is { Count: > 0 }) {
    @foreach (var m in _runResult.Markers) {
  • Line @m.StartLineNumber, col @m.StartColumn: @m.Message @m.Code
  • }
}
} @if (!string.IsNullOrEmpty(_runResult.ConsoleOutput)) {
Console output
@_runResult.ConsoleOutput
} }
} }
@code { [Parameter] public int? Id { get; set; } private bool _loading = true; private string _name = "", _script = ""; private int _timeoutSeconds = 30; private bool _traceAllCalls; private string? _params, _returns; private string? _formError; private MonacoEditor? _editor; private IReadOnlyList _markers = Array.Empty(); // Inbound-API key re-arch: the approved-keys list is driven by the IInboundApiKeyAdmin // seam, not ApiMethod.ApprovedApiKeyIds. The ApiMethod entity itself (name/script/params/etc.) // still lives on IInboundApiRepository — only the key↔method approval relationship moved to // per-key method-scopes. Keys are identified by an opaque string KeyId. private ApiMethod? _existing; private List _allKeys = new(); private HashSet _selectedKeyIds = new(StringComparer.Ordinal); // Keys approved for this method when the form loaded (empty on create), for diffing on save. private HashSet _initialKeyIds = new(StringComparer.Ordinal); private bool _showTestRun; private ApiMethodCompileStatus? _compileStatus; private ApiMethodCompileResult? _compileResult; private string? _compileAction; private bool _compiling; private bool _running; private Dictionary _paramValues = new(); private ScriptAnalysis.SandboxRunResult? _runResult; private CancellationTokenSource? _runCts; private bool _disposed; protected override async Task OnInitializedAsync() { try { // All keys come from the seam (hash-free projection). _allKeys = (await ApiKeyAdmin.ListAsync()).ToList(); } catch (Exception ex) { _formError = ex.Message; } if (Id.HasValue) { try { _existing = await InboundApiRepository.GetApiMethodByIdAsync(Id.Value); if (_existing != null) { _name = _existing.Name; _script = _existing.Script; _timeoutSeconds = _existing.TimeoutSeconds; _traceAllCalls = _existing.TraceAllCalls; _params = _existing.ParameterDefinitions; _returns = _existing.ReturnDefinition; // Seed approved keys from the seam: which keys' scopes contain this method. var keysForMethod = await ApiKeyAdmin.GetKeysForMethodAsync(_existing.Name); _initialKeyIds = new HashSet(keysForMethod, StringComparer.Ordinal); _selectedKeyIds = new HashSet(_initialKeyIds, StringComparer.Ordinal); _compileStatus = ApiMethodCompileOperations.GetStatus(MutationServices, _existing); } } catch (Exception ex) { _formError = ex.Message; } } _loading = false; } private Task ValidateSavedScriptAsync() => RunCompileActionAsync("Validate", (sp, _) => ApiMethodCompileOperations.ValidateAsync(sp, _existing!.Id)); private Task RecompileSavedScriptAsync() => RunCompileActionAsync("Recompile", (sp, user) => ApiMethodCompileOperations.RecompileAsync(sp, _existing!.Id, user)); /// /// Runs a compile action under the same Designer gate as the management commands, in a /// fresh DI scope (the recompile writes an audit row), then refreshes the status badge. /// private async Task RunCompileActionAsync( string action, Func> operation) { if (_existing is null) return; _formError = null; _compiling = true; try { var user = await MutationAuth.RequireRoleAsync(Roles.Designer); await using var scope = MutationServices.GetRequiredService().CreateAsyncScope(); _compileResult = await operation(scope.ServiceProvider, user); _compileAction = action; var saved = await scope.ServiceProvider.GetRequiredService().GetApiMethodByIdAsync(_existing.Id); _compileStatus = saved is null ? null : ApiMethodCompileOperations.GetStatus(scope.ServiceProvider, saved); } catch (Exception ex) { _formError = ex.Message; } finally { _compiling = false; } } private void ToggleKey(string keyId, bool isChecked) { if (isChecked) _selectedKeyIds.Add(keyId); else _selectedKeyIds.Remove(keyId); } private async Task Save() { _formError = null; if (string.IsNullOrWhiteSpace(_name) || string.IsNullOrWhiteSpace(_script)) { _formError = "Name and script required."; return; } try { var methodName = _existing?.Name ?? _name.Trim(); await MutationServices.ExecuteConfigurationAsync(_existing is not null // Update is preserve-if-null (CLI-2): "" clears a removed definition. ? new UpdateApiMethodCommand(_existing.Id, _script, _timeoutSeconds, _params?.Trim() ?? "", _returns?.Trim() ?? "", _traceAllCalls) : new CreateApiMethodCommand(methodName, _script, _timeoutSeconds, _params?.Trim(), _returns?.Trim(), _traceAllCalls)); // Reconcile per-key method-scopes for the affected keys (added/removed vs. load time). if (!await ReconcileKeyScopesAsync(methodName)) return; NavigationManager.NavigateTo("/design/external-systems"); } catch (Exception ex) { _formError = ex.Message; } } /// /// Pushes this method's name into / out of each affected key's scope set. Returns false /// (leaving a form error) when a revoke would empty a key's scopes — the server rejects /// empty scope sets, so we abort rather than push one. Scopes are read fresh per affected /// key so we never clobber unrelated method-scopes. /// private async Task ReconcileKeyScopesAsync(string methodName) { var affected = _selectedKeyIds.Except(_initialKeyIds, StringComparer.Ordinal) .Concat(_initialKeyIds.Except(_selectedKeyIds, StringComparer.Ordinal)) .ToHashSet(StringComparer.Ordinal); // Read each affected key's CURRENT full scope set so add/remove preserves other methods. var currentMethodsByKey = new Dictionary>(StringComparer.Ordinal); foreach (var keyId in affected) { currentMethodsByKey[keyId] = await ApiKeyAdmin.GetMethodsForKeyAsync(keyId); } var keyNamesById = _allKeys.ToDictionary(k => k.KeyId, k => k.Name, StringComparer.Ordinal); var plan = ApiMethodKeyScopeReconciler.Reconcile( methodName, _selectedKeyIds, _initialKeyIds, currentMethodsByKey, keyNamesById); // Empty-last-scope guard: a key cannot end up with zero scopes (server rejects it). if (plan.EmptyScopeKeyNames.Count > 0) { var names = string.Join(", ", plan.EmptyScopeKeyNames); _formError = $"Cannot revoke this method from key(s) [{names}] — it would leave them with no methods. " + "Disable or delete the key instead, or grant it another method first."; return false; } try { foreach (var update in plan.Updates) { await MutationAuth.RequireRoleAsync(Roles.Designer); var ok = await ApiKeyAdmin.SetMethodsAsync(update.KeyId, update.NewMethods); if (!ok) throw new InvalidOperationException( $"Key '{NameFor(update.KeyId)}' was not found in the key store."); } } catch (Exception ex) { throw new InvalidOperationException( $"Method '{methodName}' was saved, but updating approved-key scopes failed partway: {ex.Message} " + "Some keys may be partially updated — review them on the API Keys page and retry.", ex); } // Selection is now the baseline (matters if save is retried without reload). _initialKeyIds = new HashSet(_selectedKeyIds, StringComparer.Ordinal); return true; } // Returns the display name for a keyId if available from the loaded key list, else the id itself. private string NameFor(string keyId) => _allKeys.FirstOrDefault(k => string.Equals(k.KeyId, keyId, StringComparison.Ordinal))?.Name ?? keyId; private void GoBack() => NavigationManager.NavigateTo("/design/external-systems"); /// Cancels an in-flight Test Run when the component is torn down. public void Dispose() { _disposed = true; _runCts?.Cancel(); _runCts?.Dispose(); _runCts = null; } private void ToggleTestRunPanel() => _showTestRun = !_showTestRun; private async Task RunInSandboxAsync() { _runCts?.Cancel(); _runCts = new CancellationTokenSource(); _running = true; _runResult = null; StateHasChanged(); try { var jsonParams = _paramValues.ToDictionary( kv => kv.Key, kv => System.Text.Json.JsonSerializer.SerializeToElement(kv.Value)); var request = new ScriptAnalysis.SandboxRunRequest( _script, jsonParams, TimeoutSeconds: _timeoutSeconds, Kind: ScriptAnalysis.ScriptKind.InboundApi); _runResult = await AnalysisService.RunInSandboxAsync(request, _runCts.Token); } catch (OperationCanceledException) { /* superseded by next Run click */ } catch (Exception ex) { _runResult = new ScriptAnalysis.SandboxRunResult( Success: false, ReturnValueJson: null, ReturnTypeName: null, ConsoleOutput: "", Error: $"Unexpected: {ex.GetType().Name}: {ex.Message}", ErrorKind: ScriptAnalysis.SandboxErrorKind.RuntimeError, DurationMs: 0, Markers: null); } finally { _running = false; // The run may have been cancelled BY Dispose — the component is gone, don't re-render it. if (!_disposed) StateHasChanged(); } } private static string ErrorKindLabel(ScriptAnalysis.SandboxErrorKind kind) => kind switch { ScriptAnalysis.SandboxErrorKind.CompileError => "Compile error", ScriptAnalysis.SandboxErrorKind.SandboxLimitation => "Sandbox limitation", ScriptAnalysis.SandboxErrorKind.RuntimeError => "Runtime error", ScriptAnalysis.SandboxErrorKind.Timeout => "Timeout", _ => "Error" }; } @code { private readonly string _fieldScope = "field-" + Guid.NewGuid().ToString("N"); }