1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-04-01 06:50:25 +03:00

F #5422: Fix fn to check if action is available (#1993)

This commit is contained in:
Sergio Betanzos 2022-04-29 14:00:55 +02:00 committed by GitHub
parent 87480c1790
commit a8ab46c37f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 29 additions and 18 deletions

View File

@ -125,7 +125,7 @@ const PreConsoleButton = memo(
const isDisabled = useMemo(() => {
const noAction = vmView?.actions?.[connectionType] !== true
const noAvailable = !isAvailableAction(connectionType)(vm)
const noAvailable = !isAvailableAction(connectionType, vm)
const notHypervisor = !getHypervisor(vm)
return noAction || noAvailable || notHypervisor

View File

@ -66,7 +66,10 @@ const useTableStyles = makeStyles({
})
const isDisabled = (action) => (rows) =>
!isAvailableAction(action)(rows, ({ values }) => values?.state)
!isAvailableAction(
action,
rows.map(({ original }) => original)
)
const ListVmNames = ({ rows = [] }) => {
const { data: datastores = [] } = useGetDatastoresQuery()

View File

@ -39,7 +39,7 @@ const VmCapacityTab = ({ tabProps: { actions } = {}, id }) => {
const hypervisor = getHypervisor(vm)
const actionsByHypervisor = getActionsAvailable(actions, hypervisor)
const actionsByState = actionsByHypervisor.filter((action) =>
isAvailableAction(action)(vm)
isAvailableAction(action, vm)
)
return actionsByState

View File

@ -42,7 +42,7 @@ const VmHistoryTab = ({ tabProps: { actions } = {}, id }) => {
const hypervisor = getHypervisor(vm)
const actionsByHypervisor = getActionsAvailable(actions, hypervisor)
const actionsByState = actionsByHypervisor.filter((action) =>
isAvailableAction(action)(vm)
isAvailableAction(action, vm)
)
return [getHistoryRecords(vm), actionsByState]

View File

@ -57,7 +57,7 @@ const VmNetworkTab = ({ tabProps: { actions } = {}, id }) => {
const hyperV = getHypervisor(vm)
const actionsByHypervisor = getActionsAvailable(actions, hyperV)
const actionsByState = actionsByHypervisor.filter((action) =>
isAvailableAction(action)(vm)
isAvailableAction(action, vm)
)
return [groupedNics, hyperV, actionsByState]

View File

@ -65,7 +65,7 @@ const VmSchedulingTab = ({ tabProps: { actions } = {}, id }) => {
const hypervisor = getHypervisor(vm)
const actionsByHypervisor = getActionsAvailable(actions, hypervisor)
const actionsByState = actionsByHypervisor.filter((action) =>
isAvailableAction(action)(vm)
isAvailableAction(action, vm)
)
return [getScheduleActions(vm), actionsByState]

View File

@ -51,7 +51,7 @@ const VmSnapshotTab = ({ tabProps: { actions } = {}, id }) => {
const hypervisor = getHypervisor(vm)
const actionsByHypervisor = getActionsAvailable(actions, hypervisor)
const actionsByState = actionsByHypervisor.filter((action) =>
isAvailableAction(action)(vm)
isAvailableAction(action, vm)
)
return [getSnapshotList(vm), actionsByState]

View File

@ -66,7 +66,7 @@ const VmStorageTab = ({ tabProps: { actions } = {}, id }) => {
const hyperV = getHypervisor(vm)
const actionsByHypervisor = getActionsAvailable(actions, hyperV)
const actionsByState = actionsByHypervisor.filter((action) =>
isAvailableAction(action)(vm)
isAvailableAction(action, vm)
)
return [getDisks(vm), hyperV, actionsByState]

View File

@ -317,21 +317,29 @@ export const getScheduleActions = (vm) => {
}
/**
* Returns `true` if action is available by VM state.
* Check if action is available for **all VMs**.
*
* @param {object} action - VM action
* @returns {function(Array, Function):boolean}
* - The list of vms that will be perform the action
* @param {VM|VM[]} vms - Virtual machines
* @returns {boolean} If `true`, the action is available for all VMs
*/
export const isAvailableAction =
(action) =>
(vms = [], getVmState = (vm) => getState(vm)?.name) => {
if (VM_ACTIONS_BY_STATE[action]?.length === 0) return true
export const isAvailableAction = (action, vms = []) => {
if (VM_ACTIONS_BY_STATE[action]?.length === 0) return true
const states = [vms].flat().map(getVmState)
return [vms].flat().every((vm) => {
const state = VM_STATES[vm.STATE]?.name
const lcmState = VM_LCM_STATES[vm.LCM_STATE]?.name
return states?.some((state) => VM_ACTIONS_BY_STATE[action]?.includes(state))
}
return (
(state === STATES.ACTIVE &&
// if action includes ACTIVE state,
// it means that the action is available in all LCM states
(VM_ACTIONS_BY_STATE[action]?.includes(STATES.ACTIVE) ||
VM_ACTIONS_BY_STATE[action]?.includes(lcmState))) ||
VM_ACTIONS_BY_STATE[action]?.includes(state)
)
})
}
/**
* @param {VM} vm - Virtual machine