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,
field: string,
{ onFallback = () => '' }: { onFallback?: () => string },
): string =>
join(sanitizeQS(ids, { returnType: 'string[]' }), {
): { after: string; before: string[] } => {
const before = sanitizeQS(ids, { returnType: 'string[]' });
const after = join(before, {
beforeReturn: (toReturn) =>
toReturn ? `${field} IN (${toReturn})` : onFallback.call(null),
elementWrapper: "'",
separator: ', ',
}) as string;
return { after, before };
};

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

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