-
Notifications
You must be signed in to change notification settings - Fork 12
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
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
5ae5209
Sync GreenGrassDeployment (Started)
ssgueye2 d7a1ed1
Sync Deployments (waiting for one test)
ssgueye2 d3e9c80
Sync Deployment (DONE)
ssgueye2 a965d8b
remove getComponentIfExist
ssgueye2 4607e65
Sync GreenGrassDeployment (Started)
ssgueye2 89f7fe9
Sync Deployments (waiting for one test)
ssgueye2 e3148d5
Sync Deployment (DONE)
ssgueye2 9c67f6e
remove getComponentIfExist
ssgueye2 e732724
update Sync Deployment With Dynamic Thing group
ssgueye2 b3c6469
Merge branch '#2073-Sync-AWS-GreenGrass-Deployment' of https://github…
ssgueye2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
119 changes: 119 additions & 0 deletions
119
src/AzureIoTHub.Portal.Infrastructure/Jobs/AWS/SyncGreenGrassDeploymentsJob.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
|
||
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); | ||
} | ||
|
||
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) | ||
{ | ||
|
||
}*/ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check notice
Code scanning / CodeQL
Generic catch clause