Skip to content

Commit

Permalink
Create dynamic thing group for device models (#2111)
Browse files Browse the repository at this point in the history
  • Loading branch information
kbeaugrand committed Jun 22, 2023
1 parent 85e864c commit 8fc4e5f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,26 +51,25 @@ public async Task RollOutEdgeModelConfiguration(IoTEdgeModel edgeModel)

private async Task<string> GetThingGroupArn(IoTEdgeModel edgeModel)
{
await CreateThingTypeIfNotExists(edgeModel!.ModelId);
await CreateThingTypeIfNotExists(edgeModel!.Name);

var dynamicThingGroup = new DescribeThingGroupRequest
{
ThingGroupName = edgeModel?.ModelId
ThingGroupName = edgeModel?.Name
};

try
{
var existingThingGroupResponse = await this.iotClient.DescribeThingGroupAsync(dynamicThingGroup);

return existingThingGroupResponse.ThingGroupArn;

}
catch (Amazon.IoT.Model.ResourceNotFoundException)
{
var createThingGroupResponse = await this.iotClient.CreateDynamicThingGroupAsync(new CreateDynamicThingGroupRequest
{
ThingGroupName = edgeModel!.ModelId,
QueryString = $"thingTypeName: {edgeModel!.ModelId}"
ThingGroupName = edgeModel!.Name,
QueryString = $"thingTypeName: {edgeModel!.Name}"
});

return createThingGroupResponse.ThingGroupArn;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ public async Task<ExternalDeviceModelDto> CreateDeviceModel(ExternalDeviceModelD
{
try
{
var thingType = this.mapper.Map<CreateThingTypeRequest>(deviceModel);

var createThingTypeRequest = this.mapper.Map<CreateThingTypeRequest>(thingType);
var createThingTypeRequest = this.mapper.Map<CreateThingTypeRequest>(deviceModel);

createThingTypeRequest.Tags.Add(new Tag
{
Expand All @@ -44,8 +42,10 @@ public async Task<ExternalDeviceModelDto> CreateDeviceModel(ExternalDeviceModelD
});

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 @@ -80,6 +80,11 @@ public async Task DeleteDeviceModel(ExternalDeviceModelDto deviceModel)
};

_ = await this.amazonIoTClient.DeprecateThingTypeAsync(deprecated);

_ = await this.amazonIoTClient.DeleteDynamicThingGroupAsync(new DeleteDynamicThingGroupRequest
{
ThingGroupName = deviceModel.Name
});
}
catch (ResourceNotFoundException e)
{
Expand Down Expand Up @@ -191,5 +196,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}"
});
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,16 @@ public async Task CreateDeviceModel_NonExistingThingType_ThingTypeCreated()
ThingTypeName = externalDeviceModelDto.Name
};

_ = this.mockAmazonIot.Setup(e => e.CreateThingTypeAsync(It.Is<CreateThingTypeRequest>(c => externalDeviceModelDto.Name.Equals(c.ThingTypeName, StringComparison.Ordinal)), It.IsAny<CancellationToken>()))
_ = this.mockAmazonIot.Setup(e => e.CreateThingTypeAsync(It.Is<CreateThingTypeRequest>(c => externalDeviceModelDto.Name.Equals(c.ThingTypeName, StringComparison.OrdinalIgnoreCase)), It.IsAny<CancellationToken>()))
.ReturnsAsync(createThingTypeResponse);

_ = this.mockAmazonIot.Setup(c => c.DescribeThingGroupAsync(It.Is<DescribeThingGroupRequest>(d => d.ThingGroupName.Equals(externalDeviceModelDto.Name, StringComparison.OrdinalIgnoreCase)), It.IsAny<CancellationToken>()))
.ThrowsAsync(new ResourceNotFoundException(It.IsAny<string>()));

_ = this.mockAmazonIot.Setup(c => c.CreateDynamicThingGroupAsync(It.Is<CreateDynamicThingGroupRequest>(d => d.ThingGroupName.Equals(externalDeviceModelDto.Name, StringComparison.OrdinalIgnoreCase)
&& d.QueryString.Equals($"thingTypeName: {externalDeviceModelDto.Name}", StringComparison.OrdinalIgnoreCase)), It.IsAny<CancellationToken>()))
.ReturnsAsync(new CreateDynamicThingGroupResponse());

// Act
var result = await this.externalDeviceModelService.CreateDeviceModel(externalDeviceModelDto);

Expand All @@ -70,7 +77,7 @@ public async Task CreateDeviceModel_ExistingThingType_ResourceAlreadyExistsExcep
// Arrange
var externalDeviceModelDto = Fixture.Create<ExternalDeviceModelDto>();

_ = this.mockAmazonIot.Setup(e => e.CreateThingTypeAsync(It.Is<CreateThingTypeRequest>(c => externalDeviceModelDto.Name.Equals(c.ThingTypeName, StringComparison.Ordinal)), It.IsAny<CancellationToken>()))
_ = this.mockAmazonIot.Setup(e => e.CreateThingTypeAsync(It.Is<CreateThingTypeRequest>(c => externalDeviceModelDto.Name.Equals(c.ThingTypeName, StringComparison.OrdinalIgnoreCase)), It.IsAny<CancellationToken>()))
.ThrowsAsync(new ResourceAlreadyExistsException(Fixture.Create<string>()));

// Act
Expand All @@ -87,9 +94,12 @@ public async Task DeleteDeviceModel_ExistingThingType_ThingTypeDepcrecated()
// Arrange
var externalDeviceModelDto = Fixture.Create<ExternalDeviceModelDto>();

_ = this.mockAmazonIot.Setup(e => e.DeprecateThingTypeAsync(It.Is<DeprecateThingTypeRequest>(c => externalDeviceModelDto.Name.Equals(c.ThingTypeName, StringComparison.Ordinal) && !c.UndoDeprecate), It.IsAny<CancellationToken>()))
_ = this.mockAmazonIot.Setup(e => e.DeprecateThingTypeAsync(It.Is<DeprecateThingTypeRequest>(c => externalDeviceModelDto.Name.Equals(c.ThingTypeName, StringComparison.OrdinalIgnoreCase) && !c.UndoDeprecate), It.IsAny<CancellationToken>()))
.ReturnsAsync(new DeprecateThingTypeResponse());

_ = this.mockAmazonIot.Setup(c => c.DeleteDynamicThingGroupAsync(It.Is<DeleteDynamicThingGroupRequest>(d => d.ThingGroupName.Equals(externalDeviceModelDto.Name, StringComparison.OrdinalIgnoreCase)), It.IsAny<CancellationToken>()))
.ReturnsAsync(new DeleteDynamicThingGroupResponse());

// Act
await this.externalDeviceModelService.DeleteDeviceModel(externalDeviceModelDto);

Expand All @@ -103,7 +113,7 @@ public async Task DeleteDeviceModel_NonExistingThingType_ResourceNotFoundExcepti
// Arrange
var externalDeviceModelDto = Fixture.Create<ExternalDeviceModelDto>();

_ = this.mockAmazonIot.Setup(e => e.DeprecateThingTypeAsync(It.Is<DeprecateThingTypeRequest>(c => externalDeviceModelDto.Name.Equals(c.ThingTypeName, StringComparison.Ordinal) && !c.UndoDeprecate), It.IsAny<CancellationToken>()))
_ = this.mockAmazonIot.Setup(e => e.DeprecateThingTypeAsync(It.Is<DeprecateThingTypeRequest>(c => externalDeviceModelDto.Name.Equals(c.ThingTypeName, StringComparison.OrdinalIgnoreCase) && !c.UndoDeprecate), It.IsAny<CancellationToken>()))
.ThrowsAsync(new ResourceNotFoundException(Fixture.Create<string>()));

// Act
Expand Down

0 comments on commit 8fc4e5f

Please sign in to comment.