From 25afb8477ecf170f6087cd97938cceedc4596730 Mon Sep 17 00:00:00 2001 From: Alex Corey Date: Tue, 30 Aug 2022 11:09:15 -0400 Subject: [PATCH] Adds functionality to deprovision an instance from list and details view --- awx/ui/src/api/models/Instances.js | 6 + awx/ui/src/screens/Instances/Instance.js | 2 +- .../InstanceDetail/InstanceDetail.js | 50 ++++- .../Instances/InstanceList/InstanceList.js | 50 ++++- .../Instances/Shared/RemoveInstanceButton.js | 198 ++++++++++++++++++ .../util/getRelatedResourceDeleteDetails.js | 8 + 6 files changed, 302 insertions(+), 12 deletions(-) create mode 100644 awx/ui/src/screens/Instances/Shared/RemoveInstanceButton.js diff --git a/awx/ui/src/api/models/Instances.js b/awx/ui/src/api/models/Instances.js index 460b809ec1..21445ff02a 100644 --- a/awx/ui/src/api/models/Instances.js +++ b/awx/ui/src/api/models/Instances.js @@ -25,6 +25,12 @@ class Instances extends Base { readInstanceGroup(instanceId) { return this.http.get(`${this.baseUrl}${instanceId}/instance_groups/`); } + + deprovisionInstance(instanceId) { + return this.http.post(`${this.baseUrl}${instanceId}`, { + node_state: 'deprovisioning', + }); + } } export default Instances; diff --git a/awx/ui/src/screens/Instances/Instance.js b/awx/ui/src/screens/Instances/Instance.js index cc7e42267b..cd1169962b 100644 --- a/awx/ui/src/screens/Instances/Instance.js +++ b/awx/ui/src/screens/Instances/Instance.js @@ -64,7 +64,7 @@ function Instance({ setBreadcrumb }) { - + {isK8s && ( diff --git a/awx/ui/src/screens/Instances/InstanceDetail/InstanceDetail.js b/awx/ui/src/screens/Instances/InstanceDetail/InstanceDetail.js index 3dc8690a1d..c8d35ac10d 100644 --- a/awx/ui/src/screens/Instances/InstanceDetail/InstanceDetail.js +++ b/awx/ui/src/screens/Instances/InstanceDetail/InstanceDetail.js @@ -1,6 +1,6 @@ import React, { useCallback, useEffect, useState } from 'react'; -import { Link, useParams } from 'react-router-dom'; +import { Link, useHistory, useParams } from 'react-router-dom'; import { t, Plural } from '@lingui/macro'; import { Button, @@ -28,7 +28,11 @@ import ContentError from 'components/ContentError'; import ContentLoading from 'components/ContentLoading'; import { Detail, DetailList } from 'components/DetailList'; import StatusLabel from 'components/StatusLabel'; -import useRequest, { useDismissableError } from 'hooks/useRequest'; +import useRequest, { + useDeleteItems, + useDismissableError, +} from 'hooks/useRequest'; +import RemoveInstanceButton from '../Shared/RemoveInstanceButton'; const Unavailable = styled.span` color: var(--pf-global--danger-color--200); @@ -56,11 +60,11 @@ function computeForks(memCapacity, cpuCapacity, selectedCapacityAdjustment) { ); } -function InstanceDetail({ setBreadcrumb }) { +function InstanceDetail({ setBreadcrumb, isK8s }) { const { me = {} } = useConfig(); const { id } = useParams(); const [forks, setForks] = useState(); - + const history = useHistory(); const [healthCheck, setHealthCheck] = useState({}); const { @@ -148,10 +152,25 @@ function InstanceDetail({ setBreadcrumb }) { const { error, dismissError } = useDismissableError( updateInstanceError || healthCheckError ); + const { + isLoading: isRemoveLoading, + deleteItems: removeInstances, + deletionError: removeError, + clearDeletionError, + } = useDeleteItems( + async () => { + await InstancesAPI.deprovisionInstance(instance.id); + history.push('/instances'); + }, + { + fetchItems: fetchDetails, + } + ); + if (contentError) { return ; } - if (isLoading) { + if (isLoading || isRemoveLoading) { return ; } const isHopNode = instance.node_type === 'hop'; @@ -218,6 +237,7 @@ function InstanceDetail({ setBreadcrumb }) { + + + )} + + {isModalOpen && ( + toggleModal(false)} + actions={[ + , + , + ]} + > +
{t`This action will remove the following instances:`}
+ {itemsToRemove.map((item) => ( + + {item.hostname} +
+
+ ))} + {removeDetails && ( + + )} +
+ )} + + ); +} + +export default RemoveInstanceButton; diff --git a/awx/ui/src/util/getRelatedResourceDeleteDetails.js b/awx/ui/src/util/getRelatedResourceDeleteDetails.js index f9d9496aec..a3289c57f8 100644 --- a/awx/ui/src/util/getRelatedResourceDeleteDetails.js +++ b/awx/ui/src/util/getRelatedResourceDeleteDetails.js @@ -15,6 +15,7 @@ import { ExecutionEnvironmentsAPI, ApplicationsAPI, OrganizationsAPI, + InstanceGroupsAPI, } from 'api'; export async function getRelatedResourceDeleteCounts(requests) { @@ -274,4 +275,11 @@ export const relatedResourceDeleteRequests = { label: t`Templates`, }, ], + + instance: (selected) => [ + { + request: () => InstanceGroupsAPI.read({ instances: selected.id }), + label: t`Instance Groups`, + }, + ], };