parent
72e070bc38
commit
e31d4564ee
6 changed files with 108 additions and 39 deletions
@ -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'; |
||||
|
Loading…
Reference in new issue