store/cockpitApi: support getting composes
Reads all of the image entries under the cache.
This commit is contained in:
parent
449aad5306
commit
0775acdb4d
2 changed files with 101 additions and 0 deletions
|
|
@ -40,6 +40,14 @@ export const useComposeBlueprintMutation = process.env.IS_ON_PREMISE
|
|||
? cockpitQueries.useComposeBlueprintMutation
|
||||
: imageBuilderQueries.useComposeBlueprintMutation;
|
||||
|
||||
export const useGetComposesQuery = process.env.IS_ON_PREMISE
|
||||
? cockpitQueries.useGetComposesQuery
|
||||
: imageBuilderQueries.useGetComposesQuery;
|
||||
|
||||
export const useGetBlueprintComposesQuery = process.env.IS_ON_PREMISE
|
||||
? cockpitQueries.useGetBlueprintComposesQuery
|
||||
: imageBuilderQueries.useGetBlueprintComposesQuery;
|
||||
|
||||
export const useBackendPrefetch = process.env.IS_ON_PREMISE
|
||||
? cockpitApi.usePrefetch
|
||||
: imageBuilderApi.usePrefetch;
|
||||
|
|
|
|||
|
|
@ -18,10 +18,15 @@ import {
|
|||
ComposeBlueprintApiResponse,
|
||||
ComposeBlueprintApiArg,
|
||||
CreateBlueprintRequest,
|
||||
ComposesResponseItem,
|
||||
GetArchitecturesApiResponse,
|
||||
GetArchitecturesApiArg,
|
||||
GetBlueprintsApiArg,
|
||||
GetBlueprintsApiResponse,
|
||||
GetBlueprintComposesApiArg,
|
||||
GetBlueprintComposesApiResponse,
|
||||
GetComposesApiArg,
|
||||
GetComposesApiResponse,
|
||||
DeleteBlueprintApiResponse,
|
||||
DeleteBlueprintApiArg,
|
||||
BlueprintItem,
|
||||
|
|
@ -45,6 +50,37 @@ const getBlueprintsPath = async () => {
|
|||
return `${user.home}/${BLUEPRINTS_DIR}`;
|
||||
};
|
||||
|
||||
const readComposes = async (bpID: string) => {
|
||||
const blueprintsDir = await getBlueprintsPath();
|
||||
let composes: ComposesResponseItem[] = [];
|
||||
const bpInfo = await fsinfo(
|
||||
path.join(blueprintsDir, bpID),
|
||||
['entries', 'mtime'],
|
||||
{
|
||||
superuser: 'try',
|
||||
}
|
||||
);
|
||||
const bpEntries = Object.entries(bpInfo?.entries || {});
|
||||
for (const entry of bpEntries) {
|
||||
if (entry[0] === `${bpID}.json`) {
|
||||
continue;
|
||||
}
|
||||
const composeReq = await cockpit
|
||||
.file(path.join(blueprintsDir, bpID, entry[0]))
|
||||
.read();
|
||||
composes = [
|
||||
...composes,
|
||||
{
|
||||
id: entry[0],
|
||||
request: JSON.parse(composeReq),
|
||||
created_at: new Date(entry[1]!.mtime * 1000).toString(),
|
||||
blueprint_id: bpID,
|
||||
},
|
||||
];
|
||||
}
|
||||
return composes;
|
||||
};
|
||||
|
||||
export const cockpitApi = emptyCockpitApi.injectEndpoints({
|
||||
endpoints: (builder) => {
|
||||
return {
|
||||
|
|
@ -327,6 +363,61 @@ export const cockpitApi = emptyCockpitApi.injectEndpoints({
|
|||
}
|
||||
},
|
||||
}),
|
||||
getComposes: builder.query<GetComposesApiResponse, GetComposesApiArg>({
|
||||
queryFn: async () => {
|
||||
try {
|
||||
const blueprintsDir = await getBlueprintsPath();
|
||||
const info = await fsinfo(blueprintsDir, ['entries'], {
|
||||
superuser: 'try',
|
||||
});
|
||||
let composes: ComposesResponseItem[] = [];
|
||||
const entries = Object.entries(info?.entries || {});
|
||||
for (const entry of entries) {
|
||||
composes = composes.concat(await readComposes(entry[0]));
|
||||
}
|
||||
return {
|
||||
data: {
|
||||
meta: {
|
||||
count: composes.length,
|
||||
},
|
||||
links: {
|
||||
first: composes.length > 0 ? composes[0].id : '',
|
||||
last:
|
||||
composes.length > 0 ? composes[composes.length - 1].id : '',
|
||||
},
|
||||
data: composes,
|
||||
},
|
||||
};
|
||||
} catch (error) {
|
||||
return { error };
|
||||
}
|
||||
},
|
||||
}),
|
||||
getBlueprintComposes: builder.query<
|
||||
GetBlueprintComposesApiResponse,
|
||||
GetBlueprintComposesApiArg
|
||||
>({
|
||||
queryFn: async (queryArgs) => {
|
||||
try {
|
||||
const composes = await readComposes(queryArgs.id);
|
||||
return {
|
||||
data: {
|
||||
meta: {
|
||||
count: composes.length,
|
||||
},
|
||||
links: {
|
||||
first: composes.length > 0 ? composes[0].id : '',
|
||||
last:
|
||||
composes.length > 0 ? composes[composes.length - 1].id : '',
|
||||
},
|
||||
data: composes,
|
||||
},
|
||||
};
|
||||
} catch (error) {
|
||||
return { error };
|
||||
}
|
||||
},
|
||||
}),
|
||||
};
|
||||
},
|
||||
});
|
||||
|
|
@ -341,4 +432,6 @@ export const {
|
|||
useGetOscapProfilesQuery,
|
||||
useListSnapshotsByDateMutation,
|
||||
useComposeBlueprintMutation,
|
||||
useGetComposesQuery,
|
||||
useGetBlueprintComposesQuery,
|
||||
} = cockpitApi;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue