-
Notifications
You must be signed in to change notification settings - Fork 991
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #37551 - Hosts : Allow bulk reassignment of hostgroups
- Loading branch information
Showing
11 changed files
with
340 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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
197 changes: 197 additions & 0 deletions
197
...act_app/components/HostsIndex/BulkActions/reassignHostGroup/BulkReassignHostgroupModal.js
This file contains 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,197 @@ | ||
import React, { useState, useEffect } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { useDispatch, useSelector } from 'react-redux'; | ||
import { FormattedMessage } from 'react-intl'; | ||
import { | ||
Modal, | ||
Button, | ||
TextContent, | ||
Text, | ||
SelectOption, | ||
} from '@patternfly/react-core'; | ||
import { addToast } from '../../../ToastsList/slice'; | ||
import { translate as __ } from '../../../../common/I18n'; | ||
import { failedHostsToastParams } from '../helpers'; | ||
import { STATUS } from '../../../../constants'; | ||
import { | ||
selectAPIStatus, | ||
selectAPIResponse, | ||
} from '../../../../redux/API/APISelectors'; | ||
import { | ||
BULK_REASSIGN_HOSTGROUP_KEY, | ||
bulkReassignHostgroups, | ||
fetchHostgroups, | ||
HOSTGROUP_KEY, | ||
} from './actions'; | ||
import { foremanUrl } from '../../../../common/helpers'; | ||
import { APIActions } from '../../../../redux/API'; | ||
import HostGroupSelect from './HostGroupSelect'; | ||
import { | ||
HOSTS_API_PATH, | ||
API_REQUEST_KEY, | ||
} from '../../../../routes/Hosts/constants'; | ||
|
||
const BulkReassignHostgroupModal = ({ | ||
isOpen, | ||
closeModal, | ||
selectedCount, | ||
fetchBulkParams, | ||
}) => { | ||
const dispatch = useDispatch(); | ||
const [hostgroupId, setHostgroupId] = useState(''); | ||
const hostgroups = useSelector(state => | ||
selectAPIResponse(state, HOSTGROUP_KEY) | ||
); | ||
const hostgroupStatus = useSelector(state => | ||
selectAPIStatus(state, HOSTGROUP_KEY) | ||
); | ||
const hostUpdateStatus = useSelector(state => | ||
selectAPIStatus(state, BULK_REASSIGN_HOSTGROUP_KEY) | ||
); | ||
const handleModalClose = () => { | ||
setHostgroupId(''); | ||
closeModal(); | ||
}; | ||
|
||
const [hgSelectOpen, setHgSelectOpen] = useState(false); | ||
|
||
useEffect(() => { | ||
dispatch(fetchHostgroups()); | ||
}, [dispatch]); | ||
|
||
const handleError = response => { | ||
handleModalClose(); | ||
dispatch( | ||
addToast( | ||
failedHostsToastParams({ | ||
...response.data.error, | ||
key: BULK_REASSIGN_HOSTGROUP_KEY, | ||
}) | ||
) | ||
); | ||
}; | ||
|
||
const handleSuccess = response => { | ||
dispatch( | ||
addToast({ | ||
type: 'success', | ||
message: response.data.message, | ||
}) | ||
); | ||
dispatch( | ||
APIActions.get({ | ||
key: API_REQUEST_KEY, | ||
url: foremanUrl(HOSTS_API_PATH), | ||
}) | ||
); | ||
handleModalClose(); | ||
}; | ||
const handleSave = () => { | ||
const requestBody = { | ||
included: { | ||
search: fetchBulkParams(), | ||
}, | ||
hostgroup_id: hostgroupId, | ||
}; | ||
|
||
dispatch(bulkReassignHostgroups(requestBody, handleSuccess, handleError)); | ||
}; | ||
|
||
const handleHgSelect = (event, selection) => { | ||
setHostgroupId(selection); | ||
setHgSelectOpen(false); | ||
}; | ||
|
||
const modalActions = [ | ||
<Button | ||
key="add" | ||
ouiaId="bulk-reassign-hg-modal-add-button" | ||
variant="primary" | ||
onClick={handleSave} | ||
isDisabled={hostUpdateStatus === STATUS.PENDING} | ||
isLoading={hostUpdateStatus === STATUS.PENDING} | ||
> | ||
{__('Save')} | ||
</Button>, | ||
<Button | ||
key="cancel" | ||
ouiaId="bulk-reassign-hg-modal-cancel-button" | ||
variant="link" | ||
onClick={handleModalClose} | ||
> | ||
{__('Cancel')} | ||
</Button>, | ||
]; | ||
return ( | ||
<Modal | ||
isOpen={isOpen} | ||
onClose={handleModalClose} | ||
onEscapePress={handleModalClose} | ||
title={__('Change host group')} | ||
width="50%" | ||
position="top" | ||
actions={modalActions} | ||
id="bulk-reassign-hg-modal" | ||
key="bulk-reassign-hg-modal" | ||
ouiaId="bulk-reassign-hg-modal" | ||
> | ||
<TextContent> | ||
<Text ouiaId="bulk-reassign-hg-options"> | ||
<FormattedMessage | ||
defaultMessage={__( | ||
'Change the host group of {hosts}. Some hosts may already be in your chosen host group.' | ||
)} | ||
values={{ | ||
hosts: ( | ||
<strong> | ||
<FormattedMessage | ||
defaultMessage="{count, plural, one {# {singular}} other {# {plural}}}" | ||
values={{ | ||
count: selectedCount, | ||
singular: __('selected host'), | ||
plural: __('selected hosts'), | ||
}} | ||
id="bulk-hg-selected-host-options" | ||
/> | ||
</strong> | ||
), | ||
}} | ||
id="bulk-reassign-hg-description" | ||
/> | ||
</Text> | ||
</TextContent> | ||
{hostgroups && hostgroupStatus === STATUS.RESOLVED && ( | ||
<HostGroupSelect | ||
onClear={() => setHostgroupId('')} | ||
headerText={__('Select host group')} | ||
selections={hostgroupId} | ||
onChange={value => setHostgroupId(value)} | ||
isOpen={hgSelectOpen} | ||
onToggle={isExpanded => setHgSelectOpen(isExpanded)} | ||
onSelect={handleHgSelect} | ||
> | ||
{hostgroups?.results?.map(hg => ( | ||
<SelectOption key={hg.id} value={hg.id}> | ||
{hg.name} | ||
</SelectOption> | ||
))} | ||
</HostGroupSelect> | ||
)} | ||
<hr /> | ||
</Modal> | ||
); | ||
}; | ||
|
||
BulkReassignHostgroupModal.propTypes = { | ||
isOpen: PropTypes.bool, | ||
closeModal: PropTypes.func, | ||
selectedCount: PropTypes.number.isRequired, | ||
fetchBulkParams: PropTypes.func.isRequired, | ||
}; | ||
|
||
BulkReassignHostgroupModal.defaultProps = { | ||
isOpen: false, | ||
closeModal: () => {}, | ||
}; | ||
|
||
export default BulkReassignHostgroupModal; |
41 changes: 41 additions & 0 deletions
41
...ascripts/react_app/components/HostsIndex/BulkActions/reassignHostGroup/HostGroupSelect.js
This file contains 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,41 @@ | ||
import React from 'react'; | ||
import { Select, SelectVariant } from '@patternfly/react-core'; | ||
import PropTypes from 'prop-types'; | ||
import { translate as __ } from '../../../../common/I18n'; | ||
|
||
const HostGroupSelect = ({ | ||
headerText, | ||
children, | ||
onClear, | ||
...pfSelectProps | ||
}) => ( | ||
<div style={{ marginTop: '1em' }}> | ||
<h3>{headerText}</h3> | ||
<Select | ||
variant={SelectVariant.typeahead} | ||
onClear={onClear} | ||
maxHeight="20rem" | ||
menuAppendTo="parent" | ||
ouiaId="select-host-group" | ||
id="selectHostGroup" | ||
name="selectHostGroup" | ||
aria-label="selectHostGroup" | ||
{...pfSelectProps} | ||
> | ||
{children} | ||
</Select> | ||
</div> | ||
); | ||
|
||
HostGroupSelect.propTypes = { | ||
headerText: PropTypes.string, | ||
onClear: PropTypes.func.isRequired, | ||
children: PropTypes.node, | ||
}; | ||
|
||
HostGroupSelect.defaultProps = { | ||
headerText: __('Select host group'), | ||
children: [], | ||
}; | ||
|
||
export default HostGroupSelect; |
29 changes: 29 additions & 0 deletions
29
...sets/javascripts/react_app/components/HostsIndex/BulkActions/reassignHostGroup/actions.js
This file contains 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 { APIActions } from '../../../../redux/API'; | ||
import { foremanUrl } from '../../../../common/helpers'; | ||
|
||
export const BULK_REASSIGN_HOSTGROUP_KEY = 'BULK_REASSIGN_HOSTGROUP_KEY'; | ||
export const bulkReassignHostgroups = (params, handleSuccess, handleError) => { | ||
const url = foremanUrl(`/api/v2/hosts/bulk/reassign_hostgroup`); | ||
return APIActions.put({ | ||
key: BULK_REASSIGN_HOSTGROUP_KEY, | ||
url, | ||
handleSuccess, | ||
handleError, | ||
params, | ||
}); | ||
}; | ||
|
||
export const HOSTGROUP_KEY = 'HOSTGROUP_KEY'; | ||
|
||
export const fetchHostgroups = () => { | ||
const url = foremanUrl('/api/v2/hostgroups'); | ||
return APIActions.get({ | ||
key: HOSTGROUP_KEY, | ||
url, | ||
params: { | ||
per_page: 'all', | ||
}, | ||
}); | ||
}; | ||
|
||
export default bulkReassignHostgroups; |
24 changes: 24 additions & 0 deletions
24
...assets/javascripts/react_app/components/HostsIndex/BulkActions/reassignHostGroup/index.js
This file contains 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,24 @@ | ||
import React, { useContext } from 'react'; | ||
import { ForemanActionsBarContext } from '../../../../components/HostDetails/ActionsBar'; | ||
import { useForemanModal } from '../../../../components/ForemanModal/ForemanModalHooks'; | ||
import BulkReassignHostgroupModal from './BulkReassignHostgroupModal'; | ||
|
||
const BulkReassignHostgroupModalScene = () => { | ||
const { selectedCount, fetchBulkParams } = useContext( | ||
ForemanActionsBarContext | ||
); | ||
const { modalOpen, setModalClosed } = useForemanModal({ | ||
id: 'bulk-reassign-hg-modal', | ||
}); | ||
return ( | ||
<BulkReassignHostgroupModal | ||
key="bulk-reassign-hg-modal" | ||
selectedCount={selectedCount} | ||
fetchBulkParams={fetchBulkParams} | ||
isOpen={modalOpen} | ||
closeModal={setModalClosed} | ||
/> | ||
); | ||
}; | ||
|
||
export default BulkReassignHostgroupModalScene; |
Oops, something went wrong.