2025-04-08 12:51:36 +12:00
|
|
|
import { useQuery } from '@tanstack/react-query';
|
|
|
|
|
|
|
|
import axios, { parseAxiosError } from '@/portainer/services/axios';
|
|
|
|
import { withGlobalError } from '@/react-tools/react-query';
|
|
|
|
|
2025-06-05 13:13:45 +12:00
|
|
|
type Params = {
|
2025-07-13 10:37:43 +12:00
|
|
|
/** The name of the chart to get the values for */
|
2025-06-05 13:13:45 +12:00
|
|
|
chart: string;
|
2025-07-13 10:37:43 +12:00
|
|
|
/** The repository URL or registry ID */
|
2025-06-05 13:13:45 +12:00
|
|
|
repo: string;
|
2025-07-13 10:37:43 +12:00
|
|
|
/** The version of the chart to get the values for */
|
2025-06-05 13:13:45 +12:00
|
|
|
version?: string;
|
|
|
|
};
|
2025-04-08 12:51:36 +12:00
|
|
|
|
2025-06-05 13:13:45 +12:00
|
|
|
async function getHelmChartValues(params: Params) {
|
2025-04-08 12:51:36 +12:00
|
|
|
try {
|
|
|
|
const response = await axios.get<string>(`/templates/helm/values`, {
|
2025-06-05 13:13:45 +12:00
|
|
|
params,
|
2025-04-08 12:51:36 +12:00
|
|
|
});
|
|
|
|
return response.data;
|
|
|
|
} catch (err) {
|
2025-07-13 10:37:43 +12:00
|
|
|
throw parseAxiosError(err, 'Unable to get Helm chart values');
|
2025-04-08 12:51:36 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-07-13 10:37:43 +12:00
|
|
|
export function useHelmChartValues(params: Params, isLatestVersion = false) {
|
|
|
|
const hasValidRepoUrl = !!params.repo;
|
2025-04-08 12:51:36 +12:00
|
|
|
return useQuery({
|
2025-07-13 10:37:43 +12:00
|
|
|
queryKey: [
|
|
|
|
'helm-chart-values',
|
|
|
|
params.repo,
|
|
|
|
params.chart,
|
|
|
|
// if the latest version is fetched, use the latest version key to cache the latest version
|
|
|
|
isLatestVersion ? 'latest' : params.version,
|
|
|
|
],
|
2025-06-05 13:13:45 +12:00
|
|
|
queryFn: () => getHelmChartValues(params),
|
2025-07-13 10:37:43 +12:00
|
|
|
enabled: !!params.chart && hasValidRepoUrl,
|
2025-04-08 12:51:36 +12:00
|
|
|
select: (data) => ({
|
|
|
|
values: data,
|
|
|
|
}),
|
2025-07-13 10:37:43 +12:00
|
|
|
retry: 1,
|
2025-06-05 13:13:45 +12:00
|
|
|
staleTime: 60 * 1000 * 20, // 60 minutes, because values are not expected to change often
|
2025-04-08 12:51:36 +12:00
|
|
|
...withGlobalError('Unable to get Helm chart values'),
|
|
|
|
});
|
|
|
|
}
|