fix(striker-ui-api): add /host/:hostUUID WIP

main
Tsu-ba-me 2 years ago
parent 72e070bc38
commit e31d4564ee
  1. 41
      striker-ui-api/src/lib/request_handlers/host/buildQueryHostDetail.ts
  2. 58
      striker-ui-api/src/lib/request_handlers/host/getHost.ts
  3. 17
      striker-ui-api/src/lib/request_handlers/host/getHostDetail.ts
  4. 1
      striker-ui-api/src/lib/request_handlers/host/index.ts
  5. 2
      striker-ui-api/src/routes/host.ts
  6. 2
      striker-ui-api/src/types/BuildQueryFunction.d.ts

@ -0,0 +1,41 @@
import { buildKnownIDCondition } from '../../buildCondition';
import { buildQueryResultModifier } from '../../buildQueryResultModifier';
import { stdout } from '../../shell';
export const buildQueryHostDetail: BuildQueryDetailFunction = ({
keys: hostUUIDs = '*',
} = {}) => {
const condHostUUIDs = buildKnownIDCondition(hostUUIDs, 'AND hos.host_uuid');
stdout(`condHostUUIDs=[${condHostUUIDs}]`);
const query = `
SELECT
hos.host_name,
hos.host_uuid,
var.variable_name,
var.variable_value
FROM variables AS var
JOIN hosts AS hos
ON var.variable_source_uuid = hos.host_uuid
WHERE variable_name LIKE 'form::config_%'
${condHostUUIDs};`;
const afterQueryReturn: QueryResultModifierFunction =
buildQueryResultModifier((output) => {
const [hostName, hostUUID] = output[0];
return output.reduce<
{ hostName: string; hostUUID: string } & Record<string, string>
>(
(previous, [, variableName, variableValue]) => {
previous[variableName] = variableValue;
return previous;
},
{ hostName, hostUUID },
);
});
return { query, afterQueryReturn };
};

@ -1,35 +1,35 @@
import { getLocalHostUUID } from '../../accessModule'; import { getLocalHostUUID } from '../../accessModule';
import buildGetRequestHandler from '../buildGetRequestHandler'; import buildGetRequestHandler from '../buildGetRequestHandler';
import { buildUnknownIDCondition } from '../../buildCondition'; import { buildQueryHostDetail } from './buildQueryHostDetail';
import { sanitizeQS } from '../../sanitizeQS';
export const getHost = buildGetRequestHandler((request, buildQueryOptions) => { export const getHost = buildGetRequestHandler((request, buildQueryOptions) => {
const { hostUUIDs } = request.query; const { hostUUIDs } = request.query;
const hostUUIDField = 'hos.host_uuid'; let localHostUUID: string;
const { after: condHostUUIDs } = buildUnknownIDCondition(
hostUUIDs,
hostUUIDField,
{
onFallback: () => {
try { try {
return `${hostUUIDField} = '${getLocalHostUUID()}'`; localHostUUID = getLocalHostUUID();
} catch (subError) { } catch (subError) {
throw new Error(`Failed to get local host UUID; CAUSE: ${subError}`); throw new Error(`Failed to get local host UUID; CAUSE: ${subError}`);
} }
},
},
);
process.stdout.write(`condHostUUIDs=[${condHostUUIDs}]`); let query = `
SELECT
if (buildQueryOptions) { hos.host_name,
buildQueryOptions.afterQueryReturn = (queryStdout) => { hos.host_uuid
let result = queryStdout; FROM hosts AS hos;`;
let afterQueryReturn: QueryResultModifierFunction | undefined = (
if (queryStdout instanceof Array) { output: unknown,
result = queryStdout.reduce<Record<string, HostOverview>>( ) => {
let result = output;
if (output instanceof Array) {
result = output.reduce<Record<string, HostOverview>>(
(previous, [hostName, hostUUID]) => { (previous, [hostName, hostUUID]) => {
previous[hostUUID] = { hostName, hostUUID }; const key = hostUUID === localHostUUID ? 'local' : hostUUID;
previous[key] = { hostName, hostUUID };
return previous; return previous;
}, },
@ -39,11 +39,19 @@ export const getHost = buildGetRequestHandler((request, buildQueryOptions) => {
return result; return result;
}; };
if (hostUUIDs) {
({ query, afterQueryReturn } = buildQueryHostDetail({
keys: sanitizeQS(hostUUIDs, {
modifierType: 'sql',
returnType: 'string[]',
}),
}));
} }
return `SELECT if (buildQueryOptions) {
hos.host_name, buildQueryOptions.afterQueryReturn = afterQueryReturn;
hos.host_uuid }
FROM hosts AS hos
WHERE ${condHostUUIDs};`; return query;
}); });

@ -0,0 +1,17 @@
import buildGetRequestHandler from '../buildGetRequestHandler';
import { buildQueryHostDetail } from './buildQueryHostDetail';
import { sanitizeSQLParam } from '../../sanitizeSQLParam';
export const getHostDetail = buildGetRequestHandler(
({ params: { hostUUID } }, buildQueryOptions) => {
const { afterQueryReturn, query } = buildQueryHostDetail({
keys: [sanitizeSQLParam(hostUUID)],
});
if (buildQueryOptions) {
buildQueryOptions.afterQueryReturn = afterQueryReturn;
}
return query;
},
);

@ -1,3 +1,4 @@
export * from './createHost'; export * from './createHost';
export * from './getHost'; export * from './getHost';
export * from './getHostConnection'; export * from './getHostConnection';
export * from './getHostDetail';

@ -4,12 +4,14 @@ import {
createHost, createHost,
getHost, getHost,
getHostConnection, getHostConnection,
getHostDetail,
} from '../lib/request_handlers/host'; } from '../lib/request_handlers/host';
const router = express.Router(); const router = express.Router();
router router
.get('/', getHost) .get('/', getHost)
.get('/:hostUUID', getHostDetail)
.get('/connection', getHostConnection) .get('/connection', getHostConnection)
.post('/', createHost); .post('/', createHost);

@ -1,4 +1,4 @@
type QueryResultModifierFunction = (result: unknown) => unknown; type QueryResultModifierFunction = (output: unknown) => unknown;
type BuildQueryOptions = { type BuildQueryOptions = {
afterQueryReturn?: QueryResultModifierFunction; afterQueryReturn?: QueryResultModifierFunction;

Loading…
Cancel
Save