From 974a91bc7b04f0354418b79963d697855e910c8b Mon Sep 17 00:00:00 2001 From: Sergio Betanzos Date: Thu, 15 Jul 2021 16:21:35 +0200 Subject: [PATCH] F OpenNebula/one#5422: Add rename action --- .../Tabs/Common/Attribute/Attribute.js | 145 ------------------ .../components/Tabs/Common/Attribute/index.js | 134 +++++++++++++++- .../src/client/components/Tabs/Common/List.js | 15 +- .../components/Tabs/Common/Ownership.js | 8 +- .../components/Tabs/Common/Permissions.js | 23 +-- .../client/components/Tabs/Vm/Info/index.js | 43 ++++-- .../components/Tabs/Vm/Info/information.js | 25 +-- src/fireedge/src/client/constants/actions.js | 25 +++ src/fireedge/src/client/constants/index.js | 1 + src/fireedge/src/client/constants/vm.js | 14 +- .../src/client/features/One/vm/actions.js | 1 + .../src/client/features/One/vm/hooks.js | 1 + .../src/client/features/One/vm/services.js | 21 +++ 13 files changed, 256 insertions(+), 200 deletions(-) delete mode 100644 src/fireedge/src/client/components/Tabs/Common/Attribute/Attribute.js create mode 100644 src/fireedge/src/client/constants/actions.js diff --git a/src/fireedge/src/client/components/Tabs/Common/Attribute/Attribute.js b/src/fireedge/src/client/components/Tabs/Common/Attribute/Attribute.js deleted file mode 100644 index 2fdb4db5fd..0000000000 --- a/src/fireedge/src/client/components/Tabs/Common/Attribute/Attribute.js +++ /dev/null @@ -1,145 +0,0 @@ -/* ------------------------------------------------------------------------- * - * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * - * * - * Licensed under the Apache License, Version 2.0 (the "License"); you may * - * not use this file except in compliance with the License. You may obtain * - * a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, software * - * distributed under the License is distributed on an "AS IS" BASIS, * - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * - * See the License for the specific language governing permissions and * - * limitations under the License. * - * ------------------------------------------------------------------------- */ -import * as React from 'react' -import PropTypes from 'prop-types' - -import { makeStyles, Typography } from '@material-ui/core' - -import { useDialog } from 'client/hooks' -import { DialogConfirmation } from 'client/components/Dialogs' -import { Tr } from 'client/components/HOC' -import * as Actions from 'client/components/Tabs/Common/Attribute/Actions' -import * as Inputs from 'client/components/Tabs/Common/Attribute/Inputs' - -const useStyles = makeStyles({ - wrapper: { - display: 'flex', - alignItems: 'center', - '& > *:first-child': { - flexGrow: 1 - } - }, - select: { - textOverflow: 'ellipsis' - } -}) - -const Attribute = React.memo(({ - canDelete, - canEdit, - handleEdit, - handleDelete, - handleGetOptionList, - name, - value, - valueInOptionList -}) => { - const classes = useStyles() - const [isEditing, setIsEditing] = React.useState(() => false) - const [options, setOptions] = React.useState(() => []) - const { display, show, hide } = useDialog() - const inputRef = React.createRef() - - const handleEditAttribute = async () => { - await handleEdit?.(inputRef.current.value) - setIsEditing(false) - } - - const handleCancel = () => { - setIsEditing(false) - } - - const handleActiveEditForm = async () => { - const response = await handleGetOptionList?.() ?? [] - const isFormatValid = response?.every?.(({ text, value } = {}) => !!text && !!value) - - if (!handleGetOptionList || isFormatValid) { - setOptions(response) - setIsEditing(true) - } - } - - const handleDeleteAttribute = async () => { - await handleDelete?.() - hide() - } - - return ( - <> - - {Tr(name)} - -
- {isEditing ? ( - <> - {handleGetOptionList ? ( - - ) : ( - - )} - - - - ) : ( - <> - - {value} - - {canEdit && ( - - )} - {canDelete && ( - - )} - - )} - - {display && ( - -

Are you sure?

-
- )} -
- - ) -}) - -Attribute.propTypes = { - canDelete: PropTypes.bool, - canEdit: PropTypes.bool, - handleEdit: PropTypes.func, - handleDelete: PropTypes.func, - handleGetOptionList: PropTypes.func, - name: PropTypes.string.isRequired, - value: PropTypes.string.isRequired, - valueInOptionList: PropTypes.string.isRequired -} - -Attribute.displayName = 'Attribute' - -export default Attribute diff --git a/src/fireedge/src/client/components/Tabs/Common/Attribute/index.js b/src/fireedge/src/client/components/Tabs/Common/Attribute/index.js index a76e888b76..34e9ded39c 100644 --- a/src/fireedge/src/client/components/Tabs/Common/Attribute/index.js +++ b/src/fireedge/src/client/components/Tabs/Common/Attribute/index.js @@ -13,6 +13,138 @@ * See the License for the specific language governing permissions and * * limitations under the License. * * ------------------------------------------------------------------------- */ -import Attribute from 'client/components/Tabs/Common/Attribute/Attribute' +import * as React from 'react' +import PropTypes from 'prop-types' + +import { makeStyles, Typography } from '@material-ui/core' + +import { useDialog } from 'client/hooks' +import { DialogConfirmation } from 'client/components/Dialogs' +import { Tr } from 'client/components/HOC' +import * as Actions from 'client/components/Tabs/Common/Attribute/Actions' +import * as Inputs from 'client/components/Tabs/Common/Attribute/Inputs' + +const useStyles = makeStyles({ + wrapper: { + display: 'flex', + alignItems: 'center', + '& > *:first-child': { + flexGrow: 1 + } + }, + select: { + textOverflow: 'ellipsis' + } +}) + +const Attribute = React.memo(({ + canDelete, + canEdit, + handleEdit, + handleDelete, + handleGetOptionList, + name, + value, + valueInOptionList +}) => { + const classes = useStyles() + const [isEditing, setIsEditing] = React.useState(() => false) + const [options, setOptions] = React.useState(() => []) + const { display, show, hide } = useDialog() + const inputRef = React.createRef() + + const handleEditAttribute = async () => { + await handleEdit?.(inputRef.current.value) + setIsEditing(false) + } + + const handleCancel = () => { + setIsEditing(false) + } + + const handleActiveEditForm = async () => { + const response = await handleGetOptionList?.() ?? [] + const isFormatValid = response?.every?.(({ text, value } = {}) => !!text && !!value) + + if (!handleGetOptionList || isFormatValid) { + setOptions(response) + setIsEditing(true) + } + } + + const handleDeleteAttribute = async () => { + await handleDelete?.() + hide() + } + + return ( + <> + + {Tr(name)} + +
+ {isEditing ? ( + <> + {handleGetOptionList ? ( + + ) : ( + + )} + + + + ) : ( + <> + + {value} + + {canEdit && ( + + )} + {canDelete && ( + + )} + + )} + + {display && ( + +

Are you sure?

+
+ )} +
+ + ) +}) + +export const AttributePropType = { + canDelete: PropTypes.bool, + canEdit: PropTypes.bool, + handleEdit: PropTypes.func, + handleDelete: PropTypes.func, + handleGetOptionList: PropTypes.func, + name: PropTypes.string.isRequired, + value: PropTypes.oneOfType([ + PropTypes.string, + PropTypes.object + ]).isRequired, + valueInOptionList: PropTypes.string +} + +Attribute.propTypes = AttributePropType + +Attribute.displayName = 'Attribute' export default Attribute diff --git a/src/fireedge/src/client/components/Tabs/Common/List.js b/src/fireedge/src/client/components/Tabs/Common/List.js index ef5b584c6b..d0d54f0f99 100644 --- a/src/fireedge/src/client/components/Tabs/Common/List.js +++ b/src/fireedge/src/client/components/Tabs/Common/List.js @@ -19,7 +19,7 @@ import PropTypes from 'prop-types' import { makeStyles, List as MList, ListItem, Typography, Paper } from '@material-ui/core' -import Attribute from 'client/components/Tabs/Common/Attribute' +import Attribute, { AttributePropType } from 'client/components/Tabs/Common/Attribute' import { Tr } from 'client/components/HOC' const useStyles = makeStyles(theme => ({ @@ -65,16 +65,9 @@ const List = ({ title, list = [], ...props }) => { List.propTypes = { title: PropTypes.string, - list: PropTypes.arrayOf(PropTypes.shape({ - canDelete: PropTypes.bool, - canEdit: PropTypes.bool, - handleEdit: PropTypes.func, - handleDelete: PropTypes.func, - handleGetOptionList: PropTypes.func, - name: PropTypes.string.isRequired, - value: PropTypes.any.isRequired, - valueInOptionList: PropTypes.string.isRequired - })) + list: PropTypes.arrayOf( + PropTypes.shape(AttributePropType) + ) } List.displayName = 'List' diff --git a/src/fireedge/src/client/components/Tabs/Common/Ownership.js b/src/fireedge/src/client/components/Tabs/Common/Ownership.js index 2f6d1fb0da..8ef7d7168a 100644 --- a/src/fireedge/src/client/components/Tabs/Common/Ownership.js +++ b/src/fireedge/src/client/components/Tabs/Common/Ownership.js @@ -18,9 +18,10 @@ import PropTypes from 'prop-types' import { useUserApi, useGroupApi, RESOURCES } from 'client/features/One' import { List } from 'client/components/Tabs/Common' -import { T, SERVERADMIN_ID } from 'client/constants' +import { T, SERVERADMIN_ID, ACTIONS } from 'client/constants' const Ownership = React.memo(({ + actions, groupId, groupName, handleEdit, @@ -54,7 +55,7 @@ const Ownership = React.memo(({ name: T.Owner, value: userName, valueInOptionList: userId, - canEdit: true, + canEdit: actions?.includes?.(ACTIONS.CHANGE_OWNER), handleGetOptionList: getUserOptions, handleEdit: user => handleEdit?.({ user }) }, @@ -62,7 +63,7 @@ const Ownership = React.memo(({ name: T.Group, value: groupName, valueInOptionList: groupId, - canEdit: true, + canEdit: actions?.includes?.(ACTIONS.CHANGE_GROUP), handleGetOptionList: getGroupOptions, handleEdit: group => handleEdit?.({ group }) } @@ -74,6 +75,7 @@ const Ownership = React.memo(({ }) Ownership.propTypes = { + actions: PropTypes.arrayOf(PropTypes.string), userId: PropTypes.string.isRequired, userName: PropTypes.string.isRequired, groupId: PropTypes.string.isRequired, diff --git a/src/fireedge/src/client/components/Tabs/Common/Permissions.js b/src/fireedge/src/client/components/Tabs/Common/Permissions.js index 4e4f286ce3..85dc40f512 100644 --- a/src/fireedge/src/client/components/Tabs/Common/Permissions.js +++ b/src/fireedge/src/client/components/Tabs/Common/Permissions.js @@ -21,7 +21,7 @@ import { Check as CheckIcon, Square as BlankSquareIcon } from 'iconoir-react' import { Action } from 'client/components/Cards/SelectCard' import { Tr } from 'client/components/HOC' -import { T } from 'client/constants' +import { T, ACTIONS } from 'client/constants' import * as Helper from 'client/models/Helper' @@ -43,7 +43,7 @@ const useStyles = makeStyles(theme => ({ } })) -const Permissions = React.memo(({ handleEdit, ...permissions }) => { +const Permissions = React.memo(({ handleEdit, actions, ...permissions }) => { const classes = useStyles() const handleChange = async (name, value) => { @@ -54,6 +54,8 @@ const Permissions = React.memo(({ handleEdit, ...permissions }) => { await handleEdit?.(newPermission) } + const getIcon = checked => +checked ? : + return ( @@ -75,14 +77,14 @@ const Permissions = React.memo(({ handleEdit, ...permissions }) => { .filter(([key, _]) => key.toLowerCase().startsWith(category)) .map(([key, permission]) => ( - : - } - handleClick={() => handleChange(key, permission)} - /> + {actions?.includes?.(ACTIONS.CHANGE_MODE) ? ( + handleChange(key, permission)} + /> + ) : getIcon(permission)} )) } @@ -94,6 +96,7 @@ const Permissions = React.memo(({ handleEdit, ...permissions }) => { }) Permissions.propTypes = { + actions: PropTypes.arrayOf(PropTypes.string), groupAdmin: PropTypes.string.isRequired, groupManage: PropTypes.string.isRequired, groupUse: PropTypes.string.isRequired, diff --git a/src/fireedge/src/client/components/Tabs/Vm/Info/index.js b/src/fireedge/src/client/components/Tabs/Vm/Info/index.js index c5e2e05b81..419ab44ae9 100644 --- a/src/fireedge/src/client/components/Tabs/Vm/Info/index.js +++ b/src/fireedge/src/client/components/Tabs/Vm/Info/index.js @@ -18,27 +18,42 @@ import * as React from 'react' import PropTypes from 'prop-types' import { useVmApi } from 'client/features/One' -import { Permissions, Ownership } from 'client/components/Tabs/Common' import { TabContext } from 'client/components/Tabs/TabProvider' +import { Permissions, Ownership } from 'client/components/Tabs/Common' import Information from 'client/components/Tabs/Vm/Info/information' -const VmInfoTab = ({ tabProps }) => { - const { changeOwnership, changePermissions } = useVmApi() - const { handleRefetch, data } = React.useContext(TabContext) - const { ID, UNAME, UID, GNAME, GID, PERMISSIONS } = data +import * as VirtualMachine from 'client/models/VirtualMachine' +import * as Helper from 'client/models/Helper' + +const VmInfoTab = ({ tabProps = {} }) => { + const { + information_panel: informationPanel, + permissions_panel: permissionsPanel, + ownership_panel: ownershipPanel + } = tabProps + + const { changeOwnership, changePermissions, rename } = useVmApi() + const { handleRefetch, data: vm } = React.useContext(TabContext) + const { ID, UNAME, UID, GNAME, GID, PERMISSIONS } = vm const handleChangeOwnership = async newOwnership => { const response = await changeOwnership(ID, newOwnership) - String(response) === String(ID) && await handleRefetch?.() } const handleChangePermission = async newPermission => { const response = await changePermissions(ID, newPermission) - String(response) === String(ID) && await handleRefetch?.() } + const handleRename = async newName => { + const response = await rename(ID, newName) + String(response) === String(ID) && await handleRefetch?.() + } + + const hypervisor = VirtualMachine.getHypervisor(vm) + const getActions = actions => Helper.getActionsAvailable(actions, hypervisor) + return (
{ gridTemplateColumns: 'repeat(auto-fit, minmax(480px, 1fr))', padding: '0.8em' }}> - {tabProps?.information_panel?.enabled && - + {informationPanel?.enabled && + } - {tabProps?.permissions_panel?.enabled && + {permissionsPanel?.enabled && { handleEdit={handleChangePermission} /> } - {tabProps?.ownership_panel?.enabled && + {ownershipPanel?.enabled && { - const { ID, NAME, RESCHED, STIME, ETIME, LOCK, DEPLOY_ID } = data +const InformationPanel = ({ vm = {}, handleRename, actions }) => { + const { ID, NAME, RESCHED, STIME, ETIME, LOCK, DEPLOY_ID } = vm - const { name: stateName, color: stateColor } = VirtualMachine.getState(data) + const { name: stateName, color: stateColor } = VirtualMachine.getState(vm) - const { HID: hostId, HOSTNAME: hostname = '--', CID: clusterId } = VirtualMachine.getLastHistory(data) + const { HID: hostId, HOSTNAME: hostname = '--', CID: clusterId } = VirtualMachine.getLastHistory(vm) const clusterName = clusterId === '-1' ? 'default' : '--' // TODO: get from cluster list - const ips = VirtualMachine.getIps(data) + const ips = VirtualMachine.getIps(vm) const info = [ { name: T.ID, value: ID }, { name: T.Name, value: NAME, - canEdit: true, - handleEdit: newName => { - - } + canEdit: actions?.includes?.(VM_ACTIONS.RENAME), + handleEdit: handleRename }, { name: T.State, @@ -89,4 +88,10 @@ const InformationPanel = data => { InformationPanel.displayName = 'InformationPanel' +InformationPanel.propTypes = { + actions: PropTypes.arrayOf(PropTypes.string), + handleRename: PropTypes.func, + vm: PropTypes.object +} + export default InformationPanel diff --git a/src/fireedge/src/client/constants/actions.js b/src/fireedge/src/client/constants/actions.js new file mode 100644 index 0000000000..b1001cd589 --- /dev/null +++ b/src/fireedge/src/client/constants/actions.js @@ -0,0 +1,25 @@ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * Licensed under the Apache License, Version 2.0 (the "License"); you may * + * not use this file except in compliance with the License. You may obtain * + * a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, software * + * distributed under the License is distributed on an "AS IS" BASIS, * + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * + * See the License for the specific language governing permissions and * + * limitations under the License. * + * ------------------------------------------------------------------------- */ + +// INFORMATION +export const RENAME = 'rename' + +// PERMISSION +export const CHANGE_MODE = 'chmod' + +// OWNERSHIP +export const CHANGE_OWNER = 'chown' +export const CHANGE_GROUP = 'chgrp' diff --git a/src/fireedge/src/client/constants/index.js b/src/fireedge/src/client/constants/index.js index 20a28524b2..84e2fc4882 100644 --- a/src/fireedge/src/client/constants/index.js +++ b/src/fireedge/src/client/constants/index.js @@ -81,6 +81,7 @@ export const SOCKETS = { } export * as T from 'client/constants/translates' +export * as ACTIONS from 'client/constants/actions' export * as STATES from 'client/constants/states' export * from 'client/constants/flow' export * from 'client/constants/provision' diff --git a/src/fireedge/src/client/constants/vm.js b/src/fireedge/src/client/constants/vm.js index a49f1e4010..cf82783bf8 100644 --- a/src/fireedge/src/client/constants/vm.js +++ b/src/fireedge/src/client/constants/vm.js @@ -14,6 +14,7 @@ * limitations under the License. * * ------------------------------------------------------------------------- */ import * as STATES from 'client/constants/states' +import * as ACTIONS from 'client/constants/actions' import COLOR from 'client/constants/color' /** @type {STATES.StateInfo[]} Virtual machine states */ @@ -465,15 +466,10 @@ export const VM_ACTIONS = { SAVE_RDP: 'save_rdp', SAVE_VIRT_VIEWER: 'save_virt_viewer', - // INFORMATION - RENAME: 'rename', - - // PERMISSION - CHMOD: 'chmod', - - // OWNERSHIP - CHOWN: 'chown', - CHANGE_GROUP: 'chgrp', + RENAME: ACTIONS.RENAME, + CHANGE_MODE: ACTIONS.CHANGE_MODE, + CHANGE_OWNER: ACTIONS.CHANGE_OWNER, + CHANGE_GROUP: ACTIONS.CHANGE_GROUP, // CAPACITY RESIZE_CAPACITY: 'resize_capacity', diff --git a/src/fireedge/src/client/features/One/vm/actions.js b/src/fireedge/src/client/features/One/vm/actions.js index aebe4e7765..3b7693fa35 100644 --- a/src/fireedge/src/client/features/One/vm/actions.js +++ b/src/fireedge/src/client/features/One/vm/actions.js @@ -40,6 +40,7 @@ export const terminateVm = createAction( }) ) +export const rename = createAction('vm/rename', vmService.rename) export const changePermissions = createAction('vm/chmod', vmService.changePermissions) export const changeOwnership = createAction('vm/chown', vmService.changeOwnership) export const detachNic = createAction('vm/detach/nic', vmService.detachNic) diff --git a/src/fireedge/src/client/features/One/vm/hooks.js b/src/fireedge/src/client/features/One/vm/hooks.js index 969694fce3..4eae6c66e8 100644 --- a/src/fireedge/src/client/features/One/vm/hooks.js +++ b/src/fireedge/src/client/features/One/vm/hooks.js @@ -36,6 +36,7 @@ export const useVmApi = () => { getVm: id => unwrapDispatch(actions.getVm({ id })), getVms: options => unwrapDispatch(actions.getVms(options)), terminateVm: id => unwrapDispatch(actions.terminateVm({ id })), + rename: (id, name) => unwrapDispatch(actions.rename({ id, name })), changePermissions: (id, permissions) => unwrapDispatch(actions.changePermissions({ id, permissions })), changeOwnership: (id, ownership) => diff --git a/src/fireedge/src/client/features/One/vm/services.js b/src/fireedge/src/client/features/One/vm/services.js index 8088beca76..2db97a135f 100644 --- a/src/fireedge/src/client/features/One/vm/services.js +++ b/src/fireedge/src/client/features/One/vm/services.js @@ -98,6 +98,27 @@ export const vmService = ({ return res?.data?.VM ?? {} }, + /** + * Renames a virtual machine. + * + * @param {object} params - Request parameters + * @param {string|number} params.id - Virtual machine id + * @param {string} params.name - New name + * @returns {number} Virtual machine id + * @throws Fails when response isn't code 200 + */ + rename: async ({ id, name: newName }) => { + const name = Actions.VM_RENAME + const command = { name, ...Commands[name] } + const config = requestConfig({ id, name: newName }, command) + + const res = await RestClient.request(config) + + if (!res?.id || res?.id !== httpCodes.ok.id) throw res?.data + + return res?.data + }, + /** * Changes the permission bits of a virtual machine. *