mirror of
https://github.com/portainer/portainer.git
synced 2025-07-24 15:59:41 +02:00
refactor(docker/containers): migrate networks table to react [EE-4665] (#10069)
This commit is contained in:
parent
776f6a62c3
commit
b15812a74d
28 changed files with 632 additions and 259 deletions
73
app/react/docker/networks/queries/useConnectContainer.ts
Normal file
73
app/react/docker/networks/queries/useConnectContainer.ts
Normal file
|
@ -0,0 +1,73 @@
|
|||
import { EndpointSettings } from 'docker-types/generated/1.41';
|
||||
import { AxiosRequestHeaders } from 'axios';
|
||||
import { useMutation, useQueryClient } from 'react-query';
|
||||
|
||||
import axios, { parseAxiosError } from '@/portainer/services/axios';
|
||||
import { EnvironmentId } from '@/react/portainer/environments/types';
|
||||
import {
|
||||
mutationOptions,
|
||||
withError,
|
||||
withInvalidate,
|
||||
} from '@/react-tools/react-query';
|
||||
|
||||
import { queryKeys as dockerQueryKeys } from '../../queries/utils';
|
||||
|
||||
import { buildUrl } from './buildUrl';
|
||||
|
||||
interface ConnectContainerPayload {
|
||||
Container: string;
|
||||
EndpointConfig?: EndpointSettings;
|
||||
}
|
||||
|
||||
export function useConnectContainerMutation(environmentId: EnvironmentId) {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation(
|
||||
(params: Omit<ConnectContainer, 'environmentId'>) =>
|
||||
connectContainer({ ...params, environmentId }),
|
||||
mutationOptions(
|
||||
withError('Failed connecting container to network'),
|
||||
withInvalidate(queryClient, [dockerQueryKeys.containers(environmentId)])
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
interface ConnectContainer {
|
||||
environmentId: EnvironmentId;
|
||||
networkId: string;
|
||||
containerId: string;
|
||||
aliases?: EndpointSettings['Aliases'];
|
||||
nodeName?: string;
|
||||
}
|
||||
|
||||
export async function connectContainer({
|
||||
environmentId,
|
||||
containerId,
|
||||
networkId,
|
||||
aliases,
|
||||
nodeName,
|
||||
}: ConnectContainer) {
|
||||
const payload: ConnectContainerPayload = {
|
||||
Container: containerId,
|
||||
};
|
||||
if (aliases) {
|
||||
payload.EndpointConfig = {
|
||||
Aliases: aliases,
|
||||
};
|
||||
}
|
||||
|
||||
const headers: AxiosRequestHeaders = {};
|
||||
|
||||
if (nodeName) {
|
||||
headers['X-PortainerAgent-Target'] = nodeName;
|
||||
}
|
||||
|
||||
try {
|
||||
await axios.post(
|
||||
buildUrl(environmentId, { id: networkId, action: 'connect' }),
|
||||
payload
|
||||
);
|
||||
} catch (err) {
|
||||
throw parseAxiosError(err as Error, 'Unable to connect container');
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue