From 53bfad8576c9f626a1fdf4ba1158180642b3b6c1 Mon Sep 17 00:00:00 2001 From: Patrick Jungermann Date: Thu, 6 Oct 2022 00:24:20 +0200 Subject: [PATCH] feat(events,example): add simple way to add event-based entity providers Add `DemoEventBasedEntityProvider` as example implementation. Signed-off-by: Patrick Jungermann --- packages/backend/package.json | 1 + packages/backend/src/index.ts | 12 +++- packages/backend/src/plugins/catalog.ts | 3 + .../src/plugins/catalogEventBasedProviders.ts | 61 +++++++++++++++++++ yarn.lock | 1 + 5 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 packages/backend/src/plugins/catalogEventBasedProviders.ts diff --git a/packages/backend/package.json b/packages/backend/package.json index 083779951f..3285e309e1 100644 --- a/packages/backend/package.json +++ b/packages/backend/package.json @@ -39,6 +39,7 @@ "@backstage/plugin-azure-sites-backend": "workspace:^", "@backstage/plugin-badges-backend": "workspace:^", "@backstage/plugin-catalog-backend": "workspace:^", + "@backstage/plugin-catalog-node": "workspace:^", "@backstage/plugin-code-coverage-backend": "workspace:^", "@backstage/plugin-events-backend": "workspace:^", "@backstage/plugin-events-node": "workspace:^", diff --git a/packages/backend/src/index.ts b/packages/backend/src/index.ts index 856b7e8595..bacae07917 100644 --- a/packages/backend/src/index.ts +++ b/packages/backend/src/index.ts @@ -42,6 +42,7 @@ import { metricsInit, metricsHandler } from './metrics'; import auth from './plugins/auth'; import azureDevOps from './plugins/azure-devops'; import catalog from './plugins/catalog'; +import catalogEventBasedProviders from './plugins/catalogEventBasedProviders'; import codeCoverage from './plugins/codecoverage'; import events from './plugins/events'; import kubernetes from './plugins/kubernetes'; @@ -144,10 +145,17 @@ async function main() { const playlistEnv = useHotMemoize(module, () => createEnv('playlist')); const eventsEnv = useHotMemoize(module, () => createEnv('events')); + const eventBasedEntityProviders = await catalogEventBasedProviders( + catalogEnv, + ); + const apiRouter = Router(); - apiRouter.use('/catalog', await catalog(catalogEnv)); + apiRouter.use( + '/catalog', + await catalog(catalogEnv, eventBasedEntityProviders), + ); apiRouter.use('/code-coverage', await codeCoverage(codeCoverageEnv)); - apiRouter.use('/events', await events(eventsEnv, [])); + apiRouter.use('/events', await events(eventsEnv, eventBasedEntityProviders)); apiRouter.use('/rollbar', await rollbar(rollbarEnv)); apiRouter.use('/scaffolder', await scaffolder(scaffolderEnv)); apiRouter.use('/tech-insights', await techInsights(techInsightsEnv)); diff --git a/packages/backend/src/plugins/catalog.ts b/packages/backend/src/plugins/catalog.ts index 7beeb4f35e..f6fe25f86a 100644 --- a/packages/backend/src/plugins/catalog.ts +++ b/packages/backend/src/plugins/catalog.ts @@ -15,15 +15,18 @@ */ import { CatalogBuilder } from '@backstage/plugin-catalog-backend'; +import { EntityProvider } from '@backstage/plugin-catalog-node'; import { ScaffolderEntitiesProcessor } from '@backstage/plugin-scaffolder-backend'; import { Router } from 'express'; import { PluginEnvironment } from '../types'; export default async function createPlugin( env: PluginEnvironment, + providers?: Array, ): Promise { const builder = await CatalogBuilder.create(env); builder.addProcessor(new ScaffolderEntitiesProcessor()); + builder.addEntityProvider(providers ?? []); const { processingEngine, router } = await builder.build(); await processingEngine.start(); return router; diff --git a/packages/backend/src/plugins/catalogEventBasedProviders.ts b/packages/backend/src/plugins/catalogEventBasedProviders.ts new file mode 100644 index 0000000000..346ed40fa6 --- /dev/null +++ b/packages/backend/src/plugins/catalogEventBasedProviders.ts @@ -0,0 +1,61 @@ +/* + * Copyright 2020 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 { + EntityProvider, + EntityProviderConnection, +} from '@backstage/plugin-catalog-node'; +import { EventParams, EventSubscriber } from '@backstage/plugin-events-node'; +import { Logger } from 'winston'; +import { PluginEnvironment } from '../types'; + +class DemoEventBasedEntityProvider implements EntityProvider, EventSubscriber { + constructor( + private readonly logger: Logger, + private readonly topics: string[], + ) {} + + async onEvent(params: EventParams): Promise { + this.logger.info( + `onEvent: topic=${params.topic}, metadata=${JSON.stringify( + params.metadata, + )}, payload=${JSON.stringify(params.eventPayload)}`, + ); + } + + supportsEventTopics(): string[] { + return this.topics; + } + + async connect(_: EntityProviderConnection): Promise { + // not doing anything here + } + + getProviderName(): string { + return DemoEventBasedEntityProvider.name; + } +} + +export default async function createCatalogEventBasedProviders( + env: PluginEnvironment, +): Promise> { + const providers: Array< + (EntityProvider & EventSubscriber) | Array + > = []; + providers.push(new DemoEventBasedEntityProvider(env.logger, ['example'])); + // add your event-based entity providers here + return providers.flat(); +} diff --git a/yarn.lock b/yarn.lock index d5c72dbb57..535a0c483e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -22340,6 +22340,7 @@ __metadata: "@backstage/plugin-azure-sites-backend": "workspace:^" "@backstage/plugin-badges-backend": "workspace:^" "@backstage/plugin-catalog-backend": "workspace:^" + "@backstage/plugin-catalog-node": "workspace:^" "@backstage/plugin-code-coverage-backend": "workspace:^" "@backstage/plugin-events-backend": "workspace:^" "@backstage/plugin-events-node": "workspace:^"