From e6c866333b047754bc0c7e1cd56ae7f410525ab5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20TREMBLAY?= Date: Tue, 30 Aug 2022 16:50:32 +0200 Subject: [PATCH] Fix the problem of the command deletion #1139 --- .../Server/Mappers/EdgeModelMapper.cs | 21 ++++++++++++-- .../Server/Mappers/IEdgeDeviceModelMapper.cs | 3 +- .../Server/Services/EdgeModelService.cs | 28 +++++++------------ 3 files changed, 30 insertions(+), 22 deletions(-) diff --git a/src/AzureIoTHub.Portal/Server/Mappers/EdgeModelMapper.cs b/src/AzureIoTHub.Portal/Server/Mappers/EdgeModelMapper.cs index 4ef2819dc..76482a6d9 100644 --- a/src/AzureIoTHub.Portal/Server/Mappers/EdgeModelMapper.cs +++ b/src/AzureIoTHub.Portal/Server/Mappers/EdgeModelMapper.cs @@ -5,9 +5,12 @@ namespace AzureIoTHub.Portal.Server.Mappers { using System; using System.Collections.Generic; + using System.Linq; using Azure.Data.Tables; using AzureIoTHub.Portal.Models.v10; + using AzureIoTHub.Portal.Server.Entities; using AzureIoTHub.Portal.Server.Managers; + using AzureIoTHub.Portal.Shared.Models.v10; public class EdgeModelMapper : IEdgeDeviceModelMapper { @@ -36,11 +39,10 @@ public IoTEdgeModelListItem CreateEdgeDeviceModelListItem(TableEntity entity) }; } - public IoTEdgeModel CreateEdgeDeviceModel(TableEntity entity, List ioTEdgeModules) + public IoTEdgeModel CreateEdgeDeviceModel(TableEntity entity, List ioTEdgeModules, IEnumerable commands) { ArgumentNullException.ThrowIfNull(entity, nameof(entity)); - - return new IoTEdgeModel + var result = new IoTEdgeModel { ModelId = entity.RowKey, ImageUrl = this.deviceModelImageManager.ComputeImageUri(entity.RowKey), @@ -48,6 +50,19 @@ public IoTEdgeModel CreateEdgeDeviceModel(TableEntity entity, List (x.ModuleName + "-" + command.Name).Equals(command.RowKey, StringComparison.Ordinal)); + if (module == null) + { + continue; + } + module.Commands.Add(new IoTEdgeModuleCommand + { + Name = command.Name, + }); + } + return result; } public void UpdateTableEntity(TableEntity entity, IoTEdgeModel model) diff --git a/src/AzureIoTHub.Portal/Server/Mappers/IEdgeDeviceModelMapper.cs b/src/AzureIoTHub.Portal/Server/Mappers/IEdgeDeviceModelMapper.cs index 2feb1a1f6..314fd3c2e 100644 --- a/src/AzureIoTHub.Portal/Server/Mappers/IEdgeDeviceModelMapper.cs +++ b/src/AzureIoTHub.Portal/Server/Mappers/IEdgeDeviceModelMapper.cs @@ -6,12 +6,13 @@ namespace AzureIoTHub.Portal.Server.Mappers using System.Collections.Generic; using Azure.Data.Tables; using AzureIoTHub.Portal.Models.v10; + using AzureIoTHub.Portal.Server.Entities; public interface IEdgeDeviceModelMapper { IoTEdgeModelListItem CreateEdgeDeviceModelListItem(TableEntity entity); - IoTEdgeModel CreateEdgeDeviceModel(TableEntity entity, List ioTEdgeModules); + IoTEdgeModel CreateEdgeDeviceModel(TableEntity entity, List ioTEdgeModules, IEnumerable commands); void UpdateTableEntity(TableEntity entity, IoTEdgeModel model); } diff --git a/src/AzureIoTHub.Portal/Server/Services/EdgeModelService.cs b/src/AzureIoTHub.Portal/Server/Services/EdgeModelService.cs index b1996fde1..62ccbba9c 100644 --- a/src/AzureIoTHub.Portal/Server/Services/EdgeModelService.cs +++ b/src/AzureIoTHub.Portal/Server/Services/EdgeModelService.cs @@ -89,24 +89,11 @@ public async Task GetEdgeModel(string modelId) var query = await this.tableClientFactory .GetEdgeDeviceTemplates() .GetEntityAsync(DefaultPartitionKey, modelId); - var modules = await this.configService.GetConfigModuleList(modelId); - var commands = this.tableClientFactory.GetEdgeModuleCommands() - .Query($"PartitionKey eq '{modelId}'").ToArray(); - - foreach (var command in commands) - { - foreach (var module in modules) - { - if ((module.ModuleName + "-" + command.Name).Equals(command.RowKey, StringComparison.Ordinal)) - { - module.Commands.Add(new Shared.Models.v10.IoTEdgeModuleCommand { Name = command.Name }); - } - } - } - - return this.edgeDeviceModelMapper.CreateEdgeDeviceModel(query.Value, modules); + .Query(c => c.PartitionKey == modelId) + .ToArray(); + return this.edgeDeviceModelMapper.CreateEdgeDeviceModel(query.Value, modules, commands); } catch (RequestFailedException e) { @@ -114,7 +101,6 @@ public async Task GetEdgeModel(string modelId) { throw new ResourceNotFoundException($"The edge model with id {modelId} doesn't exist"); } - throw new InternalServerErrorException($"Unable to get the edge model with id {modelId}", e); } } @@ -342,9 +328,15 @@ private async Task SaveModuleCommands(IoTEdgeModel deviceModelObject) Timestamp = DateTime.Now, Name = cmd.Name, })).ToArray(); - try { + var existingCommands = this.tableClientFactory.GetEdgeModuleCommands() + .Query(c => c.PartitionKey == deviceModelObject.ModelId) + .ToArray(); + foreach (var command in existingCommands.Where(c => !moduleCommands.Any(x => x.RowKey == c.RowKey))) + { + _ = await this.tableClientFactory.GetEdgeModuleCommands().DeleteEntityAsync(command.PartitionKey, command.RowKey); + } foreach (var moduleCommand in moduleCommands) { _ = this.tableClientFactory