diff --git a/src/AzureIoTHub.Portal.Client/Pages/EdgeModels/CreateEdgeModelsPage.razor b/src/AzureIoTHub.Portal.Client/Pages/EdgeModels/CreateEdgeModelsPage.razor index c6714e515..4fdaca147 100644 --- a/src/AzureIoTHub.Portal.Client/Pages/EdgeModels/CreateEdgeModelsPage.razor +++ b/src/AzureIoTHub.Portal.Client/Pages/EdgeModels/CreateEdgeModelsPage.razor @@ -216,9 +216,9 @@ private bool isProcessing; private IoTEdgeModel Model = new IoTEdgeModel() - { - ModelId = Guid.NewGuid().ToString() - }; + { + ModelId = Guid.NewGuid().ToString() + }; // Used to manage the picture private MultipartFormDataContent content; @@ -261,7 +261,7 @@ DialogOptions options = new DialogOptions() { MaxWidth = MaxWidth.Medium, FullWidth = true, CloseButton = true }; - if (!string.IsNullOrWhiteSpace( module.ModuleName)) + if (!string.IsNullOrWhiteSpace(module.ModuleName)) { var result = await DialogService.Show(module.ModuleName, parameters, options).Result; @@ -312,7 +312,7 @@ return; } - if(Model.EdgeRoutes.Any() && !edgeRouteValidator.Validate(Model.EdgeRoutes).IsValid) + if (Model.EdgeRoutes.Any() && !edgeRouteValidator.Validate(Model.EdgeRoutes).IsValid) { Snackbar.Add("One or more validation errors occured with the routes.", Severity.Error); @@ -327,7 +327,6 @@ this.Model.EdgeModules = edgeModules; await EdgeModelService.CreateIoTEdgeModel(Model); - //result.EnsureSuccessStatusCode(); if (content is not null) { @@ -337,10 +336,14 @@ this.Snackbar.Add("Device model successfully created.", Severity.Success); this.NavigationManager.NavigateTo("/edge/models"); } - catch(ProblemDetailsException exception) + catch (ProblemDetailsException exception) { Error?.ProcessProblemDetails(exception); } + finally + { + isProcessing = false; + } } private void DeleteModule(IoTEdgeModule module) diff --git a/src/AzureIoTHub.Portal.Client/Pages/EdgeModels/EdgeModule/ModuleDialogTab1.razor b/src/AzureIoTHub.Portal.Client/Pages/EdgeModels/EdgeModule/ModuleDialogTab1.razor index d0180f8ff..99d541598 100644 --- a/src/AzureIoTHub.Portal.Client/Pages/EdgeModels/EdgeModule/ModuleDialogTab1.razor +++ b/src/AzureIoTHub.Portal.Client/Pages/EdgeModels/EdgeModule/ModuleDialogTab1.razor @@ -21,12 +21,15 @@ + For="@(() => Context.Name)" + Margin="Margin.Dense" + Variant="Variant.Outlined"> + Margin="Margin.Dense" + Variant="Variant.Outlined"> diff --git a/src/AzureIoTHub.Portal.Server/Services/ConfigService.cs b/src/AzureIoTHub.Portal.Server/Services/ConfigService.cs index fceb850b8..3dcc86ec8 100644 --- a/src/AzureIoTHub.Portal.Server/Services/ConfigService.cs +++ b/src/AzureIoTHub.Portal.Server/Services/ConfigService.cs @@ -236,16 +236,6 @@ public async Task RollOutEdgeModelConfiguration(IoTEdgeModel edgeModel) .ToLowerInvariant() .Replace(" ", "-", StringComparison.OrdinalIgnoreCase); - foreach (var item in configurations) - { - if (!item.Id.StartsWith(configurationNamePrefix, StringComparison.OrdinalIgnoreCase)) - { - continue; - } - - await this.registryManager.RemoveConfigurationAsync(item.Id); - } - var newConfiguration = new Configuration($"{configurationNamePrefix}-{DateTime.UtcNow.Ticks}"); newConfiguration.Labels.Add("created-by", "Azure IoT hub Portal"); newConfiguration.TargetCondition = $"tags.modelId = '{edgeModel.ModelId}'"; @@ -261,6 +251,16 @@ public async Task RollOutEdgeModelConfiguration(IoTEdgeModel edgeModel) { throw new InternalServerErrorException("Unable to create configuration.", e); } + + foreach (var item in configurations) + { + if (!item.Id.StartsWith(configurationNamePrefix, StringComparison.OrdinalIgnoreCase)) + { + continue; + } + + await this.registryManager.RemoveConfigurationAsync(item.Id); + } } public async Task RollOutDeviceConfiguration( diff --git a/src/AzureIoTHub.Portal.Server/Services/EdgeModelService.cs b/src/AzureIoTHub.Portal.Server/Services/EdgeModelService.cs index 56d9cfb50..d7e11ee9d 100644 --- a/src/AzureIoTHub.Portal.Server/Services/EdgeModelService.cs +++ b/src/AzureIoTHub.Portal.Server/Services/EdgeModelService.cs @@ -84,31 +84,27 @@ public IEnumerable GetEdgeModels() /// public async Task CreateEdgeModel(IoTEdgeModel edgeModel) { - if (!string.IsNullOrEmpty(edgeModel?.ModelId)) + try { - try + var edgeModelEntity = await this.edgeModelRepository.GetByIdAsync(edgeModel?.ModelId); + if (edgeModelEntity == null) { - var edgeModelEntity = await this.edgeModelRepository.GetByIdAsync(edgeModel.ModelId); - if (edgeModelEntity == null) - { - edgeModelEntity = this.mapper.Map(edgeModel); - await this.edgeModelRepository.InsertAsync(edgeModelEntity); - await this.unitOfWork.SaveAsync(); - } - else - { - throw new ResourceAlreadyExistsException($"The edge model with id {edgeModel?.ModelId} already exists"); - } + edgeModelEntity = this.mapper.Map(edgeModel); + await this.edgeModelRepository.InsertAsync(edgeModelEntity); + await this.unitOfWork.SaveAsync(); } - catch (DbUpdateException e) + else { - throw new InternalServerErrorException($"Unable to create the device model with id {edgeModel?.ModelId}", e); + throw new ResourceAlreadyExistsException($"The edge model with id {edgeModel?.ModelId} already exists"); } - - await SaveModuleCommands(edgeModel); - await this.configService.RollOutEdgeModelConfiguration(edgeModel); + } + catch (DbUpdateException e) + { + throw new InternalServerErrorException($"Unable to create the device model with id {edgeModel?.ModelId}", e); } + await SaveModuleCommands(edgeModel); + await this.configService.RollOutEdgeModelConfiguration(edgeModel); } /// @@ -204,28 +200,25 @@ public async Task GetEdgeModel(string modelId) /// public async Task UpdateEdgeModel(IoTEdgeModel edgeModel) { - if (!string.IsNullOrEmpty(edgeModel?.ModelId)) + try { - try + var edgeModelEntity = await this.edgeModelRepository.GetByIdAsync(edgeModel?.ModelId); + if (edgeModelEntity == null) { - var edgeModelEntity = await this.edgeModelRepository.GetByIdAsync(edgeModel.ModelId); - if (edgeModelEntity == null) - { - throw new ResourceNotFoundException($"The edge model with id {edgeModel.ModelId} doesn't exist"); - } + throw new ResourceNotFoundException($"The edge model with id {edgeModel?.ModelId} doesn't exist"); + } - _ = this.mapper.Map(edgeModel, edgeModelEntity); - this.edgeModelRepository.Update(edgeModelEntity); + _ = this.mapper.Map(edgeModel, edgeModelEntity); + this.edgeModelRepository.Update(edgeModelEntity); - await this.unitOfWork.SaveAsync(); + await this.unitOfWork.SaveAsync(); - await SaveModuleCommands(edgeModel); - await this.configService.RollOutEdgeModelConfiguration(edgeModel); - } - catch (DbUpdateException e) - { - throw new InternalServerErrorException($"Unable to create the device model with id {edgeModel?.ModelId}", e); - } + await SaveModuleCommands(edgeModel); + await this.configService.RollOutEdgeModelConfiguration(edgeModel); + } + catch (DbUpdateException e) + { + throw new InternalServerErrorException($"Unable to create the device model with id {edgeModel?.ModelId}", e); } } diff --git a/src/AzureIoTHub.Portal.Shared/Models/v1.0/IoTEdgeModuleEnvironmentVariable.cs b/src/AzureIoTHub.Portal.Shared/Models/v1.0/IoTEdgeModuleEnvironmentVariable.cs index 8b888b2a8..98b0eae11 100644 --- a/src/AzureIoTHub.Portal.Shared/Models/v1.0/IoTEdgeModuleEnvironmentVariable.cs +++ b/src/AzureIoTHub.Portal.Shared/Models/v1.0/IoTEdgeModuleEnvironmentVariable.cs @@ -3,11 +3,14 @@ namespace AzureIoTHub.Portal.Shared.Models.v10 { + using System.ComponentModel.DataAnnotations; + public class IoTEdgeModuleEnvironmentVariable { /// /// The module environment variable name /// + [RegularExpression("^[^\\.^\\$^\\#$\\ ]{1,128}$", ErrorMessage = "Variable name should be less than 128 characters and must not contain Control Characters, '.', '$', '#', or ' '.")] public string Name { get; set; } ///