-
Notifications
You must be signed in to change notification settings - Fork 1k
Fixes #38456 - Hosts bulk action: Disassociate hosts #10560
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
199 changes: 199 additions & 0 deletions
199
...scripts/react_app/components/HostsIndex/BulkActions/disassociate/BulkDisassociateModal.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,199 @@ | ||
| import React from 'react'; | ||
| import PropTypes from 'prop-types'; | ||
| import { useDispatch } from 'react-redux'; | ||
| import { | ||
| Modal, | ||
| Alert, | ||
| Button, | ||
| TextContent, | ||
| Text, | ||
| TreeView, | ||
| } from '@patternfly/react-core'; | ||
| import { addToast } from '../../../ToastsList/slice'; | ||
| import { foremanUrl } from '../../../../common/helpers'; | ||
| import { translate as __ } from '../../../../common/I18n'; | ||
| import { BULK_DISASSOCIATE_KEY, bulkDisassociate } from './actions'; | ||
| import { APIActions } from '../../../../redux/API'; | ||
| import { | ||
| HOSTS_API_PATH, | ||
| API_REQUEST_KEY, | ||
| } from '../../../../routes/Hosts/constants'; | ||
| import { failedHostsToastParams } from '../helpers'; | ||
|
|
||
| const BulkDisassociateModal = ({ | ||
| isOpen, | ||
| closeModal, | ||
| selectAllHostsMode, | ||
| selectedCount, | ||
| selectedResults, | ||
| fetchBulkParams, | ||
| }) => { | ||
| const dispatch = useDispatch(); | ||
| const hostsWithComputeResource = selectedResults?.filter( | ||
| h => h.compute_resource_id && h.uuid | ||
| ); | ||
| const hostsWithoutComputeResource = selectedResults?.filter( | ||
| h => !hostsWithComputeResource.includes(h) | ||
| ); | ||
| const selectedResultsEmpty = selectedResults?.length === 0; | ||
|
|
||
| const selectedTreeViewData = [ | ||
| { | ||
| name: __('Selected hosts'), | ||
| id: 'selected-hosts-tree-view-title', | ||
| customBadgeContent: selectAllHostsMode ? 'All' : selectedCount, | ||
| }, | ||
| ]; | ||
| const applicableTreeViewData = [ | ||
| { | ||
| name: __('Hosts associated to compute resources'), | ||
| id: 'applicable-hosts-tree-view-title', | ||
| customBadgeContent: hostsWithComputeResource?.length, | ||
| }, | ||
| ]; | ||
| const excludedTreeViewData = [ | ||
| { | ||
| name: __('Excluded hosts'), | ||
| id: 'excluded-hosts-tree-view-title', | ||
| customBadgeContent: hostsWithoutComputeResource?.length, | ||
| }, | ||
| ]; | ||
|
|
||
| const handleError = response => { | ||
| closeModal(); | ||
| dispatch( | ||
| addToast( | ||
| failedHostsToastParams({ | ||
| ...response.data.error, | ||
| key: BULK_DISASSOCIATE_KEY, | ||
| }) | ||
| ) | ||
| ); | ||
| }; | ||
|
|
||
| const handleSuccess = response => { | ||
| dispatch( | ||
| addToast({ | ||
| type: 'success', | ||
| message: response.data.message, | ||
| }) | ||
| ); | ||
| dispatch( | ||
| APIActions.get({ | ||
| key: API_REQUEST_KEY, | ||
| url: foremanUrl(HOSTS_API_PATH), | ||
| }) | ||
| ); | ||
| closeModal(); | ||
| }; | ||
|
|
||
| const handleConfirm = () => { | ||
| const queryString = selectedResultsEmpty | ||
| ? fetchBulkParams() | ||
| : `id ^ (${hostsWithComputeResource.map(h => h.id).join(',')})`; | ||
| const requestBody = { | ||
| included: { | ||
| search: queryString, | ||
| }, | ||
| }; | ||
|
|
||
| dispatch(bulkDisassociate(requestBody, handleSuccess, handleError)); | ||
| }; | ||
|
|
||
| const modalActions = [ | ||
| <Button | ||
| key="add" | ||
| ouiaId="bulk-disassociate-modal-add-button" | ||
| variant="primary" | ||
| onClick={handleConfirm} | ||
| isDisabled={ | ||
| hostsWithComputeResource?.length === 0 && !selectedResultsEmpty | ||
| } | ||
| > | ||
| {__('Disassociate')} | ||
| </Button>, | ||
| <Button | ||
| key="cancel" | ||
| ouiaId="bulk-disassociate-modal-cancel-button" | ||
| variant="link" | ||
| onClick={closeModal} | ||
| > | ||
| {__('Cancel')} | ||
| </Button>, | ||
| ]; | ||
|
|
||
| return ( | ||
| <Modal | ||
| isOpen={isOpen} | ||
| onClose={closeModal} | ||
| onEscapePress={closeModal} | ||
| title={__('Disassociate hosts')} | ||
| width="50%" | ||
| position="top" | ||
| actions={modalActions} | ||
| id="bulk-disassociate-modal" | ||
| key="bulk-disassociate-modal" | ||
| ouiaId="bulk-disassociate-modal" | ||
| > | ||
| <TextContent> | ||
| <Text ouiaId="bulk-disassociate-options"> | ||
| {__( | ||
| 'This will disassociate the host in Foreman from its compute resource.' | ||
| )} | ||
| <br /> | ||
| {__( | ||
| 'After disassociating, a host can be deleted from Foreman without affecting its virtual machine.' | ||
| )} | ||
| </Text> | ||
| </TextContent> | ||
| <Alert | ||
| style={{ marginTop: '2rem', marginBottom: '1rem' }} | ||
| variant="warning" | ||
| isInline | ||
| isPlain | ||
| title={__('Hosts without a compute resource will be excluded.')} | ||
| ouiaId="warning-alert" | ||
| /> | ||
| <div style={{ width: '70%', maxHeight: '50%', marginLeft: '-1rem' }}> | ||
| {selectedResultsEmpty && ( | ||
| <TreeView | ||
|
ekohl marked this conversation as resolved.
Outdated
|
||
| data={selectedTreeViewData} | ||
| aria-label={__('Selected hosts')} | ||
| hasBadges | ||
| /> | ||
| )} | ||
| {!selectedResultsEmpty && ( | ||
| <> | ||
| <TreeView | ||
| data={applicableTreeViewData} | ||
| aria-label={__('Hosts associated to compute resources')} | ||
| hasBadges | ||
| /> | ||
| <TreeView | ||
| data={excludedTreeViewData} | ||
| aria-label={__('Excluded hosts')} | ||
| hasBadges | ||
| /> | ||
| </> | ||
| )} | ||
| </div> | ||
| </Modal> | ||
| ); | ||
| }; | ||
|
|
||
| BulkDisassociateModal.propTypes = { | ||
| isOpen: PropTypes.bool, | ||
| closeModal: PropTypes.func, | ||
| selectedResults: PropTypes.array, | ||
| fetchBulkParams: PropTypes.func.isRequired, | ||
| selectedCount: PropTypes.number.isRequired, | ||
| selectAllHostsMode: PropTypes.bool.isRequired, | ||
| }; | ||
|
|
||
| BulkDisassociateModal.defaultProps = { | ||
| isOpen: false, | ||
| closeModal: () => {}, | ||
| selectedResults: [], | ||
| }; | ||
|
|
||
| export default BulkDisassociateModal; | ||
16 changes: 16 additions & 0 deletions
16
...ck/assets/javascripts/react_app/components/HostsIndex/BulkActions/disassociate/actions.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import { APIActions } from '../../../../redux/API'; | ||
| import { foremanUrl } from '../../../../common/helpers'; | ||
|
|
||
| export const BULK_DISASSOCIATE_KEY = 'BULK_DISASSOCIATE'; | ||
| export const bulkDisassociate = (params, handleSuccess, handleError) => { | ||
| const url = foremanUrl(`/api/v2/hosts/bulk/disassociate`); | ||
| return APIActions.put({ | ||
| key: BULK_DISASSOCIATE_KEY, | ||
| url, | ||
| handleSuccess, | ||
| handleError, | ||
| params, | ||
| }); | ||
| }; | ||
|
|
||
| export default bulkDisassociate; |
29 changes: 29 additions & 0 deletions
29
webpack/assets/javascripts/react_app/components/HostsIndex/BulkActions/disassociate/index.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import React, { useContext } from 'react'; | ||
| import { ForemanActionsBarContext } from '../../../../components/HostDetails/ActionsBar'; | ||
| import { useForemanModal } from '../../../../components/ForemanModal/ForemanModalHooks'; | ||
| import BulkDisassociateModal from './BulkDisassociateModal'; | ||
|
|
||
| const BulkDisassociateModalScene = () => { | ||
| const { | ||
| selectAllHostsMode, | ||
| selectedCount, | ||
| selectedResults, | ||
| fetchBulkParams, | ||
| } = useContext(ForemanActionsBarContext); | ||
| const { modalOpen, setModalClosed } = useForemanModal({ | ||
| id: 'bulk-disassociate-modal', | ||
| }); | ||
| return ( | ||
| <BulkDisassociateModal | ||
| key="bulk-disassociate-modal" | ||
| selectAllHostsMode={selectAllHostsMode} | ||
| selectedCount={selectedCount} | ||
| selectedResults={selectedResults} | ||
| fetchBulkParams={fetchBulkParams} | ||
| isOpen={modalOpen} | ||
| closeModal={setModalClosed} | ||
| /> | ||
| ); | ||
| }; | ||
|
|
||
| export default BulkDisassociateModalScene; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm going to note this down for my own sanity, because I've been looking at it multiple times.
The method
disassociate!is defined here:foreman/app/models/host/managed.rb
Lines 589 to 593 in f4246cd
Now the comment says it doesn't trigger callbacks, but not what https://api.rubyonrails.org/v8.0.2/classes/ActiveRecord/Persistence.html#method-i-save-21 states it does run callbacks. Callbacks triggers audits and hooks, possibly more. That means you can't replace it with
update_allsince that uses direct SQL to update them all in bulk.If we already accept that we need to save each individual object then there's still the question of counting the affected rows.
save!()will always returntrueor raise an exception so there's no way of knowing if it actually modified any records. That means you need to count.Last note: that I found while looking this up: there is
MyClass.in_batches.each_recordto avoid instantiating all objects. For large collections of records this can save with memory consumption. That's probably something more for a generalBulkHostsManageroptimization.Short summary is that for now this is probably the best we can do.