From 5de34a9c0b070120b9d063cb42c1bd46d2c8a003 Mon Sep 17 00:00:00 2001 From: nixocio Date: Fri, 30 Oct 2020 14:31:45 -0400 Subject: [PATCH] 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 --- .../InventorySources/InventorySourceList.jsx | 44 ++++++++++++++++--- .../InventorySourceList.test.jsx | 2 + 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/awx/ui_next/src/screens/Inventory/InventorySources/InventorySourceList.jsx b/awx/ui_next/src/screens/Inventory/InventorySources/InventorySourceList.jsx index 3334600117..84048f450a 100644 --- a/awx/ui_next/src/screens/Inventory/InventorySources/InventorySourceList.jsx +++ b/awx/ui_next/src/screens/Inventory/InventorySources/InventorySourceList.jsx @@ -105,8 +105,37 @@ function InventorySourceList({ i18n }) { ); 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 () => { - await handleDeleteSources(); + await handleDeleteRelatedResources(); + if (!deleteRelatedResourcesError) { + await handleDeleteSources(); + } setSelected([]); }; const canAdd = @@ -117,7 +146,12 @@ function InventorySourceList({ i18n }) { <> )} - {deletionError && ( + {(deletionError || deleteRelatedResourcesError) && ( {i18n._(t`Failed to delete one or more inventory sources.`)} - + )} diff --git a/awx/ui_next/src/screens/Inventory/InventorySources/InventorySourceList.test.jsx b/awx/ui_next/src/screens/Inventory/InventorySources/InventorySourceList.test.jsx index 97b2d4c8e3..5f4115b1fd 100644 --- a/awx/ui_next/src/screens/Inventory/InventorySources/InventorySourceList.test.jsx +++ b/awx/ui_next/src/screens/Inventory/InventorySources/InventorySourceList.test.jsx @@ -165,6 +165,8 @@ describe('', () => { wrapper.find('Button[aria-label="confirm delete"]').prop('onClick')() ); expect(InventorySourcesAPI.destroy).toHaveBeenCalledWith(1); + expect(InventorySourcesAPI.destroyHosts).toHaveBeenCalledWith(1); + expect(InventorySourcesAPI.destroyGroups).toHaveBeenCalledWith(1); }); test('should throw error after deletion failure', async () => {