Skip to content

Commit

Permalink
Fix #1498 - Add environment variable validation to our models (#1509)
Browse files Browse the repository at this point in the history
  • Loading branch information
kbeaugrand authored Nov 6, 2022
1 parent 75de3fc commit f970b71
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<ModuleDialog>(module.ModuleName, parameters, options).Result;

Expand Down Expand Up @@ -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);
Expand All @@ -327,7 +327,6 @@
this.Model.EdgeModules = edgeModules;

await EdgeModelService.CreateIoTEdgeModel(Model);
//result.EnsureSuccessStatusCode();

if (content is not null)
{
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@
<RowTemplate Context="Context">
<MudTd DataLabel="Key" Style="word-break: break-all;">
<MudTextField @bind-Value="@Context.Name"
Margin="Margin.Dense" Variant="Variant.Outlined"></MudTextField>
For="@(() => Context.Name)"
Margin="Margin.Dense"
Variant="Variant.Outlined"></MudTextField>
</MudTd>
<MudTd DataLabel="Value" Style="word-break: break-all; ">

<MudTextField @bind-Value="@Context.Value"
Margin="Margin.Dense" Variant="Variant.Outlined"></MudTextField>
Margin="Margin.Dense"
Variant="Variant.Outlined"></MudTextField>
</MudTd>
<MudTd DataLabel="Delete" Style="text-align: center">
<MudTooltip Text="Delete">
Expand Down
20 changes: 10 additions & 10 deletions src/AzureIoTHub.Portal.Server/Services/ConfigService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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}'";
Expand All @@ -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(
Expand Down
63 changes: 28 additions & 35 deletions src/AzureIoTHub.Portal.Server/Services/EdgeModelService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,31 +84,27 @@ public IEnumerable<IoTEdgeModelListItem> GetEdgeModels()
/// <exception cref="InternalServerErrorException"></exception>
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<EdgeDeviceModel>(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<EdgeDeviceModel>(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);
}

/// <summary>
Expand Down Expand Up @@ -204,28 +200,25 @@ public async Task<IoTEdgeModel> GetEdgeModel(string modelId)
/// <exception cref="InternalServerErrorException"></exception>
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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@

namespace AzureIoTHub.Portal.Shared.Models.v10
{
using System.ComponentModel.DataAnnotations;

public class IoTEdgeModuleEnvironmentVariable
{
/// <summary>
/// The module environment variable name
/// </summary>
[RegularExpression("^[^\\.^\\$^\\#$\\ ]{1,128}$", ErrorMessage = "Variable name should be less than 128 characters and must not contain Control Characters, '.', '$', '#', or ' '.")]
public string Name { get; set; }

/// <summary>
Expand Down

0 comments on commit f970b71

Please sign in to comment.