1
0
Fork 0
mirror of https://github.com/portainer/portainer.git synced 2025-07-25 08:19:40 +02:00

refactor(containers): migrate create view to react [EE-2307] (#9175)

This commit is contained in:
Chaim Lev-Ari 2023-10-19 13:45:50 +02:00 committed by GitHub
parent bc0050a7b4
commit d970f0e2bc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
71 changed files with 2612 additions and 1399 deletions

View file

@ -0,0 +1,17 @@
import { EnvironmentId } from '@/react/portainer/environments/types';
export function buildAgentUrl(
environmentId: EnvironmentId,
apiVersion: number,
action: string
) {
let url = `/endpoints/${environmentId}/agent/docker`;
if (apiVersion > 1) {
url += `/v${apiVersion}`;
}
url += `/${action}`;
return url;
}

View file

@ -0,0 +1,44 @@
import { useQuery } from 'react-query';
import axios, { parseAxiosError } from '@/portainer/services/axios';
import { EnvironmentId } from '@/react/portainer/environments/types';
import { buildAgentUrl } from './build-url';
interface Node {
IPAddress: string;
NodeName: string;
NodeRole: string;
}
export function useAgentNodes<T = Array<Node>>(
environmentId: EnvironmentId,
apiVersion: number,
{
select,
enabled,
}: {
select?: (data: Array<Node>) => T;
enabled?: boolean;
} = {}
) {
return useQuery(
['environment', environmentId, 'agent', 'nodes'],
() => getNodes(environmentId, apiVersion),
{
select,
enabled,
}
);
}
async function getNodes(environmentId: EnvironmentId, apiVersion: number) {
try {
const response = await axios.get<Array<Node>>(
buildAgentUrl(environmentId, apiVersion, 'agents')
);
return response.data;
} catch (error) {
throw parseAxiosError(error as Error, 'Unable to retrieve nodes');
}
}

View file

@ -0,0 +1,29 @@
import { useQuery } from 'react-query';
import axios, {
isAxiosError,
parseAxiosError,
} from '@/portainer/services/axios';
import { EnvironmentId } from '@/react/portainer/environments/types';
import { buildUrl } from '../../proxy/queries/build-url';
export function useApiVersion(environmentId: EnvironmentId) {
return useQuery(['environment', environmentId, 'agent', 'ping'], () =>
getApiVersion(environmentId)
);
}
async function getApiVersion(environmentId: EnvironmentId) {
try {
const { headers } = await axios.get(buildUrl(environmentId, 'ping'));
return parseInt(headers['portainer-agent-api-version'], 10) || 1;
} catch (error) {
// 404 - agent is up - set version to 1
if (isAxiosError(error) && error.response?.status === 404) {
return 1;
}
throw parseAxiosError(error as Error, 'Unable to ping agent');
}
}