-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
Copy pathplugin.ts
105 lines (92 loc) · 2.8 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
96
97
98
99
100
101
102
103
104
105
/*
* 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 {
snapshotRestore?: SnapshotRestoreContext;
}
}
import { first } from 'rxjs/operators';
import { i18n } from '@kbn/i18n';
import {
CoreSetup,
Plugin,
Logger,
PluginInitializerContext,
IScopedClusterClient,
} from 'kibana/server';
import { PLUGIN } from '../common';
import { License } from './services';
import { ApiRoutes } from './routes';
import { isEsError, wrapEsError } from './lib';
import { elasticsearchJsPlugin } from './client/elasticsearch_sr';
import { Dependencies } from './types';
import { SnapshotRestoreConfig } from './config';
export interface SnapshotRestoreContext {
client: IScopedClusterClient;
}
export class SnapshotRestoreServerPlugin implements Plugin<void, void, any, any> {
private readonly logger: Logger;
private readonly apiRoutes: ApiRoutes;
private readonly license: License;
constructor(private context: PluginInitializerContext) {
const { logger } = this.context;
this.logger = logger.get();
this.apiRoutes = new ApiRoutes();
this.license = new License();
}
public async setup(
{ http, elasticsearch }: CoreSetup,
{ licensing, security, cloud }: Dependencies
): Promise<void> {
const pluginConfig = await this.context.config
.create<SnapshotRestoreConfig>()
.pipe(first())
.toPromise();
if (!pluginConfig.enabled) {
return;
}
const router = http.createRouter();
this.license.setup(
{
pluginId: PLUGIN.id,
minimumLicenseType: PLUGIN.minimumLicenseType,
defaultErrorMessage: i18n.translate('xpack.snapshotRestore.licenseCheckErrorMessage', {
defaultMessage: 'License check failed',
}),
},
{
licensing,
logger: this.logger,
}
);
const esClientConfig = { plugins: [elasticsearchJsPlugin] };
const snapshotRestoreESClient = elasticsearch.createClient('snapshotRestore', esClientConfig);
http.registerRouteHandlerContext('snapshotRestore', (ctx, request) => {
return {
client: snapshotRestoreESClient.asScoped(request),
};
});
this.apiRoutes.setup({
router,
license: this.license,
config: {
isSecurityEnabled: security !== undefined,
isCloudEnabled: cloud !== undefined && cloud.isCloudEnabled,
isSlmEnabled: pluginConfig.slmUi.enabled,
},
lib: {
isEsError,
wrapEsError,
},
});
}
public start() {
this.logger.debug('Starting plugin');
}
public stop() {
this.logger.debug('Stopping plugin');
}
}