Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync GreenGrassDeployment #2120

Merged
merged 10 commits into from
May 31, 2023
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// Copyright (c) CGI France. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace AzureIoTHub.Portal.Infrastructure.Jobs.AWS
{
using Amazon.IoT;
using AutoMapper;
using AzureIoTHub.Portal.Application.Managers;
using AzureIoTHub.Portal.Domain.Repositories;
using AzureIoTHub.Portal.Domain;
using Microsoft.Extensions.Logging;
using Quartz;
using Amazon.GreengrassV2;
using AzureIoTHub.Portal.Models.v10;
using Amazon.GreengrassV2.Model;
using AzureIoTHub.Portal.Domain.Entities;

[DisallowConcurrentExecution]
public class SyncGreenGrassDeploymentsJob : IJob
{
private readonly ILogger<SyncThingTypesJob> logger;
private readonly IMapper mapper;
private readonly IUnitOfWork unitOfWork;
private readonly IEdgeDeviceModelRepository edgeDeviceModelRepository;
private readonly IAmazonIoT amazonIoTClient;
private readonly IAmazonGreengrassV2 amazonGreenGrass;
private readonly IDeviceModelImageManager deviceModelImageManager;

public SyncGreenGrassDeploymentsJob(
ILogger<SyncThingTypesJob> logger,
IMapper mapper,
IUnitOfWork unitOfWork,
IEdgeDeviceModelRepository edgeDeviceModelRepository,
IAmazonIoT amazonIoTClient,
IAmazonGreengrassV2 amazonGreenGrass,
IDeviceModelImageManager awsImageManager)
{
this.deviceModelImageManager = awsImageManager;
this.mapper = mapper;
this.unitOfWork = unitOfWork;
this.edgeDeviceModelRepository = edgeDeviceModelRepository;
this.amazonIoTClient = amazonIoTClient;
this.amazonGreenGrass = amazonGreenGrass;
this.logger = logger;
}


public async Task Execute(IJobExecutionContext context)
{
try
{
this.logger.LogInformation("Start of sync Greengrass Deployents job");

await SyncGreenGrassDeployments();

this.logger.LogInformation("End of sync Greengrass Deployents job");
}
catch (Exception e)
{
this.logger.LogError(e, "Sync Greengrass Deployents job has failed");
}
Comment on lines +59 to +62

Check notice

Code scanning / CodeQL

Generic catch clause

Generic catch clause.
}

private async Task SyncGreenGrassDeployments()
{
var awsGreenGrassDeployments = await GetAllGreenGrassDeployments();

foreach (var deployment in awsGreenGrassDeployments)
{
await CreateOrUpdateGreenGrassDeployment(deployment);
}
}

private async Task<List<IoTEdgeModel>> GetAllGreenGrassDeployments()
{
var deployments = new List<IoTEdgeModel>();

var nextToken = string.Empty;

var getAllAwsGreenGrassDeployments = await this.amazonGreenGrass.ListDeploymentsAsync(
new ListDeploymentsRequest
{
NextToken = nextToken,
});

foreach (var deployment in getAllAwsGreenGrassDeployments.Deployments)
{
var iotEdgeModel = new IoTEdgeModel
{
ModelId = deployment.DeploymentId, //Instead of giving a random Id here, we can give the deploymentID
Name = deployment.DeploymentName,
ExternalIdentifier = deployment.DeploymentId
};
deployments.Add(iotEdgeModel);
}
Fixed Show fixed Hide fixed
return deployments;
}

private async Task CreateOrUpdateGreenGrassDeployment(IoTEdgeModel iotEdgeModel)
{

var iotEdgeModels = (await this.edgeDeviceModelRepository.GetAllAsync()).Where(edge => edge.ExternalIdentifier == iotEdgeModel.ExternalIdentifier).ToList();
if (iotEdgeModels.Count == 0)
{
var edgeModel = this.mapper.Map<EdgeDeviceModel>(iotEdgeModel);

await this.edgeDeviceModelRepository.InsertAsync(edgeModel);
await this.unitOfWork.SaveAsync();
_ = this.deviceModelImageManager.SetDefaultImageToModel(edgeModel.Id);
}

}

/*private async Task DeleteGreenGrassDeployments(List<IoTEdgeModel> edgeModels)
{

}*/
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ private static IServiceCollection ConfigureAWSSyncJobs(this IServiceCollection s
.WithSimpleSchedule(s => s
.WithIntervalInMinutes(configuration.SyncDatabaseJobRefreshIntervalInMinutes)
.RepeatForever()));

_ = q.AddJob<SyncGreenGrassDeploymentsJob>(j => j.WithIdentity(nameof(SyncGreenGrassDeploymentsJob)))
.AddTrigger(t => t
.WithIdentity($"{nameof(SyncGreenGrassDeploymentsJob)}")
.ForJob(nameof(SyncGreenGrassDeploymentsJob))
.WithSimpleSchedule(s => s
.WithIntervalInMinutes(configuration.SyncDatabaseJobRefreshIntervalInMinutes)
.RepeatForever()));
});
}

Expand Down