You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
portainer/app/react/docker/agent/queries/useAgentNodes.ts

45 lines
1022 B

import { useQuery } from '@tanstack/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');
}
}