From b16d7d5d904ec0f26a5a948fcbfd05e3483e8e28 Mon Sep 17 00:00:00 2001 From: Wesley Date: Tue, 13 Sep 2022 06:35:06 +0200 Subject: [PATCH] PR feedback applied. Signed-off-by: Wesley --- plugins/azure-functions-backend/api-report.md | 22 +------ plugins/azure-functions-backend/package.json | 12 ++++ .../src/api/AzureWebManagementApi.ts | 2 +- .../azure-functions-backend/src/api/index.ts | 1 - .../src/service/router.ts | 5 +- plugins/azure-functions-common/.eslintrc.js | 1 + plugins/azure-functions-common/api-report.md | 27 +++++++++ plugins/azure-functions-common/package.json | 40 +++++++++++++ .../src/index.ts} | 12 +--- .../src}/types.ts | 0 plugins/azure-functions/README.md | 4 -- plugins/azure-functions/api-report.md | 13 +---- plugins/azure-functions/package.json | 12 ++++ .../src/api/AzureFunctionsApi.ts | 2 +- .../src/api/AzureFunctionsBackendClient.ts | 7 +-- plugins/azure-functions/src/api/index.ts | 1 - .../AzureFunctionsOverview.tsx | 25 +++++--- .../src/components/ErrorBoundary.tsx | 58 ------------------- .../OverviewTable.test.tsx | 2 +- .../OverviewTableComponent/OverviewTable.tsx | 2 +- .../azure-functions/src/hooks/useFunctions.ts | 2 +- .../src/hooks/useServiceEntityAnnotations.ts | 3 - plugins/azure-functions/src/mocks/mocks.ts | 2 +- yarn.lock | 10 ++++ 24 files changed, 135 insertions(+), 130 deletions(-) create mode 100644 plugins/azure-functions-common/.eslintrc.js create mode 100644 plugins/azure-functions-common/api-report.md create mode 100644 plugins/azure-functions-common/package.json rename plugins/{azure-functions/src/api/types.ts => azure-functions-common/src/index.ts} (73%) rename plugins/{azure-functions-backend/src/api => azure-functions-common/src}/types.ts (100%) delete mode 100644 plugins/azure-functions/src/components/ErrorBoundary.tsx diff --git a/plugins/azure-functions-backend/api-report.md b/plugins/azure-functions-backend/api-report.md index fd72fb6f42..f9e74f4161 100644 --- a/plugins/azure-functions-backend/api-report.md +++ b/plugins/azure-functions-backend/api-report.md @@ -3,18 +3,12 @@ > Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/). ```ts +import { AzureFunctionsAllowedSubscriptionsConfig } from '@backstage/plugin-azure-functions-common'; import { Config } from '@backstage/config'; import express from 'express'; +import { FunctionsData } from '@backstage/plugin-azure-functions-common'; import { Logger } from 'winston'; -// @public (undocumented) -export interface AzureFunctionsAllowedSubscriptionsConfig { - // (undocumented) - id: string; - // (undocumented) - name: string; -} - // @public (undocumented) export class AzureFunctionsConfig { constructor( @@ -50,18 +44,6 @@ export class AzureWebManagementApi { // @public (undocumented) export function createRouter(options: RouterOptions): Promise; -// @public (undocumented) -export type FunctionsData = { - href: string; - logstreamHref: string; - functionName: string; - location: string; - state: string; - usageState: string; - containerSize: number; - lastModifiedDate: Date; -}; - // @public (undocumented) export interface RouterOptions { // (undocumented) diff --git a/plugins/azure-functions-backend/package.json b/plugins/azure-functions-backend/package.json index cf5a5a5137..a1ee3494ee 100644 --- a/plugins/azure-functions-backend/package.json +++ b/plugins/azure-functions-backend/package.json @@ -13,6 +13,17 @@ "backstage": { "role": "backend-plugin" }, + "homepage": "https://backstage.io", + "repository": { + "type": "git", + "url": "https://github.com/backstage/backstage", + "directory": "plugins/azure-functions-backend" + }, + "keywords": [ + "backstage", + "functions", + "azure" + ], "scripts": { "start": "backstage-cli package start", "build": "backstage-cli package build", @@ -27,6 +38,7 @@ "@azure/identity": "^2.1.0", "@backstage/backend-common": "^0.15.1-next.2", "@backstage/config": "^1.0.1", + "@backstage/plugin-azure-functions-common": "workspace:^", "@types/express": "*", "express": "^4.17.1", "express-promise-router": "^4.1.0", diff --git a/plugins/azure-functions-backend/src/api/AzureWebManagementApi.ts b/plugins/azure-functions-backend/src/api/AzureWebManagementApi.ts index a78bf05ee2..ef801169c3 100644 --- a/plugins/azure-functions-backend/src/api/AzureWebManagementApi.ts +++ b/plugins/azure-functions-backend/src/api/AzureWebManagementApi.ts @@ -20,7 +20,7 @@ import { WebSiteManagementClient } from '@azure/arm-appservice'; import { AzureFunctionsAllowedSubscriptionsConfig, FunctionsData, -} from './types'; +} from '@backstage/plugin-azure-functions-common'; /** @public */ export class AzureFunctionsConfig { diff --git a/plugins/azure-functions-backend/src/api/index.ts b/plugins/azure-functions-backend/src/api/index.ts index bcd78290a5..b85d857d8f 100644 --- a/plugins/azure-functions-backend/src/api/index.ts +++ b/plugins/azure-functions-backend/src/api/index.ts @@ -18,4 +18,3 @@ export { AzureWebManagementApi, AzureFunctionsConfig, } from './AzureWebManagementApi'; -export * from './types'; diff --git a/plugins/azure-functions-backend/src/service/router.ts b/plugins/azure-functions-backend/src/service/router.ts index f603849895..81fb79b1ba 100644 --- a/plugins/azure-functions-backend/src/service/router.ts +++ b/plugins/azure-functions-backend/src/service/router.ts @@ -40,10 +40,11 @@ export async function createRouter( logger.info('PONG!'); response.send({ status: 'ok' }); }); - router.post('/list', async (request, response) => { + router.get('/list/:functionName', async (request, response) => { + const { functionName } = request.params; response.json( await azureWebManagementApi.list({ - functionName: request.body.functionName!.toString(), + functionName: functionName, }), ); }); diff --git a/plugins/azure-functions-common/.eslintrc.js b/plugins/azure-functions-common/.eslintrc.js new file mode 100644 index 0000000000..e2a53a6ad2 --- /dev/null +++ b/plugins/azure-functions-common/.eslintrc.js @@ -0,0 +1 @@ +module.exports = require('@backstage/cli/config/eslint-factory')(__dirname); diff --git a/plugins/azure-functions-common/api-report.md b/plugins/azure-functions-common/api-report.md new file mode 100644 index 0000000000..a7d47adb1a --- /dev/null +++ b/plugins/azure-functions-common/api-report.md @@ -0,0 +1,27 @@ +## API Report File for "@backstage/plugin-azure-functions-common" + +> Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/). + +```ts +// @public (undocumented) +export interface AzureFunctionsAllowedSubscriptionsConfig { + // (undocumented) + id: string; + // (undocumented) + name: string; +} + +// @public (undocumented) +export type FunctionsData = { + href: string; + logstreamHref: string; + functionName: string; + location: string; + state: string; + usageState: string; + containerSize: number; + lastModifiedDate: Date; +}; + +// (No @packageDocumentation comment for this package) +``` diff --git a/plugins/azure-functions-common/package.json b/plugins/azure-functions-common/package.json new file mode 100644 index 0000000000..3880b8965d --- /dev/null +++ b/plugins/azure-functions-common/package.json @@ -0,0 +1,40 @@ +{ + "name": "@backstage/plugin-azure-functions-common", + "description": "Common functionalities for the azure-functions plugin", + "version": "0.0.1", + "main": "src/index.ts", + "types": "src/index.ts", + "license": "Apache-2.0", + "publishConfig": { + "access": "public", + "main": "dist/index.cjs.js", + "module": "dist/index.esm.js", + "types": "dist/index.d.ts" + }, + "backstage": { + "role": "common-library" + }, + "homepage": "https://backstage.io", + "repository": { + "type": "git", + "url": "https://github.com/backstage/backstage", + "directory": "plugins/azure-functions-common" + }, + "keywords": [ + "backstage" + ], + "scripts": { + "build": "backstage-cli package build", + "lint": "backstage-cli package lint", + "test": "backstage-cli package test", + "clean": "backstage-cli package clean", + "prepack": "backstage-cli package prepack", + "postpack": "backstage-cli package postpack" + }, + "devDependencies": { + "@backstage/cli": "^0.19.0-next.2" + }, + "files": [ + "dist" + ] +} diff --git a/plugins/azure-functions/src/api/types.ts b/plugins/azure-functions-common/src/index.ts similarity index 73% rename from plugins/azure-functions/src/api/types.ts rename to plugins/azure-functions-common/src/index.ts index 024a7ee70b..db229eae34 100644 --- a/plugins/azure-functions/src/api/types.ts +++ b/plugins/azure-functions-common/src/index.ts @@ -14,14 +14,4 @@ * limitations under the License. */ -/** @public */ -export type FunctionsData = { - href: string; - logstreamHref: string; - functionName: string; - location: string; - state: string; - usageState: string; - containerSize: number; - lastModifiedDate: Date; -}; +export * from './types'; diff --git a/plugins/azure-functions-backend/src/api/types.ts b/plugins/azure-functions-common/src/types.ts similarity index 100% rename from plugins/azure-functions-backend/src/api/types.ts rename to plugins/azure-functions-common/src/types.ts diff --git a/plugins/azure-functions/README.md b/plugins/azure-functions/README.md index 712c0b16ff..4d3cf0e579 100644 --- a/plugins/azure-functions/README.md +++ b/plugins/azure-functions/README.md @@ -68,7 +68,3 @@ const serviceEntityPage = ( ); ``` - -## Roadmap - -- [ ] Metrics diff --git a/plugins/azure-functions/api-report.md b/plugins/azure-functions/api-report.md index 9bb5e00c9e..e217c5d65a 100644 --- a/plugins/azure-functions/api-report.md +++ b/plugins/azure-functions/api-report.md @@ -9,6 +9,7 @@ import { ApiRef } from '@backstage/core-plugin-api'; import { BackstagePlugin } from '@backstage/core-plugin-api'; import { DiscoveryApi } from '@backstage/core-plugin-api'; import { Entity } from '@backstage/catalog-model'; +import { FunctionsData } from '@backstage/plugin-azure-functions-common'; import { IdentityApi } from '@backstage/core-plugin-api'; import { RouteRef } from '@backstage/core-plugin-api'; @@ -52,18 +53,6 @@ export const EntityAzureFunctionsOverviewCard: () => JSX.Element; // @public (undocumented) export const entityContentRouteRef: RouteRef; -// @public (undocumented) -export type FunctionsData = { - href: string; - logstreamHref: string; - functionName: string; - location: string; - state: string; - usageState: string; - containerSize: number; - lastModifiedDate: Date; -}; - // @public (undocumented) export const isAzureFunctionsAvailable: (entity: Entity) => string | undefined; diff --git a/plugins/azure-functions/package.json b/plugins/azure-functions/package.json index 478f19acf5..d3b7be134f 100644 --- a/plugins/azure-functions/package.json +++ b/plugins/azure-functions/package.json @@ -13,6 +13,17 @@ "backstage": { "role": "frontend-plugin" }, + "homepage": "https://backstage.io", + "repository": { + "type": "git", + "url": "https://github.com/backstage/backstage", + "directory": "plugins/azure-functions" + }, + "keywords": [ + "backstage", + "functions", + "azure" + ], "scripts": { "start": "backstage-cli package start", "build": "backstage-cli package build", @@ -26,6 +37,7 @@ "@backstage/catalog-model": "^1.1.0", "@backstage/core-components": "^0.11.1-next.2", "@backstage/core-plugin-api": "^1.0.6-next.2", + "@backstage/plugin-azure-functions-common": "workspace:^", "@backstage/plugin-catalog-react": "^1.1.4-next.1", "@backstage/theme": "^0.2.16", "@material-ui/core": "^4.9.13", diff --git a/plugins/azure-functions/src/api/AzureFunctionsApi.ts b/plugins/azure-functions/src/api/AzureFunctionsApi.ts index 969b7596ff..914ba7c351 100644 --- a/plugins/azure-functions/src/api/AzureFunctionsApi.ts +++ b/plugins/azure-functions/src/api/AzureFunctionsApi.ts @@ -15,7 +15,7 @@ */ import { createApiRef } from '@backstage/core-plugin-api'; -import { FunctionsData } from './types'; +import { FunctionsData } from '@backstage/plugin-azure-functions-common'; /** @public */ export const azureFunctionsApiRef = createApiRef({ diff --git a/plugins/azure-functions/src/api/AzureFunctionsBackendClient.ts b/plugins/azure-functions/src/api/AzureFunctionsBackendClient.ts index 962fa3a113..bdc13f3e0c 100644 --- a/plugins/azure-functions/src/api/AzureFunctionsBackendClient.ts +++ b/plugins/azure-functions/src/api/AzureFunctionsBackendClient.ts @@ -15,7 +15,7 @@ */ import { AzureFunctionsApi } from './AzureFunctionsApi'; -import { FunctionsData } from './types'; +import { FunctionsData } from '@backstage/plugin-azure-functions-common'; import { DiscoveryApi, IdentityApi } from '@backstage/core-plugin-api'; /** @public */ @@ -38,15 +38,14 @@ export class AzureFunctionsBackendClient implements AzureFunctionsApi { try { const url = `${await this.discoveryApi.getBaseUrl( 'azure-functions', - )}/list`; + )}/list/${functionName}`; const { token: idToken } = await this.identityApi.getCredentials(); const response = await fetch(url, { - method: 'POST', + method: 'GET', headers: { 'Content-Type': 'application/json', ...(idToken && { Authorization: `Bearer ${idToken}` }), }, - body: JSON.stringify({ functionName: functionName }), }); return await response.json(); } catch (e: any) { diff --git a/plugins/azure-functions/src/api/index.ts b/plugins/azure-functions/src/api/index.ts index 4ea99fb412..de6176a600 100644 --- a/plugins/azure-functions/src/api/index.ts +++ b/plugins/azure-functions/src/api/index.ts @@ -16,4 +16,3 @@ export * from './AzureFunctionsApi'; export * from './AzureFunctionsBackendClient'; -export * from './types'; diff --git a/plugins/azure-functions/src/components/AzureFunctionsOverviewComponent/AzureFunctionsOverview.tsx b/plugins/azure-functions/src/components/AzureFunctionsOverviewComponent/AzureFunctionsOverview.tsx index 76ecc7035a..381bc8069e 100644 --- a/plugins/azure-functions/src/components/AzureFunctionsOverviewComponent/AzureFunctionsOverview.tsx +++ b/plugins/azure-functions/src/components/AzureFunctionsOverviewComponent/AzureFunctionsOverview.tsx @@ -21,8 +21,11 @@ import { AZURE_FUNCTIONS_ANNOTATION, useServiceEntityAnnotations, } from '../../hooks/useServiceEntityAnnotations'; -import { MissingAnnotationEmptyState } from '@backstage/core-components'; -import ErrorBoundary from '../ErrorBoundary'; +import { + ErrorBoundary, + MissingAnnotationEmptyState, + ResponseErrorPanel, +} from '@backstage/core-components'; import { useEntity } from '@backstage/plugin-catalog-react'; import { OverviewTable } from '../OverviewTableComponent/OverviewTable'; @@ -37,13 +40,19 @@ const AzureFunctionsOverview = ({ entity }: { entity: Entity }) => { functionsName, }); + if (functionsData.error) { + return ( +
+ +
+ ); + } + return ( - <> - - + ); }; diff --git a/plugins/azure-functions/src/components/ErrorBoundary.tsx b/plugins/azure-functions/src/components/ErrorBoundary.tsx deleted file mode 100644 index 01aede226b..0000000000 --- a/plugins/azure-functions/src/components/ErrorBoundary.tsx +++ /dev/null @@ -1,58 +0,0 @@ -/* - * 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 React, { Component } from 'react'; -import Alert from '@material-ui/lab/Alert'; - -interface Props {} -interface MyProps {} - -interface MyState { - hasError: boolean; -} - -export default class ErrorBoundary extends Component { - static getDerivedStateFromError() { - // Update state so the next render will show the fallback UI. - return { hasError: true }; - } - - constructor(props: Props) { - super(props); - this.state = { hasError: false }; - } - render() { - if (this.state.hasError) { - // You can render any custom fallback UI - return ( - - Something went wrong. Please make sure that you installed: - - - @backstage/plugin-azure-functions-backend - - - - ); - } - - return this.props.children; - } -} diff --git a/plugins/azure-functions/src/components/OverviewTableComponent/OverviewTable.test.tsx b/plugins/azure-functions/src/components/OverviewTableComponent/OverviewTable.test.tsx index 7642b76db2..a2eeb2b5e9 100644 --- a/plugins/azure-functions/src/components/OverviewTableComponent/OverviewTable.test.tsx +++ b/plugins/azure-functions/src/components/OverviewTableComponent/OverviewTable.test.tsx @@ -58,7 +58,7 @@ describe('AzureFunctionsOverviewWidget', () => { beforeEach(() => { worker.use( - rest.post('/list', (_, res, ctx) => { + rest.get('/list/func-mock', (_, res, ctx) => { res(ctx.json(functionResponseMock)); }), ); diff --git a/plugins/azure-functions/src/components/OverviewTableComponent/OverviewTable.tsx b/plugins/azure-functions/src/components/OverviewTableComponent/OverviewTable.tsx index 52044c9b1a..bb079ee020 100644 --- a/plugins/azure-functions/src/components/OverviewTableComponent/OverviewTable.tsx +++ b/plugins/azure-functions/src/components/OverviewTableComponent/OverviewTable.tsx @@ -16,7 +16,7 @@ import React from 'react'; import { Box, Card, Link, LinearProgress } from '@material-ui/core'; -import { FunctionsData } from '../../api/types'; +import { FunctionsData } from '@backstage/plugin-azure-functions-common'; import { Table, TableColumn } from '@backstage/core-components'; import FlashOnIcon from '@material-ui/icons/FlashOn'; diff --git a/plugins/azure-functions/src/hooks/useFunctions.ts b/plugins/azure-functions/src/hooks/useFunctions.ts index e70c301edf..22d8e339a7 100644 --- a/plugins/azure-functions/src/hooks/useFunctions.ts +++ b/plugins/azure-functions/src/hooks/useFunctions.ts @@ -16,7 +16,7 @@ import useAsyncRetry from 'react-use/lib/useAsyncRetry'; import { useApi, errorApiRef } from '@backstage/core-plugin-api'; -import { FunctionsData } from '../api/types'; +import { FunctionsData } from '@backstage/plugin-azure-functions-common'; import { azureFunctionsApiRef } from '../api'; import { useCallback } from 'react'; diff --git a/plugins/azure-functions/src/hooks/useServiceEntityAnnotations.ts b/plugins/azure-functions/src/hooks/useServiceEntityAnnotations.ts index ea8e653583..53d0736477 100644 --- a/plugins/azure-functions/src/hooks/useServiceEntityAnnotations.ts +++ b/plugins/azure-functions/src/hooks/useServiceEntityAnnotations.ts @@ -20,11 +20,8 @@ export const AZURE_FUNCTIONS_ANNOTATION = 'portal.azure.com/functions-name'; export const useServiceEntityAnnotations = (entity: Entity) => { const functionsName = entity?.metadata.annotations?.[AZURE_FUNCTIONS_ANNOTATION] ?? ''; - const projectName = - entity?.metadata.annotations?.['dev.azure.com/project-repo'] ?? ''; return { - projectName, functionsName, }; }; diff --git a/plugins/azure-functions/src/mocks/mocks.ts b/plugins/azure-functions/src/mocks/mocks.ts index e0fe70cafd..d6099e990a 100644 --- a/plugins/azure-functions/src/mocks/mocks.ts +++ b/plugins/azure-functions/src/mocks/mocks.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import { FunctionsData } from '../api'; +import { FunctionsData } from '@backstage/plugin-azure-functions-common'; export const entityMock = { metadata: { diff --git a/yarn.lock b/yarn.lock index 2fa9cbf8fe..d522bb31ac 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4150,6 +4150,7 @@ __metadata: "@backstage/backend-common": ^0.15.1-next.2 "@backstage/cli": ^0.19.0-next.2 "@backstage/config": ^1.0.1 + "@backstage/plugin-azure-functions-common": "workspace:^" "@types/express": "*" "@types/supertest": ^2.0.8 express: ^4.17.1 @@ -4162,6 +4163,14 @@ __metadata: languageName: unknown linkType: soft +"@backstage/plugin-azure-functions-common@workspace:^, @backstage/plugin-azure-functions-common@workspace:plugins/azure-functions-common": + version: 0.0.0-use.local + resolution: "@backstage/plugin-azure-functions-common@workspace:plugins/azure-functions-common" + dependencies: + "@backstage/cli": ^0.19.0-next.2 + languageName: unknown + linkType: soft + "@backstage/plugin-azure-functions@^0.1.0, @backstage/plugin-azure-functions@workspace:plugins/azure-functions": version: 0.0.0-use.local resolution: "@backstage/plugin-azure-functions@workspace:plugins/azure-functions" @@ -4172,6 +4181,7 @@ __metadata: "@backstage/core-components": ^0.11.1-next.2 "@backstage/core-plugin-api": ^1.0.6-next.2 "@backstage/dev-utils": ^1.0.6-next.1 + "@backstage/plugin-azure-functions-common": "workspace:^" "@backstage/plugin-catalog-react": ^1.1.4-next.1 "@backstage/test-utils": ^1.2.0-next.2 "@backstage/theme": ^0.2.16