feat(adminui): F15.2 batch 1 — Namespace + DriverInstance live-edit CRUD
v2-ci / build (push) Failing after 34s
v2-ci / unit-tests (tests/Core/ZB.MOM.WW.OtOpcUa.Cluster.Tests) (push) Has been skipped
v2-ci / unit-tests (tests/Server/ZB.MOM.WW.OtOpcUa.ControlPlane.Tests) (push) Has been skipped
v2-ci / unit-tests (tests/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer.Tests) (push) Has been skipped
v2-ci / unit-tests (tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests) (push) Has been skipped
v2-ci / unit-tests (tests/Server/ZB.MOM.WW.OtOpcUa.Security.Tests) (push) Has been skipped
v2-ci / integration (push) Has been skipped

Pattern proof for the live-edit forms gated by Phases A–D's read views.
Each entity gets a single edit page handling both create (route param
omitted) and update (route param present) modes, with RowVersion-based
optimistic concurrency checked against EF Core's
DbUpdateConcurrencyException.

Pattern:
- @page "/clusters/{id}/<thing>/new"
- @page "/clusters/{id}/<thing>/{rowId}"
- IsNew computed from rowId presence
- EditForm + DataAnnotations validation
- byte[] RowVersion stashed on FormModel; assigned to
  Entry(e).Property(e => e.RowVersion).OriginalValue before SaveChanges
- Delete button (edit mode only) flows through the same RowVersion check
- Concurrency conflict surfaces as an inline error panel; user reloads

This batch:
- NamespaceEdit.razor          — small entity, validates the pattern
- DriverEdit.razor             — keystone for everything downstream
                                 (Equipment/Tag/VirtualTag/ScriptedAlarm),
                                 JSON config editor per Q1 with reformat
                                 on save and validation pre-flight
- ClusterNamespaces row gains an Edit button + New action
- ClusterDrivers expanded view gains an Edit button + New action

Equipment/UnsArea/UnsLine/Tag/ACL/VirtualTag/ScriptedAlarm/Script forms
follow this same template in subsequent F15.2 batches.

All 9 integration tests still green; no v2 test regressions.
This commit is contained in:
Joseph Doherty
2026-05-26 08:14:36 -04:00
parent d055cb059e
commit 5ae67a48ba
4 changed files with 574 additions and 0 deletions
@@ -0,0 +1,323 @@
@page "/clusters/{ClusterId}/drivers/new"
@page "/clusters/{ClusterId}/drivers/{DriverInstanceId}"
@* Per Q1 of the AdminUI rebuild plan — JSON editor only, typed driver editors deferred.
DriverInstance is the keystone for everything downstream (Equipment, Tag, VirtualTag,
ScriptedAlarm all reference DriverInstanceId), so this is the second edit page after
Namespace. *@
@attribute [Microsoft.AspNetCore.Authorization.Authorize]
@rendermode RenderMode.InteractiveServer
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.EntityFrameworkCore
@using System.ComponentModel.DataAnnotations
@using ZB.MOM.WW.OtOpcUa.Configuration
@using ZB.MOM.WW.OtOpcUa.Configuration.Entities
@inject IDbContextFactory<OtOpcUaConfigDbContext> DbFactory
@inject NavigationManager Nav
<div class="d-flex justify-content-between align-items-center mb-3">
<h4 class="mb-0">@(IsNew ? "New driver instance" : "Edit driver instance") &middot; <span class="mono">@ClusterId</span></h4>
<a href="/clusters/@ClusterId/drivers" class="btn btn-outline-secondary btn-sm">Cancel</a>
</div>
<ClusterNav ClusterId="@ClusterId" ActiveTab="drivers" />
@if (!_loaded)
{
<p>Loading…</p>
}
else if (!IsNew && _existing is null)
{
<section class="panel notice rise" style="animation-delay:.02s">
Driver instance <span class="mono">@DriverInstanceId</span> was not found in cluster <span class="mono">@ClusterId</span>.
</section>
}
else
{
<EditForm Model="_form" OnValidSubmit="SubmitAsync" FormName="driverEdit">
<DataAnnotationsValidator />
<section class="panel rise" style="animation-delay:.02s">
<div class="panel-head">@(IsNew ? "Identity" : $"Edit {_form.DriverInstanceId}")</div>
<div style="padding:1rem">
<div class="row">
<div class="col-md-6 mb-3">
<label class="form-label" for="instId">DriverInstanceId</label>
<InputText id="instId" @bind-Value="_form.DriverInstanceId" disabled="@(!IsNew)"
class="form-control form-control-sm mono"
placeholder="drv-modbus-line3-01" />
</div>
<div class="col-md-6 mb-3">
<label class="form-label" for="name">Display name</label>
<InputText id="name" @bind-Value="_form.Name" class="form-control form-control-sm"
placeholder="Line 3 Modbus" />
</div>
</div>
<div class="row">
<div class="col-md-6 mb-3">
<label class="form-label" for="dtype">Driver type</label>
<InputSelect id="dtype" @bind-Value="_form.DriverType" disabled="@(!IsNew)"
class="form-select form-select-sm">
<option value="ModbusTcp">ModbusTcp</option>
<option value="AbCip">AbCip</option>
<option value="AbLegacy">AbLegacy</option>
<option value="S7">S7</option>
<option value="TwinCat">TwinCat</option>
<option value="Focas">Focas</option>
<option value="OpcUaClient">OpcUaClient</option>
<option value="Galaxy">Galaxy</option>
<option value="Historian.Wonderware">Historian.Wonderware</option>
</InputSelect>
<div class="form-text">Cannot be changed after creation — drives the actor type that owns this instance.</div>
</div>
<div class="col-md-6 mb-3">
<label class="form-label" for="ns">Namespace</label>
<InputSelect id="ns" @bind-Value="_form.NamespaceId" class="form-select form-select-sm">
@foreach (var ns in _namespaces)
{
<option value="@ns.NamespaceId">@ns.NamespaceId (@ns.Kind)</option>
}
</InputSelect>
</div>
</div>
<div class="mb-3">
<label class="form-label" for="enabled">Enabled</label>
<div class="form-check form-switch">
<InputCheckbox id="enabled" @bind-Value="_form.Enabled" class="form-check-input" />
<label class="form-check-label" for="enabled">Spawn this driver in deployments</label>
</div>
</div>
</div>
</section>
<section class="panel rise mt-3" style="animation-delay:.08s">
<div class="panel-head">Driver config (JSON)</div>
<div style="padding:1rem">
<InputTextArea @bind-Value="_form.DriverConfig" rows="12"
class="form-control form-control-sm mono"
placeholder='{ "endpoint": "10.0.0.42:502", "slaveId": 1 }' />
<div class="form-text">Schemaless per driver type — validated server-side at deploy time. JSON is reformatted on save.</div>
</div>
</section>
<section class="panel rise mt-3" style="animation-delay:.14s">
<div class="panel-head">Resilience overrides (optional)</div>
<div style="padding:1rem">
<InputTextArea @bind-Value="_form.ResilienceConfig" rows="6"
class="form-control form-control-sm mono"
placeholder='Leave blank to use tier defaults' />
<div class="form-text">Polly pipeline overrides per docs/v2/driver-stability.md — bulkhead, retry counts, breaker thresholds. Null = use the driver type's tier defaults.</div>
</div>
</section>
@if (!string.IsNullOrWhiteSpace(_error))
{
<div class="panel notice mt-3" style="border-color:var(--alert)">@_error</div>
}
<div class="mt-3 d-flex gap-2">
<button type="submit" class="btn btn-primary" disabled="@_busy">
@if (_busy) { <span class="spinner-border spinner-border-sm me-1"></span> }
@(IsNew ? "Create" : "Save changes")
</button>
<a href="/clusters/@ClusterId/drivers" class="btn btn-outline-secondary">Cancel</a>
@if (!IsNew)
{
<button type="button" class="btn btn-outline-danger ms-auto" @onclick="DeleteAsync" disabled="@_busy">
Delete
</button>
}
</div>
</EditForm>
}
@code {
[Parameter] public string ClusterId { get; set; } = "";
[Parameter] public string? DriverInstanceId { get; set; }
private bool IsNew => string.IsNullOrEmpty(DriverInstanceId);
private FormModel _form = new();
private DriverInstance? _existing;
private List<Namespace> _namespaces = new();
private bool _loaded;
private bool _busy;
private string? _error;
protected override async Task OnInitializedAsync()
{
await using var db = await DbFactory.CreateDbContextAsync();
_namespaces = await db.Namespaces.AsNoTracking()
.Where(n => n.ClusterId == ClusterId)
.OrderBy(n => n.NamespaceId)
.ToListAsync();
if (IsNew)
{
_form = new FormModel
{
DriverInstanceId = "",
Name = "",
DriverType = "ModbusTcp",
NamespaceId = _namespaces.FirstOrDefault()?.NamespaceId ?? "",
Enabled = true,
DriverConfig = "{}",
};
}
else
{
_existing = await db.DriverInstances.AsNoTracking()
.FirstOrDefaultAsync(d => d.ClusterId == ClusterId && d.DriverInstanceId == DriverInstanceId);
if (_existing is not null)
{
_form = new FormModel
{
DriverInstanceId = _existing.DriverInstanceId,
Name = _existing.Name,
DriverType = _existing.DriverType,
NamespaceId = _existing.NamespaceId,
Enabled = _existing.Enabled,
DriverConfig = _existing.DriverConfig,
ResilienceConfig = _existing.ResilienceConfig,
RowVersion = _existing.RowVersion,
};
}
}
_loaded = true;
}
private async Task SubmitAsync()
{
_busy = true;
_error = null;
try
{
var normalizedConfig = NormalizeJson(_form.DriverConfig);
if (normalizedConfig is null)
{
_error = "DriverConfig is not valid JSON.";
return;
}
var normalizedResilience = NormalizeOptionalJson(_form.ResilienceConfig);
if (!string.IsNullOrWhiteSpace(_form.ResilienceConfig) && normalizedResilience is null)
{
_error = "ResilienceConfig is not valid JSON. Leave blank to use defaults.";
return;
}
await using var db = await DbFactory.CreateDbContextAsync();
if (IsNew)
{
if (await db.DriverInstances.AnyAsync(d => d.DriverInstanceId == _form.DriverInstanceId))
{
_error = $"Driver instance '{_form.DriverInstanceId}' already exists.";
return;
}
db.DriverInstances.Add(new DriverInstance
{
DriverInstanceId = _form.DriverInstanceId,
ClusterId = ClusterId,
NamespaceId = _form.NamespaceId,
Name = _form.Name,
DriverType = _form.DriverType,
Enabled = _form.Enabled,
DriverConfig = normalizedConfig,
ResilienceConfig = normalizedResilience,
});
}
else
{
var entity = await db.DriverInstances.FirstOrDefaultAsync(
d => d.ClusterId == ClusterId && d.DriverInstanceId == DriverInstanceId);
if (entity is null)
{
_error = "Row no longer exists.";
return;
}
db.Entry(entity).Property(e => e.RowVersion).OriginalValue = _form.RowVersion;
entity.NamespaceId = _form.NamespaceId;
entity.Name = _form.Name;
entity.Enabled = _form.Enabled;
entity.DriverConfig = normalizedConfig;
entity.ResilienceConfig = normalizedResilience;
}
await db.SaveChangesAsync();
Nav.NavigateTo($"/clusters/{ClusterId}/drivers");
}
catch (DbUpdateConcurrencyException)
{
_error = "Another user changed this driver instance while you were editing. Reload to see the latest values, then re-apply your changes.";
}
catch (Exception ex)
{
_error = ex.Message;
}
finally
{
_busy = false;
}
}
private async Task DeleteAsync()
{
if (IsNew) return;
_busy = true;
_error = null;
try
{
await using var db = await DbFactory.CreateDbContextAsync();
var entity = await db.DriverInstances.FirstOrDefaultAsync(
d => d.ClusterId == ClusterId && d.DriverInstanceId == DriverInstanceId);
if (entity is null)
{
Nav.NavigateTo($"/clusters/{ClusterId}/drivers");
return;
}
db.Entry(entity).Property(e => e.RowVersion).OriginalValue = _form.RowVersion;
db.DriverInstances.Remove(entity);
await db.SaveChangesAsync();
Nav.NavigateTo($"/clusters/{ClusterId}/drivers");
}
catch (DbUpdateConcurrencyException)
{
_error = "Another user changed this driver instance while you were viewing it. Reload before deleting.";
}
catch (Exception ex)
{
_error = $"Delete failed: {ex.Message}. (Likely because equipment/tags still reference this driver — remove them first.)";
}
finally
{
_busy = false;
}
}
private static string? NormalizeJson(string? input)
{
if (string.IsNullOrWhiteSpace(input)) return null;
try
{
using var doc = System.Text.Json.JsonDocument.Parse(input);
return System.Text.Json.JsonSerializer.Serialize(doc.RootElement);
}
catch { return null; }
}
private static string? NormalizeOptionalJson(string? input) =>
string.IsNullOrWhiteSpace(input) ? null : NormalizeJson(input);
private sealed class FormModel
{
[Required, RegularExpression("^[A-Za-z0-9_-]+$", ErrorMessage = "Use letters, digits, dash, underscore.")]
public string DriverInstanceId { get; set; } = "";
[Required]
public string Name { get; set; } = "";
[Required]
public string DriverType { get; set; } = "ModbusTcp";
[Required]
public string NamespaceId { get; set; } = "";
public bool Enabled { get; set; } = true;
[Required]
public string DriverConfig { get; set; } = "{}";
public string? ResilienceConfig { get; set; }
public byte[] RowVersion { get; set; } = [];
}
}