Skip to content

Commit a47bc92

Browse files
[dashboard] update server interfaces to accept SavedObjectsClientContract instead of RequestHandlerContext (elastic#269750)
Closes elastic#261984 Dashboard interfaces currently require `RequestHandlerContext` when they really only require `SavedObjectsClientContract`. This makes plugins that want to use these interfaces outside of request scope have to mock a request scope. Since `RequestHandlerContext` is not needed by server interfaces, it would be best to narrow the interface to only require `SavedObjectsClientContract`. --------- Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
1 parent 1a27c5c commit a47bc92

11 files changed

Lines changed: 26 additions & 111 deletions

File tree

src/platform/plugins/shared/dashboard/server/api/read/read.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* License v3.0 only", or the "Server Side Public License, v 1".
88
*/
99

10-
import type { RequestHandlerContext } from '@kbn/core/server';
10+
import type { SavedObjectsClientContract } from '@kbn/core/server';
1111
import type { RequestTiming } from '@kbn/core-http-server';
1212
import type { DashboardSavedObjectAttributes } from '../../dashboard_saved_object';
1313
import { DASHBOARD_SAVED_OBJECT_TYPE } from '../../../common/constants';
@@ -16,20 +16,18 @@ import type { DashboardReadResponseBody } from './types';
1616
import type { getDashboardStateSchema } from '../dashboard_state_schemas';
1717

1818
export async function read(
19-
requestCtx: RequestHandlerContext,
19+
savedObjectsClient: SavedObjectsClientContract,
2020
dashboardStateSchema: ReturnType<typeof getDashboardStateSchema>,
2121
id: string,
2222
serverTiming?: RequestTiming,
2323
isDashboardAppRequest: boolean = false
2424
): Promise<{ body: DashboardReadResponseBody; resolveHeaders: Record<string, string> }> {
25-
const { core } = await requestCtx.resolve(['core']);
26-
2725
const {
2826
saved_object: savedObject,
2927
outcome,
3028
alias_purpose,
3129
alias_target_id,
32-
} = await core.savedObjects.client.resolve<DashboardSavedObjectAttributes>(
30+
} = await savedObjectsClient.resolve<DashboardSavedObjectAttributes>(
3331
DASHBOARD_SAVED_OBJECT_TYPE,
3432
id
3533
);

src/platform/plugins/shared/dashboard/server/api/read/register_read_route.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ export function registerReadRoute(
7777
telemetryHandler(req, usageCounter, async () => {
7878
try {
7979
const { body, resolveHeaders } = await read(
80-
ctx,
80+
(
81+
await ctx.resolve(['core'])
82+
).core.savedObjects.client,
8183
getCachedDashboardStateSchema(),
8284
req.params.id,
8385
req.serverTiming,

src/platform/plugins/shared/dashboard/server/plugin.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import type {
2424
CoreStart,
2525
Plugin,
2626
Logger,
27-
RequestHandlerContext,
27+
SavedObjectsClientContract,
2828
} from '@kbn/core/server';
2929
import { registerContentInsights } from '@kbn/content-management-content-insights-server';
3030
import type { UsageCounter } from '@kbn/usage-collection-plugin/server';
@@ -183,8 +183,8 @@ export class DashboardPlugin
183183
return {
184184
scanDashboards,
185185
client: {
186-
read: async (requestCtx: RequestHandlerContext, id: string) =>
187-
(await read(requestCtx, getCachedDashboardStateSchema(), id)).body,
186+
read: async (savedObjectsClient: SavedObjectsClientContract, id: string) =>
187+
(await read(savedObjectsClient, getCachedDashboardStateSchema(), id)).body,
188188
},
189189
};
190190
}

src/platform/plugins/shared/dashboard/server/scan_dashboards.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* License v3.0 only", or the "Server Side Public License, v 1".
88
*/
99

10-
import type { RequestHandlerContext } from '@kbn/core/server';
10+
import type { SavedObjectsClientContract } from '@kbn/core/server';
1111
import type { Reference } from '@kbn/content-management-utils';
1212
import type { DashboardSavedObjectAttributes } from './dashboard_saved_object';
1313
import { DASHBOARD_SAVED_OBJECT_TYPE } from '../common/constants';
@@ -33,12 +33,11 @@ export interface ScanDashboardsResult {
3333
}
3434

3535
export async function scanDashboards(
36-
ctx: RequestHandlerContext,
36+
savedObjectsClient: SavedObjectsClientContract,
3737
page: number,
3838
perPage: number
3939
): Promise<ScanDashboardsResult> {
40-
const { core } = await ctx.resolve(['core']);
41-
const soResponse = await core.savedObjects.client.find<DashboardSavedObjectAttributes>({
40+
const soResponse = await savedObjectsClient.find<DashboardSavedObjectAttributes>({
4241
type: DASHBOARD_SAVED_OBJECT_TYPE,
4342
fields: ['description', 'title', 'panelsJSON', 'sections'],
4443
perPage,

src/platform/plugins/shared/dashboard/server/types.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,18 @@
77
* License v3.0 only", or the "Server Side Public License, v 1".
88
*/
99

10-
import type { RequestHandlerContext } from '@kbn/core/server';
10+
import type { SavedObjectsClientContract } from '@kbn/core/server';
1111
import type { ScanDashboardsResult } from './scan_dashboards';
1212
import type { DashboardReadResponseBody } from './api';
1313

1414
/**
1515
* Client interface for dashboard CRUD operations
1616
*/
1717
export interface DashboardServerClient {
18-
read(requestCtx: RequestHandlerContext, id: string): Promise<DashboardReadResponseBody>;
18+
read(
19+
savedObjectsClient: SavedObjectsClientContract,
20+
id: string
21+
): Promise<DashboardReadResponseBody>;
1922
}
2023

2124
/** The setup contract for the Dashboard plugin on the server. */
@@ -39,7 +42,7 @@ export interface DashboardPluginStart {
3942
* @returns A promise that resolves to the {@link ScanDashboardsResult}.
4043
*/
4144
scanDashboards: (
42-
ctx: RequestHandlerContext,
45+
savedObjectsClient: SavedObjectsClientContract,
4346
page: number,
4447
perPage: number
4548
) => Promise<ScanDashboardsResult>;

x-pack/platform/plugins/shared/agent_builder_dashboards/server/attachment_types/dashboard.test.ts

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ const createAttachment = (
9292
});
9393

9494
describe('createDashboardAttachmentType', () => {
95-
it('resolves dashboard attachments with dashboard client.read', async () => {
95+
it('resolve should return dashboard attachement', async () => {
9696
const dashboardClient = createDashboardClient();
9797
const savedObjectsClient = createSavedObjectsClient();
9898
const definition = createDashboardAttachmentType({
@@ -106,17 +106,6 @@ describe('createDashboardAttachmentType', () => {
106106
savedObjectsClient,
107107
});
108108

109-
expect(dashboardClient.read).toHaveBeenCalledWith(
110-
expect.objectContaining({ resolve: expect.any(Function) }),
111-
'dashboard-1'
112-
);
113-
await expect(dashboardClient.read.mock.calls[0][0].resolve(['core'])).resolves.toEqual({
114-
core: {
115-
savedObjects: {
116-
client: savedObjectsClient,
117-
},
118-
},
119-
});
120109
expect(result).toEqual(
121110
expect.objectContaining({
122111
title: 'System Overview',
@@ -126,7 +115,7 @@ describe('createDashboardAttachmentType', () => {
126115
expect(result?.panels).toHaveLength(1);
127116
});
128117

129-
it('returns true when the saved dashboard changed after the snapshot', async () => {
118+
it('isStale should return true when the dashboard changed after the snapshot', async () => {
130119
const dashboardClient = createDashboardClient();
131120
const savedObjectsClient = createSavedObjectsClient();
132121
const definition = createDashboardAttachmentType({
@@ -148,20 +137,9 @@ describe('createDashboardAttachmentType', () => {
148137
);
149138

150139
expect(isStale).toBe(true);
151-
expect(dashboardClient.read).toHaveBeenCalledWith(
152-
expect.objectContaining({ resolve: expect.any(Function) }),
153-
'dashboard-1'
154-
);
155-
await expect(dashboardClient.read.mock.calls[0][0].resolve(['core'])).resolves.toEqual({
156-
core: {
157-
savedObjects: {
158-
client: savedObjectsClient,
159-
},
160-
},
161-
});
162140
});
163141

164-
it('returns false when the saved dashboard content matches the attachment', async () => {
142+
it('isStale should return false when the dashboard has not changed', async () => {
165143
const dashboardClient = createDashboardClient();
166144
const savedObjectsClient = createSavedObjectsClient();
167145
const definition = createDashboardAttachmentType({
@@ -181,17 +159,6 @@ describe('createDashboardAttachmentType', () => {
181159
});
182160

183161
expect(isStale).toBe(false);
184-
expect(dashboardClient.read).toHaveBeenLastCalledWith(
185-
expect.objectContaining({ resolve: expect.any(Function) }),
186-
'dashboard-1'
187-
);
188-
await expect(dashboardClient.read.mock.calls[1][0].resolve(['core'])).resolves.toEqual({
189-
core: {
190-
savedObjects: {
191-
client: savedObjectsClient,
192-
},
193-
},
194-
});
195162
});
196163

197164
it('treats legacy wrapped Lens configs as equal when checking staleness', async () => {

x-pack/platform/plugins/shared/agent_builder_dashboards/server/attachment_types/dashboard.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import {
2121
} from '@kbn/agent-builder-dashboards-common';
2222
import type { DashboardPluginStart } from '@kbn/dashboard-plugin/server';
2323
import type { Logger } from '@kbn/core/server';
24-
import { createRequestHandlerContext } from '../create_request_handler_context';
2524

2625
interface CreateDashboardAttachmentTypeOptions {
2726
logger: Logger;
@@ -51,10 +50,8 @@ export const createDashboardAttachmentType = ({
5150
if (!context.savedObjectsClient) {
5251
throw new Error('Saved objects client is required to read dashboard attachments');
5352
}
54-
// todo: this should be passed from agent builder
55-
const requestHandlerContext = createRequestHandlerContext(context.savedObjectsClient);
5653
const dashboardClient = await getDashboardClient();
57-
return dashboardClient.read(requestHandlerContext, origin);
54+
return dashboardClient.read(context.savedObjectsClient, origin);
5855
};
5956

6057
return {

x-pack/platform/plugins/shared/agent_builder_dashboards/server/create_request_handler_context.ts

Lines changed: 0 additions & 22 deletions
This file was deleted.

x-pack/platform/plugins/shared/agent_builder_dashboards/server/sml_types/dashboard.test.ts

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -182,14 +182,6 @@ describe('dashboardSmlType', () => {
182182
expect(result?.chunks[0].content).toContain('Operations');
183183
expect(result?.chunks[0].content).toContain('2 panels');
184184
expect(result?.chunks[0].content).toContain('1 sections');
185-
const requestHandlerContext = dashboardClient.read.mock.calls[0][0];
186-
await expect(requestHandlerContext.resolve(['core'])).resolves.toEqual({
187-
core: {
188-
savedObjects: {
189-
client: savedObjectsClient,
190-
},
191-
},
192-
});
193185
});
194186

195187
it('converts saved dashboards into dashboard attachments with origin', async () => {
@@ -244,14 +236,6 @@ describe('dashboardSmlType', () => {
244236
}),
245237
])
246238
);
247-
const requestHandlerContext = dashboardClient.read.mock.calls[0][0];
248-
await expect(requestHandlerContext.resolve(['core'])).resolves.toEqual({
249-
core: {
250-
savedObjects: {
251-
client: savedObjectsClient,
252-
},
253-
},
254-
});
255239
});
256240

257241
it('falls back to generic panel storage when a lens panel is already in API format', async () => {
@@ -323,13 +307,5 @@ describe('dashboardSmlType', () => {
323307
chunks: [expect.objectContaining({ type: 'dashboard', title: 'System Overview' })],
324308
})
325309
);
326-
const requestHandlerContext = dashboardClient.read.mock.calls[0][0];
327-
await expect(requestHandlerContext.resolve(['core'])).resolves.toEqual({
328-
core: {
329-
savedObjects: {
330-
client: savedObjectsClient,
331-
},
332-
},
333-
});
334310
});
335311
});

x-pack/platform/plugins/shared/agent_builder_dashboards/server/sml_types/dashboard.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import type {
1616
DashboardSection,
1717
DashboardState,
1818
} from '@kbn/dashboard-plugin/server';
19-
import { createRequestHandlerContext } from '../create_request_handler_context';
2019

2120
const DASHBOARD_SML_TYPE = 'dashboard';
2221

@@ -92,10 +91,8 @@ export const createDashboardSmlType = ({
9291

9392
getSmlData: async (originId, context) => {
9493
try {
95-
// todo: this should be passed from agent builder
96-
const requestHandlerContext = createRequestHandlerContext(context.savedObjectsClient);
9794
const dashboardClient = await getDashboardClient();
98-
const dashboard = await dashboardClient.read(requestHandlerContext, originId);
95+
const dashboard = await dashboardClient.read(context.savedObjectsClient, originId);
9996

10097
return {
10198
chunks: [
@@ -117,10 +114,8 @@ export const createDashboardSmlType = ({
117114

118115
toAttachment: async (item, context) => {
119116
try {
120-
// todo: this should be passed from agent builder
121-
const requestHandlerContext = createRequestHandlerContext(context.savedObjectsClient);
122117
const dashboardClient = await getDashboardClient();
123-
const dashboard = await dashboardClient.read(requestHandlerContext, item.origin_id);
118+
const dashboard = await dashboardClient.read(context.savedObjectsClient, item.origin_id);
124119

125120
return {
126121
type: DASHBOARD_ATTACHMENT_TYPE,

0 commit comments

Comments
 (0)