From 2d35c4b62281aa86f24cabc5f480fa0dbc2f38b1 Mon Sep 17 00:00:00 2001 From: Mirko Da Corte Date: Tue, 8 Oct 2024 19:01:26 +0200 Subject: [PATCH] implement update of connection string --- .../Extensions/LoggerExtensions.cs | 10 ++--- .../Api/InputModels/UpdateNodeConfigInput.cs | 11 ++++- .../Api/Services/INodesControllerService.cs | 2 +- .../Api/Services/NodesControllerService.cs | 44 ++++++++++++++++--- 4 files changed, 53 insertions(+), 14 deletions(-) diff --git a/src/BeehiveManager.Services/Extensions/LoggerExtensions.cs b/src/BeehiveManager.Services/Extensions/LoggerExtensions.cs index 840dfb0..d1ce29b 100644 --- a/src/BeehiveManager.Services/Extensions/LoggerExtensions.cs +++ b/src/BeehiveManager.Services/Extensions/LoggerExtensions.cs @@ -35,11 +35,11 @@ public static class LoggerExtensions new EventId(0, nameof(NodeCashedOut)), "Node {BeeNodeId} cashed out {BzzCashedOut} BZZ with tx hashes {TxHashes}"); - private static readonly Action _nodeConfigurationUpdated = - LoggerMessage.Define( + private static readonly Action _nodeConfigurationUpdated = + LoggerMessage.Define( LogLevel.Information, new EventId(7, nameof(NodeConfigurationUpdated)), - "Node {BeeNodeId} updated configuration. EnableBatchCreation: {EnableBatchCreation}"); + "Node {BeeNodeId} updated configuration"); private static readonly Action _nodeRegistered = LoggerMessage.Define( @@ -120,8 +120,8 @@ public static void FailedToWithdrawBzzOnNodeChequeBook(this ILogger logger, stri public static void NodeCashedOut(this ILogger logger, string beeNodeId, BzzBalance bzzCashedOut, IEnumerable txHashes) => _nodeCashedOut(logger, beeNodeId, bzzCashedOut, txHashes, null!); - public static void NodeConfigurationUpdated(this ILogger logger, string beeNodeid, bool enableBatchCreation) => - _nodeConfigurationUpdated(logger, beeNodeid, enableBatchCreation, null!); + public static void NodeConfigurationUpdated(this ILogger logger, string beeNodeid) => + _nodeConfigurationUpdated(logger, beeNodeid, null!); public static void NodeRegistered(this ILogger logger, string beeNodeId, Uri nodeUrl, int gatewayPort, bool isBatchCreationEnabled) => _nodeRegistered(logger, beeNodeId, nodeUrl, gatewayPort, isBatchCreationEnabled, null!); diff --git a/src/BeehiveManager/Areas/Api/InputModels/UpdateNodeConfigInput.cs b/src/BeehiveManager/Areas/Api/InputModels/UpdateNodeConfigInput.cs index 856e559..a1af8a3 100644 --- a/src/BeehiveManager/Areas/Api/InputModels/UpdateNodeConfigInput.cs +++ b/src/BeehiveManager/Areas/Api/InputModels/UpdateNodeConfigInput.cs @@ -12,10 +12,19 @@ // You should have received a copy of the GNU Affero General Public License along with BeehiveManager. // If not, see . +using System.ComponentModel.DataAnnotations; + namespace Etherna.BeehiveManager.Areas.Api.InputModels { public class UpdateNodeConfigInput { - public bool EnableBatchCreation { get; set; } + [Range(1, 65535)] + public int? ApiPort { get; set; } + + public string? ConnectionScheme { get; set; } + + public bool? EnableBatchCreation { get; set; } + + public string? Hostname { get; set; } } } diff --git a/src/BeehiveManager/Areas/Api/Services/INodesControllerService.cs b/src/BeehiveManager/Areas/Api/Services/INodesControllerService.cs index 30a80d3..27d7db5 100644 --- a/src/BeehiveManager/Areas/Api/Services/INodesControllerService.cs +++ b/src/BeehiveManager/Areas/Api/Services/INodesControllerService.cs @@ -37,6 +37,6 @@ public interface INodesControllerService Task NotifyPinningOfUploadedContentAsync(string id, SwarmHash hash); Task RemoveBeeNodeAsync(string id); Task ReuploadResourceToNetworkFromNodeAsync(string id, SwarmHash hash); - Task UpdateNodeConfigAsync(string id, UpdateNodeConfigInput config); + Task UpdateNodeConfigAsync(string id, UpdateNodeConfigInput newConfig); } } \ No newline at end of file diff --git a/src/BeehiveManager/Areas/Api/Services/NodesControllerService.cs b/src/BeehiveManager/Areas/Api/Services/NodesControllerService.cs index d04d507..ab8ee80 100644 --- a/src/BeehiveManager/Areas/Api/Services/NodesControllerService.cs +++ b/src/BeehiveManager/Areas/Api/Services/NodesControllerService.cs @@ -162,21 +162,51 @@ public async Task ReuploadResourceToNetworkFromNodeAsync(string id, SwarmHash ha await beeNodeInstance.Client.ReuploadContentAsync(hash); } - public async Task UpdateNodeConfigAsync(string id, UpdateNodeConfigInput config) + public async Task UpdateNodeConfigAsync(string id, UpdateNodeConfigInput newConfig) { ArgumentNullException.ThrowIfNull(id, nameof(id)); - ArgumentNullException.ThrowIfNull(config, nameof(config)); + ArgumentNullException.ThrowIfNull(newConfig, nameof(newConfig)); - // Update live instance. + // Update live instance and db config. + bool rebuildLiveInstance = false; var nodeLiveInstance = await beeNodeLiveManager.GetBeeNodeLiveInstanceAsync(id); - nodeLiveInstance.IsBatchCreationEnabled = config.EnableBatchCreation; + var nodeDb = await beehiveDbContext.BeeNodes.FindOneAsync(id); + + if (newConfig.ConnectionScheme != null) + { + rebuildLiveInstance = true; + nodeDb.ConnectionScheme = newConfig.ConnectionScheme; + } + + if (newConfig.EnableBatchCreation != null) + { + nodeLiveInstance.IsBatchCreationEnabled = newConfig.EnableBatchCreation.Value; + nodeDb.IsBatchCreationEnabled = newConfig.EnableBatchCreation.Value; + } + + if (newConfig.ApiPort != null) + { + rebuildLiveInstance = true; + nodeDb.GatewayPort = newConfig.ApiPort.Value; + } + + if (newConfig.Hostname != null) + { + rebuildLiveInstance = true; + nodeDb.Hostname = newConfig.Hostname; + } // Update config on db. - var node = await beehiveDbContext.BeeNodes.FindOneAsync(id); - node.IsBatchCreationEnabled = config.EnableBatchCreation; await beehiveDbContext.SaveChangesAsync(); + + // Rebuild live instance if necessary (changed connection string) + if (rebuildLiveInstance) + { + beeNodeLiveManager.RemoveBeeNode(id); + await beeNodeLiveManager.AddBeeNodeAsync(nodeDb); + } - logger.NodeConfigurationUpdated(id, config.EnableBatchCreation); + logger.NodeConfigurationUpdated(id); } } }