From 7d7d94735237cd931093ad6dc26c43743b1967bd Mon Sep 17 00:00:00 2001 From: Johan Haals Date: Thu, 25 Aug 2022 11:41:35 +0200 Subject: [PATCH] catalog-node: Export experimental catalogServiceRef Signed-off-by: Johan Haals --- .changeset/chatty-cars-kick.md | 5 ++ plugins/catalog-node/api-report.md | 5 ++ plugins/catalog-node/package.json | 6 +- .../catalog-node/src/catalogService.test.ts | 59 +++++++++++++++++++ plugins/catalog-node/src/catalogService.ts | 44 ++++++++++++++ plugins/catalog-node/src/index.ts | 1 + 6 files changed, 118 insertions(+), 2 deletions(-) create mode 100644 .changeset/chatty-cars-kick.md create mode 100644 plugins/catalog-node/src/catalogService.test.ts create mode 100644 plugins/catalog-node/src/catalogService.ts diff --git a/.changeset/chatty-cars-kick.md b/.changeset/chatty-cars-kick.md new file mode 100644 index 0000000000..e6f0bbe965 --- /dev/null +++ b/.changeset/chatty-cars-kick.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-catalog-node': patch +--- + +Adds experimental catalogServiceRef for optaining a `catalogClient` in the new backend system diff --git a/plugins/catalog-node/api-report.md b/plugins/catalog-node/api-report.md index 26617910b9..7d12c2e065 100644 --- a/plugins/catalog-node/api-report.md +++ b/plugins/catalog-node/api-report.md @@ -5,10 +5,12 @@ ```ts /// +import { CatalogApi } from '@backstage/catalog-client'; import { CompoundEntityRef } from '@backstage/catalog-model'; import { Entity } from '@backstage/catalog-model'; import { ExtensionPoint } from '@backstage/backend-plugin-api'; import { JsonValue } from '@backstage/types'; +import { ServiceRef } from '@backstage/backend-plugin-api'; // @alpha (undocumented) export interface CatalogProcessingExtensionPoint { @@ -106,6 +108,9 @@ export type CatalogProcessorResult = | CatalogProcessorErrorResult | CatalogProcessorRefreshKeysResult; +// @alpha +export const catalogServiceRef: ServiceRef; + // @public export type DeferredEntity = { entity: Entity; diff --git a/plugins/catalog-node/package.json b/plugins/catalog-node/package.json index 10d1d9d2dd..0024899628 100644 --- a/plugins/catalog-node/package.json +++ b/plugins/catalog-node/package.json @@ -26,16 +26,18 @@ }, "dependencies": { "@backstage/backend-plugin-api": "^0.1.2-next.0", + "@backstage/catalog-client": "^1.0.5-next.0", "@backstage/catalog-model": "^1.1.0", "@backstage/errors": "1.1.0", "@backstage/types": "^1.0.0" }, "devDependencies": { "@backstage/backend-common": "^0.15.1-next.1", - "@backstage/cli": "^0.19.0-next.1" + "@backstage/cli": "^0.19.0-next.1", + "@backstage/backend-test-utils": "^0.1.28-next.1" }, "files": [ "dist", "alpha" ] -} +} \ No newline at end of file diff --git a/plugins/catalog-node/src/catalogService.test.ts b/plugins/catalog-node/src/catalogService.test.ts new file mode 100644 index 0000000000..3f11e8a9f0 --- /dev/null +++ b/plugins/catalog-node/src/catalogService.test.ts @@ -0,0 +1,59 @@ +/* + * Copyright 2022 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { PluginEndpointDiscovery } from '@backstage/backend-common'; +import { + createBackendModule, + createServiceFactory, + createServiceRef, +} from '@backstage/backend-plugin-api'; +import { startTestBackend } from '@backstage/backend-test-utils'; +import { CatalogClient } from '@backstage/catalog-client'; +import { catalogServiceRef } from './catalogService'; + +describe('catalogServiceRef', () => { + it('should return a catalogClient', async () => { + const mockDiscoveryFactory = createServiceFactory({ + service: createServiceRef({ + id: 'core.discovery', + }), + deps: {}, + factory: async ({}) => { + return async () => jest.fn() as unknown as PluginEndpointDiscovery; + }, + }); + + const testModule = createBackendModule({ + moduleId: 'test.module', + pluginId: 'test', + register(env) { + env.registerInit({ + deps: { + catalog: catalogServiceRef, + }, + async init({ catalog }) { + expect(catalog).toBeInstanceOf(CatalogClient); + }, + }); + }, + }); + + await startTestBackend({ + services: [mockDiscoveryFactory], + features: [testModule({})], + }); + }); +}); diff --git a/plugins/catalog-node/src/catalogService.ts b/plugins/catalog-node/src/catalogService.ts new file mode 100644 index 0000000000..ddfff75462 --- /dev/null +++ b/plugins/catalog-node/src/catalogService.ts @@ -0,0 +1,44 @@ +/* + * Copyright 2022 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { + createServiceFactory, + createServiceRef, + discoveryServiceRef, +} from '@backstage/backend-plugin-api'; +import { CatalogApi, CatalogClient } from '@backstage/catalog-client'; + +/** + * The catalogService provides the catalog API. + * @alpha + */ +export const catalogServiceRef = createServiceRef({ + id: 'catalog-client', + defaultFactory: async service => + createServiceFactory({ + service, + deps: { + discoveryFactory: discoveryServiceRef, + }, + factory: async ({ discoveryFactory }) => { + const discoveryApi = await discoveryFactory('root'); + const catalogClient = new CatalogClient({ discoveryApi }); + return async _pluginId => { + return catalogClient; + }; + }, + }), +}); diff --git a/plugins/catalog-node/src/index.ts b/plugins/catalog-node/src/index.ts index 02ae325a9a..6393c55f31 100644 --- a/plugins/catalog-node/src/index.ts +++ b/plugins/catalog-node/src/index.ts @@ -22,5 +22,6 @@ export type { CatalogProcessingExtensionPoint } from './extensions'; export { catalogProcessingExtensionPoint } from './extensions'; +export { catalogServiceRef } from './catalogService'; export * from './api'; export * from './processing';