diff --git a/source/common/changes/@awssolutions/cdf-assetlibrary-client/feat_bulk_get_groups_2023-10-11-16-43.json b/source/common/changes/@awssolutions/cdf-assetlibrary-client/feat_bulk_get_groups_2023-10-11-16-43.json new file mode 100644 index 000000000..c1d24a06f --- /dev/null +++ b/source/common/changes/@awssolutions/cdf-assetlibrary-client/feat_bulk_get_groups_2023-10-11-16-43.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@awssolutions/cdf-assetlibrary-client", + "comment": "Added \"bulkGetGroups\" to groups service", + "type": "none" + } + ], + "packageName": "@awssolutions/cdf-assetlibrary-client" +} \ No newline at end of file diff --git a/source/packages/integration-tests/features/assetlibrary/full/groups.feature b/source/packages/integration-tests/features/assetlibrary/full/groups.feature index 93117bf7e..fee6e02fd 100644 --- a/source/packages/integration-tests/features/assetlibrary/full/groups.feature +++ b/source/packages/integration-tests/features/assetlibrary/full/groups.feature @@ -335,6 +335,12 @@ Feature: Group lifecycle When I delete assetlibrary group template "TEST-groups-groupTemplate001" Then it fails with a 409 And published assetlibrary group template "TEST-groups-groupTemplate001" exists + + Scenario: Retrieving a list of groups in bulk + Given my authorization is + | / | * | + When I get bulk groups "/TEST-groups-group001,/TEST-groups-group002" + Then result contains groups "/test-groups-group001,/test-groups-group002" Scenario: Teardown device artifacts Given my authorization is diff --git a/source/packages/integration-tests/src/step_definitions/assetlibrary/group.steps.ts b/source/packages/integration-tests/src/step_definitions/assetlibrary/group.steps.ts index a7d3a04df..e17a05a52 100644 --- a/source/packages/integration-tests/src/step_definitions/assetlibrary/group.steps.ts +++ b/source/packages/integration-tests/src/step_definitions/assetlibrary/group.steps.ts @@ -205,6 +205,15 @@ When('I get group {string}', async function (groupPath: string) { } }); +When('I get bulk groups {string}', async function (paths: string) { + try { + const groupPaths: string[] = paths.split(","); + this['members'] = await groupService.bulkGetGroups(groupPaths, false, getAdditionalHeaders(this)); + } catch (err) { + this[RESPONSE_STATUS] = err.status; + } +}); + When( 'I detatch group {string} from group {string} via {string}', async function (thisGroup: string, otherGroup: string, relation: string) { @@ -363,3 +372,18 @@ Then('group contains device {string}', async function (deviceId: string) { Then('response should fail with {int}', async function (status: number) { expect(this[RESPONSE_STATUS]).eq(status); }); + +Then('result contains groups {string}', async function (paths: string) { + let found = false; + const groupPaths: string[] = paths.split(","); + (this['members']).results.forEach((group) => { + if (groupPaths.indexOf(group.groupPath) > -1) { + // found + found = true; + } else { + // not found + found = false; + } + }); + expect(found).eq(true); +}); \ No newline at end of file diff --git a/source/packages/libraries/clients/assetlibrary-client/src/client/groups.apigw.service.ts b/source/packages/libraries/clients/assetlibrary-client/src/client/groups.apigw.service.ts index 081bc6416..60d24b39f 100644 --- a/source/packages/libraries/clients/assetlibrary-client/src/client/groups.apigw.service.ts +++ b/source/packages/libraries/clients/assetlibrary-client/src/client/groups.apigw.service.ts @@ -391,4 +391,43 @@ export class GroupsApigwService extends GroupsServiceBase implements GroupsServi throw createError(err.response.status, err.response.text); }); } + + /** + * Gets the group details in bulk for the given group paths. + * @param groupPaths List of group paths to get details for + * @param includeGroups Optional flag to include the groups in the response. + * @param additionalHeaders Optional additional headers to send with the request. + * @returns Group details for the given group paths. + * @throws Error if the groupPaths is not an array. + * + */ + async bulkGetGroups( + groupPaths: string[], + includeGroups?:boolean, + additionalHeaders?:RequestHeaders, + ): Promise { + ow(groupPaths, 'groupPaths', ow.array.nonEmpty); + let url = `${this.baseUrl}${super.bulkGroupsRelativeUrl()}`; + const qs = QSHelper.getQueryString({includeGroups}); + let query = ""; + groupPaths.forEach(gp => { + query += gp + ","; + }); + query = query.substring(0, query.length - 1); + if (qs) { + url += `?${qs}&groupPaths=${query}`; + } else { + url += `?groupPaths=${query}`; + } + return await request + .get(url) + .set(this.buildHeaders(additionalHeaders)) + .use(await signClientRequest()) + .then((res) => { + return res.body; + }) + .catch((err) => { + throw createError(err.response.status, err.response.text); + }); + } } diff --git a/source/packages/libraries/clients/assetlibrary-client/src/client/groups.lambda.service.ts b/source/packages/libraries/clients/assetlibrary-client/src/client/groups.lambda.service.ts index cdf716566..34f9e1c7f 100644 --- a/source/packages/libraries/clients/assetlibrary-client/src/client/groups.lambda.service.ts +++ b/source/packages/libraries/clients/assetlibrary-client/src/client/groups.lambda.service.ts @@ -329,4 +329,34 @@ export class GroupsLambdaService extends GroupsServiceBase implements GroupsServ const res = await this.lambdaInvoker.invoke(this.functionName, event); return res.body; } + + /** + * Gets the group details in bulk for the given group paths. + * @param groupPaths List of group paths to get details for + * @param includeGroups Optional flag to include the groups in the response. + * @param additionalHeaders Optional additional headers to send with the request. + * @returns Group details for the given group paths. + * @throws Error if the groupPaths is not an array. + * + */ + async bulkGetGroups( + groupPaths: string[], + includeGroups?:boolean, + additionalHeaders?:RequestHeaders, + ): Promise { + ow(groupPaths, 'groupPaths', ow.array.nonEmpty); + let query = ""; + groupPaths.forEach(gp => { + query += gp + ","; + }); + query = query.substring(0, query.length - 1); + const event = new LambdaApiGatewayEventBuilder() + .setMethod('GET') + .setPath(super.bulkGroupsRelativeUrl()) + .setQueryStringParameters({groupPaths: query, includeGroups: `${includeGroups}`}) + .setHeaders(super.buildHeaders(additionalHeaders)); + + const res = await this.lambdaInvoker.invoke(this.functionName, event); + return res.body; + } } diff --git a/source/packages/libraries/clients/assetlibrary-client/src/client/groups.service.ts b/source/packages/libraries/clients/assetlibrary-client/src/client/groups.service.ts index c58602b0d..c6b88f6ab 100644 --- a/source/packages/libraries/clients/assetlibrary-client/src/client/groups.service.ts +++ b/source/packages/libraries/clients/assetlibrary-client/src/client/groups.service.ts @@ -199,6 +199,18 @@ export interface GroupsService { sort?: string, additionalHeaders?: RequestHeaders ): Promise; + + /** + * Provide a list of groups based on the supplied group paths. + * @param groupPaths List of group paths to return + * @param includeGroups Flag to indicate whether the group details need to be fetched + * @param additionalHeaders additional headers to pass along with the request. + */ + bulkGetGroups( + groupPaths: string[], + includeGroups?:boolean, + additionalHeaders?:RequestHeaders, + ): Promise; } @injectable()