2024-04-14 14:54:25 +00:00
|
|
|
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
|
2022-09-21 04:49:42 +00:00
|
|
|
|
2022-10-23 06:53:25 +00:00
|
|
|
import { EnvironmentId } from '@/react/portainer/environments/types';
|
2022-09-21 04:49:42 +00:00
|
|
|
import {
|
|
|
|
mutationOptions,
|
2024-10-01 01:15:51 +00:00
|
|
|
withGlobalError,
|
2022-09-21 04:49:42 +00:00
|
|
|
withInvalidate,
|
|
|
|
} from '@/react-tools/react-query';
|
2022-10-06 02:24:59 +00:00
|
|
|
|
2022-09-21 04:49:42 +00:00
|
|
|
import {
|
|
|
|
getIngresses,
|
|
|
|
getIngress,
|
|
|
|
createIngress,
|
|
|
|
deleteIngresses,
|
|
|
|
updateIngress,
|
|
|
|
getIngressControllers,
|
|
|
|
} from './service';
|
|
|
|
import { DeleteIngressesRequest, Ingress } from './types';
|
|
|
|
|
2024-10-01 01:15:51 +00:00
|
|
|
const queryKeys = {
|
|
|
|
base: ['environments', 'kubernetes', 'ingress'] as const,
|
|
|
|
clusterIngresses: (environmentId: EnvironmentId) =>
|
|
|
|
[...queryKeys.base, String(environmentId)] as const,
|
|
|
|
namespaceIngresses: (
|
2022-09-21 04:49:42 +00:00
|
|
|
environmentId: EnvironmentId,
|
|
|
|
namespace: string,
|
|
|
|
ingress: string
|
2024-10-01 01:15:51 +00:00
|
|
|
) => [...queryKeys.base, String(environmentId), namespace, ingress] as const,
|
|
|
|
ingress: (environmentId: EnvironmentId, namespace: string, name: string) =>
|
|
|
|
[...queryKeys.base, String(environmentId), namespace, name] as const,
|
|
|
|
ingressControllers: (environmentId: EnvironmentId, namespace: string) => [
|
|
|
|
...queryKeys.base,
|
|
|
|
String(environmentId),
|
|
|
|
namespace,
|
|
|
|
'ingresscontrollers',
|
|
|
|
],
|
2022-09-21 04:49:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export function useIngress(
|
|
|
|
environmentId: EnvironmentId,
|
|
|
|
namespace: string,
|
|
|
|
name: string
|
|
|
|
) {
|
|
|
|
return useQuery(
|
2024-10-01 01:15:51 +00:00
|
|
|
queryKeys.ingress(environmentId, namespace, name),
|
2022-09-21 04:49:42 +00:00
|
|
|
async () => {
|
|
|
|
const ing = await getIngress(environmentId, namespace, name);
|
|
|
|
return ing;
|
|
|
|
},
|
|
|
|
{
|
2024-10-01 01:15:51 +00:00
|
|
|
...withGlobalError('Unable to get ingress'),
|
2022-09-21 04:49:42 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function useIngresses(
|
|
|
|
environmentId: EnvironmentId,
|
2024-10-01 01:15:51 +00:00
|
|
|
options?: {
|
|
|
|
autoRefreshRate?: number;
|
|
|
|
enabled?: boolean;
|
|
|
|
withServices?: boolean;
|
|
|
|
}
|
2022-09-21 04:49:42 +00:00
|
|
|
) {
|
2024-10-01 01:15:51 +00:00
|
|
|
const { enabled, autoRefreshRate, ...params } = options ?? {};
|
2022-10-06 02:24:59 +00:00
|
|
|
|
2024-10-01 01:15:51 +00:00
|
|
|
return useQuery(
|
|
|
|
['environments', environmentId, 'kubernetes', 'ingress', params],
|
|
|
|
async () => getIngresses(environmentId, params),
|
2022-09-21 04:49:42 +00:00
|
|
|
{
|
2024-10-01 01:15:51 +00:00
|
|
|
...withGlobalError('Unable to get ingresses'),
|
|
|
|
refetchInterval: autoRefreshRate,
|
|
|
|
enabled,
|
2022-09-21 04:49:42 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function useCreateIngress() {
|
|
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation(
|
|
|
|
({
|
|
|
|
environmentId,
|
|
|
|
ingress,
|
|
|
|
}: {
|
|
|
|
environmentId: EnvironmentId;
|
|
|
|
ingress: Ingress;
|
|
|
|
}) => createIngress(environmentId, ingress),
|
|
|
|
mutationOptions(
|
2024-10-01 01:15:51 +00:00
|
|
|
withGlobalError('Unable to create ingress controller'),
|
|
|
|
withInvalidate(queryClient, [queryKeys.base])
|
2022-09-21 04:49:42 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function useUpdateIngress() {
|
|
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation(
|
|
|
|
({
|
|
|
|
environmentId,
|
|
|
|
ingress,
|
|
|
|
}: {
|
|
|
|
environmentId: EnvironmentId;
|
|
|
|
ingress: Ingress;
|
|
|
|
}) => updateIngress(environmentId, ingress),
|
|
|
|
mutationOptions(
|
2024-10-01 01:15:51 +00:00
|
|
|
withGlobalError('Unable to update ingress controller'),
|
|
|
|
withInvalidate(queryClient, [queryKeys.base])
|
2022-09-21 04:49:42 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function useDeleteIngresses() {
|
|
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation(
|
|
|
|
({
|
|
|
|
environmentId,
|
|
|
|
data,
|
|
|
|
}: {
|
|
|
|
environmentId: EnvironmentId;
|
|
|
|
data: DeleteIngressesRequest;
|
|
|
|
}) => deleteIngresses(environmentId, data),
|
|
|
|
mutationOptions(
|
2024-10-01 01:15:51 +00:00
|
|
|
withGlobalError('Unable to update ingress controller'),
|
|
|
|
withInvalidate(queryClient, [queryKeys.base])
|
2022-09-21 04:49:42 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ingress Controllers
|
|
|
|
*/
|
|
|
|
export function useIngressControllers(
|
|
|
|
environmentId: EnvironmentId,
|
2023-10-11 19:32:02 +00:00
|
|
|
namespace?: string,
|
|
|
|
allowedOnly?: boolean
|
2022-09-21 04:49:42 +00:00
|
|
|
) {
|
|
|
|
return useQuery(
|
2024-10-01 01:15:51 +00:00
|
|
|
queryKeys.ingressControllers(environmentId, namespace ?? ''),
|
2023-06-26 04:21:19 +00:00
|
|
|
async () =>
|
2023-10-11 19:32:02 +00:00
|
|
|
namespace
|
|
|
|
? getIngressControllers(environmentId, namespace, allowedOnly)
|
|
|
|
: [],
|
2022-09-21 04:49:42 +00:00
|
|
|
{
|
|
|
|
enabled: !!namespace,
|
2024-10-01 01:15:51 +00:00
|
|
|
...withGlobalError('Unable to get ingress controllers'),
|
2022-09-21 04:49:42 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|