2024-10-01 01:15:51 +00:00
|
|
|
import { useQuery } from '@tanstack/react-query';
|
|
|
|
|
|
|
|
import { withGlobalError } from '@/react-tools/react-query';
|
|
|
|
import axios, { parseAxiosError } from '@/portainer/services/axios';
|
|
|
|
import { EnvironmentId } from '@/react/portainer/environments/types';
|
|
|
|
|
|
|
|
import { Configuration } from '../types';
|
|
|
|
|
|
|
|
import { configMapQueryKeys } from './query-keys';
|
|
|
|
import { ConfigMapQueryParams } from './types';
|
|
|
|
|
2024-11-10 19:17:20 +00:00
|
|
|
export function useConfigMap<T = Configuration>(
|
2024-10-01 01:15:51 +00:00
|
|
|
environmentId: EnvironmentId,
|
|
|
|
namespace: string,
|
|
|
|
configMap: string,
|
2024-11-10 19:17:20 +00:00
|
|
|
options?: {
|
|
|
|
autoRefreshRate?: number;
|
|
|
|
select?: (data: Configuration) => T;
|
|
|
|
enabled?: boolean;
|
|
|
|
} & ConfigMapQueryParams
|
2024-10-01 01:15:51 +00:00
|
|
|
) {
|
|
|
|
return useQuery(
|
|
|
|
configMapQueryKeys.configMap(environmentId, namespace, configMap),
|
|
|
|
() => getConfigMap(environmentId, namespace, configMap, { withData: true }),
|
|
|
|
{
|
2024-11-10 19:17:20 +00:00
|
|
|
select: options?.select,
|
|
|
|
enabled: options?.enabled,
|
|
|
|
refetchInterval: () => options?.autoRefreshRate ?? false,
|
|
|
|
...withGlobalError(`Unable to retrieve ConfigMap '${configMap}'`),
|
2024-10-01 01:15:51 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// get a configmap
|
2024-11-10 19:17:20 +00:00
|
|
|
export async function getConfigMap(
|
2024-10-01 01:15:51 +00:00
|
|
|
environmentId: EnvironmentId,
|
|
|
|
namespace: string,
|
|
|
|
configMap: string,
|
|
|
|
params?: { withData?: boolean }
|
|
|
|
) {
|
|
|
|
try {
|
2024-11-10 19:17:20 +00:00
|
|
|
const { data } = await axios.get<Configuration>(
|
2024-10-01 01:15:51 +00:00
|
|
|
`/kubernetes/${environmentId}/namespaces/${namespace}/configmaps/${configMap}`,
|
|
|
|
{ params }
|
|
|
|
);
|
|
|
|
return data;
|
|
|
|
} catch (e) {
|
|
|
|
// use parseAxiosError instead of parseKubernetesAxiosError
|
|
|
|
// because this is an internal portainer api endpoint, not through the kube proxy
|
|
|
|
throw parseAxiosError(e, 'Unable to retrieve ConfigMaps');
|
|
|
|
}
|
|
|
|
}
|