mirror of
https://github.com/portainer/portainer.git
synced 2025-08-02 20:35:25 +02:00
refactor(k8s): namespace core logic (#12142)
Co-authored-by: testA113 <aliharriss1995@gmail.com> Co-authored-by: Anthony Lapenna <anthony.lapenna@portainer.io> Co-authored-by: James Carppe <85850129+jamescarppe@users.noreply.github.com> Co-authored-by: Ali <83188384+testA113@users.noreply.github.com>
This commit is contained in:
parent
da010f3d08
commit
ea228c3d6d
276 changed files with 9241 additions and 3361 deletions
77
app/react/kubernetes/configs/queries/useDeleteConfigMaps.ts
Normal file
77
app/react/kubernetes/configs/queries/useDeleteConfigMaps.ts
Normal file
|
@ -0,0 +1,77 @@
|
|||
import { useMutation } from '@tanstack/react-query';
|
||||
|
||||
import { queryClient, withGlobalError } from '@/react-tools/react-query';
|
||||
import axios from '@/portainer/services/axios';
|
||||
import { EnvironmentId } from '@/react/portainer/environments/types';
|
||||
import {
|
||||
error as notifyError,
|
||||
notifySuccess,
|
||||
} from '@/portainer/services/notifications';
|
||||
import { isFulfilled, isRejected } from '@/portainer/helpers/promise-utils';
|
||||
import { pluralize } from '@/portainer/helpers/strings';
|
||||
|
||||
import { parseKubernetesAxiosError } from '../../axiosError';
|
||||
|
||||
import { configMapQueryKeys } from './query-keys';
|
||||
|
||||
export function useDeleteConfigMaps(environmentId: EnvironmentId) {
|
||||
return useMutation(
|
||||
async (configMaps: { namespace: string; name: string }[]) => {
|
||||
const promises = await Promise.allSettled(
|
||||
configMaps.map(({ namespace, name }) =>
|
||||
deleteConfigMap(environmentId, namespace, name)
|
||||
)
|
||||
);
|
||||
const successfulConfigMaps = promises
|
||||
.filter(isFulfilled)
|
||||
.map((_, index) => configMaps[index].name);
|
||||
const failedConfigMaps = promises
|
||||
.filter(isRejected)
|
||||
.map(({ reason }, index) => ({
|
||||
name: configMaps[index].name,
|
||||
reason,
|
||||
}));
|
||||
return { failedConfigMaps, successfulConfigMaps };
|
||||
},
|
||||
{
|
||||
...withGlobalError('Unable to remove ConfigMaps'),
|
||||
onSuccess: ({ failedConfigMaps, successfulConfigMaps }) => {
|
||||
// Promise.allSettled can also resolve with errors, so check for errors here
|
||||
// show an error message for each configmap that failed to delete
|
||||
failedConfigMaps.forEach(({ name, reason }) => {
|
||||
notifyError(
|
||||
`Failed to remove ConfigMap '${name}'`,
|
||||
new Error(reason.message) as Error
|
||||
);
|
||||
});
|
||||
// show one summary message for all successful deletes
|
||||
if (successfulConfigMaps.length) {
|
||||
notifySuccess(
|
||||
`${pluralize(
|
||||
successfulConfigMaps.length,
|
||||
'ConfigMap'
|
||||
)} successfully removed`,
|
||||
successfulConfigMaps.join(', ')
|
||||
);
|
||||
}
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: configMapQueryKeys.configMapsForCluster(environmentId),
|
||||
});
|
||||
},
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
async function deleteConfigMap(
|
||||
environmentId: EnvironmentId,
|
||||
namespace: string,
|
||||
name: string
|
||||
) {
|
||||
try {
|
||||
await axios.delete(
|
||||
`/endpoints/${environmentId}/kubernetes/api/v1/namespaces/${namespace}/configmaps/${name}`
|
||||
);
|
||||
} catch (e) {
|
||||
throw parseKubernetesAxiosError(e, 'Unable to remove ConfigMap');
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue