Add FireHydrant annotation option

Signed-off-by: Dustin Brewer <mannkind@thenullpointer.net>
This commit is contained in:
Dustin Brewer
2023-03-09 22:47:17 +00:00
parent cd5cf69a61
commit e2e3dd08a5
10 changed files with 69 additions and 7 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-firehydrant': minor
---
Allow firehydrant to use component annotation
+8
View File
@@ -56,3 +56,11 @@ proxy:
# Supply the token you generated from https://app.firehydrant.io/organizations/bots
Authorization: Bearer fhb-e4911b22bcd788c4a4afeb0c111ffbfa
```
4. Optionally add an annotation to the yaml config file of a component
```yaml
metadata:
annotations:
firehydrant.com/service-name: <firehydrant-service-name>
```
+1
View File
@@ -23,6 +23,7 @@
"clean": "backstage-cli package clean"
},
"dependencies": {
"@backstage/catalog-model": "workspace:^",
"@backstage/core-components": "workspace:^",
"@backstage/core-plugin-api": "workspace:^",
"@backstage/plugin-catalog-react": "workspace:^",
+4 -1
View File
@@ -30,6 +30,7 @@ export interface FireHydrantAPI {
getServiceDetails(options: {
serviceName: string;
lookupByName: boolean;
}): Promise<ServiceDetailsResponse>;
getServiceIncidents(options: {
@@ -77,10 +78,12 @@ export class FireHydrantAPIClient implements FireHydrantAPI {
async getServiceDetails(options: {
serviceName: string;
lookupByName: boolean;
}): Promise<ServiceDetailsResponse> {
const queryOpt = options.lookupByName ? 'name' : 'query';
const proxyUrl = await this.getApiUrl();
const response = await fetch(
`${proxyUrl}/services?query=${options.serviceName}`,
`${proxyUrl}/services?${queryOpt}=${options.serviceName}`,
);
if (!response.ok) {
@@ -39,6 +39,7 @@ import {
ResponseErrorPanel,
} from '@backstage/core-components';
import { configApiRef, useApi } from '@backstage/core-plugin-api';
import { isFireHydrantAvailable, getFireHydrantServiceName } from '../hooks';
const useStyles = makeStyles(theme => ({
button: {
@@ -149,14 +150,14 @@ export const ServiceDetailsCard = () => {
const startDate = DateTime.now().minus({ days: 30 }).toUTC();
const endDate = DateTime.now().toUTC();
// The service name is provided by an annotation or a Backstage generated service name.
// The Backstage service name in FireHydrant is a unique formatted string
// that requires the entity's kind, name, and namespace.
const fireHydrantServiceName = `${entity?.kind}:${
entity?.metadata?.namespace ?? 'default'
}/${entity?.metadata?.name}`;
const fireHydrantServiceName = getFireHydrantServiceName(entity);
const { loading, value, error } = useServiceDetails({
serviceName: fireHydrantServiceName,
lookupByName: isFireHydrantAvailable(entity),
});
const activeIncidents: string[] = value?.service?.active_incidents ?? [];
@@ -0,0 +1,27 @@
/*
* 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.
*/
import { Entity } from '@backstage/catalog-model';
export const FIREHYDRANT_SERVICE_NAME_ANNOTATION =
'firehydrant.com/service-name';
export const isFireHydrantAvailable = (entity: Entity) =>
Boolean(entity.metadata.annotations?.[FIREHYDRANT_SERVICE_NAME_ANNOTATION]);
export const getFireHydrantServiceName = (entity: Entity) =>
isFireHydrantAvailable(entity)
? entity?.metadata.annotations?.[FIREHYDRANT_SERVICE_NAME_ANNOTATION] ?? ''
: `${entity?.kind}:${entity?.metadata?.namespace ?? 'default'}/${
entity?.metadata?.name
}`;
@@ -17,13 +17,22 @@ import useAsyncRetry from 'react-use/lib/useAsyncRetry';
import { fireHydrantApiRef } from '../api';
import { errorApiRef, useApi } from '@backstage/core-plugin-api';
export const useServiceDetails = ({ serviceName }: { serviceName: string }) => {
export const useServiceDetails = ({
serviceName,
lookupByName,
}: {
serviceName: string;
lookupByName: boolean;
}) => {
const api = useApi(fireHydrantApiRef);
const errorApi = useApi(errorApiRef);
const { loading, value, error, retry } = useAsyncRetry(async () => {
try {
return await api.getServiceDetails({ serviceName: serviceName });
return await api.getServiceDetails({
serviceName: serviceName,
lookupByName: lookupByName,
});
} catch (e) {
errorApi.post(e);
return Promise.reject(e);
+5 -1
View File
@@ -20,4 +20,8 @@
* @packageDocumentation
*/
export { firehydrantPlugin, FirehydrantCard } from './plugin';
export {
firehydrantPlugin,
FirehydrantCard,
isFireHydrantAvailable,
} from './plugin';
+3
View File
@@ -50,3 +50,6 @@ export const FirehydrantCard = firehydrantPlugin.provide(
},
}),
);
/** @public */
export { isFireHydrantAvailable } from './components/hooks';
+1
View File
@@ -6249,6 +6249,7 @@ __metadata:
version: 0.0.0-use.local
resolution: "@backstage/plugin-firehydrant@workspace:plugins/firehydrant"
dependencies:
"@backstage/catalog-model": "workspace:^"
"@backstage/cli": "workspace:^"
"@backstage/core-app-api": "workspace:^"
"@backstage/core-components": "workspace:^"