mirror of
https://github.com/OpenNebula/one.git
synced 2026-12-02 05:06:14 +09:00
F OpenNebula/one#5422: Add actions to attribute panel (#1377)
This commit is contained in:
1 parent
ef2bfcdc2d
commit
fbc9c4bcfa
18 files changed
+12866
-277
No files matched your search
Generated
+12218
-205
File diff suppressed because it is too large.
Load diff
@@ -84,6 +84,7 @@
|
||||
"dagre": "0.8.5",
|
||||
"dompurify": "2.2.6",
|
||||
"express": "4.17.1",
|
||||
"fast-xml-parser": "3.19.0",
|
||||
"fs-extra": "9.0.1",
|
||||
"fuse.js": "6.4.1",
|
||||
"helmet": "4.1.1",
|
||||
|
||||
@@ -33,7 +33,10 @@ const useStyles = makeStyles({
|
||||
},
|
||||
select: {
|
||||
textOverflow: 'ellipsis'
|
||||
}
|
||||
},
|
||||
nested: ({ numberOfParents }) => numberOfParents > 0 && ({
|
||||
paddingLeft: `${numberOfParents}em`
|
||||
})
|
||||
})
|
||||
|
||||
const Attribute = React.memo(({
|
||||
@@ -43,17 +46,20 @@ const Attribute = React.memo(({
|
||||
handleDelete,
|
||||
handleGetOptionList,
|
||||
name,
|
||||
path = name,
|
||||
value,
|
||||
valueInOptionList
|
||||
}) => {
|
||||
const classes = useStyles()
|
||||
const numberOfParents = React.useMemo(() => path.split('.').length - 1, [path])
|
||||
const classes = useStyles({ numberOfParents })
|
||||
|
||||
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)
|
||||
await handleEdit?.(inputRef.current.value, path)
|
||||
setIsEditing(false)
|
||||
}
|
||||
|
||||
@@ -72,13 +78,13 @@ const Attribute = React.memo(({
|
||||
}
|
||||
|
||||
const handleDeleteAttribute = async () => {
|
||||
await handleDelete?.(name)
|
||||
await handleDelete?.(path)
|
||||
hide()
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<Typography noWrap variant='body2' title={Tr(name)}>
|
||||
<Typography noWrap variant='body2' title={Tr(name)} className={classes.nested}>
|
||||
{Tr(name)}
|
||||
</Typography>
|
||||
<div className={classes.wrapper}>
|
||||
@@ -116,7 +122,7 @@ const Attribute = React.memo(({
|
||||
|
||||
{display && (
|
||||
<DialogConfirmation
|
||||
title={`Delete attribute: ${name}`}
|
||||
title={`Delete attribute: ${path}`}
|
||||
handleAccept={handleDeleteAttribute}
|
||||
handleCancel={hide}
|
||||
>
|
||||
@@ -139,6 +145,7 @@ export const AttributePropTypes = {
|
||||
PropTypes.string,
|
||||
PropTypes.object
|
||||
]).isRequired,
|
||||
path: PropTypes.string,
|
||||
valueInOptionList: PropTypes.string
|
||||
}
|
||||
|
||||
|
||||
@@ -43,8 +43,8 @@ const AttributeCreateForm = React.memo(({ handleAdd }) => {
|
||||
|
||||
await fakeDelay(2000)
|
||||
await handleAdd?.(
|
||||
inputNameRef.current.value,
|
||||
inputValueRef.current.value
|
||||
inputValueRef.current.value,
|
||||
inputNameRef.current.value
|
||||
)
|
||||
|
||||
inputNameRef.current.disabled = false
|
||||
|
||||
@@ -34,27 +34,46 @@ import PropTypes from 'prop-types'
|
||||
import { List } from 'client/components/Tabs/Common'
|
||||
import { ACTIONS } from 'client/constants'
|
||||
|
||||
const AttributePanel = React.memo((
|
||||
{ title, attributes, handleEdit, handleDelete, handleAdd, actions }
|
||||
) => {
|
||||
// This attributes has special restrictions
|
||||
const SPECIAL_ATTRIBUTES = {
|
||||
VCENTER_CCR_REF: { edit: false, delete: false },
|
||||
VCENTER_HOST: { edit: false, delete: false },
|
||||
VCENTER_INSTANCE_ID: { edit: false, delete: false },
|
||||
VCENTER_PASSWORD: { edit: true, delete: false },
|
||||
VCENTER_USER: { edit: false, delete: false },
|
||||
VCENTER_VERSION: { edit: false, delete: false }
|
||||
}
|
||||
|
||||
const AttributePanel = React.memo(({
|
||||
title,
|
||||
attributes = {},
|
||||
handleEdit,
|
||||
handleDelete,
|
||||
handleAdd,
|
||||
actions
|
||||
}) => {
|
||||
const formatAttributes = Object.entries(attributes)
|
||||
.filter(([_, value]) => typeof value === 'string')
|
||||
.map(([name, value]) => ({
|
||||
name,
|
||||
value,
|
||||
canEdit: actions?.includes?.(ACTIONS.EDIT_ATTRIBUTE),
|
||||
canDelete: actions?.includes?.(ACTIONS.DELETE_ATTRIBUTE),
|
||||
canEdit:
|
||||
actions?.includes?.(ACTIONS.EDIT_ATTRIBUTE) &&
|
||||
SPECIAL_ATTRIBUTES[name]?.edit !== false,
|
||||
canDelete:
|
||||
actions?.includes?.(ACTIONS.DELETE_ATTRIBUTE) &&
|
||||
SPECIAL_ATTRIBUTES[name]?.delete !== false,
|
||||
handleEdit,
|
||||
handleDelete
|
||||
}))
|
||||
|
||||
return (
|
||||
<List
|
||||
containerProps={{ style: { gridColumn: '1 / -1' } }}
|
||||
subListProps={{ disablePadding: true }}
|
||||
itemProps={{ dense: true }}
|
||||
title={title}
|
||||
list={formatAttributes}
|
||||
{...(actions?.includes?.(ACTIONS.ADD_ATTRIBUTE) && {
|
||||
handleAdd
|
||||
})}
|
||||
handleAdd={actions?.includes?.(ACTIONS.ADD_ATTRIBUTE) && handleAdd}
|
||||
/>
|
||||
)
|
||||
})
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
import * as React from 'react'
|
||||
import PropTypes from 'prop-types'
|
||||
|
||||
import clsx from 'clsx'
|
||||
import { makeStyles, List as MList, ListItem, Typography, Paper } from '@material-ui/core'
|
||||
|
||||
import { Attribute, AttributePropTypes } from 'client/components/Tabs/Common/Attribute'
|
||||
@@ -32,18 +33,59 @@ const useStyles = makeStyles(theme => ({
|
||||
item: {
|
||||
gap: '1em',
|
||||
'& > *': {
|
||||
width: '50%'
|
||||
flex: '1 1 50%',
|
||||
overflow: 'hidden'
|
||||
}
|
||||
},
|
||||
typo: theme.typography.body2
|
||||
}))
|
||||
|
||||
const List = ({ title, list = [], handleAdd, containerProps }) => {
|
||||
const AttributeList = ({
|
||||
title,
|
||||
list = [],
|
||||
handleAdd,
|
||||
containerProps = {},
|
||||
itemProps = {},
|
||||
listProps = {},
|
||||
subListProps = {}
|
||||
}) => {
|
||||
const classes = useStyles()
|
||||
const { className: itemClassName, ...restOfItemProps } = itemProps
|
||||
|
||||
const renderList = (attribute, parentPath = false) => {
|
||||
const { name, value } = attribute
|
||||
const isParent = typeof value === 'object' && !React.isValidElement(value)
|
||||
|
||||
return (
|
||||
<>
|
||||
<ListItem
|
||||
key={`${title}.${parentPath || name}`}
|
||||
className={clsx(classes.item, itemClassName)}
|
||||
{...restOfItemProps}
|
||||
>
|
||||
<Attribute
|
||||
path={parentPath || name}
|
||||
{...attribute}
|
||||
{ ...(isParent && { canEdit: false, value: undefined })}
|
||||
/>
|
||||
</ListItem>
|
||||
{isParent && (
|
||||
<MList {...subListProps}>
|
||||
{Object.entries(value).map(([childName, childValue]) => {
|
||||
const subAttributeProps = { ...attribute, name: childName, value: childValue }
|
||||
const attributePath = `${parentPath || name}.${childName}`
|
||||
|
||||
return renderList(subAttributeProps, attributePath)
|
||||
})}
|
||||
</MList>
|
||||
)}
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<Paper variant='outlined' {...containerProps}>
|
||||
<MList className={classes.list}>
|
||||
<MList {...listProps}>
|
||||
{/* TITLE */}
|
||||
{title && (
|
||||
<ListItem className={classes.title}>
|
||||
@@ -53,14 +95,7 @@ const List = ({ title, list = [], handleAdd, containerProps }) => {
|
||||
</ListItem>
|
||||
)}
|
||||
{/* LIST */}
|
||||
{list.map((attribute, idx) => (
|
||||
<ListItem
|
||||
key={`${attribute.name}-${idx}`}
|
||||
className={classes.item}
|
||||
>
|
||||
<Attribute {...attribute}/>
|
||||
</ListItem>
|
||||
))}
|
||||
{list.map(attr => renderList(attr))}
|
||||
{/* ADD ACTION */}
|
||||
{handleAdd && (
|
||||
<ListItem className={classes.item}>
|
||||
@@ -72,15 +107,21 @@ const List = ({ title, list = [], handleAdd, containerProps }) => {
|
||||
)
|
||||
}
|
||||
|
||||
List.propTypes = {
|
||||
AttributeList.propTypes = {
|
||||
containerProps: PropTypes.object,
|
||||
handleAdd: PropTypes.func,
|
||||
handleAdd: PropTypes.oneOfType([
|
||||
PropTypes.func,
|
||||
PropTypes.bool
|
||||
]),
|
||||
itemProps: PropTypes.object,
|
||||
title: PropTypes.any,
|
||||
list: PropTypes.arrayOf(
|
||||
PropTypes.shape(AttributePropTypes)
|
||||
)
|
||||
),
|
||||
listProps: PropTypes.object,
|
||||
subListProps: PropTypes.object
|
||||
}
|
||||
|
||||
List.displayName = 'List'
|
||||
AttributeList.displayName = 'AttributeList'
|
||||
|
||||
export default List
|
||||
export default AttributeList
|
||||
@@ -32,7 +32,7 @@ const VmConfigurationTab = data => {
|
||||
</AccordionSummary>
|
||||
<AccordionDetails>
|
||||
<pre>
|
||||
<code>
|
||||
<code style={{ whiteSpace: 'break-spaces' }}>
|
||||
{JSON.stringify(USER_TEMPLATE, null, 2)}
|
||||
</code>
|
||||
</pre>
|
||||
|
||||
@@ -26,10 +26,12 @@ import { Tr } from 'client/components/HOC'
|
||||
import { T } from 'client/constants'
|
||||
import * as VirtualMachine from 'client/models/VirtualMachine'
|
||||
import * as Helper from 'client/models/Helper'
|
||||
import { cloneObject, set } from 'client/utils'
|
||||
|
||||
const LXC_ATTRIBUTES_REG = /^LXC_/
|
||||
const VCENTER_ATTRIBUTES_REG = /^VCENTER_/
|
||||
const HIDDEN_ATTRIBUTES_REG = /^(CPU|MEMORY|NETTX|NETRX|STATE|DISK_SIZE|SNAPSHOT_SIZE)$/
|
||||
const HIDDEN_ATTRIBUTES_REG = /^(USER_INPUTS|BACKUP|HOT_RESIZE)$|SCHED_|ERROR/
|
||||
const HIDDEN_MONITORING_REG = /^(CPU|MEMORY|NETTX|NETRX|STATE|DISK_SIZE|SNAPSHOT_SIZE)$/
|
||||
|
||||
const VmInfoTab = ({ tabProps = {} }) => {
|
||||
const {
|
||||
@@ -61,12 +63,15 @@ const VmInfoTab = ({ tabProps = {} }) => {
|
||||
String(response) === String(ID) && await handleRefetch?.()
|
||||
}
|
||||
|
||||
const handleCreateAttribute = async (newName, newValue) => {
|
||||
const newAttribute = `${String(newName).toUpperCase()} = "${newValue}"`
|
||||
const handleAttributeInXml = async (newValue, path) => {
|
||||
const newTemplate = cloneObject({ USER_TEMPLATE })
|
||||
|
||||
// 1: Merge the new attribute to existing user template
|
||||
const response = await updateUserTemplate(ID, newAttribute, 1)
|
||||
console.log({ response })
|
||||
set(newTemplate, `USER_TEMPLATE.${path}`, newValue)
|
||||
|
||||
const xml = Helper.jsonToXml(newTemplate)
|
||||
|
||||
// 0: Replace the whole user template
|
||||
const response = await updateUserTemplate(ID, xml, 0)
|
||||
|
||||
String(response) === String(ID) && await handleRefetch?.()
|
||||
}
|
||||
@@ -74,7 +79,11 @@ const VmInfoTab = ({ tabProps = {} }) => {
|
||||
const hypervisor = VirtualMachine.getHypervisor(vm)
|
||||
const getActions = actions => Helper.getActionsAvailable(actions, hypervisor)
|
||||
|
||||
const filteredAttributes = Helper.filterAttributes(USER_TEMPLATE, {
|
||||
const {
|
||||
attributes,
|
||||
lxc: lxcAttributes,
|
||||
vcenter: vcenterAttributes
|
||||
} = Helper.filterAttributes(USER_TEMPLATE, {
|
||||
extra: {
|
||||
vcenter: VCENTER_ATTRIBUTES_REG,
|
||||
lxc: LXC_ATTRIBUTES_REG
|
||||
@@ -82,12 +91,14 @@ const VmInfoTab = ({ tabProps = {} }) => {
|
||||
hidden: HIDDEN_ATTRIBUTES_REG
|
||||
})
|
||||
|
||||
const { vcenter: vcenterAttributes, lxc: lxcAttributes, attributes } = filteredAttributes
|
||||
const {
|
||||
attributes: monitoringAttributes
|
||||
} = Helper.filterAttributes(MONITORING, { hidden: HIDDEN_MONITORING_REG })
|
||||
|
||||
const ATTRIBUTE_FUNCTION = {
|
||||
handleAdd: handleCreateAttribute,
|
||||
handleEdit: console.log,
|
||||
handleDelete: console.log
|
||||
handleAdd: handleAttributeInXml,
|
||||
handleEdit: handleAttributeInXml,
|
||||
handleDelete: path => handleAttributeInXml(undefined, path)
|
||||
}
|
||||
|
||||
return (
|
||||
@@ -132,30 +143,31 @@ const VmInfoTab = ({ tabProps = {} }) => {
|
||||
{attributesPanel?.enabled && attributes && (
|
||||
<AttributePanel
|
||||
{...ATTRIBUTE_FUNCTION}
|
||||
attributes={attributes}
|
||||
actions={getActions(attributesPanel?.actions)}
|
||||
title={Tr(T.Attributes)}
|
||||
attributes={attributes} />
|
||||
/>
|
||||
)}
|
||||
{vcenterPanel?.enabled && vcenterAttributes && (
|
||||
<AttributePanel
|
||||
{...ATTRIBUTE_FUNCTION}
|
||||
actions={getActions(vcenterPanel?.actions)}
|
||||
attributes={vcenterAttributes}
|
||||
title={`vCenter ${Tr(T.Information)}`}
|
||||
attributes={vcenterAttributes} />
|
||||
/>
|
||||
)}
|
||||
{lxcPanel?.enabled && lxcAttributes && (
|
||||
<AttributePanel
|
||||
{...ATTRIBUTE_FUNCTION}
|
||||
actions={getActions(lxcPanel?.actions)}
|
||||
attributes={lxcAttributes}
|
||||
title={`LXC ${Tr(T.Information)}`}
|
||||
attributes={lxcAttributes} />
|
||||
/>
|
||||
)}
|
||||
{monitoringPanel?.enabled && (
|
||||
{monitoringPanel?.enabled && monitoringAttributes && (
|
||||
<AttributePanel
|
||||
{...ATTRIBUTE_FUNCTION}
|
||||
actions={getActions(monitoringPanel?.actions)}
|
||||
attributes={monitoringAttributes}
|
||||
title={Tr(T.Monitoring)}
|
||||
attributes={MONITORING}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
/* ------------------------------------------------------------------------- *
|
||||
* 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. *
|
||||
* ------------------------------------------------------------------------- */
|
||||
/* eslint-disable jsdoc/require-jsdoc */
|
||||
import * as React from 'react'
|
||||
import PropTypes from 'prop-types'
|
||||
|
||||
import { Folder, User } from 'iconoir-react'
|
||||
import { Typography, Paper } from '@material-ui/core'
|
||||
|
||||
import { rowStyles } from 'client/components/Tables/styles'
|
||||
import * as VirtualMachine from 'client/models/VirtualMachine'
|
||||
import * as Helper from 'client/models/Helper'
|
||||
|
||||
const HistoryItem = ({ history }) => {
|
||||
const classes = rowStyles()
|
||||
|
||||
const {
|
||||
SEQ,
|
||||
UID,
|
||||
GID,
|
||||
REQUEST_ID,
|
||||
HOSTNAME,
|
||||
HID,
|
||||
DS_ID,
|
||||
ACTION,
|
||||
STIME,
|
||||
ETIME,
|
||||
PSTIME,
|
||||
PETIME
|
||||
} = history
|
||||
|
||||
const now = Math.round(Date.now() / 1000)
|
||||
|
||||
const startTime = Helper.timeFromMilliseconds(+STIME)
|
||||
|
||||
const monitorEndTime = +ETIME === 0 ? now : +ETIME
|
||||
const monitorDiffTime = Helper.timeDiff(+STIME, monitorEndTime)
|
||||
|
||||
const prologEndTime = +PSTIME === 0 ? 0 : +PETIME === 0 ? now : +PETIME
|
||||
const prologDiffTime = Helper.timeDiff(+PSTIME, prologEndTime)
|
||||
|
||||
const ownerInfo = `${UID} | ${GID} | ${REQUEST_ID}`
|
||||
|
||||
const action = VirtualMachine.getHistoryAction(+ACTION)
|
||||
|
||||
return (
|
||||
<Paper variant='outlined' className={classes.root}>
|
||||
<div className={classes.main}>
|
||||
<div className={classes.title}>
|
||||
<Typography component='span'>
|
||||
{`#${SEQ} | #${HID} ${HOSTNAME} | Action: ${action}`}
|
||||
</Typography>
|
||||
</div>
|
||||
<div className={classes.caption}>
|
||||
<span title={`Datastore ID: ${DS_ID}`}>
|
||||
<Folder size={16} />
|
||||
<span>{` ${DS_ID}`}</span>
|
||||
</span>
|
||||
{+UID !== -1 && (
|
||||
<span title={`Owner | Group | Request ID: ${ownerInfo}`}>
|
||||
<User size={16} />
|
||||
<span>{` ${ownerInfo}`}</span>
|
||||
</span>
|
||||
)}
|
||||
<span title={`Time when the state changed: ${startTime.toFormat('ff')}`}>
|
||||
{`| start ${startTime.toRelative()}`}
|
||||
</span>
|
||||
<span title={'Total time in this state'}>
|
||||
{`| total ${monitorDiffTime}`}
|
||||
</span>
|
||||
<span title={'Prolog time for this state'}>
|
||||
{`| prolog ${prologDiffTime}`}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
</Paper>
|
||||
)
|
||||
}
|
||||
|
||||
HistoryItem.propTypes = {
|
||||
history: PropTypes.object.isRequired,
|
||||
actions: PropTypes.arrayOf(PropTypes.string)
|
||||
}
|
||||
|
||||
HistoryItem.displayName = 'HistoryItem'
|
||||
|
||||
export default HistoryItem
|
||||
+16
-8
@@ -15,15 +15,23 @@
|
||||
* ------------------------------------------------------------------------- */
|
||||
/* eslint-disable jsdoc/require-jsdoc */
|
||||
import * as React from 'react'
|
||||
import PropTypes from 'prop-types'
|
||||
|
||||
const VmPlacementTab = data => {
|
||||
return (
|
||||
<div>
|
||||
<p>WIP - PLACEMENT</p>
|
||||
</div>
|
||||
)
|
||||
import HistoryItem from 'client/components/Tabs/Vm/Placement/Item'
|
||||
|
||||
const HistoryList = ({ records, actions }) => (
|
||||
<div style={{ display: 'grid', gap: '1em', paddingBlock: '0.8em' }}>
|
||||
{records.map((history, idx) => (
|
||||
<HistoryItem key={idx} history={history} actions={actions} />
|
||||
))}
|
||||
</div>
|
||||
)
|
||||
|
||||
HistoryList.propTypes = {
|
||||
records: PropTypes.array,
|
||||
actions: PropTypes.arrayOf(PropTypes.string)
|
||||
}
|
||||
|
||||
VmPlacementTab.displayName = 'VmPlacementTab'
|
||||
HistoryList.displayName = 'HistoryList'
|
||||
|
||||
export default VmPlacementTab
|
||||
export default HistoryList
|
||||
@@ -0,0 +1,46 @@
|
||||
/* ------------------------------------------------------------------------- *
|
||||
* 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. *
|
||||
* ------------------------------------------------------------------------- */
|
||||
/* eslint-disable jsdoc/require-jsdoc */
|
||||
import * as React from 'react'
|
||||
import PropTypes from 'prop-types'
|
||||
|
||||
import { TabContext } from 'client/components/Tabs/TabProvider'
|
||||
import HistoryList from 'client/components/Tabs/Vm/Placement/List'
|
||||
|
||||
import * as VirtualMachine from 'client/models/VirtualMachine'
|
||||
import * as Helper from 'client/models/Helper'
|
||||
|
||||
const VmPlacementTab = ({ tabProps = {} }) => {
|
||||
const { data: vm } = React.useContext(TabContext)
|
||||
const { actions = [] } = tabProps
|
||||
|
||||
const records = VirtualMachine.getHistoryRecords(vm)
|
||||
|
||||
const hypervisor = VirtualMachine.getHypervisor(vm)
|
||||
const actionsAvailable = Helper.getActionsAvailable(actions, hypervisor)
|
||||
|
||||
return (
|
||||
<HistoryList actions={actionsAvailable} records={records} />
|
||||
)
|
||||
}
|
||||
|
||||
VmPlacementTab.propTypes = {
|
||||
tabProps: PropTypes.object
|
||||
}
|
||||
|
||||
VmPlacementTab.displayName = 'VmPlacementTab'
|
||||
|
||||
export default VmPlacementTab
|
||||
@@ -0,0 +1,81 @@
|
||||
/* ------------------------------------------------------------------------- *
|
||||
* 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. *
|
||||
* ------------------------------------------------------------------------- */
|
||||
/* eslint-disable jsdoc/require-jsdoc */
|
||||
import * as React from 'react'
|
||||
import PropTypes from 'prop-types'
|
||||
|
||||
import { Trash, UndoAction } from 'iconoir-react'
|
||||
import { Typography, Paper } from '@material-ui/core'
|
||||
|
||||
// import { useVmApi } from 'client/features/One'
|
||||
import { Action } from 'client/components/Cards/SelectCard'
|
||||
import { rowStyles } from 'client/components/Tables/styles'
|
||||
|
||||
import * as Helper from 'client/models/Helper'
|
||||
import { VM_ACTIONS } from 'client/constants'
|
||||
|
||||
const SnapshotItem = ({ snapshot, actions = [] }) => {
|
||||
const classes = rowStyles()
|
||||
|
||||
const { SNAPSHOT_ID, NAME, TIME } = snapshot
|
||||
|
||||
const time = Helper.timeFromMilliseconds(+TIME)
|
||||
const timeAgo = `created ${time.toRelative()}`
|
||||
|
||||
return (
|
||||
<Paper variant='outlined' className={classes.root}>
|
||||
<div className={classes.main}>
|
||||
<div className={classes.title}>
|
||||
<Typography component='span'>
|
||||
{NAME}
|
||||
</Typography>
|
||||
</div>
|
||||
<div className={classes.caption}>
|
||||
<span title={time.toFormat('ff')}>
|
||||
{`#${SNAPSHOT_ID} ${timeAgo}`}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
{!!actions.length && (
|
||||
<div className={classes.actions}>
|
||||
{actions?.includes?.(VM_ACTIONS.SNAPSHOT_REVERT) && (
|
||||
<Action
|
||||
cy={`${VM_ACTIONS.SNAPSHOT_REVERT}-${SNAPSHOT_ID}`}
|
||||
icon={<UndoAction size={18} />}
|
||||
handleClick={() => undefined}
|
||||
/>
|
||||
)}
|
||||
{actions?.includes?.(VM_ACTIONS.SNAPSHOT_DELETE) && (
|
||||
<Action
|
||||
cy={`${VM_ACTIONS.SNAPSHOT_DELETE}-${SNAPSHOT_ID}`}
|
||||
icon={<Trash size={18} />}
|
||||
handleClick={() => undefined}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</Paper>
|
||||
)
|
||||
}
|
||||
|
||||
SnapshotItem.propTypes = {
|
||||
snapshot: PropTypes.object.isRequired,
|
||||
actions: PropTypes.arrayOf(PropTypes.string)
|
||||
}
|
||||
|
||||
SnapshotItem.displayName = 'SnapshotItem'
|
||||
|
||||
export default SnapshotItem
|
||||
+16
-8
@@ -15,15 +15,23 @@
|
||||
* ------------------------------------------------------------------------- */
|
||||
/* eslint-disable jsdoc/require-jsdoc */
|
||||
import * as React from 'react'
|
||||
import PropTypes from 'prop-types'
|
||||
|
||||
const VmSnapshotTab = data => {
|
||||
return (
|
||||
<div>
|
||||
<p>WIP - SNAPSHOT</p>
|
||||
</div>
|
||||
)
|
||||
import SnapshotItem from 'client/components/Tabs/Vm/Snapshot/Item'
|
||||
|
||||
const SnapshotList = ({ snapshots, actions }) => (
|
||||
<div style={{ display: 'grid', gap: '1em', paddingBlock: '0.8em' }}>
|
||||
{snapshots.map((snapshot, idx) => (
|
||||
<SnapshotItem key={idx} snapshot={snapshot} actions={actions} />
|
||||
))}
|
||||
</div>
|
||||
)
|
||||
|
||||
SnapshotList.propTypes = {
|
||||
snapshots: PropTypes.array,
|
||||
actions: PropTypes.arrayOf(PropTypes.string)
|
||||
}
|
||||
|
||||
VmSnapshotTab.displayName = 'VmSnapshotTab'
|
||||
SnapshotList.displayName = 'SnapshotList'
|
||||
|
||||
export default VmSnapshotTab
|
||||
export default SnapshotList
|
||||
@@ -0,0 +1,76 @@
|
||||
/* ------------------------------------------------------------------------- *
|
||||
* 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. *
|
||||
* ------------------------------------------------------------------------- */
|
||||
/* eslint-disable jsdoc/require-jsdoc */
|
||||
import * as React from 'react'
|
||||
import PropTypes from 'prop-types'
|
||||
import { Button } from '@material-ui/core'
|
||||
|
||||
import { useDialog } from 'client/hooks'
|
||||
import { TabContext } from 'client/components/Tabs/TabProvider'
|
||||
import { DialogConfirmation } from 'client/components/Dialogs'
|
||||
import SnapshotList from 'client/components/Tabs/Vm/Snapshot/List'
|
||||
import { Tr } from 'client/components/HOC'
|
||||
|
||||
import * as VirtualMachine from 'client/models/VirtualMachine'
|
||||
import * as Helper from 'client/models/Helper'
|
||||
import { T, VM_ACTIONS } from 'client/constants'
|
||||
|
||||
const VmSnapshotTab = ({ tabProps = {} }) => {
|
||||
const { display, show, hide } = useDialog()
|
||||
const { data: vm } = React.useContext(TabContext)
|
||||
const { actions = [] } = tabProps
|
||||
|
||||
const snapshots = VirtualMachine.getSnapshotList(vm)
|
||||
|
||||
const hypervisor = VirtualMachine.getHypervisor(vm)
|
||||
const actionsAvailable = Helper.getActionsAvailable(actions, hypervisor)
|
||||
|
||||
return (
|
||||
<>
|
||||
{actionsAvailable?.includes?.(VM_ACTIONS.SNAPSHOT_CREATE) && (
|
||||
<Button
|
||||
data-cy='snapshot-create'
|
||||
size='small'
|
||||
color='secondary'
|
||||
onClick={show}
|
||||
variant='contained'
|
||||
>
|
||||
{Tr(T.TakeSnapshot)}
|
||||
</Button>
|
||||
)}
|
||||
|
||||
<SnapshotList actions={actionsAvailable} snapshots={snapshots} />
|
||||
|
||||
{display && (
|
||||
<DialogConfirmation
|
||||
title={T.TakeSnapshot}
|
||||
handleAccept={hide}
|
||||
handleCancel={hide}
|
||||
>
|
||||
<p>TODO: should define in view yaml ??</p>
|
||||
</DialogConfirmation>
|
||||
)}
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
VmSnapshotTab.propTypes = {
|
||||
tabProps: PropTypes.object
|
||||
}
|
||||
|
||||
VmSnapshotTab.displayName = 'VmSnapshotTab'
|
||||
|
||||
export default VmSnapshotTab
|
||||
@@ -49,6 +49,7 @@ module.exports = {
|
||||
AttachDisk: 'Attach disk',
|
||||
AttachNic: 'Attach nic',
|
||||
Detach: 'Detach',
|
||||
TakeSnapshot: 'Take snapshot',
|
||||
|
||||
/* questions */
|
||||
DoYouWantProceed: 'Do you want proceed?',
|
||||
|
||||
@@ -501,6 +501,113 @@ export const VM_ACTIONS = {
|
||||
UPDATE_CONF: 'update_configuration'
|
||||
}
|
||||
|
||||
/**
|
||||
* @enum {(
|
||||
* 'none' |
|
||||
* 'migrate' |
|
||||
* 'live-migrate' |
|
||||
* 'shutdown' |
|
||||
* 'shutdown-hard' |
|
||||
* 'undeploy' |
|
||||
* 'undeploy-hard' |
|
||||
* 'hold' |
|
||||
* 'release' |
|
||||
* 'stop' |
|
||||
* 'suspend' |
|
||||
* 'resume' |
|
||||
* 'boot' |
|
||||
* 'delete' |
|
||||
* 'delete-recreate' |
|
||||
* 'reboot' |
|
||||
* 'reboot-hard' |
|
||||
* 'resched' |
|
||||
* 'unresched' |
|
||||
* 'poweroff' |
|
||||
* 'poweroff-hard' |
|
||||
* 'disk-attach' |
|
||||
* 'disk-detach' |
|
||||
* 'nic-attach' |
|
||||
* 'nic-detach' |
|
||||
* 'disk-snapshot-create' |
|
||||
* 'disk-snapshot-delete' |
|
||||
* 'terminate' |
|
||||
* 'terminate-hard' |
|
||||
* 'disk-resize' |
|
||||
* 'deploy' |
|
||||
* 'chown' |
|
||||
* 'chmod' |
|
||||
* 'updateconf' |
|
||||
* 'rename' |
|
||||
* 'resize' |
|
||||
* 'update' |
|
||||
* 'snapshot-resize' |
|
||||
* 'snapshot-delete' |
|
||||
* 'snapshot-revert' |
|
||||
* 'disk-saveas' |
|
||||
* 'disk-snapshot-revert' |
|
||||
* 'recover' |
|
||||
* 'retry' |
|
||||
* 'monitor' |
|
||||
* 'disk-snapshot-rename' |
|
||||
* 'alias-attach' |
|
||||
* 'alias-detach' |
|
||||
* 'poweroff-migrate' |
|
||||
* 'poweroff-hard-migrate'
|
||||
* )} History actions
|
||||
*/
|
||||
export const HISTORY_ACTIONS = [
|
||||
'none',
|
||||
'migrate',
|
||||
'live-migrate',
|
||||
'shutdown',
|
||||
'shutdown-hard',
|
||||
'undeploy',
|
||||
'undeploy-hard',
|
||||
'hold',
|
||||
'release',
|
||||
'stop',
|
||||
'suspend',
|
||||
'resume',
|
||||
'boot',
|
||||
'delete',
|
||||
'delete-recreate',
|
||||
'reboot',
|
||||
'reboot-hard',
|
||||
'resched',
|
||||
'unresched',
|
||||
'poweroff',
|
||||
'poweroff-hard',
|
||||
'disk-attach',
|
||||
'disk-detach',
|
||||
'nic-attach',
|
||||
'nic-detach',
|
||||
'disk-snapshot-create',
|
||||
'disk-snapshot-delete',
|
||||
'terminate',
|
||||
'terminate-hard',
|
||||
'disk-resize',
|
||||
'deploy',
|
||||
'chown',
|
||||
'chmod',
|
||||
'updateconf',
|
||||
'rename',
|
||||
'resize',
|
||||
'update',
|
||||
'snapshot-resize',
|
||||
'snapshot-delete',
|
||||
'snapshot-revert',
|
||||
'disk-saveas',
|
||||
'disk-snapshot-revert',
|
||||
'recover',
|
||||
'retry',
|
||||
'monitor',
|
||||
'disk-snapshot-rename',
|
||||
'alias-attach',
|
||||
'alias-detach',
|
||||
'poweroff-migrate',
|
||||
'poweroff-hard-migrate'
|
||||
]
|
||||
|
||||
/**
|
||||
* @enum {(
|
||||
* 'IP'|
|
||||
|
||||
@@ -15,6 +15,16 @@
|
||||
* ------------------------------------------------------------------------- */
|
||||
import { T } from 'client/constants'
|
||||
import { DateTime } from 'luxon'
|
||||
import { j2xParser as Parser } from 'fast-xml-parser'
|
||||
|
||||
/**
|
||||
* @param {object} json - JSON
|
||||
* @returns {string} Xml in string format
|
||||
*/
|
||||
export const jsonToXml = json => {
|
||||
const parser = new Parser()
|
||||
return parser.parse(json)
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the boolean value into a readable format.
|
||||
@@ -48,11 +58,39 @@ export const timeToString = time =>
|
||||
* Converts the given time into DateTime luxon type.
|
||||
*
|
||||
* @param {number|string} time - Time to convert
|
||||
* @returns {DateTime} - Datetime object.
|
||||
* @returns {DateTime} - DateTime object.
|
||||
*/
|
||||
export const timeFromMilliseconds = time =>
|
||||
DateTime.fromMillis(+time * 1000)
|
||||
|
||||
/**
|
||||
* Get the diff from two times and it converts them
|
||||
* into string with format: ``dd hh mm ss``.
|
||||
*
|
||||
* @param {number|string} start - Time to convert
|
||||
* @param {number|string} end - Time to convert
|
||||
* @returns {string} - Duration time with format.
|
||||
*/
|
||||
export const timeDiff = (start, end) => {
|
||||
const startTime = timeFromMilliseconds(start)
|
||||
const endTime = timeFromMilliseconds(end)
|
||||
|
||||
const diff = endTime.diff(startTime, ['days', 'hours', 'minutes', 'seconds'])
|
||||
|
||||
const { days, hours, minutes, seconds } = diff.toObject()
|
||||
|
||||
let total = ''
|
||||
days > 0 && (total = total.concat(`${days}d `))
|
||||
hours > 0 && (total = total.concat(`${hours}h`))
|
||||
minutes > 0 && (total = total.concat(`${minutes}m`))
|
||||
|
||||
if (seconds > 0 || !total.length) {
|
||||
total = total.concat(`${seconds}s`)
|
||||
}
|
||||
|
||||
return total
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the lock level to its string value.
|
||||
*
|
||||
|
||||
@@ -13,9 +13,23 @@
|
||||
* See the License for the specific language governing permissions and *
|
||||
* limitations under the License. *
|
||||
* ------------------------------------------------------------------------- */
|
||||
import { STATES, VM_STATES, VM_LCM_STATES, NIC_ALIAS_IP_ATTRS, StateInfo } from 'client/constants'
|
||||
import { getSecurityGroupsFromResource, prettySecurityGroup } from 'client/models/SecurityGroup'
|
||||
|
||||
import {
|
||||
STATES,
|
||||
VM_STATES,
|
||||
VM_LCM_STATES,
|
||||
NIC_ALIAS_IP_ATTRS,
|
||||
HISTORY_ACTIONS,
|
||||
StateInfo
|
||||
} from 'client/constants'
|
||||
|
||||
/**
|
||||
* @param {string|number} action - Action code
|
||||
* @returns {HISTORY_ACTIONS} History action name
|
||||
*/
|
||||
export const getHistoryAction = action => HISTORY_ACTIONS[+action]
|
||||
|
||||
/**
|
||||
* This function removes, from the given list,
|
||||
* the Virtual machines in state DONE.
|
||||
@@ -26,14 +40,21 @@ import { getSecurityGroupsFromResource, prettySecurityGroup } from 'client/model
|
||||
export const filterDoneVms = (vms = []) =>
|
||||
vms.filter(({ STATE }) => VM_STATES[STATE]?.name !== STATES.DONE)
|
||||
|
||||
/**
|
||||
* @param {object} vm - Virtual machine
|
||||
* @returns {object} History records from resource
|
||||
*/
|
||||
export const getHistoryRecords = vm =>
|
||||
[vm?.HISTORY_RECORDS?.HISTORY ?? {}].flat()
|
||||
|
||||
/**
|
||||
* @param {object} vm - Virtual machine
|
||||
* @returns {object} Last history record from resource
|
||||
*/
|
||||
export const getLastHistory = vm => {
|
||||
const history = vm?.HISTORY_RECORDS?.HISTORY ?? {}
|
||||
const records = getHistoryRecords(vm)
|
||||
|
||||
return Array.isArray(history) ? history[history.length - 1] : history
|
||||
return records[records.length - 1]
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -166,3 +187,13 @@ export const splitNicAlias = vm =>
|
||||
|
||||
return result
|
||||
}, { nics: [], alias: [] })
|
||||
|
||||
/**
|
||||
* @param {object} vm - Virtual machine
|
||||
* @returns {Array} List of snapshots from resource
|
||||
*/
|
||||
export const getSnapshotList = vm => {
|
||||
const { TEMPLATE = {} } = vm ?? {}
|
||||
|
||||
return [TEMPLATE.SNAPSHOT].flat()
|
||||
}
|
||||
Reference in new issue
Block a user