From 4b8938203574ab984e54befdb91dc1ef629d1a91 Mon Sep 17 00:00:00 2001 From: Sergio Betanzos Date: Mon, 27 Sep 2021 18:46:19 +0200 Subject: [PATCH] F #5552: Add cleanup option to delete provision form (#1485) --- .../client/components/Cards/ProvisionCard.js | 16 ++++-- .../components/Forms/ButtonToTriggerForm.js | 2 +- .../CreateForm/Steps/Provider/index.js | 8 +-- .../Forms/Provision/DeleteForm/index.js | 21 ++++++++ .../Forms/Provision/DeleteForm/schema.js | 35 +++++++++++++ .../components/Forms/Provision/index.js | 4 +- .../src/client/containers/Provisions/index.js | 50 +++++++++++-------- .../client/features/One/provision/hooks.js | 8 +-- .../client/features/One/provision/services.js | 8 ++- .../api/provision/provision-functions.js | 3 +- .../server/routes/api/provision/provision.js | 3 +- 11 files changed, 118 insertions(+), 40 deletions(-) create mode 100644 src/fireedge/src/client/components/Forms/Provision/DeleteForm/index.js create mode 100644 src/fireedge/src/client/components/Forms/Provision/DeleteForm/schema.js diff --git a/src/fireedge/src/client/components/Cards/ProvisionCard.js b/src/fireedge/src/client/components/Cards/ProvisionCard.js index d5fa89e818..fe79b12ad9 100644 --- a/src/fireedge/src/client/components/Cards/ProvisionCard.js +++ b/src/fireedge/src/client/components/Cards/ProvisionCard.js @@ -18,6 +18,7 @@ import PropTypes from 'prop-types' import { Db as ProviderIcon, Cloud as ProvisionIcon } from 'iconoir-react' +import ButtonToTriggerForm from 'client/components/Forms/ButtonToTriggerForm' import SelectCard, { Action } from 'client/components/Cards/SelectCard' import { StatusBadge } from 'client/components/Status' import Image from 'client/components/Image' @@ -31,7 +32,7 @@ import { } from 'client/constants' const ProvisionCard = memo( - ({ value, image: propImage, isSelected, handleClick, isProvider, actions }) => { + ({ value, image: propImage, isSelected, handleClick, isProvider, actions, deleteAction }) => { const { ID, NAME, TEMPLATE: { BODY = {} } } = value const IMAGES_URL = isProvider ? PROVIDER_IMAGES_URL : PROVISION_IMAGES_URL @@ -48,8 +49,15 @@ const ProvisionCard = memo( return ( - + action={(actions?.length > 0 || deleteAction) && ( + <> + {actions?.map(action => + + )} + {deleteAction && ( + + )} + )} dataCy={isProvider ? 'provider' : 'provision'} handleClick={handleClick} @@ -89,6 +97,7 @@ ProvisionCard.propTypes = { handleClick: PropTypes.func, isProvider: PropTypes.bool, image: PropTypes.string, + deleteAction: PropTypes.func, actions: PropTypes.arrayOf( PropTypes.shape({ handleClick: PropTypes.func.isRequired, @@ -104,6 +113,7 @@ ProvisionCard.defaultProps = { isProvider: false, isSelected: undefined, image: undefined, + deleteAction: undefined, value: {} } diff --git a/src/fireedge/src/client/components/Forms/ButtonToTriggerForm.js b/src/fireedge/src/client/components/Forms/ButtonToTriggerForm.js index 4f5f993304..1936f04191 100644 --- a/src/fireedge/src/client/components/Forms/ButtonToTriggerForm.js +++ b/src/fireedge/src/client/components/Forms/ButtonToTriggerForm.js @@ -30,8 +30,8 @@ import { NavArrowDown } from 'iconoir-react' import { useDialog } from 'client/hooks' import { DialogConfirmation, DialogForm, DialogPropTypes } from 'client/components/Dialogs' -import { SubmitButton, SubmitButtonPropTypes } from 'client/components/FormControl' import FormWithSchema from 'client/components/Forms/FormWithSchema' +import SubmitButton, { SubmitButtonPropTypes } from 'client/components/FormControl/SubmitButton' import FormStepper from 'client/components/FormStepper' import { Translate } from 'client/components/HOC' diff --git a/src/fireedge/src/client/components/Forms/Provision/CreateForm/Steps/Provider/index.js b/src/fireedge/src/client/components/Forms/Provision/CreateForm/Steps/Provider/index.js index 4dda06f941..b6a1238db9 100644 --- a/src/fireedge/src/client/components/Forms/Provision/CreateForm/Steps/Provider/index.js +++ b/src/fireedge/src/client/components/Forms/Provision/CreateForm/Steps/Provider/index.js @@ -57,18 +57,14 @@ const Provider = () => ({ const { handleSelect, - handleUnselect + handleClear } = useListForm({ key: STEP_ID, setList: setFormData }) const handleClick = (provider, isSelected) => { - const { ID } = provider - // reset inputs when selected provider changes setFormData(prev => ({ ...prev, [INPUTS_ID]: undefined })) - isSelected - ? handleUnselect(ID, item => item.ID !== ID) - : handleSelect(provider) + isSelected ? handleClear() : handleSelect(provider) } return ( diff --git a/src/fireedge/src/client/components/Forms/Provision/DeleteForm/index.js b/src/fireedge/src/client/components/Forms/Provision/DeleteForm/index.js new file mode 100644 index 0000000000..49e3dd9d44 --- /dev/null +++ b/src/fireedge/src/client/components/Forms/Provision/DeleteForm/index.js @@ -0,0 +1,21 @@ +/* ------------------------------------------------------------------------- * + * 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 { createForm } from 'client/utils' +import { SCHEMA, FIELDS } from 'client/components/Forms/Provision/DeleteForm/schema' + +const DeleteForm = createForm(SCHEMA, FIELDS) + +export default DeleteForm diff --git a/src/fireedge/src/client/components/Forms/Provision/DeleteForm/schema.js b/src/fireedge/src/client/components/Forms/Provision/DeleteForm/schema.js new file mode 100644 index 0000000000..a2dee63748 --- /dev/null +++ b/src/fireedge/src/client/components/Forms/Provision/DeleteForm/schema.js @@ -0,0 +1,35 @@ +/* ------------------------------------------------------------------------- * + * 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 yup from 'yup' +import { INPUT_TYPES } from 'client/constants' +import { getValidationFromFields } from 'client/utils' + +const CLEANUP = { + name: 'cleanup', + label: 'Cleanup', + type: INPUT_TYPES.SWITCH, + tooltip: ` + Force to terminate VMs running on provisioned Hosts + and delete all images in the datastores.`, + validation: yup.boolean().notRequired().default(() => false) +} + +export const FIELDS = [ + CLEANUP +] + +export const SCHEMA = yup.object(getValidationFromFields(FIELDS)) diff --git a/src/fireedge/src/client/components/Forms/Provision/index.js b/src/fireedge/src/client/components/Forms/Provision/index.js index 4af9c90bbc..e5b37374b5 100644 --- a/src/fireedge/src/client/components/Forms/Provision/index.js +++ b/src/fireedge/src/client/components/Forms/Provision/index.js @@ -14,7 +14,9 @@ * limitations under the License. * * ------------------------------------------------------------------------- */ import CreateForm from 'client/components/Forms/Provision/CreateForm' +import DeleteForm from 'client/components/Forms/Provision/DeleteForm' export { - CreateForm + CreateForm, + DeleteForm } diff --git a/src/fireedge/src/client/containers/Provisions/index.js b/src/fireedge/src/client/containers/Provisions/index.js index 8bb9c7f4a2..d072ca7458 100644 --- a/src/fireedge/src/client/containers/Provisions/index.js +++ b/src/fireedge/src/client/containers/Provisions/index.js @@ -25,9 +25,11 @@ import { useFetch, useSearch } from 'client/hooks' import { useProvision, useProvisionApi } from 'client/features/One' import { useGeneralApi } from 'client/features/General' +import { DeleteForm } from 'client/components/Forms/Provision' import { ListHeader, ListCards } from 'client/components/List' import AlertError from 'client/components/Alerts/Error' import { ProvisionCard } from 'client/components/Cards' +import { Translate } from 'client/components/HOC' import { DialogRequest } from 'client/components/Dialogs' import DialogInfo from 'client/containers/Provisions/DialogInfo' @@ -98,28 +100,34 @@ function Provisions () { .then(() => fetchRequest(undefined, { reload: true })), icon: , cy: 'provision-configure' - }, - { - handleClick: () => setShowDialog({ - id: ID, - content: props => createElement(DialogInfo, { - ...props, - disableAllActions: true, - displayName: 'DialogDeleteProvision' - }), - title: `DELETE - #${ID} ${NAME}`, - handleAccept: () => { - handleCloseDialog() - - return deleteProvision(ID) - .then(() => enqueueInfo(`Deleting provision - ID: ${ID}`)) - .then(() => fetchRequest(undefined, { reload: true })) - } - }), - icon: , - cy: 'provision-delete' } - ] + ], + deleteAction: { + buttonProps: { + 'data-cy': 'provision-delete', + icon: + }, + options: [{ + dialogProps: { + title: ( + + ) + }, + form: DeleteForm, + onSubmit: async formData => { + try { + await deleteProvision(ID, formData) + enqueueInfo(`Deleting provision - ID: ${ID}`) + } finally { + handleCloseDialog() + fetchRequest(undefined, { reload: true }) + } + } + }] + } })} /> )} diff --git a/src/fireedge/src/client/features/One/provision/hooks.js b/src/fireedge/src/client/features/One/provision/hooks.js index 6afbc3cb63..cec06843e8 100644 --- a/src/fireedge/src/client/features/One/provision/hooks.js +++ b/src/fireedge/src/client/features/One/provision/hooks.js @@ -19,14 +19,14 @@ import { useDispatch, useSelector } from 'react-redux' import { unwrapResult } from '@reduxjs/toolkit' import * as actions from 'client/features/One/provision/actions' -import { RESOURCES } from 'client/features/One/slice' +import { name, RESOURCES } from 'client/features/One/slice' export const useProvisionTemplate = () => ( - useSelector(state => state.one[RESOURCES.document.defaults]) + useSelector(state => state[name]?.[RESOURCES.document.defaults]) ) export const useProvision = () => ( - useSelector(state => state.one[RESOURCES.document[103]]) + useSelector(state => state[name]?.[RESOURCES.document[103]]) ) export const useProvisionApi = () => { @@ -45,7 +45,7 @@ export const useProvisionApi = () => { getProvisions: () => dispatch(actions.getProvisions()), createProvision: data => unwrapDispatch(actions.createProvision({ data })), configureProvision: id => unwrapDispatch(actions.configureProvision({ id })), - deleteProvision: id => unwrapDispatch(actions.deleteProvision({ id })), + deleteProvision: (id, data) => unwrapDispatch(actions.deleteProvision({ id, ...data })), getProvisionLog: id => unwrapDispatch(actions.getProvisionLog({ id })), deleteDatastore: id => unwrapDispatch(actions.deleteDatastore({ id })), diff --git a/src/fireedge/src/client/features/One/provision/services.js b/src/fireedge/src/client/features/One/provision/services.js index 5ebea4ff3d..224cb54cf2 100644 --- a/src/fireedge/src/client/features/One/provision/services.js +++ b/src/fireedge/src/client/features/One/provision/services.js @@ -138,13 +138,17 @@ export const provisionService = ({ * * @param {object} params - Request parameters * @param {object} params.id - Provider id + * @param {object} params.cleanup + * - If `true`, force to terminate VMs running + * on provisioned Hosts and delete all images in the datastores * @returns {object} Object of document deleted * @throws Fails when response isn't code 200 */ - deleteProvision: async ({ id }) => { + deleteProvision: async ({ id, ...data }) => { const res = await RestClient.request({ method: DELETE, - url: `/api/${PROVISION}/delete/${id}` + url: `/api/${PROVISION}/delete/${id}`, + data }) if (!res?.id || res?.id !== httpCodes.ok.id) { diff --git a/src/fireedge/src/server/routes/api/provision/provision-functions.js b/src/fireedge/src/server/routes/api/provision/provision-functions.js index 242e738f8b..68d7605096 100644 --- a/src/fireedge/src/server/routes/api/provision/provision-functions.js +++ b/src/fireedge/src/server/routes/api/provision/provision-functions.js @@ -498,7 +498,8 @@ const deleteProvision = (res = {}, next = defaultEmptyFunction, params = {}, use const command = 'delete' const endpoint = getEndpoint() const authCommand = ['--user', user, '--password', password] - const paramsCommand = [command, params.id, '--batch', '--debug', '--json', ...authCommand, ...endpoint] + const cleanup = params.cleanup ? ['--cleanup'] : [] + const paramsCommand = [command, params.id, '--batch', '--debug', '--json', ...cleanup, ...authCommand, ...endpoint] // get Log file const dataLog = logData(params.id, true) diff --git a/src/fireedge/src/server/routes/api/provision/provision.js b/src/fireedge/src/server/routes/api/provision/provision.js index 44a6d53408..dad1c92266 100644 --- a/src/fireedge/src/server/routes/api/provision/provision.js +++ b/src/fireedge/src/server/routes/api/provision/provision.js @@ -142,7 +142,8 @@ const routes = { delete: { action: deleteProvision, params: { - id: { from: fromData.resource, name: 'id', front: true } + id: { from: fromData.resource, name: 'id', front: true }, + cleanup: { from: fromData.postBody, name: 'cleanup', front: true } }, websocket: true },