fix(striker-ui-api): allow multiple host views in GET /host/connection

main
Tsu-ba-me 2 years ago
parent 642ebfc4c3
commit 3bbc9c03c7
  1. 8
      striker-ui-api/src/lib/buildIDCondition.ts
  2. 2
      striker-ui-api/src/lib/request_handlers/host/getHost.ts
  3. 141
      striker-ui-api/src/lib/request_handlers/host/getHostConnection.ts
  4. 10
      striker-ui-api/src/types/DatabaseHash.d.ts
  5. 25
      striker-ui-api/src/types/HostConnectionOverview.d.ts

@ -5,10 +5,14 @@ export const buildIDCondition = (
ids: unknown, ids: unknown,
field: string, field: string,
{ onFallback = () => '' }: { onFallback?: () => string }, { onFallback = () => '' }: { onFallback?: () => string },
): string => ): { after: string; before: string[] } => {
join(sanitizeQS(ids, { returnType: 'string[]' }), { const before = sanitizeQS(ids, { returnType: 'string[]' });
const after = join(before, {
beforeReturn: (toReturn) => beforeReturn: (toReturn) =>
toReturn ? `${field} IN (${toReturn})` : onFallback.call(null), toReturn ? `${field} IN (${toReturn})` : onFallback.call(null),
elementWrapper: "'", elementWrapper: "'",
separator: ', ', separator: ', ',
}) as string; }) as string;
return { after, before };
};

@ -6,7 +6,7 @@ export const getHost = buildGetRequestHandler((request, buildQueryOptions) => {
const { hostUUIDs } = request.query; const { hostUUIDs } = request.query;
const hostUUIDField = 'hos.host_uuid'; const hostUUIDField = 'hos.host_uuid';
const condHostUUIDs = buildIDCondition(hostUUIDs, hostUUIDField, { const { after: condHostUUIDs } = buildIDCondition(hostUUIDs, hostUUIDField, {
onFallback: () => { onFallback: () => {
try { try {
return `${hostUUIDField} = '${getLocalHostUUID()}'`; return `${hostUUIDField} = '${getLocalHostUUID()}'`;

@ -2,12 +2,45 @@ import { getAnvilData, getLocalHostUUID } from '../../accessModule';
import { buildIDCondition } from '../../buildIDCondition'; import { buildIDCondition } from '../../buildIDCondition';
import buildGetRequestHandler from '../buildGetRequestHandler'; import buildGetRequestHandler from '../buildGetRequestHandler';
const buildHostConnections = (
fromHostUUID: string,
databaseHash: DatabaseHash,
{
defaultPort = 5432,
defaultUser = 'admin',
}: { defaultPort?: number; defaultUser?: string } = {},
) =>
Object.entries(databaseHash).reduce<HostConnectionOverview>(
(previous, [hostUUID, { host: ipAddress, ping, port: rawPort, user }]) => {
const port = parseInt(rawPort);
if (hostUUID === fromHostUUID) {
previous.inbound.port = port;
previous.inbound.user = user;
} else {
previous.peer[ipAddress] = {
hostUUID,
ipAddress,
isPing: ping === '1',
port,
user,
};
}
return previous;
},
{
inbound: { ipAddress: {}, port: defaultPort, user: defaultUser },
peer: {},
},
);
export const getHostConnection = buildGetRequestHandler( export const getHostConnection = buildGetRequestHandler(
(request, buildQueryOptions) => { (request, buildQueryOptions) => {
const { hostUUIDs } = request.query; const { hostUUIDs: rawHostUUIDs } = request.query;
let localHostUUID: string; let localHostUUID: string;
let rawDatabases: { let rawDatabaseData: {
[hostUUID: string]: { [hostUUID: string]: {
host: string; host: string;
name: string; name: string;
@ -25,87 +58,57 @@ export const getHostConnection = buildGetRequestHandler(
} }
const hostUUIDField = 'ip_add.ip_address_host_uuid'; const hostUUIDField = 'ip_add.ip_address_host_uuid';
const condHostUUIDs = buildIDCondition(hostUUIDs, hostUUIDField, { const { after: condHostUUIDs, before: beforeBuildIDCond } =
onFallback: () => `${hostUUIDField} = '${localHostUUID}'`, buildIDCondition(rawHostUUIDs, hostUUIDField, {
}); onFallback: () => `${hostUUIDField} = '${localHostUUID}'`,
});
const hostUUIDs =
beforeBuildIDCond.length > 0 ? beforeBuildIDCond : [localHostUUID];
const getConnectionKey = (hostUUID: string) =>
hostUUID === localHostUUID ? 'local' : hostUUID;
process.stdout.write(`condHostUUIDs=[${condHostUUIDs}]\n`); process.stdout.write(`condHostUUIDs=[${condHostUUIDs}]\n`);
try { try {
({ database: rawDatabases } = getAnvilData({ database: true })); ({ database: rawDatabaseData } = getAnvilData({ database: true }));
} catch (subError) { } catch (subError) {
throw new Error(`Failed to get anvil data; CAUSE: ${subError}`); throw new Error(`Failed to get anvil data; CAUSE: ${subError}`);
} }
const connections = Object.entries(rawDatabases).reduce<{ const connections = hostUUIDs.reduce<{
inbound: { [hostUUID: string]: HostConnectionOverview;
ipAddresses: { }>((previous, hostUUID) => {
[ipAddress: string]: { const connectionKey = getConnectionKey(hostUUID);
hostUUID: string;
ipAddress: string; previous[connectionKey] = buildHostConnections(hostUUID, rawDatabaseData);
ipAddressUUID: string;
networkLinkNumber: number;
networkNumber: number;
networkType: string;
};
};
port: number;
user: string;
};
peer: {
[ipAddress: string]: {
hostUUID: string;
ipAddress: string;
isPing: boolean;
port: number;
user: string;
};
};
}>(
(
previous,
[hostUUID, { host: ipAddress, ping, port: rawPort, user }],
) => {
const port = parseInt(rawPort);
if (hostUUID === localHostUUID) {
previous.inbound.port = port;
previous.inbound.user = user;
} else {
previous.peer[ipAddress] = {
hostUUID,
ipAddress,
isPing: ping === '1',
port,
user,
};
}
return previous; return previous;
}, }, {});
{ inbound: { ipAddresses: {}, port: 5432, user: 'admin' }, peer: {} },
);
process.stdout.write(`Connections=[\n${JSON.stringify(connections)}\n]\n`); process.stdout.write(`connections=[${JSON.stringify(connections)}]\n`);
if (buildQueryOptions) { if (buildQueryOptions) {
buildQueryOptions.afterQueryReturn = (queryStdout) => { buildQueryOptions.afterQueryReturn = (queryStdout) => {
let result = queryStdout; let result = queryStdout;
if (queryStdout instanceof Array) { if (queryStdout instanceof Array) {
queryStdout.forEach(([ipAddressUUID, ipAddress, network]) => { queryStdout.forEach(
const [, networkType, networkNumber, networkLinkNumber] = ([ipAddressUUID, hostUUID, ipAddress, network]) => {
network.match(/^([^\s]+)(\d+)_[^\s]+(\d+)$/); const [, networkType, rawNetworkNumber, rawNetworkLinkNumber] =
network.match(/^([^\s]+)(\d+)_[^\s]+(\d+)$/);
connections.inbound.ipAddresses[ipAddress] = { const connectionKey = getConnectionKey(hostUUID);
hostUUID: localHostUUID,
ipAddress, connections[connectionKey].inbound.ipAddress[ipAddress] = {
ipAddressUUID, hostUUID,
networkLinkNumber, ipAddress,
networkNumber, ipAddressUUID,
networkType, networkLinkNumber: parseInt(rawNetworkLinkNumber),
}; networkNumber: parseInt(rawNetworkNumber),
}); networkType,
};
},
);
result = connections; result = connections;
} }
@ -116,6 +119,7 @@ export const getHostConnection = buildGetRequestHandler(
return `SELECT return `SELECT
ip_add.ip_address_uuid, ip_add.ip_address_uuid,
ip_add.ip_address_host_uuid,
ip_add.ip_address_address, ip_add.ip_address_address,
CASE CASE
WHEN ip_add.ip_address_on_type = 'interface' WHEN ip_add.ip_address_on_type = 'interface'
@ -130,6 +134,7 @@ export const getHostConnection = buildGetRequestHandler(
LEFT JOIN bonds AS bon LEFT JOIN bonds AS bon
ON bri.bridge_uuid = bon.bond_bridge_uuid ON bri.bridge_uuid = bon.bond_bridge_uuid
OR ip_add.ip_address_on_uuid = bon.bond_uuid OR ip_add.ip_address_on_uuid = bon.bond_uuid
WHERE ${condHostUUIDs};`; WHERE ${condHostUUIDs}
AND ip_add.ip_address_note != 'DELETED';`;
}, },
); );

@ -0,0 +1,10 @@
type DatabaseHash = {
[hostUUID: string]: {
host: string;
name: string;
password: string;
ping: string;
port: string;
user: string;
};
};

@ -0,0 +1,25 @@
type HostConnectionOverview = {
inbound: {
ipAddress: {
[ipAddress: string]: {
hostUUID: string;
ipAddress: string;
ipAddressUUID: string;
networkLinkNumber: number;
networkNumber: number;
networkType: string;
};
};
port: number;
user: string;
};
peer: {
[ipAddress: string]: {
hostUUID: string;
ipAddress: string;
isPing: boolean;
port: number;
user: string;
};
};
};
Loading…
Cancel
Save