Get rid of hook

Signed-off-by: josh <josh.timmons@hashicorp.com>
This commit is contained in:
josh
2023-05-29 21:17:12 -04:00
parent 86497612b6
commit 90913537ff
6 changed files with 66 additions and 60 deletions
+1 -1
View File
@@ -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:^",
@@ -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,
+23 -1
View File
@@ -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"
}
}
}
}
}
}
@@ -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<Allocation[]>([]);
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 <Progress />;
}
if (error || !value) {
if (response.error || !response.value) {
return null;
}
return <GroupListTable allocations={value.allocations} />;
return <GroupListTable allocations={allocations} />;
};
@@ -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;
};
+2 -2
View File
@@ -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:^"