From 0215f93a5d1ff0c3d829fe6ae59f398474b47df2 Mon Sep 17 00:00:00 2001 From: josh Date: Thu, 1 Jun 2023 23:28:50 -0400 Subject: [PATCH] Flesh out allocations table Signed-off-by: josh --- plugins/nomad-backend/package.json | 27 +----- plugins/nomad/package.json | 27 +++++- plugins/nomad/src/api.ts | 2 + .../GroupList/AllocationListTable.tsx | 96 +++++++++++++++++++ .../GroupList/GroupListForEntity.tsx | 32 ++++--- .../components/GroupList/GroupListTable.tsx | 47 --------- 6 files changed, 143 insertions(+), 88 deletions(-) create mode 100644 plugins/nomad/src/components/GroupList/AllocationListTable.tsx delete mode 100644 plugins/nomad/src/components/GroupList/GroupListTable.tsx diff --git a/plugins/nomad-backend/package.json b/plugins/nomad-backend/package.json index 0967c8ab65..344d120001 100644 --- a/plugins/nomad-backend/package.json +++ b/plugins/nomad-backend/package.json @@ -40,30 +40,5 @@ }, "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": "backend" - }, - "token": { - "type": "string", - "description": "The token to call the Nomad API with. See: https://developer.hashicorp.com/nomad/api-docs#authentication", - "visibility": "secret" - } - }, - "required": [ - "addr" - ] - } - } - } + ] } diff --git a/plugins/nomad/package.json b/plugins/nomad/package.json index 79a0fb0607..c4d5ad3d75 100644 --- a/plugins/nomad/package.json +++ b/plugins/nomad/package.json @@ -51,5 +51,30 @@ }, "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" + } + }, + "required": [ + "addr" + ] + } + } + } } diff --git a/plugins/nomad/src/api.ts b/plugins/nomad/src/api.ts index 6280bcbeda..b3057317fe 100644 --- a/plugins/nomad/src/api.ts +++ b/plugins/nomad/src/api.ts @@ -54,6 +54,8 @@ export interface Allocation { CreateTime: number; DeploymentStatus: DeploymentStatus; ID: string; + JobID: string; + JobVersion: string; ModifyTime: number; Name: string; Namespace: string; diff --git a/plugins/nomad/src/components/GroupList/AllocationListTable.tsx b/plugins/nomad/src/components/GroupList/AllocationListTable.tsx new file mode 100644 index 0000000000..bfabfca3a9 --- /dev/null +++ b/plugins/nomad/src/components/GroupList/AllocationListTable.tsx @@ -0,0 +1,96 @@ +/* + * 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 { + Link, + StatusError, + StatusOK, + StatusPending, + StatusRunning, + Table, + TableColumn, +} from '@backstage/core-components'; +import React from 'react'; +import { Allocation } from '../../api'; + +const columns: TableColumn[] = [ + { + title: 'ID', + render: row => ( + + {row.ID.split('-')[0]} + + ), + }, + { + title: 'Task Group', + render: row => ( + + {row.TaskGroup} + + ), + }, + { + title: 'Created', + render: row => new Date(row.CreateTime / 1000000).toLocaleString(), + }, + { + title: 'Status', + render: row => + ({ + pending: pending, + running: running, + failed: failed, + complete: complete, + }[row.ClientStatus] || {row.ClientStatus}), + }, + { + title: 'Version', + render: row => row.JobVersion, + }, + { + title: 'Client', + render: row => ( + + {row.ID.split('-')[0]} + + ), + }, +]; + +/** + * AllocationListTable is roughly based off Nomad's Allocations tab's view. + */ +export const AllocationListTable = (props: { + allocations: Allocation[]; + nomadAddr: string; +}) => ( + ({ + ...allocation, + id: allocation.ID, + nomadAddr: props.nomadAddr, + }))} + /> +); diff --git a/plugins/nomad/src/components/GroupList/GroupListForEntity.tsx b/plugins/nomad/src/components/GroupList/GroupListForEntity.tsx index ac11cb2665..9586136fec 100644 --- a/plugins/nomad/src/components/GroupList/GroupListForEntity.tsx +++ b/plugins/nomad/src/components/GroupList/GroupListForEntity.tsx @@ -16,15 +16,16 @@ import { Progress } from '@backstage/core-components'; import React, { useState } from 'react'; -import { GroupListTable } from './GroupListTable'; +import { AllocationListTable } from './AllocationListTable'; import { useEntity } from '@backstage/plugin-catalog-react'; import { Allocation, nomadApiRef } from '../../api'; -import { ErrorApiError, errorApiRef, useApi } from '@backstage/core-plugin-api'; +import { configApiRef, useApi } from '@backstage/core-plugin-api'; import useAsync from 'react-use/lib/useAsync'; import { NOMAD_GROUP_ANNOTATION, NOMAD_NAMESPACE_ANNOTATION, } from '../../Router'; +import { Alert } from '@material-ui/lab'; export const GroupListForEntity = () => { const { entity } = useEntity(); @@ -33,8 +34,11 @@ export const GroupListForEntity = () => { entity.metadata.annotations?.[NOMAD_NAMESPACE_ANNOTATION] ?? 'default'; const group = entity.metadata.annotations?.[NOMAD_GROUP_ANNOTATION] ?? ''; + const configApi = useApi(configApiRef); const nomadApi = useApi(nomadApiRef); - const errorApi = useApi(errorApiRef); + + // Get the Nomad server address for links in the table + const nomadAddr = configApi.getString('nomad.addr'); // Retrieve allocations for the group const [allocations, setAllocations] = useState([]); @@ -45,23 +49,23 @@ export const GroupListForEntity = () => { } // Issue call to nomad-backend - try { - const resp = await nomadApi.listAllocations({ - namespace, - filter: `TaskGroup == "${group}"`, - }); - setAllocations(resp.allocations); - } catch (e) { - errorApi.post(e as ErrorApiError); - } + const resp = await nomadApi.listAllocations({ + namespace, + filter: `TaskGroup == "${group}"`, + }); + setAllocations( + resp.allocations.sort((a, b) => b.CreateTime - a.CreateTime), + ); }, [group]); if (response.loading) { return ; } if (response.error) { - return null; + return {response.error.message}; } - return ; + return ( + + ); }; diff --git a/plugins/nomad/src/components/GroupList/GroupListTable.tsx b/plugins/nomad/src/components/GroupList/GroupListTable.tsx deleted file mode 100644 index b427946805..0000000000 --- a/plugins/nomad/src/components/GroupList/GroupListTable.tsx +++ /dev/null @@ -1,47 +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 { Table, TableColumn } from '@backstage/core-components'; -import React from 'react'; -import { Allocation } from '../../api'; - -const columns: TableColumn[] = [ - { - title: 'Allocation ID', - field: 'ID', - }, - { - title: 'Name', - field: 'Name', - }, -]; - -export const GroupListTable = ({ - allocations, -}: { - allocations: Allocation[]; -}) => { - return ( -
- ); -};