-
Notifications
You must be signed in to change notification settings - Fork 991
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
Hostgroup bulk actions : Reassign hostgroups #10202
Merged
+340
−11
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 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 = () => { | ||
sjha4 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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.
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.
REST API wise I think
POST /hostgroups/:id/hosts
would be cleaner but I see the bulk pattern is already established so I'm not going to block on this.