2022-05-09 21:01:15 +00:00
|
|
|
import { useQuery, useMutation, useQueryClient } from 'react-query';
|
|
|
|
|
2022-10-23 06:53:25 +00:00
|
|
|
import { EnvironmentId } from '@/react/portainer/environments/types';
|
2022-05-09 21:01:15 +00:00
|
|
|
import {
|
|
|
|
error as notifyError,
|
|
|
|
success as notifySuccess,
|
|
|
|
} from '@/portainer/services/notifications';
|
2022-06-26 14:16:50 +00:00
|
|
|
import { ContainerId } from '@/react/docker/containers/types';
|
2022-05-09 21:01:15 +00:00
|
|
|
|
|
|
|
import {
|
|
|
|
getNetwork,
|
|
|
|
deleteNetwork,
|
|
|
|
disconnectContainer,
|
|
|
|
} from './network.service';
|
|
|
|
import { NetworkId } from './types';
|
|
|
|
|
|
|
|
export function useNetwork(environmentId: EnvironmentId, networkId: NetworkId) {
|
|
|
|
return useQuery(
|
|
|
|
['environments', environmentId, 'docker', 'networks', networkId],
|
|
|
|
() => getNetwork(environmentId, networkId),
|
|
|
|
{
|
|
|
|
onError: (err) => {
|
|
|
|
notifyError('Failure', err as Error, 'Unable to get network');
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function useDeleteNetwork() {
|
|
|
|
return useMutation(
|
|
|
|
({
|
|
|
|
environmentId,
|
|
|
|
networkId,
|
|
|
|
}: {
|
|
|
|
environmentId: EnvironmentId;
|
|
|
|
networkId: NetworkId;
|
|
|
|
}) => deleteNetwork(environmentId, networkId),
|
|
|
|
{
|
|
|
|
onSuccess: (networkId) => {
|
|
|
|
notifySuccess('Network successfully removed', networkId);
|
|
|
|
},
|
|
|
|
onError: (err) => {
|
|
|
|
notifyError('Failure', err as Error, 'Unable to remove network');
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function useDisconnectContainer() {
|
|
|
|
const client = useQueryClient();
|
|
|
|
|
|
|
|
return useMutation(
|
|
|
|
({
|
|
|
|
containerId,
|
|
|
|
environmentId,
|
|
|
|
networkId,
|
|
|
|
}: {
|
|
|
|
containerId: ContainerId;
|
|
|
|
environmentId: EnvironmentId;
|
|
|
|
networkId: NetworkId;
|
|
|
|
}) => disconnectContainer(environmentId, networkId, containerId),
|
|
|
|
{
|
|
|
|
onSuccess: ({ networkId, environmentId }) => {
|
|
|
|
notifySuccess('Container successfully disconnected', networkId);
|
|
|
|
return client.invalidateQueries([
|
|
|
|
'environments',
|
|
|
|
environmentId,
|
|
|
|
'docker',
|
|
|
|
'networks',
|
|
|
|
networkId,
|
|
|
|
]);
|
|
|
|
},
|
|
|
|
onError: (err) => {
|
|
|
|
notifyError(
|
|
|
|
'Failure',
|
|
|
|
err as Error,
|
|
|
|
'Unable to disconnect container from network'
|
|
|
|
);
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|