diff --git a/.changeset/three-bats-mate.md b/.changeset/three-bats-mate.md new file mode 100644 index 000000000..7bc71d6e8 --- /dev/null +++ b/.changeset/three-bats-mate.md @@ -0,0 +1,12 @@ +--- +"@solace-labs/ep-sdk": minor +--- + +add managing custom attributes for application domains + +- **EpSdkApplicationDomainsServiceClass** + - `setCustomAttributes()` + - `unsetCustomAttributes()` + - `removeAssociatedEntityTypeFromCustomAttributeDefinitions()` + - `listAll()` + - new optional parameter: `attributesQuery?: IEpSdkAttributesQuery;` diff --git a/package.json b/package.json index 2633f4558..2f4850ddc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@solace-labs/solace-tools-typescript", - "version": "2.9.3", + "version": "2.9.4", "private": true, "workspaces": [ "packages/*" diff --git a/packages/ep-sdk/src/services/EpSdkApplicationDomainsService.ts b/packages/ep-sdk/src/services/EpSdkApplicationDomainsService.ts index 0e1c43a16..eeee0df9f 100644 --- a/packages/ep-sdk/src/services/EpSdkApplicationDomainsService.ts +++ b/packages/ep-sdk/src/services/EpSdkApplicationDomainsService.ts @@ -3,6 +3,7 @@ import { ApplicationDomainResponse, ApplicationDomainsResponse, ApplicationDomainsService, + CustomAttribute, Pagination, } from '@solace-labs/ep-openapi-node'; import { @@ -15,14 +16,105 @@ import { EpApiMaxPageSize } from '../constants'; import { EpSdkServiceClass } from './EpSdkService'; +import { EEpSdkCustomAttributeEntityTypes, IEpSdkAttributesQuery, TEpSdkCustomAttributeList } from '../types'; +import EpSdkCustomAttributesQueryService from './EpSdkCustomAttributesQueryService'; +import EpSdkCustomAttributesService from './EpSdkCustomAttributesService'; +import EpSdkCustomAttributeDefinitionsService from './EpSdkCustomAttributeDefinitionsService'; /** @category Services */ export class EpSdkApplicationDomainsServiceClass extends EpSdkServiceClass { - public listAll = async({ pageSize = EpApiMaxPageSize, xContextId }:{ + private async updateApplicationDomain({ xContextId, update }:{ + xContextId?: string; + update: ApplicationDomain; + }): Promise { + const funcName = 'updateApplicationDomain'; + const logName = `${EpSdkApplicationDomainsServiceClass.name}.${funcName}()`; + /* istanbul ignore next */ + if(update.id === undefined) throw new EpSdkApiContentError(logName, this.constructor.name, 'update.id === undefined', { + update: update + }); + const applicationDomainResponse: ApplicationDomainResponse = await ApplicationDomainsService.updateApplicationDomain({ + xContextId, + id: update.id, + requestBody: update + }); + /* istanbul ignore next */ + if(applicationDomainResponse.data === undefined) throw new EpSdkApiContentError(logName, this.constructor.name, 'applicationDomainResponse.data === undefined', { + applicationDomainResponse: applicationDomainResponse + }); + return applicationDomainResponse.data; + } + + /** + * Sets the custom attributes in the list on the application domain. + * Creates attribute definitions / adds entity type 'applicationDomain' if it doesn't exist. + */ + public async setCustomAttributes({ xContextId, applicationDomainId, epSdkCustomAttributeList}:{ + xContextId?: string; + applicationDomainId: string; + epSdkCustomAttributeList: TEpSdkCustomAttributeList; + }): Promise { + const applicationDomain: ApplicationDomain = await this.getById({ xContextId, applicationDomainId }); + const customAttributes: Array = await EpSdkCustomAttributesService.createCustomAttributesWithNew({ + xContextId, + existingCustomAttributes: applicationDomain.customAttributes, + epSdkCustomAttributeList: epSdkCustomAttributeList, + epSdkCustomAttributeEntityType: EEpSdkCustomAttributeEntityTypes.APPLICATION_DOMAIN + }); + return await this.updateApplicationDomain({ + xContextId, + update: { + ...applicationDomain, + customAttributes: customAttributes, + } + }); + } + + /** + * Unsets the custom attributes in the list on the application domain. + * Leaves attibute definitions as-is. + */ + public async unsetCustomAttributes({ xContextId, applicationDomainId, epSdkCustomAttributeList }:{ + xContextId?: string; + applicationDomainId: string; + epSdkCustomAttributeList: TEpSdkCustomAttributeList; + }): Promise { + const applicationDomain: ApplicationDomain = await this.getById({ xContextId, applicationDomainId }); + const customAttributes: Array = await EpSdkCustomAttributesService.createCustomAttributesExcluding({ + existingCustomAttributes: applicationDomain.customAttributes, + epSdkCustomAttributeList: epSdkCustomAttributeList, + }); + return await this.updateApplicationDomain({ + xContextId, + update: { + ...applicationDomain, + customAttributes: customAttributes, + } + }); + } + + /** + * Removes the custom attribute association for application domains. + */ + public async removeAssociatedEntityTypeFromCustomAttributeDefinitions({ xContextId, customAttributeNames }: { + xContextId?: string; + customAttributeNames: Array; + }): Promise { + for(const customAttributeName of customAttributeNames) { + await EpSdkCustomAttributeDefinitionsService.removeAssociatedEntityTypeFromCustomAttributeDefinition({ + xContextId, + attributeName: customAttributeName, + associatedEntityType: EEpSdkCustomAttributeEntityTypes.APPLICATION_DOMAIN, + }); + } + } + + public listAll = async({ pageSize = EpApiMaxPageSize, xContextId, attributesQuery }:{ xContextId?: string; pageSize?: number; /** for testing */ + attributesQuery?: IEpSdkAttributesQuery; }): Promise => { const funcName = 'listAll'; const logName = `${EpSdkApplicationDomainsServiceClass.name}.${funcName}()`; @@ -38,7 +130,14 @@ export class EpSdkApplicationDomainsServiceClass extends EpSdkServiceClass { }); if(applicationDomainsResponse.data === undefined || applicationDomainsResponse.data.length === 0) nextPage = undefined; else { - applicationDomainList.push(...applicationDomainsResponse.data); + if(attributesQuery) { + for(const applicationDomain of applicationDomainsResponse.data) { + if(EpSdkCustomAttributesQueryService.resolve({ + customAttributes: applicationDomain.customAttributes, + attributesQuery: attributesQuery, + })) applicationDomainList.push(applicationDomain); + } + } else applicationDomainList.push(...applicationDomainsResponse.data); /* istanbul ignore next */ if(applicationDomainsResponse.meta === undefined) throw new EpSdkApiContentError(logName, this.constructor.name,'applicationDomainsResponse.meta === undefined', { applicationDomainsResponse: applicationDomainsResponse diff --git a/packages/ep-sdk/test/specs/services/applicationDomainsService.spec.ts b/packages/ep-sdk/test/specs/services/applicationDomainsService.spec.ts index dc8d85aa9..b1eb27758 100644 --- a/packages/ep-sdk/test/specs/services/applicationDomainsService.spec.ts +++ b/packages/ep-sdk/test/specs/services/applicationDomainsService.spec.ts @@ -7,6 +7,7 @@ import { ApplicationDomainResponse, ApplicationDomainsResponse, ApplicationDomainsService, + CustomAttribute, } from "@solace-labs/ep-openapi-node"; import { TestContext, TestUtils } from "@internal/tools/src"; import { TestLogger, TestConfig, TestHelpers } from "../../lib"; @@ -14,6 +15,9 @@ import { EpSdkError, EpSdkServiceError, EpSdkApplicationDomainsService, + TEpSdkCustomAttribute, + IEpSdkAttributesQuery, + EEpSdkComparisonOps, } from "../../../src"; const scriptName: string = path.basename(__filename); @@ -26,6 +30,27 @@ let ApplicationDomainName: string; let ApplicationDomainId: string | undefined; let ApplicationDomainIdList: Array = []; +const CorrectAttribute: TEpSdkCustomAttribute = { + name: `${TestSpecName}.APPS`, + value: "ep-developer-portal", +}; +const AnotherAttribute: TEpSdkCustomAttribute = { + name: `${TestSpecName}.another`, + value: "another value", +}; + +const CorrectAttributesQuery: IEpSdkAttributesQuery = { + AND: { + queryList: [ + { + attributeName: CorrectAttribute.name, + comparisonOp: EEpSdkComparisonOps.CONTAINS, + value: CorrectAttribute.value, + }, + ], + }, +}; + const recordApplicationDomainId = (applicationDomainId: string) => { ApplicationDomainId = applicationDomainId; ApplicationDomainIdList.push(applicationDomainId); @@ -71,7 +96,7 @@ describe(`${scriptName}`, () => { name: ApplicationDomainName, }, }); - recordApplicationDomainId(applicationDomainResponse.data.id); + recordApplicationDomainId(applicationDomainResponse.data.id); // // DEBUG // expect(false, `ApplicationDomainId=${ApplicationDomainId}`).to.be.true; } catch (e) { @@ -85,54 +110,91 @@ describe(`${scriptName}`, () => { } }); - it(`${scriptName}: should list all application domains`, async () => { + it(`${scriptName}: should create custom attribute for the application domain`, async () => { try { - const applicationDomainsResponse: ApplicationDomainsResponse = - await EpSdkApplicationDomainsService.listAll({ pageSize: 5 }); - const message = `applicationDomainsResponse=\n${JSON.stringify( - applicationDomainsResponse, - null, - 2 - )}`; - expect( - applicationDomainsResponse.meta.pagination.count, - TestLogger.createApiTestFailMessage(message) - ).to.be.greaterThanOrEqual(1); - expect( - applicationDomainsResponse.data, - TestLogger.createApiTestFailMessage(message) - ).to.not.be.undefined; - expect( - applicationDomainsResponse.data.length, - TestLogger.createApiTestFailMessage(message) - ).to.be.greaterThanOrEqual(1); - const found = applicationDomainsResponse.data.find((x) => { - return x.id === ApplicationDomainId; + const applicationDomain: ApplicationDomain = await EpSdkApplicationDomainsService.setCustomAttributes({ + applicationDomainId: ApplicationDomainId, + epSdkCustomAttributeList: [ + CorrectAttribute, + AnotherAttribute, + ], }); - expect(found, TestLogger.createApiTestFailMessage(message)).to.not.be - .undefined; + // test it is set + expect(applicationDomain.customAttributes).to.not.be.undefined; + const found: CustomAttribute | undefined = applicationDomain.customAttributes.find( (customAttribute: CustomAttribute) => { + return ( customAttribute.customAttributeDefinitionName === CorrectAttribute.name ); + }); + expect(found, `applicationDomain=${JSON.stringify(applicationDomain, null, 2)}`).to.not.be.undefined; + // // DEBUG + // expect(false, `ApplicationDomainId=${ApplicationDomainId}`).to.be.true; + } catch (e) { + if (e instanceof ApiError) expect(false, TestLogger.createApiTestFailMessage("failed", e)).to.be.true; + expect(e instanceof EpSdkError, TestLogger.createNotEpSdkErrorMessage(e)).to.be.true; + expect(false, TestLogger.createEpSdkTestFailMessage("failed", e)).to.be.true; + } + }); + + it(`${scriptName}: should list all application domains`, async () => { + try { + const applicationDomainsResponse: ApplicationDomainsResponse = await EpSdkApplicationDomainsService.listAll({ pageSize: 5 }); + const message = `applicationDomainsResponse=\n${JSON.stringify(applicationDomainsResponse, null, 2 )}`; + expect(applicationDomainsResponse.meta.pagination.count, TestLogger.createApiTestFailMessage(message)).to.be.greaterThanOrEqual(1); + expect(applicationDomainsResponse.data, TestLogger.createApiTestFailMessage(message)).to.not.be.undefined; + expect(applicationDomainsResponse.data.length, TestLogger.createApiTestFailMessage(message)).to.be.greaterThanOrEqual(1); + const found = applicationDomainsResponse.data.find((x) => { return x.id === ApplicationDomainId; }); + expect(found, TestLogger.createApiTestFailMessage(message)).to.not.be.undefined; const firstCount = applicationDomainsResponse.meta.pagination.count; - const secondApplicationDomainsResponse: ApplicationDomainsResponse = - await EpSdkApplicationDomainsService.listAll({}); - expect( - secondApplicationDomainsResponse.meta.pagination.count, - TestLogger.createApiTestFailMessage( - `secondApplicationDomainsResponse=\n${JSON.stringify( - secondApplicationDomainsResponse, - null, - 2 - )}` - ) - ).to.equal(firstCount); + const secondApplicationDomainsResponse: ApplicationDomainsResponse = await EpSdkApplicationDomainsService.listAll({}); + expect(secondApplicationDomainsResponse.meta.pagination.count, TestLogger.createApiTestFailMessage(`secondApplicationDomainsResponse=\n${JSON.stringify(secondApplicationDomainsResponse, null, 2 )}`)).to.equal(firstCount); // // DEBUG // expect(false, TestLogger.createApiTestFailMessage(message)).to.be.true; } catch (e) { - if (e instanceof ApiError) - expect(false, TestLogger.createApiTestFailMessage("failed")).to.be.true; - expect(e instanceof EpSdkError, TestLogger.createNotEpSdkErrorMessage(e)) - .to.be.true; - expect(false, TestLogger.createEpSdkTestFailMessage("failed", e)).to.be - .true; + if (e instanceof ApiError) expect(false, TestLogger.createApiTestFailMessage("failed")).to.be.true; + expect(e instanceof EpSdkError, TestLogger.createNotEpSdkErrorMessage(e)).to.be.true; + expect(false, TestLogger.createEpSdkTestFailMessage("failed", e)).to.be.true; + } + }); + + it(`${scriptName}: should list all application domains filtering by custom attribute`, async () => { + try { + const applicationDomainsResponse: ApplicationDomainsResponse = await EpSdkApplicationDomainsService.listAll({ attributesQuery: CorrectAttributesQuery }); + const message = `applicationDomainsResponse=\n${JSON.stringify(applicationDomainsResponse, null, 2 )}`; + expect(applicationDomainsResponse.meta.pagination.count, TestLogger.createApiTestFailMessage(message)).to.be.equal(1); + expect(applicationDomainsResponse.data, TestLogger.createApiTestFailMessage(message)).to.not.be.undefined; + expect(applicationDomainsResponse.data.length, TestLogger.createApiTestFailMessage(message)).to.be.equal(1); + const found = applicationDomainsResponse.data.find((x) => { return x.id === ApplicationDomainId; }); + expect(found, TestLogger.createApiTestFailMessage(message)).to.not.be.undefined; + // // DEBUG + // expect(false, TestLogger.createApiTestFailMessage(message)).to.be.true; + } catch (e) { + if (e instanceof ApiError) expect(false, TestLogger.createApiTestFailMessage("failed")).to.be.true; + expect(e instanceof EpSdkError, TestLogger.createNotEpSdkErrorMessage(e)).to.be.true; + expect(false, TestLogger.createEpSdkTestFailMessage("failed", e)).to.be.true; + } + }); + + it(`${scriptName}: should unset custom attributes from application domain`, async () => { + try { + const applicationDomain: ApplicationDomain = await EpSdkApplicationDomainsService.unsetCustomAttributes({ + applicationDomainId: ApplicationDomainId, + epSdkCustomAttributeList: [ + CorrectAttribute, + AnotherAttribute, + ], + }); + // test no custom attributes left + expect(applicationDomain.customAttributes.length, 'wrong length').to.equal(0); + // remove association + await EpSdkApplicationDomainsService.removeAssociatedEntityTypeFromCustomAttributeDefinitions({ + customAttributeNames: [ + CorrectAttribute.name, + AnotherAttribute.name + ] + }); + } catch (e) { + if (e instanceof ApiError) expect(false, TestLogger.createApiTestFailMessage("failed")).to.be.true; + expect(e instanceof EpSdkError, TestLogger.createNotEpSdkErrorMessage(e)).to.be.true; + expect(false, TestLogger.createEpSdkTestFailMessage("failed", e)).to.be.true; } }); diff --git a/packages/ep-sdk/test/specs/services/eventApiProductsService-list.spec.ts b/packages/ep-sdk/test/specs/services/eventApiProductsService-list.spec.ts index d3e99d96a..1dab5ea9d 100644 --- a/packages/ep-sdk/test/specs/services/eventApiProductsService-list.spec.ts +++ b/packages/ep-sdk/test/specs/services/eventApiProductsService-list.spec.ts @@ -127,37 +127,27 @@ describe(`${scriptName}`, () => { }); // test it is set expect(eventApiProduct.customAttributes).to.not.be.undefined; - const found: CustomAttribute | undefined = - eventApiProduct.customAttributes.find( - (customAttribute: CustomAttribute) => { - return ( - customAttribute.customAttributeDefinitionName === - PublishDestinationsAttribute.name - ); - } + const found: CustomAttribute | undefined = eventApiProduct.customAttributes.find((customAttribute: CustomAttribute) => { + return ( + customAttribute.customAttributeDefinitionName === + PublishDestinationsAttribute.name ); - expect( - found, - `eventApiProduct=${JSON.stringify(eventApiProduct, null, 2)}` - ).to.not.be.undefined; + }); + expect(found, `eventApiProduct=${JSON.stringify(eventApiProduct, null, 2)}`).to.not.be.undefined; } else if (i % 3 === 1) { - const eventApiProduct: EventApiProduct = - await EpSdkEventApiProductsService.setCustomAttributes({ - eventApiProductId: eventApiProductResponse.data.id, - epSdkCustomAttributeList: [AnotherAttribute], - }); + const eventApiProduct: EventApiProduct = await EpSdkEventApiProductsService.setCustomAttributes({ + eventApiProductId: eventApiProductResponse.data.id, + epSdkCustomAttributeList: [AnotherAttribute], + }); } else { // no attributes } i++; } } catch (e) { - if (e instanceof ApiError) - expect(false, TestLogger.createApiTestFailMessage("failed")).to.be.true; - expect(e instanceof EpSdkError, TestLogger.createNotEpSdkErrorMessage(e)) - .to.be.true; - expect(false, TestLogger.createEpSdkTestFailMessage("failed", e)).to.be - .true; + if (e instanceof ApiError) expect(false, TestLogger.createApiTestFailMessage("failed")).to.be.true; + expect(e instanceof EpSdkError, TestLogger.createNotEpSdkErrorMessage(e)).to.be.true; + expect(false, TestLogger.createEpSdkTestFailMessage("failed", e)).to.be.true; } }); @@ -172,37 +162,21 @@ describe(`${scriptName}`, () => { // // DEBUG // expect(false, `eventApiProductsResponse.data=${JSON.stringify(eventApiProductsResponse.data, null, 2)}`).to.be.true; expect(eventApiProductsResponse.data).to.not.be.undefined; - expect( - eventApiProductsResponse.data.length, - `wrong length, eventApiProductsResponse.data=${JSON.stringify( - eventApiProductsResponse.data, - null, - 2 - )}` - ).to.equal((NumEventApiProducts / 3) * 2); + expect(eventApiProductsResponse.data.length, `wrong length, eventApiProductsResponse.data=${JSON.stringify( eventApiProductsResponse.data, null, 2 )}`).to.equal((NumEventApiProducts / 3) * 2); for (const eventApiProduct of eventApiProductsResponse.data) { expect(eventApiProduct.customAttributes).to.not.be.undefined; - const found: CustomAttribute | undefined = - eventApiProduct.customAttributes.find( - (customAttribute: CustomAttribute) => { - return ( - customAttribute.customAttributeDefinitionName === - PublishDestinationsAttribute.name - ); - } + const found: CustomAttribute | undefined = eventApiProduct.customAttributes.find( (customAttribute: CustomAttribute) => { + return ( + customAttribute.customAttributeDefinitionName === + PublishDestinationsAttribute.name ); - expect( - found, - `eventApiProduct=${JSON.stringify(eventApiProduct, null, 2)}` - ).to.be.undefined; + }); + expect(found, `eventApiProduct=${JSON.stringify(eventApiProduct, null, 2)}`).to.be.undefined; } } catch (e) { - if (e instanceof ApiError) - expect(false, TestLogger.createApiTestFailMessage("failed")).to.be.true; - expect(e instanceof EpSdkError, TestLogger.createNotEpSdkErrorMessage(e)) - .to.be.true; - expect(false, TestLogger.createEpSdkTestFailMessage("failed", e)).to.be - .true; + if (e instanceof ApiError) expect(false, TestLogger.createApiTestFailMessage("failed")).to.be.true; + expect(e instanceof EpSdkError, TestLogger.createNotEpSdkErrorMessage(e)).to.be.true; + expect(false, TestLogger.createEpSdkTestFailMessage("failed", e)).to.be.true; } }); });