From 90913537ffcab9dccf6d196aea39e16dba920b71 Mon Sep 17 00:00:00 2001 From: josh Date: Mon, 29 May 2023 21:17:12 -0400 Subject: [PATCH] Get rid of hook Signed-off-by: josh --- packages/app/package.json | 2 +- .../app/src/components/catalog/EntityPage.tsx | 2 +- plugins/nomad/package.json | 24 ++++++++- .../GroupList/GroupListForEntity.tsx | 45 ++++++++++++++--- plugins/nomad/src/hooks/useGroupForEntity.tsx | 49 ------------------- yarn.lock | 4 +- 6 files changed, 66 insertions(+), 60 deletions(-) delete mode 100644 plugins/nomad/src/hooks/useGroupForEntity.tsx diff --git a/packages/app/package.json b/packages/app/package.json index a41e7f1abf..60f753926b 100644 --- a/packages/app/package.json +++ b/packages/app/package.json @@ -50,7 +50,7 @@ "@backstage/plugin-microsoft-calendar": "workspace:^", "@backstage/plugin-newrelic": "workspace:^", "@backstage/plugin-newrelic-dashboard": "workspace:^", - "@backstage/plugin-nomad": "^0.0.0", + "@backstage/plugin-nomad": "workspace:^", "@backstage/plugin-octopus-deploy": "workspace:^", "@backstage/plugin-org": "workspace:^", "@backstage/plugin-pagerduty": "workspace:^", diff --git a/packages/app/src/components/catalog/EntityPage.tsx b/packages/app/src/components/catalog/EntityPage.tsx index c4450c8261..f6950f5c62 100644 --- a/packages/app/src/components/catalog/EntityPage.tsx +++ b/packages/app/src/components/catalog/EntityPage.tsx @@ -112,7 +112,7 @@ import { EntityOwnershipCard, EntityUserProfileCard, } from '@backstage/plugin-org'; -import { EntityNomadContent, isNomadAvailable } from '@backstage/plugin-nomad'; +import { EntityNomadContent } from '@backstage/plugin-nomad'; import { EntityPagerDutyCard, isPagerDutyAvailable, diff --git a/plugins/nomad/package.json b/plugins/nomad/package.json index 79a0fb0607..18a343ebd2 100644 --- a/plugins/nomad/package.json +++ b/plugins/nomad/package.json @@ -51,5 +51,27 @@ }, "files": [ "dist" - ] + ], + "configSchema": { + "$schema": "https://backstage.io/schema/config-v1", + "title": "@backstage/nomad", + "type": "object", + "properties": { + "nomad": { + "type": "object", + "properties": { + "addr": { + "type": "string", + "description": "The address of the Nomad API. See: https://developer.hashicorp.com/nomad/api-docs#addressing-and-ports", + "visibility": "frontend" + }, + "token": { + "type": "string", + "description": "The token to call the Nomad API with. See: https://developer.hashicorp.com/nomad/api-docs#authentication", + "visibility": "secret" + } + } + } + } + } } diff --git a/plugins/nomad/src/components/GroupList/GroupListForEntity.tsx b/plugins/nomad/src/components/GroupList/GroupListForEntity.tsx index b6323dcf87..d8af936569 100644 --- a/plugins/nomad/src/components/GroupList/GroupListForEntity.tsx +++ b/plugins/nomad/src/components/GroupList/GroupListForEntity.tsx @@ -15,19 +15,52 @@ */ import { Progress } from '@backstage/core-components'; -import React from 'react'; -import { useGroupForEntity } from '../../hooks/useGroupForEntity'; +import React, { useState } from 'react'; import { GroupListTable } from './GroupListTable'; +import { useEntity } from '@backstage/plugin-catalog-react'; +import { Allocation, nomadApiRef } from '../../api'; +import { ErrorApiError, errorApiRef, useApi } from '@backstage/core-plugin-api'; +import useAsync from 'react-use/lib/useAsync'; +import { + NOMAD_GROUP_ANNOTATION, + NOMAD_NAMESPACE_ANNOTATION, +} from '../../Router'; export const GroupListForEntity = () => { - const { value, loading, error } = useGroupForEntity(); + const { entity } = useEntity(); - if (loading) { + const namespace = + entity.metadata.annotations?.[NOMAD_NAMESPACE_ANNOTATION] ?? 'default'; + const group = entity.metadata.annotations?.[NOMAD_GROUP_ANNOTATION] ?? ''; + + const nomadApi = useApi(nomadApiRef); + const errorApi = useApi(errorApiRef); + + // Retrieve allocations for the group + const [allocations, setAllocations] = useState([]); + const response = useAsync(async () => { + // Wait until entity is loaded + if (!entity) { + return; + } + + try { + const resp = await nomadApi.listAllocations({ + namespace, + filter: `TaskGroup == "${group}"`, + }); + setAllocations(resp.allocations); + } catch (e) { + errorApi.post(e as ErrorApiError); + } + }, [group]); + + if (response.loading) { return ; } - if (error || !value) { + if (response.error || !response.value) { return null; } - return ; + return ; }; diff --git a/plugins/nomad/src/hooks/useGroupForEntity.tsx b/plugins/nomad/src/hooks/useGroupForEntity.tsx deleted file mode 100644 index fac3bff598..0000000000 --- a/plugins/nomad/src/hooks/useGroupForEntity.tsx +++ /dev/null @@ -1,49 +0,0 @@ -/* - * 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 { useEntity } from '@backstage/plugin-catalog-react'; -import { NOMAD_GROUP_ANNOTATION, NOMAD_NAMESPACE_ANNOTATION } from '../Router'; -import { errorApiRef, useApi } from '@backstage/core-plugin-api'; -import { nomadApiRef } from '../api'; -import useAsync from 'react-use/lib/useAsync'; - -/** - * Get the entity's group and query it from the Nomad API. - */ -export const useGroupForEntity = () => { - const { entity } = useEntity(); - - const namespace = - entity.metadata.annotations?.[NOMAD_NAMESPACE_ANNOTATION] ?? 'default'; - const group = entity.metadata.annotations?.[NOMAD_GROUP_ANNOTATION] ?? ''; - - const nomadApi = useApi(nomadApiRef); - const errorApi = useApi(errorApiRef); - - const response = useAsync( - () => - nomadApi.listAllocations({ - namespace, - filter: `TaskGroup == "${group}"`, - }), - [group], - ); - - if (response.error) { - errorApi.post(response.error); - } - return response; -}; diff --git a/yarn.lock b/yarn.lock index 919ab12667..34eba288b3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7926,7 +7926,7 @@ __metadata: languageName: unknown linkType: soft -"@backstage/plugin-nomad@^0.0.0, @backstage/plugin-nomad@workspace:plugins/nomad": +"@backstage/plugin-nomad@workspace:^, @backstage/plugin-nomad@workspace:plugins/nomad": version: 0.0.0-use.local resolution: "@backstage/plugin-nomad@workspace:plugins/nomad" dependencies: @@ -24551,7 +24551,7 @@ __metadata: "@backstage/plugin-microsoft-calendar": "workspace:^" "@backstage/plugin-newrelic": "workspace:^" "@backstage/plugin-newrelic-dashboard": "workspace:^" - "@backstage/plugin-nomad": ^0.0.0 + "@backstage/plugin-nomad": "workspace:^" "@backstage/plugin-octopus-deploy": "workspace:^" "@backstage/plugin-org": "workspace:^" "@backstage/plugin-pagerduty": "workspace:^"