mirror of
https://github.com/portainer/portainer.git
synced 2025-07-24 15:59:41 +02:00
refactor(docker): move components to react [EE-3348] (#7084)
This commit is contained in:
parent
7238372d8d
commit
46e1a01625
41 changed files with 61 additions and 76 deletions
129
app/react/docker/containers/containers.service.ts
Normal file
129
app/react/docker/containers/containers.service.ts
Normal file
|
@ -0,0 +1,129 @@
|
|||
import { EnvironmentId } from '@/portainer/environments/types';
|
||||
import PortainerError from '@/portainer/error';
|
||||
import axios from '@/portainer/services/axios';
|
||||
import { genericHandler } from '@/docker/rest/response/handlers';
|
||||
|
||||
import { NetworkId } from '../networks/types';
|
||||
|
||||
import { ContainerId, DockerContainer } from './types';
|
||||
|
||||
export interface Filters {
|
||||
label?: string[];
|
||||
network?: NetworkId[];
|
||||
}
|
||||
|
||||
export async function startContainer(
|
||||
endpointId: EnvironmentId,
|
||||
id: ContainerId
|
||||
) {
|
||||
await axios.post<void>(
|
||||
urlBuilder(endpointId, id, 'start'),
|
||||
{},
|
||||
{ transformResponse: genericHandler }
|
||||
);
|
||||
}
|
||||
|
||||
export async function stopContainer(
|
||||
endpointId: EnvironmentId,
|
||||
id: ContainerId
|
||||
) {
|
||||
await axios.post<void>(urlBuilder(endpointId, id, 'stop'), {});
|
||||
}
|
||||
|
||||
export async function restartContainer(
|
||||
endpointId: EnvironmentId,
|
||||
id: ContainerId
|
||||
) {
|
||||
await axios.post<void>(urlBuilder(endpointId, id, 'restart'), {});
|
||||
}
|
||||
|
||||
export async function killContainer(
|
||||
endpointId: EnvironmentId,
|
||||
id: ContainerId
|
||||
) {
|
||||
await axios.post<void>(urlBuilder(endpointId, id, 'kill'), {});
|
||||
}
|
||||
|
||||
export async function pauseContainer(
|
||||
endpointId: EnvironmentId,
|
||||
id: ContainerId
|
||||
) {
|
||||
await axios.post<void>(urlBuilder(endpointId, id, 'pause'), {});
|
||||
}
|
||||
|
||||
export async function resumeContainer(
|
||||
endpointId: EnvironmentId,
|
||||
id: ContainerId
|
||||
) {
|
||||
await axios.post<void>(urlBuilder(endpointId, id, 'unpause'), {});
|
||||
}
|
||||
|
||||
export async function renameContainer(
|
||||
endpointId: EnvironmentId,
|
||||
id: ContainerId,
|
||||
name: string
|
||||
) {
|
||||
await axios.post<void>(
|
||||
urlBuilder(endpointId, id, 'rename'),
|
||||
{},
|
||||
{ params: { name }, transformResponse: genericHandler }
|
||||
);
|
||||
}
|
||||
|
||||
export async function removeContainer(
|
||||
endpointId: EnvironmentId,
|
||||
container: DockerContainer,
|
||||
removeVolumes: boolean
|
||||
) {
|
||||
try {
|
||||
const { data } = await axios.delete<null | { message: string }>(
|
||||
urlBuilder(endpointId, container.Id),
|
||||
{
|
||||
params: { v: removeVolumes ? 1 : 0, force: true },
|
||||
transformResponse: genericHandler,
|
||||
}
|
||||
);
|
||||
|
||||
if (data && data.message) {
|
||||
throw new PortainerError(data.message);
|
||||
}
|
||||
} catch (e) {
|
||||
throw new PortainerError('Unable to remove container', e as Error);
|
||||
}
|
||||
}
|
||||
|
||||
export async function getContainers(
|
||||
environmentId: EnvironmentId,
|
||||
filters?: Filters
|
||||
) {
|
||||
try {
|
||||
const { data } = await axios.get<DockerContainer[]>(
|
||||
urlBuilder(environmentId, '', 'json'),
|
||||
{
|
||||
params: { all: 0, filters },
|
||||
}
|
||||
);
|
||||
|
||||
return data;
|
||||
} catch (e) {
|
||||
throw new PortainerError('Unable to retrieve containers', e as Error);
|
||||
}
|
||||
}
|
||||
|
||||
function urlBuilder(
|
||||
endpointId: EnvironmentId,
|
||||
id?: ContainerId,
|
||||
action?: string
|
||||
) {
|
||||
let url = `/endpoints/${endpointId}/docker/containers`;
|
||||
|
||||
if (id) {
|
||||
url += `/${id}`;
|
||||
}
|
||||
|
||||
if (action) {
|
||||
url += `/${action}`;
|
||||
}
|
||||
|
||||
return url;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue