2022-09-21 04:49:42 +00:00
|
|
|
import { useQuery } from 'react-query';
|
|
|
|
|
2022-10-23 06:53:25 +00:00
|
|
|
import { EnvironmentId } from '@/react/portainer/environments/types';
|
2022-09-21 04:49:42 +00:00
|
|
|
import { error as notifyError } from '@/portainer/services/notifications';
|
2022-11-07 06:03:11 +00:00
|
|
|
|
|
|
|
import { getIngresses } from '../ingresses/service';
|
2022-09-21 04:49:42 +00:00
|
|
|
|
|
|
|
import { getNamespaces, getNamespace } from './service';
|
2022-10-27 03:14:54 +00:00
|
|
|
import { Namespaces } from './types';
|
2022-09-21 04:49:42 +00:00
|
|
|
|
|
|
|
export function useNamespaces(environmentId: EnvironmentId) {
|
|
|
|
return useQuery(
|
|
|
|
['environments', environmentId, 'kubernetes', 'namespaces'],
|
2022-10-27 03:14:54 +00:00
|
|
|
async () => {
|
|
|
|
const namespaces = await getNamespaces(environmentId);
|
|
|
|
const settledNamespacesPromise = await Promise.allSettled(
|
|
|
|
Object.keys(namespaces).map((namespace) =>
|
|
|
|
getIngresses(environmentId, namespace).then(() => namespace)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
const ns: Namespaces = {};
|
|
|
|
settledNamespacesPromise.forEach((namespace) => {
|
|
|
|
if (namespace.status === 'fulfilled') {
|
|
|
|
ns[namespace.value] = namespaces[namespace.value];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return ns;
|
|
|
|
},
|
2022-09-21 04:49:42 +00:00
|
|
|
{
|
|
|
|
onError: (err) => {
|
|
|
|
notifyError('Failure', err as Error, 'Unable to get namespaces.');
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function useNamespace(environmentId: EnvironmentId, namespace: string) {
|
|
|
|
return useQuery(
|
|
|
|
['environments', environmentId, 'kubernetes', 'namespaces', namespace],
|
|
|
|
() => getNamespace(environmentId, namespace),
|
|
|
|
{
|
|
|
|
onError: (err) => {
|
|
|
|
notifyError('Failure', err as Error, 'Unable to get namespace.');
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|