Remove groups/hosts when deleting inventory sources

Remove related resources groups/hosts when deleting inventory sources.

The current UI deletes `groups` and `hosts` once the inventory source is
deleted. Add this behavior to the new UI.

See: https://github.com/ansible/awx/issues/8098
This commit is contained in:
nixocio
2020-10-30 14:31:45 -04:00
parent 1a4f2f43b7
commit 5de34a9c0b
2 changed files with 41 additions and 5 deletions

View File

@@ -105,8 +105,37 @@ function InventorySourceList({ i18n }) {
); );
const { error: syncError, dismissError } = useDismissableError(syncAllError); const { error: syncError, dismissError } = useDismissableError(syncAllError);
const deleteRelatedInventoryResources = resourceId => {
return [
InventorySourcesAPI.destroyHosts(resourceId),
InventorySourcesAPI.destroyGroups(resourceId),
];
};
const {
isLoading: deleteRelatedResourcesLoading,
deletionError: deleteRelatedResourcesError,
deleteItems: handleDeleteRelatedResources,
} = useDeleteItems(
useCallback(async () => {
return (
Promise.all(
selected
.map(({ id: resourceId }) =>
deleteRelatedInventoryResources(resourceId)
)
.flat()
),
[]
);
}, [selected])
);
const handleDelete = async () => { const handleDelete = async () => {
await handleDeleteSources(); await handleDeleteRelatedResources();
if (!deleteRelatedResourcesError) {
await handleDeleteSources();
}
setSelected([]); setSelected([]);
}; };
const canAdd = const canAdd =
@@ -117,7 +146,12 @@ function InventorySourceList({ i18n }) {
<> <>
<PaginatedDataList <PaginatedDataList
contentError={fetchError} contentError={fetchError}
hasContentLoading={isLoading || isDeleteLoading || isSyncAllLoading} hasContentLoading={
isLoading ||
isDeleteLoading ||
isSyncAllLoading ||
deleteRelatedResourcesLoading
}
items={sources} items={sources}
itemCount={sourceCount} itemCount={sourceCount}
pluralizedItemName={i18n._(t`Inventory Sources`)} pluralizedItemName={i18n._(t`Inventory Sources`)}
@@ -193,16 +227,16 @@ function InventorySourceList({ i18n }) {
</AlertModal> </AlertModal>
)} )}
{deletionError && ( {(deletionError || deleteRelatedResourcesError) && (
<AlertModal <AlertModal
aria-label={i18n._(t`Delete error`)} aria-label={i18n._(t`Delete error`)}
isOpen={deletionError} isOpen={deletionError || deleteRelatedResourcesError}
variant="error" variant="error"
title={i18n._(t`Error!`)} title={i18n._(t`Error!`)}
onClose={clearDeletionError} onClose={clearDeletionError}
> >
{i18n._(t`Failed to delete one or more inventory sources.`)} {i18n._(t`Failed to delete one or more inventory sources.`)}
<ErrorDetail error={deletionError} /> <ErrorDetail error={deletionError || deleteRelatedResourcesError} />
</AlertModal> </AlertModal>
)} )}
</> </>

View File

@@ -165,6 +165,8 @@ describe('<InventorySourceList />', () => {
wrapper.find('Button[aria-label="confirm delete"]').prop('onClick')() wrapper.find('Button[aria-label="confirm delete"]').prop('onClick')()
); );
expect(InventorySourcesAPI.destroy).toHaveBeenCalledWith(1); expect(InventorySourcesAPI.destroy).toHaveBeenCalledWith(1);
expect(InventorySourcesAPI.destroyHosts).toHaveBeenCalledWith(1);
expect(InventorySourcesAPI.destroyGroups).toHaveBeenCalledWith(1);
}); });
test('should throw error after deletion failure', async () => { test('should throw error after deletion failure', async () => {