fix(cli): site/data-connection/api-method update are preserve-if-null partial updates (CLI-2, WP2 task 2.8)

Decision 2.8: preserve-if-null on the server. UpdateSiteCommand,
UpdateDataConnectionCommand and UpdateApiMethodCommand fields (except the id)
are now nullable; the ManagementActor handlers apply only non-null values and
treat an empty string as 'clear this optional field'. The CLI update commands
no longer require --name/--protocol/--script and drop the --failover-retry-count
(3) and --timeout (30) defaults, so an omitted option travels as null instead of
overwriting the stored value (site update --name used to null both gRPC
addresses). The site gRPC-address validation now runs on the merged values.

Additive/compatible: the fields only became nullable, so an older CLI that
always sends every field still deserializes (pinned by a test) and now simply
updates every field as before. The Central UI full-entity forms send "" for a
field the operator cleared (description/addresses, removed backup endpoint,
cleared parameter/return definitions) so clearing keeps working.
This commit is contained in:
Joseph Doherty
2026-12-03 09:48:00 -05:00
parent c207809ef3
commit 61c6278bd0
17 changed files with 533 additions and 93 deletions
@@ -2062,16 +2062,25 @@ public class ManagementActor : ReceiveActor
private static async Task<object?> HandleUpdateSite(IServiceProvider sp, UpdateSiteCommand cmd, string user)
{
ValidateSiteGrpcAddresses(cmd.GrpcNodeAAddress, cmd.GrpcNodeBAddress);
var repo = sp.GetRequiredService<ISiteRepository>();
var site = await repo.GetSiteByIdAsync(cmd.SiteId)
?? throw new ManagementCommandException($"Site with ID {cmd.SiteId} not found.");
site.Name = cmd.Name;
site.Description = cmd.Description;
site.NodeAAddress = cmd.NodeAAddress;
site.NodeBAddress = cmd.NodeBAddress;
site.GrpcNodeAAddress = cmd.GrpcNodeAAddress;
site.GrpcNodeBAddress = cmd.GrpcNodeBAddress;
// CLI-2: preserve-if-null partial update. Validate the EFFECTIVE addresses
// (the merge of the command and the stored row) before anything is written.
var grpcNodeA = PartialUpdate.Optional(cmd.GrpcNodeAAddress, site.GrpcNodeAAddress);
var grpcNodeB = PartialUpdate.Optional(cmd.GrpcNodeBAddress, site.GrpcNodeBAddress);
ValidateSiteGrpcAddresses(grpcNodeA, grpcNodeB);
if (cmd.Name is not null)
{
if (string.IsNullOrWhiteSpace(cmd.Name))
throw new ManagementCommandException("Site name must not be empty.");
site.Name = cmd.Name;
}
site.Description = PartialUpdate.Optional(cmd.Description, site.Description);
site.NodeAAddress = PartialUpdate.Optional(cmd.NodeAAddress, site.NodeAAddress);
site.NodeBAddress = PartialUpdate.Optional(cmd.NodeBAddress, site.NodeBAddress);
site.GrpcNodeAAddress = grpcNodeA;
site.GrpcNodeBAddress = grpcNodeB;
await repo.UpdateSiteAsync(site);
await repo.SaveChangesAsync();
var commService = sp.GetService<CommunicationService>();
@@ -2234,14 +2243,33 @@ public class ManagementActor : ReceiveActor
var repo = sp.GetRequiredService<ISiteRepository>();
var conn = await repo.GetDataConnectionByIdAsync(cmd.DataConnectionId)
?? throw new ManagementCommandException($"DataConnection with ID {cmd.DataConnectionId} not found.");
conn.Name = cmd.Name;
conn.Protocol = cmd.Protocol;
// CLI-2: preserve-if-null partial update — an omitted field keeps the stored
// value; an empty string clears an optional configuration.
if (cmd.Name is not null)
{
if (string.IsNullOrWhiteSpace(cmd.Name))
throw new ManagementCommandException("Data connection name must not be empty.");
conn.Name = cmd.Name;
}
if (cmd.Protocol is not null)
{
if (string.IsNullOrWhiteSpace(cmd.Protocol))
throw new ManagementCommandException("Data connection protocol must not be empty.");
conn.Protocol = cmd.Protocol;
}
// Sentinel-preserving merge: a client that round-trips a scrubbed config
// (secrets replaced with the redaction sentinel) keeps the stored secret
// rather than wiping it. Non-sentinel values overwrite as usual.
conn.PrimaryConfiguration = ConfigSecretScrubber.MergeSentinels(cmd.PrimaryConfiguration, conn.PrimaryConfiguration);
conn.BackupConfiguration = ConfigSecretScrubber.MergeSentinels(cmd.BackupConfiguration, conn.BackupConfiguration);
conn.FailoverRetryCount = cmd.FailoverRetryCount;
if (cmd.PrimaryConfiguration is not null)
conn.PrimaryConfiguration = string.IsNullOrWhiteSpace(cmd.PrimaryConfiguration)
? null
: ConfigSecretScrubber.MergeSentinels(cmd.PrimaryConfiguration, conn.PrimaryConfiguration);
if (cmd.BackupConfiguration is not null)
conn.BackupConfiguration = string.IsNullOrWhiteSpace(cmd.BackupConfiguration)
? null
: ConfigSecretScrubber.MergeSentinels(cmd.BackupConfiguration, conn.BackupConfiguration);
if (cmd.FailoverRetryCount is { } failoverRetryCount)
conn.FailoverRetryCount = failoverRetryCount;
await repo.UpdateDataConnectionAsync(conn);
await repo.SaveChangesAsync();
var publicShape = DataConnectionPublicShape(conn);
@@ -3975,10 +4003,19 @@ public class ManagementActor : ReceiveActor
var repo = sp.GetRequiredService<IInboundApiRepository>();
var method = await repo.GetApiMethodByIdAsync(cmd.ApiMethodId)
?? throw new ManagementCommandException($"ApiMethod with ID {cmd.ApiMethodId} not found.");
method.Script = cmd.Script;
method.TimeoutSeconds = cmd.TimeoutSeconds;
method.ParameterDefinitions = cmd.ParameterDefinitions;
method.ReturnDefinition = cmd.ReturnDefinition;
// CLI-2: preserve-if-null partial update — an omitted field keeps the stored
// value (an omitted --timeout used to reset it to 30 and drop both
// definitions); an empty string clears an optional definition.
if (cmd.Script is not null)
{
if (string.IsNullOrWhiteSpace(cmd.Script))
throw new ManagementCommandException("API method script must not be empty.");
method.Script = cmd.Script;
}
if (cmd.TimeoutSeconds is { } timeoutSeconds)
method.TimeoutSeconds = timeoutSeconds;
method.ParameterDefinitions = PartialUpdate.Optional(cmd.ParameterDefinitions, method.ParameterDefinitions);
method.ReturnDefinition = PartialUpdate.Optional(cmd.ReturnDefinition, method.ReturnDefinition);
if (cmd.TraceAllCalls is { } traceAllCalls)
method.TraceAllCalls = traceAllCalls;
await repo.UpdateApiMethodAsync(method);