Skip to content

Commit

Permalink
detach associated certificate in thing
Browse files Browse the repository at this point in the history
  • Loading branch information
ssgueye2 committed Jun 2, 2023
1 parent f37aadc commit 2b1f4cc
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,35 @@ public async Task<UpdateThingResponse> UpdateDevice(UpdateThingRequest device)

public async Task<DeleteThingResponse> DeleteDevice(DeleteThingRequest device)
{
//Retreive all thing princpals and detach it before deleting the thing
var principals = await this.amazonIotClient.ListThingPrincipalsAsync(new ListThingPrincipalsRequest
{
NextToken = string.Empty,
ThingName = device.ThingName
});

if (principals.HttpStatusCode != System.Net.HttpStatusCode.OK)
{
throw new InternalServerErrorException($"Unable to retreive Thing {device.ThingName} principals due to an error in the Amazon IoT API : {principals.HttpStatusCode}");

}

foreach (var principal in principals.Principals)
{
var detachPrincipal = await this.amazonIotClient.DetachThingPrincipalAsync(new DetachThingPrincipalRequest
{
Principal = principal,
ThingName = device.ThingName
});

if (detachPrincipal.HttpStatusCode != System.Net.HttpStatusCode.OK)
{
throw new InternalServerErrorException($"Unable to detach Thing {device.ThingName} principal due to an error in the Amazon IoT API : {detachPrincipal.HttpStatusCode}");

}
}

//Delete the thing type before detaching the princiapl
var deleteResponse = await this.amazonIotClient.DeleteThingAsync(device);

if (deleteResponse.HttpStatusCode != System.Net.HttpStatusCode.OK)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace AzureIoTHub.Portal.Tests.Unit.Infrastructure.Services
{
using System.Collections.Generic;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -253,6 +254,19 @@ public async Task DeleteDeviceShouldReturnAValue()
HttpStatusCode = HttpStatusCode.OK
};

_ = this.mockAmazonIotClient.Setup(iotClient => iotClient.ListThingPrincipalsAsync(It.IsAny<ListThingPrincipalsRequest>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new ListThingPrincipalsResponse
{
HttpStatusCode = HttpStatusCode.OK,
Principals = Fixture.Create<List<string>>()
});

_ = this.mockAmazonIotClient.Setup(iotClient => iotClient.DetachThingPrincipalAsync(It.IsAny<DetachThingPrincipalRequest>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new DetachThingPrincipalResponse
{
HttpStatusCode = HttpStatusCode.OK
});

_ = this.mockAmazonIotClient.Setup(iotClient => iotClient.DeleteThingAsync(It.IsAny<DeleteThingRequest>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(expected);

Expand All @@ -272,9 +286,8 @@ public Task DeleteDeviceShouldThrowInternalServerErrorIfHttpStatusCodeIsNotOK()
{
ThingName = Fixture.Create<string>(),
};

_ = this.mockAmazonIotClient.Setup(iotClient => iotClient.DeleteThingAsync(It.IsAny<DeleteThingRequest>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new DeleteThingResponse
_ = this.mockAmazonIotClient.Setup(iotClient => iotClient.ListThingPrincipalsAsync(It.IsAny<ListThingPrincipalsRequest>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new ListThingPrincipalsResponse
{
HttpStatusCode = HttpStatusCode.BadRequest
});
Expand Down

0 comments on commit 2b1f4cc

Please sign in to comment.