From 2c90abc444a72007fbc0cf6d113b1fe00f154e92 Mon Sep 17 00:00:00 2001 From: Kevin BEAUGRAND Date: Wed, 24 May 2023 18:14:35 +0200 Subject: [PATCH] Fix #1869 - create and delete dynamic thing group --- .../Services/AwsExternalDeviceService.cs | 30 ++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/src/AzureIoTHub.Portal.Infrastructure/Services/AwsExternalDeviceService.cs b/src/AzureIoTHub.Portal.Infrastructure/Services/AwsExternalDeviceService.cs index 343f70ea6..d0e935bca 100644 --- a/src/AzureIoTHub.Portal.Infrastructure/Services/AwsExternalDeviceService.cs +++ b/src/AzureIoTHub.Portal.Infrastructure/Services/AwsExternalDeviceService.cs @@ -44,8 +44,10 @@ public async Task CreateDeviceModel(ExternalDeviceModelD }); var response = await this.amazonIoTClient.CreateThingTypeAsync(createThingTypeRequest); + await this.CreateDynamicGroupForThingType(response.ThingTypeName); deviceModel.Id = response.ThingTypeId; + return deviceModel; } catch (ResourceAlreadyExistsException e) @@ -79,7 +81,12 @@ public async Task DeleteDeviceModel(ExternalDeviceModelDto deviceModel) UndoDeprecate = false }; - _ = await this.amazonIoTClient.DeprecateThingTypeAsync(deprecated); + var response = await this.amazonIoTClient.DeprecateThingTypeAsync(deprecated); + + _ = await this.amazonIoTClient.DeleteDynamicThingGroupAsync(new DeleteDynamicThingGroupRequest + { + ThingGroupName = deviceModel.Name + }); } catch (ResourceNotFoundException e) { @@ -191,5 +198,26 @@ public Task UpdateDeviceTwin(Twin twin) { throw new NotImplementedException(); } + + private async Task CreateDynamicGroupForThingType(string thingTypeName) + { + try + { + var dynamicThingGroup = new DescribeThingGroupRequest + { + ThingGroupName = thingTypeName + }; + + _ = await this.amazonIoTClient.DescribeThingGroupAsync(dynamicThingGroup); + } + catch (ResourceNotFoundException) + { + _ = await this.amazonIoTClient.CreateDynamicThingGroupAsync(new CreateDynamicThingGroupRequest + { + ThingGroupName = thingTypeName, + QueryString = $"thingTypeName: {thingTypeName}" + }); + } + } } }