From bc1564eddbfede61d219f3fbcace5907b1af59fa Mon Sep 17 00:00:00 2001 From: Gianluca Zuccarelli Date: Tue, 28 Jan 2025 15:33:33 +0000 Subject: [PATCH] store/cockpit: add content sources api Add a `contentSourcesApi` for the on-prem frontend. We need to add a small workaround and put these endpoints under the `cockpitApi` reducer. since RTK query here, doesn't like splitting out apis when they are fundamentally the same. To workaround this we can will just chain the endpoints so: `emptyCockpitApi` -> `contentSourcesApi` -> `cockpitApi` This allows us to keep the `contentSourcesApi` separate so we can export some of the endpoints so that the `cockpitApi` doesn't become a monolith. --- .../steps/Review/ReviewStepTextLists.tsx | 2 +- src/store/backendApi.ts | 5 --- src/store/cockpit/cockpitApi.ts | 32 +++++++------------ src/store/cockpit/contentSourcesApi.ts | 27 ++++++++++++++++ src/store/contentSourcesApi.ts | 21 +++++++++++- 5 files changed, 60 insertions(+), 27 deletions(-) create mode 100644 src/store/cockpit/contentSourcesApi.ts diff --git a/src/Components/CreateImageWizard/steps/Review/ReviewStepTextLists.tsx b/src/Components/CreateImageWizard/steps/Review/ReviewStepTextLists.tsx index 2d4c3bc3..a285bf55 100644 --- a/src/Components/CreateImageWizard/steps/Review/ReviewStepTextLists.tsx +++ b/src/Components/CreateImageWizard/steps/Review/ReviewStepTextLists.tsx @@ -35,7 +35,7 @@ import { targetOptions, UNIT_GIB, } from '../../../../constants'; -import { useListSnapshotsByDateMutation } from '../../../../store/backendApi'; +import { useListSnapshotsByDateMutation } from '../../../../store/contentSourcesApi'; import { useAppSelector } from '../../../../store/hooks'; import { useGetSourceListQuery } from '../../../../store/provisioningApi'; import { useShowActivationKeyQuery } from '../../../../store/rhsmApi'; diff --git a/src/store/backendApi.ts b/src/store/backendApi.ts index 1ae4a6ac..2985178c 100644 --- a/src/store/backendApi.ts +++ b/src/store/backendApi.ts @@ -1,6 +1,5 @@ import * as cockpitQueries from './cockpit/cockpitApi'; import { cockpitApi } from './cockpit/enhancedCockpitApi'; -import * as sourcesQueries from './service/contentSourcesApi'; import { imageBuilderApi } from './service/enhancedImageBuilderApi'; import * as serviceQueries from './service/imageBuilderApi'; @@ -32,10 +31,6 @@ export const useGetOscapProfilesQuery = process.env.IS_ON_PREMISE ? cockpitQueries.useGetOscapProfilesQuery : serviceQueries.useGetOscapProfilesQuery; -export const useListSnapshotsByDateMutation = process.env.IS_ON_PREMISE - ? cockpitQueries.useListSnapshotsByDateMutation - : sourcesQueries.useListSnapshotsByDateMutation; - export const useComposeBlueprintMutation = process.env.IS_ON_PREMISE ? cockpitQueries.useComposeBlueprintMutation : serviceQueries.useComposeBlueprintMutation; diff --git a/src/store/cockpit/cockpitApi.ts b/src/store/cockpit/cockpitApi.ts index 557613be..dd47e31e 100644 --- a/src/store/cockpit/cockpitApi.ts +++ b/src/store/cockpit/cockpitApi.ts @@ -10,14 +10,16 @@ import cockpit from 'cockpit'; import { fsinfo } from 'cockpit/fsinfo'; import { v4 as uuidv4 } from 'uuid'; -import { emptyCockpitApi } from './emptyCockpitApi'; +// We have to work around RTK query here, since it doesn't like splitting +// out the same api into two separate apis. So, instead, we can just +// inherit/import the `contentSourcesApi` and build on top of that. +// This is fine since all the api endpoints for on-prem should query +// the same unix socket. This allows us to split out the code a little +// bit so that the `cockpitApi` doesn't become a monolith. +import { contentSourcesApi } from './contentSourcesApi'; import { mapHostedToOnPrem } from '../../Components/Blueprints/helpers/onPremToHostedBlueprintMapper'; import { BLUEPRINTS_DIR } from '../../constants'; -import { - ListSnapshotsByDateApiArg, - ListSnapshotsByDateApiResponse, -} from '../service/contentSourcesApi'; import { ComposeBlueprintApiResponse, ComposeBlueprintApiArg, @@ -84,7 +86,7 @@ const readComposes = async (bpID: string) => { return composes; }; -export const cockpitApi = emptyCockpitApi.injectEndpoints({ +export const cockpitApi = contentSourcesApi.injectEndpoints({ endpoints: (builder) => { return { getArchitectures: builder.query< @@ -278,19 +280,6 @@ export const cockpitApi = emptyCockpitApi.injectEndpoints({ }; }, }), - // add an empty response for now - // just so we can step through the create - // image wizard for on prem - listSnapshotsByDate: builder.mutation< - ListSnapshotsByDateApiResponse, - ListSnapshotsByDateApiArg - >({ - queryFn: () => ({ - data: { - data: [], - }, - }), - }), composeBlueprint: builder.mutation< ComposeBlueprintApiResponse, ComposeBlueprintApiArg @@ -455,6 +444,10 @@ export const cockpitApi = emptyCockpitApi.injectEndpoints({ }), }; }, + // since we are inheriting some endpoints, + // we want to make sure that we don't override + // any existing endpoints. + overrideExisting: 'throw', }); export const { @@ -465,7 +458,6 @@ export const { useCreateBlueprintMutation, useDeleteBlueprintMutation, useGetOscapProfilesQuery, - useListSnapshotsByDateMutation, useComposeBlueprintMutation, useGetComposesQuery, useGetBlueprintComposesQuery, diff --git a/src/store/cockpit/contentSourcesApi.ts b/src/store/cockpit/contentSourcesApi.ts new file mode 100644 index 00000000..6aaba9a6 --- /dev/null +++ b/src/store/cockpit/contentSourcesApi.ts @@ -0,0 +1,27 @@ +import { emptyCockpitApi } from './emptyCockpitApi'; + +import type { + ListSnapshotsByDateApiArg, + ListSnapshotsByDateApiResponse, +} from '../service/contentSourcesApi'; + +export const contentSourcesApi = emptyCockpitApi.injectEndpoints({ + endpoints: (builder) => ({ + // add an empty response for now + // just so we can step through the create + // image wizard for on prem + listSnapshotsByDate: builder.mutation< + ListSnapshotsByDateApiResponse, + ListSnapshotsByDateApiArg + >({ + queryFn: () => ({ + data: { + data: [], + }, + }), + }), + }), + overrideExisting: false, +}); + +export const { useListSnapshotsByDateMutation } = contentSourcesApi; diff --git a/src/store/contentSourcesApi.ts b/src/store/contentSourcesApi.ts index 9f39f595..e840d87a 100644 --- a/src/store/contentSourcesApi.ts +++ b/src/store/contentSourcesApi.ts @@ -1 +1,20 @@ -export * from './service/contentSourcesApi'; +import * as cockpitQueries from './cockpit/contentSourcesApi'; +import * as serviceQueries from './service/contentSourcesApi'; + +export const useListSnapshotsByDateMutation = process.env.IS_ON_PREMISE + ? cockpitQueries.useListSnapshotsByDateMutation + : serviceQueries.useListSnapshotsByDateMutation; + +export const { + useListFeaturesQuery, + useSearchRpmMutation, + useSearchPackageGroupMutation, + useListRepositoriesQuery, + useCreateRepositoryMutation, + useBulkImportRepositoriesMutation, + useListRepositoriesRpmsQuery, + contentSourcesApi, +} = serviceQueries; + +// we need to re-export all the types +export type * from './service/contentSourcesApi';