From 85db926bb6a40d0546113dbc13b7f70b961f22ce Mon Sep 17 00:00:00 2001 From: Deepankumar Loganathan Date: Tue, 20 Feb 2024 14:32:12 +0100 Subject: [PATCH 1/3] New backend system for Azure Site backend plugin Signed-off-by: Deepankumar Loganathan --- .changeset/five-mayflies-juggle.md | 5 +++ plugins/azure-sites-backend/README.md | 29 +++++++++++- plugins/azure-sites-backend/api-report.md | 4 ++ plugins/azure-sites-backend/package.json | 2 + plugins/azure-sites-backend/src/index.ts | 1 + plugins/azure-sites-backend/src/plugin.ts | 54 +++++++++++++++++++++++ yarn.lock | 2 + 7 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 .changeset/five-mayflies-juggle.md create mode 100644 plugins/azure-sites-backend/src/plugin.ts diff --git a/.changeset/five-mayflies-juggle.md b/.changeset/five-mayflies-juggle.md new file mode 100644 index 0000000000..56bf28a8e9 --- /dev/null +++ b/.changeset/five-mayflies-juggle.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-azure-sites-backend': patch +--- + +Added new backend system for the Azure Sites backend plugin diff --git a/plugins/azure-sites-backend/README.md b/plugins/azure-sites-backend/README.md index 892cbe4024..843e9ff92b 100644 --- a/plugins/azure-sites-backend/README.md +++ b/plugins/azure-sites-backend/README.md @@ -33,6 +33,8 @@ Configuration Details: Here's how to get the backend plugin up and running: +#### Legacy Backend System + 1. First we need to add the `@backstage/plugin-azure-sites-backend` package to your backend: ```sh @@ -105,6 +107,29 @@ Here's how to get the backend plugin up and running: } ``` -5. Now run `yarn start-backend` from the repo root. +#### New Backend System -6. Finally, open `http://localhost:7007/api/azure/health` in a browser, it should return `{"status":"ok"}`. +The Azure Sites backend plugin has support for the [new backend system](https://backstage.io/docs/backend-system/), here's how you can set that up: + +In your `packages/backend/src/index.ts` make the following changes: + +```diff + import { createBackend } from '@backstage/backend-defaults'; ++ import { azureSitesPlugin } from '@backstage/plugin-azure-sites-backend; + + const backend = createBackend(); + + // ... other feature additions + ++ backend.add(azureSitesPlugin); + + // ... + + backend.start(); +``` + +#### Start Backed & Test + +1. Now run `yarn start-backend` from the repo root. + +2. Finally, open `http://localhost:7007/api/azure/health` in a browser, it should return `{"status":"ok"}`. diff --git a/plugins/azure-sites-backend/api-report.md b/plugins/azure-sites-backend/api-report.md index 04cd22596a..5adc3c3c47 100644 --- a/plugins/azure-sites-backend/api-report.md +++ b/plugins/azure-sites-backend/api-report.md @@ -6,6 +6,7 @@ import { AzureSiteListRequest } from '@backstage/plugin-azure-sites-common'; import { AzureSiteListResponse } from '@backstage/plugin-azure-sites-common'; import { AzureSiteStartStopRequest } from '@backstage/plugin-azure-sites-common'; +import { BackendFeature } from '@backstage/backend-plugin-api'; import { CatalogApi } from '@backstage/catalog-client'; import { Config } from '@backstage/config'; import express from 'express'; @@ -50,6 +51,9 @@ export class AzureSitesConfig { readonly tenantId: string; } +// @public +export const azureSitesPlugin: () => BackendFeature; + // @public (undocumented) export function createRouter(options: RouterOptions): Promise; diff --git a/plugins/azure-sites-backend/package.json b/plugins/azure-sites-backend/package.json index 05eafa97f1..ff4b41441f 100644 --- a/plugins/azure-sites-backend/package.json +++ b/plugins/azure-sites-backend/package.json @@ -36,12 +36,14 @@ "@azure/arm-resourcegraph": "^4.2.1", "@azure/identity": "^4.0.0", "@backstage/backend-common": "workspace:^", + "@backstage/backend-plugin-api": "workspace:^", "@backstage/catalog-client": "workspace:^", "@backstage/catalog-model": "workspace:^", "@backstage/config": "workspace:^", "@backstage/errors": "workspace:^", "@backstage/plugin-auth-node": "workspace:^", "@backstage/plugin-azure-sites-common": "workspace:^", + "@backstage/plugin-catalog-node": "workspace:^", "@backstage/plugin-permission-common": "workspace:^", "@backstage/plugin-permission-node": "workspace:^", "@types/express": "^4.17.6", diff --git a/plugins/azure-sites-backend/src/index.ts b/plugins/azure-sites-backend/src/index.ts index 97f94e4470..22d120acc5 100644 --- a/plugins/azure-sites-backend/src/index.ts +++ b/plugins/azure-sites-backend/src/index.ts @@ -17,3 +17,4 @@ export * from './service/router'; export * from './api'; export * from './config'; +export { azureSitesPlugin } from './plugin'; diff --git a/plugins/azure-sites-backend/src/plugin.ts b/plugins/azure-sites-backend/src/plugin.ts new file mode 100644 index 0000000000..90a9c416bd --- /dev/null +++ b/plugins/azure-sites-backend/src/plugin.ts @@ -0,0 +1,54 @@ +/* + * Copyright 2024 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 { loggerToWinstonLogger } from '@backstage/backend-common'; +import { + coreServices, + createBackendPlugin, +} from '@backstage/backend-plugin-api'; +import { createRouter } from './service/router'; +import { catalogServiceRef } from '@backstage/plugin-catalog-node/alpha'; +import { AzureSitesApi } from './api'; + +/** + * Azure Sites Backend Plugin + * + * @public + */ +export const azureSitesPlugin = createBackendPlugin({ + pluginId: 'azure-sites', + register(env) { + env.registerInit({ + deps: { + config: coreServices.rootConfig, + logger: coreServices.logger, + httpRouter: coreServices.httpRouter, + permissions: coreServices.permissions, + catalogApi: catalogServiceRef, + }, + async init({ config, logger, httpRouter, permissions, catalogApi }) { + const azureSitesApi = AzureSitesApi.fromConfig(config); + httpRouter.use( + await createRouter({ + logger: loggerToWinstonLogger(logger), + azureSitesApi, + permissions, + catalogApi, + }), + ); + }, + }); + }, +}); diff --git a/yarn.lock b/yarn.lock index b612c502e2..f223ccdff7 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5093,6 +5093,7 @@ __metadata: "@azure/arm-resourcegraph": ^4.2.1 "@azure/identity": ^4.0.0 "@backstage/backend-common": "workspace:^" + "@backstage/backend-plugin-api": "workspace:^" "@backstage/catalog-client": "workspace:^" "@backstage/catalog-model": "workspace:^" "@backstage/cli": "workspace:^" @@ -5100,6 +5101,7 @@ __metadata: "@backstage/errors": "workspace:^" "@backstage/plugin-auth-node": "workspace:^" "@backstage/plugin-azure-sites-common": "workspace:^" + "@backstage/plugin-catalog-node": "workspace:^" "@backstage/plugin-permission-common": "workspace:^" "@backstage/plugin-permission-node": "workspace:^" "@types/express": ^4.17.6 From 0cd6d20f712d5c45974567e396027c283ff270fc Mon Sep 17 00:00:00 2001 From: Deepankumar Loganathan Date: Tue, 20 Feb 2024 15:03:33 +0100 Subject: [PATCH 2/3] fixed plugin export and README Signed-off-by: Deepankumar Loganathan --- plugins/azure-sites-backend/README.md | 6 ++++-- plugins/azure-sites-backend/api-report.md | 3 ++- plugins/azure-sites-backend/src/index.ts | 2 +- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/plugins/azure-sites-backend/README.md b/plugins/azure-sites-backend/README.md index 843e9ff92b..554a3d0cc4 100644 --- a/plugins/azure-sites-backend/README.md +++ b/plugins/azure-sites-backend/README.md @@ -51,14 +51,17 @@ Here's how to get the backend plugin up and running: } from '@backstage/plugin-azure-sites-backend'; import { Router } from 'express'; import { PluginEnvironment } from '../types'; + import { CatalogClient } from '@backstage/catalog-client' export default async function createPlugin( env: PluginEnvironment, ): Promise { return await createRouter({ + const catalogApi = new CatalogClient({ discoveryApi: env.discovery }) logger: env.logger, azureSitesApi: AzureSitesApi.fromConfig(env.config), permissions: env.permissions, + catalogApi }); } ``` @@ -115,13 +118,12 @@ In your `packages/backend/src/index.ts` make the following changes: ```diff import { createBackend } from '@backstage/backend-defaults'; -+ import { azureSitesPlugin } from '@backstage/plugin-azure-sites-backend; const backend = createBackend(); // ... other feature additions -+ backend.add(azureSitesPlugin); ++ backend.add(import('@backstage/plugin-azure-sites-backend')); // ... diff --git a/plugins/azure-sites-backend/api-report.md b/plugins/azure-sites-backend/api-report.md index 5adc3c3c47..9c2875e286 100644 --- a/plugins/azure-sites-backend/api-report.md +++ b/plugins/azure-sites-backend/api-report.md @@ -52,7 +52,8 @@ export class AzureSitesConfig { } // @public -export const azureSitesPlugin: () => BackendFeature; +const azureSitesPlugin: () => BackendFeature; +export default azureSitesPlugin; // @public (undocumented) export function createRouter(options: RouterOptions): Promise; diff --git a/plugins/azure-sites-backend/src/index.ts b/plugins/azure-sites-backend/src/index.ts index 22d120acc5..7f56b8e92a 100644 --- a/plugins/azure-sites-backend/src/index.ts +++ b/plugins/azure-sites-backend/src/index.ts @@ -17,4 +17,4 @@ export * from './service/router'; export * from './api'; export * from './config'; -export { azureSitesPlugin } from './plugin'; +export { azureSitesPlugin as default } from './plugin'; From 6f2442e977fd5db75c0c2e57598175a995007b17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20Adel=C3=B6w?= Date: Mon, 26 Feb 2024 16:34:04 +0100 Subject: [PATCH 3/3] Update plugins/azure-sites-backend/README.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fredrik Adelöw --- plugins/azure-sites-backend/README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/plugins/azure-sites-backend/README.md b/plugins/azure-sites-backend/README.md index 554a3d0cc4..18c51239c3 100644 --- a/plugins/azure-sites-backend/README.md +++ b/plugins/azure-sites-backend/README.md @@ -51,17 +51,17 @@ Here's how to get the backend plugin up and running: } from '@backstage/plugin-azure-sites-backend'; import { Router } from 'express'; import { PluginEnvironment } from '../types'; - import { CatalogClient } from '@backstage/catalog-client' + import { CatalogClient } from '@backstage/catalog-client'; export default async function createPlugin( env: PluginEnvironment, ): Promise { + const catalogApi = new CatalogClient({ discoveryApi: env.discovery }); return await createRouter({ - const catalogApi = new CatalogClient({ discoveryApi: env.discovery }) logger: env.logger, azureSitesApi: AzureSitesApi.fromConfig(env.config), permissions: env.permissions, - catalogApi + catalogApi, }); } ```