diff --git a/striker-ui-api/src/lib/request_handlers/fence/getFence.ts b/striker-ui-api/src/lib/request_handlers/fence/getFence.ts new file mode 100644 index 00000000..bde42bf7 --- /dev/null +++ b/striker-ui-api/src/lib/request_handlers/fence/getFence.ts @@ -0,0 +1,56 @@ +import { RequestHandler } from 'express'; + +import buildGetRequestHandler from '../buildGetRequestHandler'; +import { buildQueryResultReducer } from '../../buildQueryResultModifier'; +import { stdout } from '../../shell'; + +export const getFence: RequestHandler = buildGetRequestHandler( + (request, buildQueryOptions) => { + const query = ` + SELECT + fence_uuid, + fence_name, + fence_agent, + fence_arguments + FROM fences + ORDER BY fence_name ASC;`; + const afterQueryReturn: QueryResultModifierFunction | undefined = + buildQueryResultReducer<{ [fenceUUID: string]: FenceOverview }>( + (previous, [fenceUUID, fenceName, fenceAgent, fenceArgumentString]) => { + const fenceParameters = fenceArgumentString + .split(/\s+/) + .reduce((previous, parameterPair) => { + const [parameterId, parameterValue] = parameterPair.split(/=/); + + previous[parameterId] = parameterValue.replace(/['"]/g, ''); + + return previous; + }, {}); + + stdout( + `${fenceAgent}: ${fenceName} (${fenceUUID})\n${JSON.stringify( + fenceParameters, + null, + 2, + )}`, + ); + + previous[fenceUUID] = { + fenceAgent, + fenceParameters, + fenceName, + fenceUUID, + }; + + return previous; + }, + {}, + ); + + if (buildQueryOptions) { + buildQueryOptions.afterQueryReturn = afterQueryReturn; + } + + return query; + }, +); diff --git a/striker-ui-api/src/lib/request_handlers/fence/getFenceTemplate.ts b/striker-ui-api/src/lib/request_handlers/fence/getFenceTemplate.ts new file mode 100644 index 00000000..e285cc1a --- /dev/null +++ b/striker-ui-api/src/lib/request_handlers/fence/getFenceTemplate.ts @@ -0,0 +1,22 @@ +import { RequestHandler } from 'express'; +import { getAnvilData } from '../../accessModule'; +import { stderr } from '../../shell'; + +export const getFenceTemplate: RequestHandler = (request, response) => { + let rawFenceData; + + try { + ({ fence_data: rawFenceData } = getAnvilData( + { fence_data: true }, + { predata: [['Striker->get_fence_data']] }, + )); + } catch (subError) { + stderr(`Failed to get fence device template; CAUSE: ${subError}`); + + response.status(500).send(); + + return; + } + + response.status(200).send(rawFenceData); +}; diff --git a/striker-ui-api/src/lib/request_handlers/fence/index.ts b/striker-ui-api/src/lib/request_handlers/fence/index.ts new file mode 100644 index 00000000..9bdff0fc --- /dev/null +++ b/striker-ui-api/src/lib/request_handlers/fence/index.ts @@ -0,0 +1,2 @@ +export * from './getFence'; +export * from './getFenceTemplate'; diff --git a/striker-ui-api/src/routes/fence.ts b/striker-ui-api/src/routes/fence.ts new file mode 100644 index 00000000..0df4f266 --- /dev/null +++ b/striker-ui-api/src/routes/fence.ts @@ -0,0 +1,9 @@ +import express from 'express'; + +import { getFence, getFenceTemplate } from '../lib/request_handlers/fence'; + +const router = express.Router(); + +router.get('/', getFence).get('/template', getFenceTemplate); + +export default router; diff --git a/striker-ui-api/src/routes/index.ts b/striker-ui-api/src/routes/index.ts index a7c9147c..e37e78f8 100644 --- a/striker-ui-api/src/routes/index.ts +++ b/striker-ui-api/src/routes/index.ts @@ -3,6 +3,7 @@ import { Router } from 'express'; import anvilRouter from './anvil'; import commandRouter from './command'; import echoRouter from './echo'; +import fenceRouter from './fence'; import fileRouter from './file'; import hostRouter from './host'; import jobRouter from './job'; @@ -15,6 +16,7 @@ const routes: Readonly> = { anvil: anvilRouter, command: commandRouter, echo: echoRouter, + fence: fenceRouter, file: fileRouter, host: hostRouter, job: jobRouter, diff --git a/striker-ui-api/src/types/APIFence.d.ts b/striker-ui-api/src/types/APIFence.d.ts new file mode 100644 index 00000000..32ebcdce --- /dev/null +++ b/striker-ui-api/src/types/APIFence.d.ts @@ -0,0 +1,10 @@ +type FenceParameters = { + [parameterId: string]: string; +}; + +type FenceOverview = { + fenceAgent: string; + fenceParameters: FenceParameters; + fenceName: string; + fenceUUID: string; +};