Skip to content

Commit

Permalink
server code NP migration
Browse files Browse the repository at this point in the history
  • Loading branch information
alisonelizabeth committed Mar 10, 2020
1 parent 6689519 commit b681a18
Show file tree
Hide file tree
Showing 17 changed files with 284 additions and 139 deletions.
9 changes: 1 addition & 8 deletions x-pack/legacy/plugins/painless_lab/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
import { resolve } from 'path';
import { PLUGIN_ID } from './common/constants';

import { registerLicenseChecker } from './server/register_license_checker';
import { registerExecuteRoute } from './server/register_execute_route';
import { Legacy } from '../../../../kibana';

export const painlessLab = (kibana: any) =>
new kibana.Plugin({
id: PLUGIN_ID,
Expand All @@ -25,8 +21,5 @@ export const painlessLab = (kibana: any) =>
styleSheetPaths: resolve(__dirname, 'public/index.scss'),
devTools: [resolve(__dirname, 'public/register')],
},
init: (server: Legacy.Server) => {
registerLicenseChecker(server);
registerExecuteRoute(server);
},
init: () => {},
});
18 changes: 9 additions & 9 deletions x-pack/legacy/plugins/painless_lab/public/register.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,16 @@ npSetup.plugins.devTools.register({
async mount(context, { element }) {
registerPainless();

const licenseCheck = {
showPage: xpackInfo.get('features.painlessLab.enableLink'),
message: xpackInfo.get('features.painlessLab.message'),
};
// const licenseCheck = {
// showPage: xpackInfo.get('features.painlessLab.enableLink'),
// message: xpackInfo.get('features.painlessLab.message'),
// };

if (!licenseCheck.showPage) {
npStart.core.notifications.toasts.addDanger(licenseCheck.message);
window.location.hash = '/dev_tools';
return () => {};
}
// if (!licenseCheck.showPage) {
// npStart.core.notifications.toasts.addDanger(licenseCheck.message);
// window.location.hash = '/dev_tools';
// return () => {};
// }

const { renderApp } = await import('./render_app');
return renderApp(element, npStart.core);
Expand Down
46 changes: 0 additions & 46 deletions x-pack/legacy/plugins/painless_lab/server/lib/check_license.ts

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

19 changes: 19 additions & 0 deletions x-pack/plugins/painless_lab/common/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* 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.
*/
import { LicenseType } from '../../licensing/common/types';

const basicLicense: LicenseType = 'basic';

export const PLUGIN = {
id: 'painlessLab',
minimumLicenseType: basicLicense,
getI18nName: (i18n: any): string =>
i18n.translate('xpack.painlessLab.appTitle', {
defaultMessage: 'Painless Lab',
}),
};

export const API_BASE_PATH = '/api/painless_lab';
15 changes: 15 additions & 0 deletions x-pack/plugins/painless_lab/kibana.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"id": "painlessLab",
"version": "8.0.0",
"kibanaVersion": "kibana",
"requiredPlugins": [
"devTools",
"licensing"
],
"configPath": [
"xpack",
"painless_lab"
],
"server": true,
"ui": false
}
11 changes: 11 additions & 0 deletions x-pack/plugins/painless_lab/server/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* 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.
*/
import { PluginInitializerContext } from 'kibana/server';
import { PainlessLabServerPlugin } from './plugin';

export const plugin = (ctx: PluginInitializerContext) => {
return new PainlessLabServerPlugin(ctx);
};
7 changes: 7 additions & 0 deletions x-pack/plugins/painless_lab/server/lib/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*
* 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.
*/

export { isEsError } from './is_es_error';
13 changes: 13 additions & 0 deletions x-pack/plugins/painless_lab/server/lib/is_es_error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* 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.
*/

import * as legacyElasticsearch from 'elasticsearch';

const esErrorsParent = legacyElasticsearch.errors._Abstract;

export function isEsError(err: Error) {
return err instanceof esErrorsParent;
}
47 changes: 47 additions & 0 deletions x-pack/plugins/painless_lab/server/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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.
*/

import { i18n } from '@kbn/i18n';
import { CoreSetup, Logger, Plugin, PluginInitializerContext } from 'kibana/server';

import { PLUGIN } from '../common/constants';
import { License } from './services';
import { Dependencies } from './types';
import { registerExecuteRoute } from './routes/api';

export class PainlessLabServerPlugin implements Plugin {
private readonly license: License;
private readonly logger: Logger;

constructor({ logger }: PluginInitializerContext) {
this.logger = logger.get();
this.license = new License();
}

async setup({ http }: CoreSetup, { licensing }: Dependencies) {
const router = http.createRouter();

this.license.setup(
{
pluginId: PLUGIN.id,
minimumLicenseType: PLUGIN.minimumLicenseType,
defaultErrorMessage: i18n.translate('xpack.painlessLab.licenseCheckErrorMessage', {
defaultMessage: 'License check failed',
}),
},
{
licensing,
logger: this.logger,
}
);

registerExecuteRoute({ router, license: this.license });
}

start() {}

stop() {}
}
47 changes: 47 additions & 0 deletions x-pack/plugins/painless_lab/server/routes/api/execute.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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.
*/
import { schema } from '@kbn/config-schema';

import { RouteDependencies } from '../../types';
import { API_BASE_PATH } from '../../../common/constants';
import { isEsError } from '../../lib';

const bodySchema = schema.object({
script: schema.object({
source: schema.string(),
}),
});

export function registerExecuteRoute({ router, license }: RouteDependencies) {
router.post(
{
path: `${API_BASE_PATH}/execute`,
validate: {
body: bodySchema,
},
},
license.guardApiRoute(async (ctx, req, res) => {
const body = req.body as typeof bodySchema.type;

try {
const callAsCurrentUser = ctx.core.elasticsearch.dataClient.callAsCurrentUser;

const response = await callAsCurrentUser('scriptsPainlessExecute', {
body,
});

return res.ok({
body: response,
});
} catch (e) {
if (isEsError(e)) {
return res.customError({ statusCode: e.statusCode, body: e });
}
return res.internalError({ body: e });
}
})
);
}
7 changes: 7 additions & 0 deletions x-pack/plugins/painless_lab/server/routes/api/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*
* 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.
*/

export { registerExecuteRoute } from './execute';
7 changes: 7 additions & 0 deletions x-pack/plugins/painless_lab/server/services/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*
* 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.
*/

export { License } from './license';
Loading

0 comments on commit b681a18

Please sign in to comment.