2024-06-10 18:54:31 +00:00
|
|
|
import { useMutation, useQueryClient } from '@tanstack/react-query';
|
|
|
|
|
|
|
|
import {
|
|
|
|
mutationOptions,
|
|
|
|
withError,
|
|
|
|
withInvalidate,
|
|
|
|
} from '@/react-tools/react-query';
|
|
|
|
import { EnvironmentId } from '@/react/portainer/environments/types';
|
|
|
|
import axios, { parseAxiosError } from '@/portainer/services/axios';
|
|
|
|
|
|
|
|
import { buildDockerProxyUrl } from '../../proxy/queries/buildDockerProxyUrl';
|
2024-07-08 15:31:18 +00:00
|
|
|
import { withAgentTargetHeader } from '../../proxy/queries/utils';
|
2024-06-10 18:54:31 +00:00
|
|
|
import { NetworkId } from '../types';
|
|
|
|
|
|
|
|
import { queryKeys } from './queryKeys';
|
|
|
|
|
|
|
|
export function useDeleteNetwork(environmentId: EnvironmentId) {
|
|
|
|
const queryClient = useQueryClient();
|
|
|
|
|
|
|
|
return useMutation(
|
2024-07-08 15:31:18 +00:00
|
|
|
({ networkId, nodeName }: { networkId: NetworkId; nodeName?: string }) =>
|
|
|
|
deleteNetwork(environmentId, networkId, { nodeName }),
|
2024-06-10 18:54:31 +00:00
|
|
|
mutationOptions(
|
|
|
|
withInvalidate(queryClient, [queryKeys.base(environmentId)]),
|
|
|
|
withError('Unable to remove network')
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Raw docker API proxy
|
|
|
|
* @param environmentId
|
|
|
|
* @param networkId
|
|
|
|
* @returns
|
|
|
|
*/
|
|
|
|
export async function deleteNetwork(
|
|
|
|
environmentId: EnvironmentId,
|
2024-07-08 15:31:18 +00:00
|
|
|
networkId: NetworkId,
|
|
|
|
{ nodeName }: { nodeName?: string } = {}
|
2024-06-10 18:54:31 +00:00
|
|
|
) {
|
|
|
|
try {
|
|
|
|
await axios.delete(
|
2024-07-08 15:31:18 +00:00
|
|
|
buildDockerProxyUrl(environmentId, 'networks', networkId),
|
|
|
|
{
|
|
|
|
headers: { ...withAgentTargetHeader(nodeName) },
|
|
|
|
}
|
2024-06-10 18:54:31 +00:00
|
|
|
);
|
|
|
|
return networkId;
|
|
|
|
} catch (err) {
|
|
|
|
throw parseAxiosError(err, 'Unable to remove network');
|
|
|
|
}
|
|
|
|
}
|