-
Notifications
You must be signed in to change notification settings - Fork 985
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
9 changed files
with
294 additions
and
10 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
194 changes: 194 additions & 0 deletions
194
...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,194 @@ | ||
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, | ||
FormGroup, | ||
FormSelect, | ||
FormSelectOption, | ||
} 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 { visit } from '../../../../../foreman_navigation'; | ||
import { foremanUrl } from '../../../../common/helpers'; | ||
|
||
const BulkReassignHostgroupModal = ({ | ||
isOpen, | ||
closeModal, | ||
selectedCount, | ||
orgId, | ||
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(); | ||
}; | ||
|
||
useEffect(() => { | ||
dispatch(fetchHostgroups()); | ||
}, [dispatch]); | ||
|
||
const handleError = ({ response }) => { | ||
handleModalClose(); | ||
dispatch( | ||
addToast( | ||
failedHostsToastParams({ | ||
...response.data.error, | ||
key: BULK_REASSIGN_HOSTGROUP_KEY, | ||
}) | ||
) | ||
); | ||
}; | ||
const handleSave = () => { | ||
const requestBody = { | ||
included: { | ||
search: fetchBulkParams(), | ||
}, | ||
hostgroup_id: hostgroupId, | ||
}; | ||
|
||
dispatch( | ||
bulkReassignHostgroups( | ||
requestBody, | ||
() => visit(foremanUrl('/new/hosts')), | ||
handleError | ||
) | ||
); | ||
}; | ||
|
||
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="ccs-options-i18n" | ||
Check warning on line 135 in webpack/assets/javascripts/react_app/components/HostsIndex/BulkActions/reassignHostGroup/BulkReassignHostgroupModal.js
|
||
/> | ||
</strong> | ||
), | ||
}} | ||
id="bulk-reassign-hg-description" | ||
/> | ||
</Text> | ||
{hostgroups && hostgroupStatus === STATUS.RESOLVED && ( | ||
<> | ||
<FormGroup type="string" fieldId="host_group"> | ||
<FormSelect | ||
ouiaId="hostgroup-select" | ||
isRequired | ||
value={hostgroupId} | ||
onChange={value => { | ||
setHostgroupId(value); | ||
}} | ||
aria-label="hostgroup_select" | ||
> | ||
{[ | ||
<FormSelectOption | ||
key="placeholder" | ||
value="" | ||
label={__('Select a hostgroup')} | ||
isDisabled | ||
/>, | ||
].concat( | ||
hostgroups?.results?.map(hg => ( | ||
<FormSelectOption | ||
key={hg.id} | ||
value={hg.id} | ||
label={hg.name} | ||
/> | ||
)) | ||
)} | ||
</FormSelect> | ||
</FormGroup> | ||
</> | ||
)} | ||
</TextContent> | ||
<hr /> | ||
</Modal> | ||
); | ||
}; | ||
|
||
BulkReassignHostgroupModal.propTypes = { | ||
isOpen: PropTypes.bool, | ||
closeModal: PropTypes.func, | ||
selectedCount: PropTypes.number.isRequired, | ||
fetchBulkParams: PropTypes.func.isRequired, | ||
orgId: PropTypes.number.isRequired, | ||
}; | ||
|
||
BulkReassignHostgroupModal.defaultProps = { | ||
isOpen: false, | ||
closeModal: () => {}, | ||
}; | ||
|
||
export default BulkReassignHostgroupModal; |
30 changes: 30 additions & 0 deletions
30
...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,30 @@ | ||
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, | ||
successToast: response => response.data.message, | ||
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; |
27 changes: 27 additions & 0 deletions
27
...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,27 @@ | ||
import React, { useContext } from 'react'; | ||
import { ForemanActionsBarContext } from '../../../../components/HostDetails/ActionsBar'; | ||
import { useForemanModal } from '../../../../components/ForemanModal/ForemanModalHooks'; | ||
import { useForemanOrganization } from '../../../../Root/Context/ForemanContext'; | ||
import BulkReassignHostgroupModal from './BulkReassignHostgroupModal'; | ||
|
||
const BulkReassignHostgroupModalScene = () => { | ||
const { selectedCount, fetchBulkParams } = useContext( | ||
ForemanActionsBarContext | ||
); | ||
const { modalOpen, setModalClosed } = useForemanModal({ | ||
id: 'bulk-reassign-hg-modal', | ||
}); | ||
const org = useForemanOrganization(); | ||
return ( | ||
<BulkReassignHostgroupModal | ||
key="bulk-reassign-hg-modal" | ||
selectedCount={selectedCount} | ||
fetchBulkParams={fetchBulkParams} | ||
isOpen={modalOpen} | ||
closeModal={setModalClosed} | ||
orgId={org?.id} | ||
/> | ||
); | ||
}; | ||
|
||
export default BulkReassignHostgroupModalScene; |
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