Skip to content

Commit

Permalink
Fix #1869 - create and delete dynamic thing group
Browse files Browse the repository at this point in the history
  • Loading branch information
kbeaugrand committed May 24, 2023
1 parent 9f1820f commit 2ac3225
Showing 1 changed file with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ public async Task<ExternalDeviceModelDto> CreateDeviceModel(ExternalDeviceModelD
var createThingTypeRequest = this.mapper.Map<CreateThingTypeRequest>(thingType);

var response = await this.amazonIoTClient.CreateThingTypeAsync(createThingTypeRequest);
await this.CreateDynamicGroupForThingType(response.ThingTypeName);

deviceModel.Id = response.ThingTypeId;

return deviceModel;
}
catch (ResourceAlreadyExistsException e)
Expand Down Expand Up @@ -73,7 +75,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)
{
Expand Down Expand Up @@ -185,5 +192,26 @@ public Task<Twin> 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}"
});
}
}
}
}

0 comments on commit 2ac3225

Please sign in to comment.