From 361792bdbe950c331df60e48cfcb83cb3dc1fcf9 Mon Sep 17 00:00:00 2001 From: delager Date: Fri, 26 May 2023 10:13:24 +0200 Subject: [PATCH] #1922 Delete AWS IoT Device - Delete AWS Device - TU Delete AWS Device --- .../Mappers/AWS/AWSDeviceThingProfile.cs | 5 + .../Services/AWS/IAWSExternalDeviceService.cs | 2 + .../Services/AWS/AWSDeviceService.cs | 11 +- .../Services/AWS/AWSExternalDeviceService.cs | 12 ++ .../Services/AWSDeviceServiceTests.cs | 118 ++++++++++++++++++ .../Services/AWSExternalDeviceServiceTest.cs | 49 ++++++++ 6 files changed, 194 insertions(+), 3 deletions(-) diff --git a/src/AzureIoTHub.Portal.Application/Mappers/AWS/AWSDeviceThingProfile.cs b/src/AzureIoTHub.Portal.Application/Mappers/AWS/AWSDeviceThingProfile.cs index 31fbbbaf0..3f9dddf82 100644 --- a/src/AzureIoTHub.Portal.Application/Mappers/AWS/AWSDeviceThingProfile.cs +++ b/src/AzureIoTHub.Portal.Application/Mappers/AWS/AWSDeviceThingProfile.cs @@ -7,6 +7,7 @@ namespace AzureIoTHub.Portal.Application.Mappers.AWS using Amazon.IoT.Model; using Amazon.IotData.Model; using AutoMapper; + using AzureIoTHub.Portal.Domain.Entities; using Models.v10; using Newtonsoft.Json; @@ -25,6 +26,10 @@ public AWSDeviceThingProfile() .ForPath(dest => dest.AttributePayload.Attributes, opts => opts.MapFrom(src => src.Tags)) .ReverseMap(); + _ = CreateMap() + .ForMember(dest => dest.ThingName, opts => opts.MapFrom(src => src.Name)) + .ReverseMap(); + _ = CreateMap() .ForMember(dest => dest.ThingName, opts => opts.MapFrom(src => src.DeviceName)) .ForMember(dest => dest.Payload, opts => opts.MapFrom(src => EmptyPayload())) diff --git a/src/AzureIoTHub.Portal.Application/Services/AWS/IAWSExternalDeviceService.cs b/src/AzureIoTHub.Portal.Application/Services/AWS/IAWSExternalDeviceService.cs index c3a7ee7e1..57474977f 100644 --- a/src/AzureIoTHub.Portal.Application/Services/AWS/IAWSExternalDeviceService.cs +++ b/src/AzureIoTHub.Portal.Application/Services/AWS/IAWSExternalDeviceService.cs @@ -15,6 +15,8 @@ public interface IAWSExternalDeviceService Task UpdateDevice(UpdateThingRequest device); + Task DeleteDevice(DeleteThingRequest device); + Task GetDeviceShadow(string deviceName); Task UpdateDeviceShadow(UpdateThingShadowRequest shadow); diff --git a/src/AzureIoTHub.Portal.Infrastructure/Services/AWS/AWSDeviceService.cs b/src/AzureIoTHub.Portal.Infrastructure/Services/AWS/AWSDeviceService.cs index 40a1f5a00..24261d205 100644 --- a/src/AzureIoTHub.Portal.Infrastructure/Services/AWS/AWSDeviceService.cs +++ b/src/AzureIoTHub.Portal.Infrastructure/Services/AWS/AWSDeviceService.cs @@ -3,7 +3,6 @@ namespace AzureIoTHub.Portal.Infrastructure.Services.AWS { - using System; using System.Threading.Tasks; using AzureIoTHub.Portal.Application.Services; using Models.v10; @@ -67,9 +66,15 @@ public override async Task UpdateDevice(DeviceDetails device) return await UpdateDeviceInDatabase(device); } - public override Task DeleteDevice(string deviceId) + public override async Task DeleteDevice(string deviceId) { - throw new NotImplementedException(); + //Delete Thing + var device = await deviceRepository.GetByIdAsync(deviceId); + var deleteThingRequest = this.mapper.Map(device); + _ = await this.externalDevicesService.DeleteDevice(deleteThingRequest); + + //Delete Thing in DB + await DeleteDeviceInDatabase(deviceId); } } } diff --git a/src/AzureIoTHub.Portal.Infrastructure/Services/AWS/AWSExternalDeviceService.cs b/src/AzureIoTHub.Portal.Infrastructure/Services/AWS/AWSExternalDeviceService.cs index 6d82ffbae..d13fcae3f 100644 --- a/src/AzureIoTHub.Portal.Infrastructure/Services/AWS/AWSExternalDeviceService.cs +++ b/src/AzureIoTHub.Portal.Infrastructure/Services/AWS/AWSExternalDeviceService.cs @@ -65,6 +65,18 @@ public async Task UpdateDevice(UpdateThingRequest device) return thingResponse; } + public async Task DeleteDevice(DeleteThingRequest device) + { + var deleteResponse = await this.amazonIotClient.DeleteThingAsync(device); + + if (deleteResponse.HttpStatusCode != System.Net.HttpStatusCode.OK) + { + throw new InternalServerErrorException($"Unable to delete the thing with device name : {device.ThingName} due to an error in the Amazon IoT API : {deleteResponse.HttpStatusCode}"); + } + + return deleteResponse; + } + public async Task GetDeviceShadow(string deviceName) { var shadowRequest = new GetThingShadowRequest { ThingName = deviceName }; diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Infrastructure/Services/AWSDeviceServiceTests.cs b/src/AzureIoTHub.Portal.Tests.Unit/Infrastructure/Services/AWSDeviceServiceTests.cs index c0d9ffc76..5704152b1 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Infrastructure/Services/AWSDeviceServiceTests.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Infrastructure/Services/AWSDeviceServiceTests.cs @@ -24,6 +24,7 @@ namespace AzureIoTHub.Portal.Tests.Unit.Infrastructure.Services using AzureIoTHub.Portal.Tests.Unit.UnitTests.Bases; using EntityFramework.Exceptions.Common; using FluentAssertions; + using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Moq; @@ -267,5 +268,122 @@ public async Task UpdateDeviceWhenDbUpdateExceptionIsRaisedCannotInsertNullExcep _ = await act.Should().ThrowAsync(); MockRepository.VerifyAll(); } + + [Test] + public async Task DeleteDevice() + { + // Arrange + var deviceDto = new DeviceDetails + { + DeviceID = Fixture.Create() + }; + + var device = new Device + { + Id = deviceDto.DeviceID, + Tags = Fixture.CreateMany(5).ToList(), + Labels = Fixture.CreateMany