Skip to content

Commit a104e5a

Browse files
[Ingest Manager] Support registration of server side callbacks for Create Datasource API (#69428)
* Ingest: Expose `registerExternalCallback()` method out of Ingest server `start` lifecycle * Ingest: Add support for External Callbacks on REST `createDatasourceHandler()` * Ingest: expose DatasourceServices to Plugin start interface * Endpoint: Added Endpoint Ingest handler for Create Datasources - Also moved the temporary logic from the middleware to the handler (still temporary) Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
1 parent 904be02 commit a104e5a

File tree

15 files changed

+553
-48
lines changed

15 files changed

+553
-48
lines changed

x-pack/plugins/ingest_manager/server/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export {
1111
IngestManagerSetupContract,
1212
IngestManagerSetupDeps,
1313
IngestManagerStartContract,
14+
ExternalCallback,
1415
} from './plugin';
1516

1617
export const config = {
@@ -42,6 +43,8 @@ export const config = {
4243

4344
export type IngestManagerConfigType = TypeOf<typeof config.schema>;
4445

46+
export { DatasourceServiceInterface } from './services/datasource';
47+
4548
export const plugin = (initializerContext: PluginInitializerContext) => {
4649
return new IngestManagerPlugin(initializerContext);
4750
};
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
import { loggingSystemMock, savedObjectsServiceMock } from 'src/core/server/mocks';
8+
import { IngestManagerAppContext } from './plugin';
9+
import { encryptedSavedObjectsMock } from '../../encrypted_saved_objects/server/mocks';
10+
import { securityMock } from '../../security/server/mocks';
11+
import { DatasourceServiceInterface } from './services/datasource';
12+
13+
export const createAppContextStartContractMock = (): IngestManagerAppContext => {
14+
return {
15+
encryptedSavedObjectsStart: encryptedSavedObjectsMock.createStart(),
16+
savedObjects: savedObjectsServiceMock.createStartContract(),
17+
security: securityMock.createSetup(),
18+
logger: loggingSystemMock.create().get(),
19+
isProductionMode: true,
20+
kibanaVersion: '8.0.0',
21+
};
22+
};
23+
24+
export const createDatasourceServiceMock = () => {
25+
return {
26+
assignPackageStream: jest.fn(),
27+
buildDatasourceFromPackage: jest.fn(),
28+
bulkCreate: jest.fn(),
29+
create: jest.fn(),
30+
delete: jest.fn(),
31+
get: jest.fn(),
32+
getByIDs: jest.fn(),
33+
list: jest.fn(),
34+
update: jest.fn(),
35+
} as jest.Mocked<DatasourceServiceInterface>;
36+
};

x-pack/plugins/ingest_manager/server/plugin.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,14 @@ import {
4545
registerSettingsRoutes,
4646
registerAppRoutes,
4747
} from './routes';
48-
import { IngestManagerConfigType } from '../common';
48+
import { IngestManagerConfigType, NewDatasource } from '../common';
4949
import {
5050
appContextService,
5151
licenseService,
5252
ESIndexPatternSavedObjectService,
5353
ESIndexPatternService,
5454
AgentService,
55+
datasourceService,
5556
} from './services';
5657
import { getAgentStatusById } from './services/agents';
5758
import { CloudSetup } from '../../cloud/server';
@@ -92,12 +93,31 @@ const allSavedObjectTypes = [
9293
ENROLLMENT_API_KEYS_SAVED_OBJECT_TYPE,
9394
];
9495

96+
/**
97+
* Callbacks supported by the Ingest plugin
98+
*/
99+
export type ExternalCallback = [
100+
'datasourceCreate',
101+
(newDatasource: NewDatasource) => Promise<NewDatasource>
102+
];
103+
104+
export type ExternalCallbacksStorage = Map<ExternalCallback[0], Set<ExternalCallback[1]>>;
105+
95106
/**
96107
* Describes public IngestManager plugin contract returned at the `startup` stage.
97108
*/
98109
export interface IngestManagerStartContract {
99110
esIndexPatternService: ESIndexPatternService;
100111
agentService: AgentService;
112+
/**
113+
* Services for Ingest's Datasources
114+
*/
115+
datasourceService: typeof datasourceService;
116+
/**
117+
* Register callbacks for inclusion in ingest API processing
118+
* @param args
119+
*/
120+
registerExternalCallback: (...args: ExternalCallback) => void;
101121
}
102122

103123
export class IngestManagerPlugin
@@ -237,6 +257,10 @@ export class IngestManagerPlugin
237257
agentService: {
238258
getAgentStatusById,
239259
},
260+
datasourceService,
261+
registerExternalCallback: (...args: ExternalCallback) => {
262+
return appContextService.addExternalCallback(...args);
263+
},
240264
};
241265
}
242266

0 commit comments

Comments
 (0)