store/cockpitApi: add delete endpoint

This commit is contained in:
Gianluca Zuccarelli 2024-12-20 13:58:33 +00:00 committed by Lucas Garfield
parent ebe387992c
commit 79532e4ac5
4 changed files with 81 additions and 5 deletions

View file

@ -1,3 +1,5 @@
import path from 'path';
// Note: for the on-prem version of the frontend we have configured
// this so that we check `node_modules` and `pkg/lib` for packages.
// To get around this for the hosted service, we have configured
@ -14,6 +16,8 @@ import {
GetArchitecturesApiArg,
GetBlueprintsApiArg,
GetBlueprintsApiResponse,
DeleteBlueprintApiResponse,
DeleteBlueprintApiArg,
BlueprintItem,
} from './imageBuilderApi';
@ -45,18 +49,18 @@ export const cockpitApi = emptyCockpitApi.injectEndpoints({
>({
queryFn: async () => {
try {
const path = await getBlueprintsPath();
const blueprintsDir = await getBlueprintsPath();
// we probably don't need any more information other
// than the entries from the directory
const info = await fsinfo(path, ['entries'], {
const info = await fsinfo(blueprintsDir, ['entries'], {
superuser: 'try',
});
const entries = Object.entries(info?.entries || {});
const blueprints: BlueprintItem[] = await Promise.all(
entries.map(async ([filename]) => {
const file = cockpit.file(`${path}/${filename}`);
const file = cockpit.file(path.join(blueprintsDir, filename));
const contents = await file.read();
const parsed = toml.parse(contents);
@ -89,8 +93,33 @@ export const cockpitApi = emptyCockpitApi.injectEndpoints({
}
},
}),
deleteBlueprint: builder.mutation<
DeleteBlueprintApiResponse,
DeleteBlueprintApiArg
>({
queryFn: async ({ id: filename }) => {
try {
const blueprintsDir = await getBlueprintsPath();
const filepath = path.join(blueprintsDir, filename);
await cockpit.spawn(['rm', filepath], {
superuser: 'try',
});
return {
data: {},
};
} catch (error) {
return { error };
}
},
}),
};
},
});
export const { useGetBlueprintsQuery, useGetArchitecturesQuery } = cockpitApi;
export const {
useGetBlueprintsQuery,
useDeleteBlueprintMutation,
useGetArchitecturesQuery,
} = cockpitApi;

View file

@ -0,0 +1,42 @@
import { addNotification } from '@redhat-cloud-services/frontend-components-notifications/redux';
import { cockpitApi } from './cockpitApi';
import { errorMessage } from './enhancedImageBuilderApi';
const enhancedApi = cockpitApi.enhanceEndpoints({
addTagTypes: ['Blueprints'],
endpoints: {
getBlueprints: {
providesTags: () => {
return [{ type: 'Blueprints' }];
},
},
deleteBlueprint: {
invalidatesTags: [{ type: 'Blueprints' }],
onQueryStarted: async (_, { dispatch, queryFulfilled }) => {
queryFulfilled
.then(() => {
dispatch(
addNotification({
variant: 'success',
title: 'Blueprint was deleted',
})
);
})
.catch((err) => {
dispatch(
addNotification({
variant: 'danger',
title: 'Blueprint could not be deleted',
description: `Status code ${err.error.status}: ${errorMessage(
err
)}`,
})
);
});
},
},
},
});
export { enhancedApi as cockpitApi };

View file

@ -3,7 +3,7 @@ import { addNotification } from '@redhat-cloud-services/frontend-components-noti
import { imageBuilderApi } from './imageBuilderApi';
/* eslint-disable @typescript-eslint/no-explicit-any */
const errorMessage = (err: any) => {
export const errorMessage = (err: any) => {
let msg = err.error.statusText;
if (
err.error.data?.errors &&

View file

@ -22,4 +22,9 @@ export default {
close: () => {},
};
},
spawn: (args: string[], attributes: object): Promise<string | Uint8Array> => {
return new Promise((resolve) => {
resolve('');
});
},
};