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

detach associated certificate in thing #2151

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 @@ -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}");

}
}
Comment on lines +85 to +98

Check notice

Code scanning / CodeQL

Missed opportunity to use Select

This foreach loop immediately [maps its iteration variable to another variable](1) - consider mapping the sequence explicitly using '.Select(...)'.

//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