-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
Copy pathplugin.ts
95 lines (84 loc) · 2.96 KB
/
plugin.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
declare module 'kibana/server' {
interface RequestHandlerContext {
watcher?: WatcherContext;
}
}
import {
CoreSetup,
IScopedClusterClient,
Logger,
Plugin,
PluginInitializerContext,
} from 'kibana/server';
import { PLUGIN } from '../common/constants';
import { Dependencies, LicenseStatus, RouteDependencies } from './types';
import { LICENSE_CHECK_STATE } from '../../licensing/server';
import { registerSettingsRoutes } from './routes/api/settings';
import { registerIndicesRoutes } from './routes/api/indices';
import { registerLicenseRoutes } from './routes/api/license';
import { registerWatchesRoutes } from './routes/api/watches';
import { registerWatchRoutes } from './routes/api/watch';
import { registerListFieldsRoute } from './routes/api/register_list_fields_route';
import { registerLoadHistoryRoute } from './routes/api/register_load_history_route';
import { elasticsearchJsPlugin } from './lib/elasticsearch_js_plugin';
export interface WatcherContext {
client: IScopedClusterClient;
}
export class WatcherServerPlugin implements Plugin<void, void, any, any> {
log: Logger;
private licenseStatus: LicenseStatus = {
hasRequired: false,
};
constructor(ctx: PluginInitializerContext) {
this.log = ctx.logger.get();
}
async setup(
{ http, elasticsearch: elasticsearchService }: CoreSetup,
{ licensing }: Dependencies
) {
const router = http.createRouter();
const routeDependencies: RouteDependencies = {
router,
getLicenseStatus: () => this.licenseStatus,
};
const config = { plugins: [elasticsearchJsPlugin] };
const watcherESClient = elasticsearchService.createClient('watcher', config);
http.registerRouteHandlerContext('watcher', (ctx, request) => {
return {
client: watcherESClient.asScoped(request),
};
});
registerListFieldsRoute(routeDependencies);
registerLoadHistoryRoute(routeDependencies);
registerIndicesRoutes(routeDependencies);
registerLicenseRoutes(routeDependencies);
registerSettingsRoutes(routeDependencies);
registerWatchesRoutes(routeDependencies);
registerWatchRoutes(routeDependencies);
licensing.license$.subscribe(async license => {
const { state, message } = license.check(PLUGIN.ID, PLUGIN.MINIMUM_LICENSE_REQUIRED);
const hasMinimumLicense = state === LICENSE_CHECK_STATE.Valid;
if (hasMinimumLicense && license.getFeature(PLUGIN.ID)) {
this.log.info('Enabling Watcher plugin.');
this.licenseStatus = {
hasRequired: true,
};
} else {
if (message) {
this.log.info(message);
}
this.licenseStatus = {
hasRequired: false,
message,
};
}
});
}
start() {}
stop() {}
}