fix(striker-ui-api): add /fence

main
Tsu-ba-me 2 years ago committed by digimer
parent 3399920557
commit 92f00cea15
  1. 56
      striker-ui-api/src/lib/request_handlers/fence/getFence.ts
  2. 22
      striker-ui-api/src/lib/request_handlers/fence/getFenceTemplate.ts
  3. 2
      striker-ui-api/src/lib/request_handlers/fence/index.ts
  4. 9
      striker-ui-api/src/routes/fence.ts
  5. 2
      striker-ui-api/src/routes/index.ts
  6. 10
      striker-ui-api/src/types/APIFence.d.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<FenceParameters>((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;
},
);

@ -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);
};

@ -0,0 +1,2 @@
export * from './getFence';
export * from './getFenceTemplate';

@ -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;

@ -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<Record<string, Router>> = {
anvil: anvilRouter,
command: commandRouter,
echo: echoRouter,
fence: fenceRouter,
file: fileRouter,
host: hostRouter,
job: jobRouter,

@ -0,0 +1,10 @@
type FenceParameters = {
[parameterId: string]: string;
};
type FenceOverview = {
fenceAgent: string;
fenceParameters: FenceParameters;
fenceName: string;
fenceUUID: string;
};
Loading…
Cancel
Save