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

Refactoring Sync Things Job #2172

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public interface IAWSExternalDeviceService
Task<UpdateThingShadowResponse> UpdateDeviceShadow(UpdateThingShadowRequest shadow);
Task<int> GetEdgeDeviceNbDevices(IoTEdgeDevice device);

Task<bool?> IsEdgeThingType(DescribeThingTypeResponse thingType);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,35 +81,42 @@ private async Task<List<IoTEdgeModel>> GetAllGreenGrassDeployments()

var nextToken = string.Empty;

var getAllAwsGreenGrassDeployments = await this.amazonGreenGrass.ListDeploymentsAsync(
new ListDeploymentsRequest
do
{
var request = new ListDeploymentsRequest
{
NextToken = nextToken,
HistoryFilter = DeploymentHistoryFilter.LATEST_ONLY
});
};

foreach (var deployment in getAllAwsGreenGrassDeployments.Deployments)
{
var awsThingGroupRegex = new Regex(@"/([^/]+)$");
var matches = awsThingGroupRegex.Match(deployment.TargetArn);
var response = await this.amazonGreenGrass.ListDeploymentsAsync(request);

if (matches.Success && matches.Groups.Count > 1)
foreach (var deployment in response.Deployments)
{
var thinggroupName = matches.Groups[1].Value;
var s = await this.amazonIoTClient.DescribeThingGroupAsync(new Amazon.IoT.Model.DescribeThingGroupRequest { ThingGroupName = thinggroupName });
if (s.QueryString != null)
var awsThingGroupRegex = new Regex(@"/([^/]+)$");
var matches = awsThingGroupRegex.Match(deployment.TargetArn);

if (matches.Success && matches.Groups.Count > 1)
{
var iotEdgeModel = new IoTEdgeModel
var thinggroupName = matches.Groups[1].Value;
var s = await this.amazonIoTClient.DescribeThingGroupAsync(new Amazon.IoT.Model.DescribeThingGroupRequest { ThingGroupName = thinggroupName });
if (s.QueryString != null)
{
ModelId = deployment.DeploymentId, //Instead of giving a random Id here, we can give the deploymentID
Name = deployment.DeploymentName,
ExternalIdentifier = deployment.DeploymentId
};
deployments.Add(iotEdgeModel);
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);
}
}
}

nextToken = response.NextToken;
}
while (!string.IsNullOrEmpty(nextToken));

return deployments;
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace AzureIoTHub.Portal.Infrastructure.Jobs.AWS
using Amazon.IoT.Model;
using AutoMapper;
using AzureIoTHub.Portal.Application.Managers;
using AzureIoTHub.Portal.Application.Services.AWS;
using AzureIoTHub.Portal.Domain;
using AzureIoTHub.Portal.Domain.Entities;
using AzureIoTHub.Portal.Domain.Repositories;
Expand All @@ -24,20 +25,23 @@ public class SyncThingTypesJob : IJob
private readonly IDeviceModelRepository deviceModelRepository;
private readonly IAmazonIoT amazonIoTClient;
private readonly IDeviceModelImageManager deviceModelImageManager;
private readonly IAWSExternalDeviceService awsExternalDeviceService;

public SyncThingTypesJob(
ILogger<SyncThingTypesJob> logger,
IMapper mapper,
IUnitOfWork unitOfWork,
IDeviceModelRepository deviceModelRepository,
IAmazonIoT amazonIoTClient,
IDeviceModelImageManager awsImageManager)
IDeviceModelImageManager awsImageManager,
IAWSExternalDeviceService awsExternalDeviceService)
{
this.deviceModelImageManager = awsImageManager;
this.mapper = mapper;
this.unitOfWork = unitOfWork;
this.deviceModelRepository = deviceModelRepository;
this.amazonIoTClient = amazonIoTClient;
this.awsExternalDeviceService = awsExternalDeviceService;
this.logger = logger;
}

Expand All @@ -64,19 +68,15 @@ private async Task SyncThingTypesAsDeviceModels()

foreach (var thingType in thingTypes)
{
var isEdge = await IsEdgeThingType(thingType);
var isEdge = await awsExternalDeviceService.IsEdgeThingType(thingType);

// Cannot know if the thing type was created for an iotEdge or not, so skipping...
if (!isEdge.HasValue)
{
continue;
}

if (isEdge == true)
{
// TODO: Implement CreateOrUpdateEdgeModel here.
}
else
if (isEdge == false)
{
await CreateOrUpdateDeviceModel(thingType);
}
Expand All @@ -86,38 +86,6 @@ private async Task SyncThingTypesAsDeviceModels()
await DeleteThingTypes(thingTypes);
}

private async Task<bool?> IsEdgeThingType(DescribeThingTypeResponse thingType)
{
var response = await this.amazonIoTClient.ListTagsForResourceAsync(new ListTagsForResourceRequest
{
ResourceArn = thingType.ThingTypeArn
});

do
{
if (response == null || !response.Tags.Any())
{
return null;
}

var iotEdgeTag = response.Tags.Where(c => c.Key.Equals("iotEdge", StringComparison.OrdinalIgnoreCase));

if (!iotEdgeTag.Any())
{
response = await this.amazonIoTClient.ListTagsForResourceAsync(new ListTagsForResourceRequest
{
ResourceArn = thingType.ThingTypeArn,
NextToken = response.NextToken
});

continue;
}

return bool.TryParse(iotEdgeTag.Single().Value, out var result) ? result : null;

} while (true);
}

private async Task<List<DescribeThingTypeResponse>> GetAllThingTypes()
{
var thingTypes = new List<DescribeThingTypeResponse>();
Expand Down
Loading