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. 84
      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,49 +1,57 @@
import { getLocalHostUUID } from '../../accessModule';
import buildGetRequestHandler from '../buildGetRequestHandler';
import { buildUnknownIDCondition } from '../../buildCondition';
import { buildQueryHostDetail } from './buildQueryHostDetail';
import { sanitizeQS } from '../../sanitizeQS';
export const getHost = buildGetRequestHandler((request, buildQueryOptions) => {
const { hostUUIDs } = request.query;
const hostUUIDField = 'hos.host_uuid';
const { after: condHostUUIDs } = buildUnknownIDCondition(
hostUUIDs,
hostUUIDField,
{
onFallback: () => {
try {
return `${hostUUIDField} = '${getLocalHostUUID()}'`;
} catch (subError) {
throw new Error(`Failed to get local host UUID; CAUSE: ${subError}`);
}
},
},
);
process.stdout.write(`condHostUUIDs=[${condHostUUIDs}]`);
let localHostUUID: string;
try {
localHostUUID = getLocalHostUUID();
} catch (subError) {
throw new Error(`Failed to get local host UUID; CAUSE: ${subError}`);
}
let query = `
SELECT
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<Record<string, HostOverview>>(
(previous, [hostName, hostUUID]) => {
const key = hostUUID === localHostUUID ? 'local' : hostUUID;
previous[key] = { hostName, hostUUID };
return previous;
},
{},
);
}
return result;
};
if (hostUUIDs) {
({ query, afterQueryReturn } = buildQueryHostDetail({
keys: sanitizeQS(hostUUIDs, {
modifierType: 'sql',
returnType: 'string[]',
}),
}));
}
if (buildQueryOptions) {
buildQueryOptions.afterQueryReturn = (queryStdout) => {
let result = queryStdout;
if (queryStdout instanceof Array) {
result = queryStdout.reduce<Record<string, HostOverview>>(
(previous, [hostName, hostUUID]) => {
previous[hostUUID] = { hostName, hostUUID };
return previous;
},
{},
);
}
return result;
};
buildQueryOptions.afterQueryReturn = afterQueryReturn;
}
return `SELECT
hos.host_name,
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 './getHost';
export * from './getHostConnection';
export * from './getHostDetail';

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

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

Loading…
Cancel
Save