From 81974ef6268eec574c6415663f09d3359ce479a0 Mon Sep 17 00:00:00 2001 From: Tsu-ba-me Date: Tue, 22 Nov 2022 16:55:08 -0500 Subject: [PATCH] fix(striker-ui-api): add short host name to GET /host --- striker-ui-api/src/lib/getShortHostName.ts | 2 + .../src/lib/request_handlers/host/getHost.ts | 37 +++++++++---------- striker-ui-api/src/types/HostOverview.d.ts | 1 + 3 files changed, 20 insertions(+), 20 deletions(-) create mode 100644 striker-ui-api/src/lib/getShortHostName.ts diff --git a/striker-ui-api/src/lib/getShortHostName.ts b/striker-ui-api/src/lib/getShortHostName.ts new file mode 100644 index 00000000..1cd3daa1 --- /dev/null +++ b/striker-ui-api/src/lib/getShortHostName.ts @@ -0,0 +1,2 @@ +export const getShortHostName = (hostName: string) => + hostName.replace(/[.].*$/, ''); diff --git a/striker-ui-api/src/lib/request_handlers/host/getHost.ts b/striker-ui-api/src/lib/request_handlers/host/getHost.ts index cc5daf7e..f1173610 100644 --- a/striker-ui-api/src/lib/request_handlers/host/getHost.ts +++ b/striker-ui-api/src/lib/request_handlers/host/getHost.ts @@ -1,7 +1,9 @@ import { getLocalHostUUID } from '../../accessModule'; import buildGetRequestHandler from '../buildGetRequestHandler'; import { buildQueryHostDetail } from './buildQueryHostDetail'; +import { buildQueryResultReducer } from '../../buildQueryResultModifier'; import { toLocal } from '../../convertHostUUID'; +import { getShortHostName } from '../../getShortHostName'; import { sanitize } from '../../sanitize'; export const getHost = buildGetRequestHandler((request, buildQueryOptions) => { @@ -14,26 +16,21 @@ export const getHost = buildGetRequestHandler((request, buildQueryOptions) => { hos.host_name, hos.host_uuid FROM hosts AS hos;`; - let afterQueryReturn: QueryResultModifierFunction | undefined = ( - output: unknown, - ) => { - let result = output; - - if (output instanceof Array) { - result = output.reduce>( - (previous, [hostName, hostUUID]) => { - const key = toLocal(hostUUID, localHostUUID); - - previous[key] = { hostName, hostUUID }; - - return previous; - }, - {}, - ); - } - - return result; - }; + let afterQueryReturn: QueryResultModifierFunction | undefined = + buildQueryResultReducer<{ [hostUUID: string]: HostOverview }>( + (previous, [hostName, hostUUID]) => { + const key = toLocal(hostUUID, localHostUUID); + + previous[key] = { + hostName, + hostUUID, + shortHostName: getShortHostName(hostName), + }; + + return previous; + }, + {}, + ); if (hostUUIDs) { ({ query, afterQueryReturn } = buildQueryHostDetail({ diff --git a/striker-ui-api/src/types/HostOverview.d.ts b/striker-ui-api/src/types/HostOverview.d.ts index 64fab5ec..660798bd 100644 --- a/striker-ui-api/src/types/HostOverview.d.ts +++ b/striker-ui-api/src/types/HostOverview.d.ts @@ -1,4 +1,5 @@ type HostOverview = { hostName: string; hostUUID: string; + shortHostName: string; };