@openshift-assisted/ui-lib
Version:
React component library for the Assisted Installer UI
440 lines • 22.9 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.useAgentsFilter = exports.filterHosts = exports.useAgentsTable = exports.canUnbindAgent = exports.canChangeHostname = exports.canEditAgent = exports.canEditBMH = exports.infraEnvColumn = exports.clusterColumn = exports.agentStatusColumn = exports.discoveryTypeColumn = exports.agentHostnameColumn = void 0;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const react_router_dom_v5_compat_1 = require("react-router-dom-v5-compat");
const common_1 = require("../../../common");
const AgentStatus_1 = tslib_1.__importDefault(require("./AgentStatus"));
const tableUtils_1 = require("../../../common/components/hosts/tableUtils");
const helpers_1 = require("../helpers");
const helpers_2 = require("../ClusterDeployment/helpers");
const common_2 = require("../common");
const BMHStatus_1 = tslib_1.__importDefault(require("./BMHStatus"));
const status_1 = require("../helpers/status");
const utils_1 = require("../../../common/components/hosts/utils");
const agentStatus_1 = require("../helpers/agentStatus");
const noop_js_1 = tslib_1.__importDefault(require("lodash-es/noop.js"));
const use_translation_wrapper_1 = require("../../../common/hooks/use-translation-wrapper");
const agentHostnameColumn = (t, hosts, agents, bareMetalHosts, onEditHostname, canEditHostname, canEditBMH) => ({
header: {
title: t('ai:Hostname'),
props: {
id: 'col-header-hostname',
modifier: 'breakWord',
},
sort: true,
},
cell: (host) => {
const inventory = (0, common_1.getInventory)(host);
const editHostname = onEditHostname ? () => onEditHostname(host) : undefined;
const computedHostname = (0, common_1.getHostname)(host, inventory);
let readonly = true;
const agent = agents.find((a) => { var _a; return ((_a = a.metadata) === null || _a === void 0 ? void 0 : _a.uid) === host.id; });
if (agent) {
readonly = canEditHostname ? !canEditHostname(host) : false;
}
else {
const bmh = bareMetalHosts.find((bmh) => { var _a; return ((_a = bmh.metadata) === null || _a === void 0 ? void 0 : _a.uid) === host.id; });
if (bmh && canEditBMH) {
readonly = !canEditBMH(host);
}
}
return {
title: (React.createElement(common_1.Hostname, { host: host, inventory: inventory, onEditHostname: editHostname, hosts: hosts, readonly: readonly })),
props: { 'data-testid': 'host-name' },
sortableValue: computedHostname || '',
};
},
});
exports.agentHostnameColumn = agentHostnameColumn;
const discoveryTypeColumn = (agents, bareMetalHosts, t) => {
return {
header: {
title: t('ai:Discovery type'),
props: {
id: 'col-header-discovery-type',
},
sort: true,
},
cell: (host) => {
var _a, _b;
const agent = agents.find((a) => { var _a; return ((_a = a.metadata) === null || _a === void 0 ? void 0 : _a.uid) === host.id; });
let discoveryType = t('ai:Unknown');
if (agent) {
// eslint-disable-next-line no-prototype-builtins
discoveryType = ((_b = (_a = agent === null || agent === void 0 ? void 0 : agent.metadata) === null || _a === void 0 ? void 0 : _a.labels) === null || _b === void 0 ? void 0 : _b.hasOwnProperty(common_2.AGENT_BMH_NAME_LABEL_KEY))
? t('ai:BMC')
: t('ai:Discovery ISO');
}
else {
const bmh = bareMetalHosts.find((bmh) => { var _a; return ((_a = bmh.metadata) === null || _a === void 0 ? void 0 : _a.uid) === host.id; });
if (bmh) {
discoveryType = t('ai:BMC');
}
}
return {
title: discoveryType,
props: { 'data-testid': 'discovery-type' },
sortableValue: discoveryType,
};
},
};
};
exports.discoveryTypeColumn = discoveryTypeColumn;
const agentStatusColumn = ({ agents, agentStatuses, bareMetalHosts, bmhStatuses, onEditHostname, onApprove, wizardStepId, t, isDay2, }) => {
return {
header: {
title: t('ai:Status'),
props: {
id: 'col-header-infraenvstatus',
},
sort: true,
},
cell: (host) => {
const agent = agents.find((a) => { var _a; return ((_a = a.metadata) === null || _a === void 0 ? void 0 : _a.uid) === host.id; });
const bmh = bareMetalHosts === null || bareMetalHosts === void 0 ? void 0 : bareMetalHosts.find((b) => { var _a; return ((_a = b.metadata) === null || _a === void 0 ? void 0 : _a.uid) === host.id; });
let bmhStatus;
let title = '--';
if (agent) {
const editHostname = onEditHostname ? () => onEditHostname(agent) : undefined;
title = (React.createElement(AgentStatus_1.default, { agent: agent, onApprove: onApprove, onEditHostname: editHostname, wizardStepId: wizardStepId, isDay2: isDay2 }));
}
else if (bmh && bmhStatuses) {
bmhStatus = (0, status_1.getBMHStatus)(bmh, bmhStatuses);
title = React.createElement(BMHStatus_1.default, { bmhStatus: bmhStatus });
}
return {
title,
props: { 'data-testid': 'host-status' },
sortableValue: agent
? wizardStepId
? (0, status_1.getWizardStepAgentStatus)(agent, wizardStepId, t).status.title
: (0, status_1.getAgentStatus)(agent, agentStatuses).status.title
: (bmhStatus === null || bmhStatus === void 0 ? void 0 : bmhStatus.state.title) || '',
};
},
};
};
exports.agentStatusColumn = agentStatusColumn;
const clusterColumn = (agents, agentMachines, getClusterDeploymentLink, t) => {
return {
header: {
title: t('ai:Cluster'),
props: {
id: 'col-header-cluster',
},
sort: true,
},
cell: (host) => {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
const agent = agents.find((a) => { var _a; return ((_a = a.metadata) === null || _a === void 0 ? void 0 : _a.uid) === host.id; });
let cdLink = undefined;
if ((_a = agent === null || agent === void 0 ? void 0 : agent.spec) === null || _a === void 0 ? void 0 : _a.clusterDeploymentName) {
//hypershift
const amNamespace = (_c = (_b = agent.metadata) === null || _b === void 0 ? void 0 : _b.annotations) === null || _c === void 0 ? void 0 : _c['agentMachineRefNamespace'];
if (amNamespace) {
const am = agentMachines.find((am) => {
var _a, _b, _c, _d, _e, _f;
return ((_b = (_a = am.status) === null || _a === void 0 ? void 0 : _a.agentRef) === null || _b === void 0 ? void 0 : _b.name) === ((_c = agent.metadata) === null || _c === void 0 ? void 0 : _c.name) &&
((_e = (_d = am.status) === null || _d === void 0 ? void 0 : _d.agentRef) === null || _e === void 0 ? void 0 : _e.namespace) === ((_f = agent.metadata) === null || _f === void 0 ? void 0 : _f.namespace);
});
const nodePool = (_e = (_d = am === null || am === void 0 ? void 0 : am.metadata) === null || _d === void 0 ? void 0 : _d.annotations) === null || _e === void 0 ? void 0 : _e['hypershift.openshift.io/nodePool'];
if (nodePool) {
const hcNamespace = nodePool.split('/')[0];
const hcName = agent.spec.clusterDeploymentName.name;
cdLink = getClusterDeploymentLink({ name: hcName, namespace: hcNamespace });
}
}
else {
cdLink = getClusterDeploymentLink(agent.spec.clusterDeploymentName);
}
}
return {
title: cdLink ? React.createElement(react_router_dom_v5_compat_1.Link, { to: cdLink }, (_g = (_f = agent === null || agent === void 0 ? void 0 : agent.spec) === null || _f === void 0 ? void 0 : _f.clusterDeploymentName) === null || _g === void 0 ? void 0 : _g.name) : '--',
props: { 'data-testid': 'cluster' },
sortableValue: (_k = (_j = (_h = agent === null || agent === void 0 ? void 0 : agent.spec) === null || _h === void 0 ? void 0 : _h.clusterDeploymentName) === null || _j === void 0 ? void 0 : _j.name) !== null && _k !== void 0 ? _k : '--',
};
},
};
};
exports.clusterColumn = clusterColumn;
const infraEnvColumn = (agents, t) => {
return {
header: {
title: t('ai:Infrastructure env'),
props: {
id: 'col-header-infraenv',
},
sort: true,
},
cell: (host) => {
var _a;
const agent = agents.find((a) => { var _a; return ((_a = a.metadata) === null || _a === void 0 ? void 0 : _a.uid) === host.id; });
const infraEnvName = (0, helpers_1.getInfraEnvNameOfAgent)(agent);
const title = infraEnvName ? (React.createElement(react_router_dom_v5_compat_1.Link, { to: `/multicloud/infrastructure/environments/details/${((_a = agent.metadata) === null || _a === void 0 ? void 0 : _a.namespace) || ''}/${infraEnvName}/overview` }, infraEnvName)) : ('N/A');
return {
title,
props: { 'data-testid': 'infra-env' },
sortableValue: infraEnvName,
};
},
};
};
exports.infraEnvColumn = infraEnvColumn;
const canEditBMH = (bmh, t) => {
const canEdit = ['deprovisioning', 'pending', 'registering'].includes((0, status_1.getBMHStatusKey)(bmh) || '');
return [
canEdit,
canEdit
? undefined
: t('ai:Bare metal host cannot be edited. Remove this host and add it again if a change is needed.'),
];
};
exports.canEditBMH = canEditBMH;
const canEditAgent = (agent, agentStatuses, t) => {
const enabled = (0, status_1.getAgentStatus)(agent, agentStatuses).status.category !== 'Installation related';
return [
enabled,
enabled
? undefined
: t('ai:Hostname cannot be edited while host is either installed or being installed.'),
];
};
exports.canEditAgent = canEditAgent;
const canChangeHostname = (agents, agentStatuses, bareMetalHosts, t) => (h) => {
const agent = agents.find((a) => { var _a; return ((_a = a.metadata) === null || _a === void 0 ? void 0 : _a.uid) === h.id; });
if (agent) {
return (0, exports.canEditAgent)(agent, agentStatuses, t);
}
const bmh = bareMetalHosts.find((bmh) => { var _a; return ((_a = bmh.metadata) === null || _a === void 0 ? void 0 : _a.uid) === h.id; });
if (bmh) {
return (0, exports.canEditBMH)(bmh, t);
}
return [true, undefined];
};
exports.canChangeHostname = canChangeHostname;
const canUnbindAgent = (agentClusterInstalls, agent, t) => {
var _a, _b, _c, _d, _e, _f, _g;
if (!((_a = agent === null || agent === void 0 ? void 0 : agent.spec.clusterDeploymentName) === null || _a === void 0 ? void 0 : _a.name)) {
return [false, t('ai:The agent is not bound to a cluster.')];
}
const statusKey = (0, status_1.getAgentStatusKey)(agent);
if ([
'preparing-for-installation',
'installing',
'installing-in-progress',
'installing-pending-user-action',
'resetting-pending-user-action',
].includes(statusKey)) {
return [false, t('ai:It is not possible to remove a host which is being installed.')];
}
if (['installed', 'error', 'cancelled'].includes(statusKey) &&
(((_b = agent.status) === null || _b === void 0 ? void 0 : _b.role) === 'master' || ((_c = agent.status) === null || _c === void 0 ? void 0 : _c.role) === 'bootstrap')) {
return [
false,
t('ai:It is not possible to remove control plane node from an installed cluster.'),
];
}
if (agentClusterInstalls) {
const agentClusterInstall = (0, helpers_1.getAgentClusterInstallOfAgent)(agentClusterInstalls, agent);
if (agentClusterInstall) {
if ((0, helpers_2.isInstallationInProgress)(agentClusterInstall)) {
return [
false,
t('ai:It is not possible to remove a node from a cluster during installation.'),
];
}
}
}
if (!((_e = (_d = agent.metadata) === null || _d === void 0 ? void 0 : _d.labels) === null || _e === void 0 ? void 0 : _e.hasOwnProperty(common_2.AGENT_BMH_NAME_LABEL_KEY)) &&
((_g = (_f = agent.metadata) === null || _f === void 0 ? void 0 : _f.labels) === null || _g === void 0 ? void 0 : _g.hasOwnProperty(common_2.INFRAENV_GENERATED_AI_FLOW)) &&
!!agent.spec.clusterDeploymentName) {
return [false, t('ai:It is not possible to remove this node from the cluster.')];
}
return [true, undefined];
};
exports.canUnbindAgent = canUnbindAgent;
const canDeleteAgent = (agent, agentStatuses, t) => {
const enabled = (0, status_1.getAgentStatus)(agent, agentStatuses).status.category !== 'Installation related';
return [
enabled,
enabled
? undefined
: t('ai:Host cannot be deleted while host is either installed or being installed.'),
];
};
const useAgentsTable = ({ agents, bmhs, infraEnv, agentClusterInstalls }, tableActions) => {
const { onEditHost, onDeleteHost, onEditRole, onSelect, onEditBMH, onUnbindHost, onSetInstallationDiskId, } = tableActions || {};
const { t } = (0, use_translation_wrapper_1.useTranslation)();
const agentStatuses = (0, agentStatus_1.agentStatus)(t);
const [hosts, actions] = React.useMemo(() => [
(0, helpers_1.getAIHosts)(agents, bmhs, infraEnv),
{
onEditHost: onEditHost
? (host) => {
const agent = agents.find((a) => { var _a; return ((_a = a.metadata) === null || _a === void 0 ? void 0 : _a.uid) === host.id; });
if (agent) {
return onEditHost(agent);
}
const bmh = bmhs === null || bmhs === void 0 ? void 0 : bmhs.find((bmh) => { var _a; return ((_a = bmh.metadata) === null || _a === void 0 ? void 0 : _a.uid) === host.id; });
if (bmh && onEditBMH) {
return onEditBMH(bmh);
}
return noop_js_1.default;
}
: undefined,
canEditHost: (host) => {
const agent = agents.find((a) => { var _a; return ((_a = a.metadata) === null || _a === void 0 ? void 0 : _a.uid) === host.id; });
if (!agent) {
return false;
}
return (0, exports.canEditAgent)(agent, agentStatuses, t);
},
onDeleteHost: onDeleteHost
? (host) => {
const agent = agents.find((a) => { var _a; return ((_a = a.metadata) === null || _a === void 0 ? void 0 : _a.uid) === host.id; });
const bmh = bmhs === null || bmhs === void 0 ? void 0 : bmhs.find((a) => { var _a; return ((_a = a.metadata) === null || _a === void 0 ? void 0 : _a.uid) === host.id; });
return onDeleteHost(agent, bmh);
}
: undefined,
canDelete: (host) => {
const agent = agents.find((a) => { var _a; return ((_a = a.metadata) === null || _a === void 0 ? void 0 : _a.uid) === host.id; });
const bmh = bmhs === null || bmhs === void 0 ? void 0 : bmhs.find((a) => { var _a; return ((_a = a.metadata) === null || _a === void 0 ? void 0 : _a.uid) === host.id; });
if (agent) {
return canDeleteAgent(agent, agentStatuses, t);
}
else if (bmh) {
return [true, undefined];
}
return [false, t('ai:Host not found')];
},
onEditRole: onEditRole
? (host, role) => {
const agent = agents.find((a) => { var _a; return ((_a = a.metadata) === null || _a === void 0 ? void 0 : _a.uid) === host.id; });
return agent ? onEditRole(agent, role) : Promise.resolve(agent);
}
: undefined,
canEditRole: () => !!onEditRole,
onDiskRole: onSetInstallationDiskId
? (hostId, diskId) => {
const agent = agents.find((a) => { var _a; return ((_a = a.metadata) === null || _a === void 0 ? void 0 : _a.uid) === hostId; });
if (agent && diskId) {
return onSetInstallationDiskId(agent, diskId);
}
return Promise.resolve(agent);
}
: undefined,
canEditDisks: () => !!onSetInstallationDiskId,
onSelect: onSelect
? (host, selected) => {
const agent = agents.find((a) => { var _a; return ((_a = a.metadata) === null || _a === void 0 ? void 0 : _a.uid) === host.id; });
return agent ? onSelect(agent, selected) : noop_js_1.default;
}
: undefined,
onEditBMH: onEditBMH
? (host) => {
const bmh = bmhs === null || bmhs === void 0 ? void 0 : bmhs.find((h) => { var _a; return ((_a = h.metadata) === null || _a === void 0 ? void 0 : _a.uid) === host.id; });
return bmh ? onEditBMH(bmh) : noop_js_1.default;
}
: undefined,
canEditBMH: (host) => {
const bmh = bmhs === null || bmhs === void 0 ? void 0 : bmhs.find((h) => { var _a; return ((_a = h.metadata) === null || _a === void 0 ? void 0 : _a.uid) === host.id; });
if (!bmh) {
return [false, t('ai:Bare metal host not found')];
}
return (0, exports.canEditBMH)(bmh, t);
},
onUnbindHost: onUnbindHost
? (host) => {
const agent = agents.find((a) => { var _a; return ((_a = a.metadata) === null || _a === void 0 ? void 0 : _a.uid) === host.id; });
return agent ? onUnbindHost(agent) : noop_js_1.default;
}
: undefined,
canUnbindHost: (host) => {
const agent = agents.find((a) => { var _a; return ((_a = a.metadata) === null || _a === void 0 ? void 0 : _a.uid) === host.id; });
if (agent) {
return (0, exports.canUnbindAgent)(agentClusterInstalls, agent, t);
}
const bmh = bmhs === null || bmhs === void 0 ? void 0 : bmhs.find((bmh) => { var _a; return ((_a = bmh.metadata) === null || _a === void 0 ? void 0 : _a.uid) === host.id; });
if (bmh) {
return [false, t('ai:Bare metal host cannot be removed from cluster.')];
}
return [false, t('ai:Host not found')];
},
},
], [
agents,
t,
bmhs,
infraEnv,
onEditHost,
onDeleteHost,
onEditRole,
onSetInstallationDiskId,
onSelect,
onEditBMH,
onUnbindHost,
agentStatuses,
agentClusterInstalls,
]);
const actionResolver = React.useMemo(() => (0, tableUtils_1.hostActionResolver)(Object.assign({ t }, actions)), [actions, t]);
return [hosts, actions, actionResolver];
};
exports.useAgentsTable = useAgentsTable;
const filterByStatus = (hosts, agents, agentStatuses, bareMetalHosts, statusFilter) => {
if (!(statusFilter === null || statusFilter === void 0 ? void 0 : statusFilter.length)) {
return hosts;
}
const statusKeys = Object.keys(agentStatuses).filter((k) => statusFilter.includes(agentStatuses[k].title));
return hosts.filter((h) => {
const agent = agents.find((a) => { var _a; return ((_a = a.metadata) === null || _a === void 0 ? void 0 : _a.uid) === h.id; });
if (!agent) {
const bmh = bareMetalHosts.find((bmh) => { var _a; return ((_a = bmh.metadata) === null || _a === void 0 ? void 0 : _a.uid) === h.id; });
if (!bmh) {
return false;
}
return statusKeys.includes((0, status_1.getBMHStatusKey)(bmh) || '');
}
const { status } = (0, status_1.getAgentStatus)(agent, agentStatuses);
return statusKeys.includes(status.key);
});
};
const filterHosts = (hosts, agents, agentStatuses, bareMetalHosts, hostnameFilter, statusFilter) => {
const { hosts: byHostname, sorted } = (0, utils_1.filterByHostname)(hosts, hostnameFilter);
const byStatus = filterByStatus(byHostname, agents, agentStatuses, bareMetalHosts, statusFilter);
return { hosts: byStatus, sorted };
};
exports.filterHosts = filterHosts;
const useAgentsFilter = ({ agents, agentStatuses, bmhs, bmhStatuses, hosts, }) => {
const [hostnameFilter, setHostnameFilter] = React.useState();
const [statusFilter, setStatusFilter] = React.useState([]);
const statusCount = Object.keys(agentStatuses).reduce((acc, curr) => {
acc[agentStatuses[curr].title] = 0;
return acc;
}, {});
hosts.forEach((host) => {
const agent = agents.find((a) => { var _a; return ((_a = a.metadata) === null || _a === void 0 ? void 0 : _a.uid) === host.id; });
const bmh = bmhs.find((bmh) => { var _a; return ((_a = bmh.metadata) === null || _a === void 0 ? void 0 : _a.uid) === host.id; });
if (agent) {
const { status } = (0, status_1.getAgentStatus)(agent, agentStatuses);
statusCount[status.title]++;
}
else if (bmh) {
const { state } = (0, status_1.getBMHStatus)(bmh, bmhStatuses);
statusCount[state.title]++;
}
});
const { hosts: filteredHosts, sorted } = (0, exports.filterHosts)(hosts, agents, agentStatuses, bmhs, hostnameFilter, statusFilter);
return {
statusCount,
hostnameFilter,
setHostnameFilter,
statusFilter,
setStatusFilter,
filteredHosts,
sorted,
};
};
exports.useAgentsFilter = useAgentsFilter;
//# sourceMappingURL=tableUtils.js.map
;