From 561ce41191db8a9f5e87e8546af27efdd10fe1c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20Adel=C3=B6w?= Date: Wed, 8 Mar 2023 15:20:46 +0100 Subject: [PATCH] Added a catalogModulePuppetDbEntityProvider alpha export for the new backend system MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fredrik Adelöw --- .changeset/cold-dogs-watch.md | 5 ++ .../alpha-api-report.md | 12 +++ .../api-report.md | 4 +- .../package.json | 22 +++++- .../src/alpha.ts | 17 ++++ ...atalogModulePuppetDbEntityProvider.test.ts | 79 +++++++++++++++++++ .../catalogModulePuppetDbEntityProvider.ts | 51 ++++++++++++ .../src/module/index.ts | 17 ++++ .../providers/PuppetDbEntityProvider.test.ts | 2 +- .../src/providers/PuppetDbEntityProvider.ts | 6 +- yarn.lock | 3 +- 11 files changed, 207 insertions(+), 11 deletions(-) create mode 100644 .changeset/cold-dogs-watch.md create mode 100644 plugins/catalog-backend-module-puppetdb/alpha-api-report.md create mode 100644 plugins/catalog-backend-module-puppetdb/src/alpha.ts create mode 100644 plugins/catalog-backend-module-puppetdb/src/module/catalogModulePuppetDbEntityProvider.test.ts create mode 100644 plugins/catalog-backend-module-puppetdb/src/module/catalogModulePuppetDbEntityProvider.ts create mode 100644 plugins/catalog-backend-module-puppetdb/src/module/index.ts diff --git a/.changeset/cold-dogs-watch.md b/.changeset/cold-dogs-watch.md new file mode 100644 index 0000000000..99b25a833e --- /dev/null +++ b/.changeset/cold-dogs-watch.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-catalog-backend-module-puppetdb': patch +--- + +Added a `catalogModulePuppetDbEntityProvider` alpha export for the new backend system diff --git a/plugins/catalog-backend-module-puppetdb/alpha-api-report.md b/plugins/catalog-backend-module-puppetdb/alpha-api-report.md new file mode 100644 index 0000000000..0856fdb836 --- /dev/null +++ b/plugins/catalog-backend-module-puppetdb/alpha-api-report.md @@ -0,0 +1,12 @@ +## API Report File for "@backstage/plugin-catalog-backend-module-puppetdb" + +> Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/). + +```ts +import { BackendFeature } from '@backstage/backend-plugin-api'; + +// @alpha +export const catalogModulePuppetDbEntityProvider: () => BackendFeature; + +// (No @packageDocumentation comment for this package) +``` diff --git a/plugins/catalog-backend-module-puppetdb/api-report.md b/plugins/catalog-backend-module-puppetdb/api-report.md index 8764ac1e7c..a4954ce6bd 100644 --- a/plugins/catalog-backend-module-puppetdb/api-report.md +++ b/plugins/catalog-backend-module-puppetdb/api-report.md @@ -4,8 +4,8 @@ ```ts import { Config } from '@backstage/config'; -import { EntityProvider } from '@backstage/plugin-catalog-backend'; -import { EntityProviderConnection } from '@backstage/plugin-catalog-backend'; +import { EntityProvider } from '@backstage/plugin-catalog-node'; +import { EntityProviderConnection } from '@backstage/plugin-catalog-node'; import { JsonValue } from '@backstage/types'; import { Logger } from 'winston'; import { PluginTaskScheduler } from '@backstage/backend-tasks'; diff --git a/plugins/catalog-backend-module-puppetdb/package.json b/plugins/catalog-backend-module-puppetdb/package.json index 861b535f4d..252fe21969 100644 --- a/plugins/catalog-backend-module-puppetdb/package.json +++ b/plugins/catalog-backend-module-puppetdb/package.json @@ -6,9 +6,22 @@ "types": "src/index.ts", "license": "Apache-2.0", "publishConfig": { - "access": "public", - "main": "dist/index.cjs.js", - "types": "dist/index.d.ts" + "access": "public" + }, + "exports": { + ".": "./src/index.ts", + "./alpha": "./src/alpha.ts", + "./package.json": "./package.json" + }, + "typesVersions": { + "*": { + "alpha": [ + "src/alpha.ts" + ], + "package.json": [ + "package.json" + ] + } }, "backstage": { "role": "backend-plugin-module" @@ -35,11 +48,12 @@ }, "dependencies": { "@backstage/backend-common": "workspace:^", + "@backstage/backend-plugin-api": "workspace:^", "@backstage/backend-tasks": "workspace:^", "@backstage/catalog-model": "workspace:^", "@backstage/config": "workspace:^", "@backstage/errors": "workspace:^", - "@backstage/plugin-catalog-backend": "workspace:^", + "@backstage/plugin-catalog-node": "workspace:^", "@backstage/types": "workspace:^", "lodash": "^4.17.21", "luxon": "^3.0.0", diff --git a/plugins/catalog-backend-module-puppetdb/src/alpha.ts b/plugins/catalog-backend-module-puppetdb/src/alpha.ts new file mode 100644 index 0000000000..c277a0a680 --- /dev/null +++ b/plugins/catalog-backend-module-puppetdb/src/alpha.ts @@ -0,0 +1,17 @@ +/* + * Copyright 2023 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. + */ + +export * from './module'; diff --git a/plugins/catalog-backend-module-puppetdb/src/module/catalogModulePuppetDbEntityProvider.test.ts b/plugins/catalog-backend-module-puppetdb/src/module/catalogModulePuppetDbEntityProvider.test.ts new file mode 100644 index 0000000000..e2745e9268 --- /dev/null +++ b/plugins/catalog-backend-module-puppetdb/src/module/catalogModulePuppetDbEntityProvider.test.ts @@ -0,0 +1,79 @@ +/* + * 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 { getVoidLogger } from '@backstage/backend-common'; +import { coreServices } from '@backstage/backend-plugin-api'; +import { + PluginTaskScheduler, + TaskScheduleDefinition, +} from '@backstage/backend-tasks'; +import { startTestBackend } from '@backstage/backend-test-utils'; +import { ConfigReader } from '@backstage/config'; +import { catalogProcessingExtensionPoint } from '@backstage/plugin-catalog-node/alpha'; +import { catalogModulePuppetDbEntityProvider } from './catalogModulePuppetDbEntityProvider'; +import { PuppetDbEntityProvider } from '../providers/PuppetDbEntityProvider'; + +describe('catalogModulePuppetDbEntityProvider', () => { + it('should register provider at the catalog extension point', async () => { + let addedProviders: Array | undefined; + let usedSchedule: TaskScheduleDefinition | undefined; + + const extensionPoint = { + addEntityProvider: (providers: any) => { + addedProviders = providers; + }, + }; + const runner = jest.fn(); + const scheduler = { + createScheduledTaskRunner: (schedule: TaskScheduleDefinition) => { + usedSchedule = schedule; + return runner; + }, + } as unknown as PluginTaskScheduler; + + const config = new ConfigReader({ + catalog: { + providers: { + puppetdb: { + baseUrl: 'http://puppetdb:8080', + schedule: { + frequency: { minutes: 10 }, + timeout: { minutes: 10 }, + }, + }, + }, + }, + }); + + await startTestBackend({ + extensionPoints: [[catalogProcessingExtensionPoint, extensionPoint]], + services: [ + [coreServices.config, config], + [coreServices.logger, getVoidLogger()], + [coreServices.scheduler, scheduler], + ], + features: [catalogModulePuppetDbEntityProvider()], + }); + + expect(usedSchedule?.frequency).toEqual({ minutes: 10 }); + expect(usedSchedule?.timeout).toEqual({ minutes: 10 }); + expect(addedProviders?.length).toEqual(1); + expect(addedProviders?.pop()?.getProviderName()).toEqual( + 'puppetdb-provider:default', + ); + expect(runner).not.toHaveBeenCalled(); + }); +}); diff --git a/plugins/catalog-backend-module-puppetdb/src/module/catalogModulePuppetDbEntityProvider.ts b/plugins/catalog-backend-module-puppetdb/src/module/catalogModulePuppetDbEntityProvider.ts new file mode 100644 index 0000000000..9a7d279cd3 --- /dev/null +++ b/plugins/catalog-backend-module-puppetdb/src/module/catalogModulePuppetDbEntityProvider.ts @@ -0,0 +1,51 @@ +/* + * 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 { + createBackendModule, + coreServices, +} from '@backstage/backend-plugin-api'; +import { loggerToWinstonLogger } from '@backstage/backend-common'; +import { catalogProcessingExtensionPoint } from '@backstage/plugin-catalog-node/alpha'; +import { PuppetDbEntityProvider } from '../providers/PuppetDbEntityProvider'; + +/** + * Registers the `PuppetDbEntityProvider` with the catalog processing extension point. + * + * @alpha + */ +export const catalogModulePuppetDbEntityProvider = createBackendModule({ + pluginId: 'catalog', + moduleId: 'puppetDbEntityProvider', + register(env) { + env.registerInit({ + deps: { + catalog: catalogProcessingExtensionPoint, + config: coreServices.config, + logger: coreServices.logger, + scheduler: coreServices.scheduler, + }, + async init({ catalog, config, logger, scheduler }) { + catalog.addEntityProvider( + PuppetDbEntityProvider.fromConfig(config, { + logger: loggerToWinstonLogger(logger), + scheduler, + }), + ); + }, + }); + }, +}); diff --git a/plugins/catalog-backend-module-puppetdb/src/module/index.ts b/plugins/catalog-backend-module-puppetdb/src/module/index.ts new file mode 100644 index 0000000000..f30f236df4 --- /dev/null +++ b/plugins/catalog-backend-module-puppetdb/src/module/index.ts @@ -0,0 +1,17 @@ +/* + * Copyright 2023 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. + */ + +export { catalogModulePuppetDbEntityProvider } from './catalogModulePuppetDbEntityProvider'; diff --git a/plugins/catalog-backend-module-puppetdb/src/providers/PuppetDbEntityProvider.test.ts b/plugins/catalog-backend-module-puppetdb/src/providers/PuppetDbEntityProvider.test.ts index 0ab6fb35b7..ad89e1fd9a 100644 --- a/plugins/catalog-backend-module-puppetdb/src/providers/PuppetDbEntityProvider.test.ts +++ b/plugins/catalog-backend-module-puppetdb/src/providers/PuppetDbEntityProvider.test.ts @@ -21,7 +21,7 @@ import { PuppetDbEntityProvider } from './PuppetDbEntityProvider'; import { DeferredEntity, EntityProviderConnection, -} from '@backstage/plugin-catalog-backend'; +} from '@backstage/plugin-catalog-node'; import * as puppetFunctions from '../puppet/read'; import { ANNOTATION_PUPPET_CERTNAME } from '../puppet'; import { diff --git a/plugins/catalog-backend-module-puppetdb/src/providers/PuppetDbEntityProvider.ts b/plugins/catalog-backend-module-puppetdb/src/providers/PuppetDbEntityProvider.ts index b2d2738fd3..8c63baf467 100644 --- a/plugins/catalog-backend-module-puppetdb/src/providers/PuppetDbEntityProvider.ts +++ b/plugins/catalog-backend-module-puppetdb/src/providers/PuppetDbEntityProvider.ts @@ -17,7 +17,7 @@ import { EntityProvider, EntityProviderConnection, -} from '@backstage/plugin-catalog-backend'; +} from '@backstage/plugin-catalog-node'; import { Logger } from 'winston'; import { PuppetDbEntityProviderConfig, @@ -116,13 +116,13 @@ export class PuppetDbEntityProvider implements EntityProvider { this.transformer = transformer; } - /** {@inheritdoc @backstage/plugin-catalog-backend#EntityProvider.connect} */ + /** {@inheritdoc @backstage/plugin-catalog-node#EntityProvider.connect} */ async connect(connection: EntityProviderConnection): Promise { this.connection = connection; await this.scheduleFn(); } - /** {@inheritdoc @backstage/plugin-catalog-backend#EntityProvider.getProviderName} */ + /** {@inheritdoc @backstage/plugin-catalog-node#EntityProvider.getProviderName} */ getProviderName(): string { return `puppetdb-provider:${this.config.id}`; } diff --git a/yarn.lock b/yarn.lock index f0c5235907..ebfbdf2b5f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5252,13 +5252,14 @@ __metadata: resolution: "@backstage/plugin-catalog-backend-module-puppetdb@workspace:plugins/catalog-backend-module-puppetdb" dependencies: "@backstage/backend-common": "workspace:^" + "@backstage/backend-plugin-api": "workspace:^" "@backstage/backend-tasks": "workspace:^" "@backstage/backend-test-utils": "workspace:^" "@backstage/catalog-model": "workspace:^" "@backstage/cli": "workspace:^" "@backstage/config": "workspace:^" "@backstage/errors": "workspace:^" - "@backstage/plugin-catalog-backend": "workspace:^" + "@backstage/plugin-catalog-node": "workspace:^" "@backstage/types": "workspace:^" "@types/lodash": ^4.14.151 lodash: ^4.17.21