2022-07-26 19:44:08 +00:00
|
|
|
import { useQuery } from 'react-query';
|
|
|
|
|
|
|
|
import axios, { parseAxiosError } from '@/portainer/services/axios';
|
2022-10-23 06:53:25 +00:00
|
|
|
import { EnvironmentId } from '@/react/portainer/environments/types';
|
2022-07-26 19:44:08 +00:00
|
|
|
import { withError } from '@/react-tools/react-query';
|
|
|
|
|
|
|
|
import { azureErrorParser } from '../services/utils';
|
|
|
|
import { Subscription } from '../types';
|
|
|
|
|
|
|
|
import { queryKeys } from './query-keys';
|
|
|
|
import { buildSubscriptionsUrl } from './utils';
|
|
|
|
|
|
|
|
export function useSubscriptions(environmentId: EnvironmentId) {
|
|
|
|
return useQuery(
|
|
|
|
queryKeys.subscriptions(environmentId),
|
|
|
|
() => getSubscriptions(environmentId),
|
|
|
|
{
|
|
|
|
...withError('Unable to retrieve Azure subscriptions'),
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getSubscriptions(environmentId: EnvironmentId) {
|
|
|
|
try {
|
|
|
|
const { data } = await axios.get<{ value: Subscription[] }>(
|
|
|
|
buildSubscriptionsUrl(environmentId),
|
|
|
|
{ params: { 'api-version': '2016-06-01' } }
|
|
|
|
);
|
|
|
|
return data.value;
|
|
|
|
} catch (e) {
|
|
|
|
throw parseAxiosError(
|
|
|
|
e as Error,
|
|
|
|
'Unable to retrieve subscriptions',
|
|
|
|
azureErrorParser
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|