Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
0967c89
WIP, trying things.
justinkambic Sep 5, 2019
96b5568
Delete call to hapi register function, use route for bootstrapping GQL.
justinkambic Sep 5, 2019
edfd343
Add files to use for new platform shim.
justinkambic Sep 6, 2019
7ad44f3
Add server shim.
justinkambic Sep 9, 2019
5fdf8c0
Update mock in test file to conform to new shim interface.
justinkambic Sep 9, 2019
a8518f4
Delete unneeded import and var declaration.
justinkambic Sep 10, 2019
c4e1a96
Fix broken type files.
justinkambic Sep 10, 2019
5e7a6e6
Add plugin shim initializer to app bootstrapper.
justinkambic Sep 12, 2019
787038a
Merge branch 'master' into uptime_server-new-platform
justinkambic Sep 12, 2019
3a01810
Merge branch 'master' into uptime_server-new-platform
justinkambic Sep 19, 2019
453b6c5
Merge branch 'master' into uptime_server-new-platform
justinkambic Sep 24, 2019
d918cb7
Merge branch 'master' into uptime_server-new-platform
justinkambic Oct 10, 2019
9267c03
Extract plugins to separate param for shim.
justinkambic Oct 10, 2019
7af1a87
Rename interface and update tests.
justinkambic Oct 10, 2019
49bc115
Merge branch 'master' into uptime_server-new-platform
justinkambic Oct 10, 2019
e79059f
Merge branch 'master' into uptime_server-new-platform
justinkambic Oct 11, 2019
c53d6a6
Merge branch 'master' into uptime_server-new-platform
justinkambic Oct 14, 2019
70add2f
Merge branch 'master' into uptime_server-new-platform
elasticmachine Oct 18, 2019
890d349
Merge branch 'master' into uptime_server-new-platform
justinkambic Oct 18, 2019
e13e5d0
Delete unneeded file.
justinkambic Oct 21, 2019
c654473
Merge branch 'uptime_server-new-platform' of github.com:justinkambic/…
justinkambic Oct 21, 2019
fca8229
Merge branch 'master' into uptime_server-new-platform
elasticmachine Oct 21, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions x-pack/legacy/plugins/uptime/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@

import { i18n } from '@kbn/i18n';
import { resolve } from 'path';
import { PluginInitializerContext } from 'src/core/server';
import { PLUGIN } from './common/constants';
import { initServerWithKibana, KibanaServer } from './server';
import { KibanaServer, plugin } from './server';

export const uptime = (kibana: any) =>
new kibana.Plugin({
Expand All @@ -33,6 +34,19 @@ export const uptime = (kibana: any) =>
home: ['plugins/uptime/register_feature'],
},
init(server: KibanaServer) {
initServerWithKibana(server);
const initializerContext = {} as PluginInitializerContext;
const { savedObjects } = server;
const { elasticsearch, xpack_main } = server.plugins;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

optional: you can also use the NP elasticsearch client by taking it from server.newPlatform.setup.core.elasticsearch

plugin(initializerContext).setup(
{
route: (arg: any) => server.route(arg),
},
{
elasticsearch,
savedObjects,
usageCollector: server.usage,
xpack: xpack_main,
}
);
},
});
1 change: 1 addition & 0 deletions x-pack/legacy/plugins/uptime/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
*/

export { initServerWithKibana, KibanaServer } from './kibana.index';
export { plugin } from './plugin';
14 changes: 9 additions & 5 deletions x-pack/legacy/plugins/uptime/server/kibana.index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { PLUGIN } from '../common/constants';
import { KibanaTelemetryAdapter } from './lib/adapters/telemetry';
import { compose } from './lib/compose/kibana';
import { initUptimeServer } from './uptime_server';
import { UptimeCorePlugins, UptimeCoreSetup } from './lib/adapters/framework';

export interface KibanaRouteOptions {
path: string;
Expand All @@ -28,13 +29,16 @@ export interface KibanaServer extends Server {
};
}

export const initServerWithKibana = (server: KibanaServer) => {
const libs = compose(server);
server.usage.collectorSet.register(KibanaTelemetryAdapter.initUsageCollector(server));
export const initServerWithKibana = (server: UptimeCoreSetup, plugins: UptimeCorePlugins) => {
const { usageCollector, xpack } = plugins;
const libs = compose(
server,
plugins
);
usageCollector.collectorSet.register(KibanaTelemetryAdapter.initUsageCollector(usageCollector));
initUptimeServer(libs);

const xpackMainPlugin = server.plugins.xpack_main;
xpackMainPlugin.registerFeature({
xpack.registerFeature({
id: PLUGIN.ID,
name: i18n.translate('xpack.uptime.featureRegistry.uptimeFeatureName', {
defaultMessage: 'Uptime',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { GraphQLOptions } from 'apollo-server-core';
import { GraphQLSchema } from 'graphql';
import { Lifecycle, ResponseToolkit } from 'hapi';
import { RouteOptions } from 'hapi';
import { SavedObjectsLegacyService } from 'src/core/server';

export interface UMFrameworkRequest {
user: string;
Expand All @@ -29,6 +30,17 @@ export interface UMFrameworkRouteOptions<
config?: any;
}

export interface UptimeCoreSetup {
route: any;
}

export interface UptimeCorePlugins {
elasticsearch: any;
savedObjects: SavedObjectsLegacyService<any>;
usageCollector: any;
xpack: any;
}

export type UMFrameworkRouteHandler<RouteRequest extends UMFrameworkRequest> = (
request: UMFrameworkRequest,
h: ResponseToolkit
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,24 @@
*/

import { GraphQLSchema } from 'graphql';
import { Server } from 'hapi';
import { Request, ResponseToolkit } from 'hapi';
import { runHttpQuery } from 'apollo-server-core';
import { UptimeCorePlugins, UptimeCoreSetup } from './adapter_types';
import {
UMBackendFrameworkAdapter,
UMFrameworkRequest,
UMFrameworkResponse,
UMFrameworkRouteOptions,
UMHapiGraphQLPluginOptions,
} from './adapter_types';
import { uptimeGraphQLHapiPlugin } from './apollo_framework_adapter';
import { DEFAULT_GRAPHQL_PATH } from '../../../graphql';

export class UMKibanaBackendFrameworkAdapter implements UMBackendFrameworkAdapter {
private server: Server;

constructor(hapiServer: Server) {
this.server = hapiServer;
constructor(
private readonly server: UptimeCoreSetup,
private readonly plugins: UptimeCorePlugins
) {
this.server = server;
this.plugins = plugins;
}

public registerRoute<
Expand All @@ -30,24 +33,53 @@ export class UMKibanaBackendFrameworkAdapter implements UMBackendFrameworkAdapte
}

public registerGraphQLEndpoint(routePath: string, schema: GraphQLSchema): void {
this.server.register<UMHapiGraphQLPluginOptions>({
options: {
graphQLOptions: (req: any) => ({
context: { req },
schema,
}),
path: routePath,
route: {
tags: ['access:uptime'],
},
const options = {
graphQLOptions: (req: any) => ({
context: { req },
schema,
}),
path: routePath,
route: {
tags: ['access:uptime'],
},
};
this.server.route({
options: options.route,
handler: async (request: Request, h: ResponseToolkit) => {
try {
const { method } = request;
const query =
method === 'post'
? (request.payload as Record<string, any>)
: (request.query as Record<string, any>);

const graphQLResponse = await runHttpQuery([request], {
method: method.toUpperCase(),
options: options.graphQLOptions,
query,
});

return h.response(graphQLResponse).type('application/json');
} catch (error) {
if (error.isGraphQLError === true) {
return h
.response(error.message)
.code(error.statusCode)
.type('application/json');
}
return h.response(error).type('application/json');
}
},
plugin: uptimeGraphQLHapiPlugin,
method: ['get', 'post'],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a useful example for other teams for how to replace the hapi plugin. When you switch to using the NP router in your shim (from server.newPlatform.setup.core.http.createRouter()) you won't be able to define one route with two methods (get & post) so this will have to be split into two routes.

path: options.path || DEFAULT_GRAPHQL_PATH,
vhost: undefined,
});
}

public getSavedObjectsClient() {
const { SavedObjectsClient, getSavedObjectsRepository } = this.server.savedObjects;
const { callWithInternalUser } = this.server.plugins.elasticsearch.getCluster('admin');
const { elasticsearch, savedObjects } = this.plugins;
const { SavedObjectsClient, getSavedObjectsRepository } = savedObjects;
const { callWithInternalUser } = elasticsearch.getCluster('admin');
const internalRepository = getSavedObjectsRepository(callWithInternalUser);
return new SavedObjectsClient(internalRepository);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { SavedObjectsLegacyService } from 'src/core/server';
import { UMSavedObjectsAdapter } from './types';
import uptimeIndexPattern from './heartbeat_index_pattern.json';

export class UMKibanaSavedObjectsAdapter implements UMSavedObjectsAdapter {
private readonly savedObjectsClient: any;
constructor(server: any) {
const { SavedObjectsClient, getSavedObjectsRepository } = server.savedObjects;
const { callWithInternalUser } = server.plugins.elasticsearch.getCluster('admin');
constructor(savedObjects: SavedObjectsLegacyService<any>, elasticsearch: any) {
const { SavedObjectsClient, getSavedObjectsRepository } = savedObjects;
const { callWithInternalUser } = elasticsearch.getCluster('admin');
const internalRepository = getSavedObjectsRepository(callWithInternalUser);
this.savedObjectsClient = new SavedObjectsClient(internalRepository);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,21 @@
import { KibanaTelemetryAdapter } from '../kibana_telemetry_adapter';

describe('KibanaTelemetryAdapter', () => {
let server: any;
let telemetry: any;
let collector: { type: string; fetch: () => Promise<any>; isReady: () => boolean };
beforeEach(() => {
server = {
usage: {
collectorSet: {
makeUsageCollector: (val: any) => {
collector = val;
},
telemetry = {
collectorSet: {
makeUsageCollector: (val: any) => {
collector = val;
},
},
};
});

it('collects monitor and overview data', async () => {
expect.assertions(1);
KibanaTelemetryAdapter.initUsageCollector(server);
KibanaTelemetryAdapter.initUsageCollector(telemetry);
KibanaTelemetryAdapter.countMonitor();
KibanaTelemetryAdapter.countOverview();
KibanaTelemetryAdapter.countOverview();
Expand All @@ -35,7 +33,7 @@ describe('KibanaTelemetryAdapter', () => {
expect.assertions(1);
// give a time of > 24 hours ago
Date.now = jest.fn(() => 1559053560000);
KibanaTelemetryAdapter.initUsageCollector(server);
KibanaTelemetryAdapter.initUsageCollector(telemetry);
KibanaTelemetryAdapter.countMonitor();
KibanaTelemetryAdapter.countOverview();
// give a time of now
Expand All @@ -49,7 +47,7 @@ describe('KibanaTelemetryAdapter', () => {
});

it('defaults ready to `true`', async () => {
KibanaTelemetryAdapter.initUsageCollector(server);
KibanaTelemetryAdapter.initUsageCollector(telemetry);
expect(collector.isReady()).toBe(true);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ const BUCKET_SIZE = 3600;
const BUCKET_NUMBER = 24;

export class KibanaTelemetryAdapter {
public static initUsageCollector(server: any) {
const { collectorSet } = server.usage;
public static initUsageCollector(usageCollector: any) {
const { collectorSet } = usageCollector;
return collectorSet.makeUsageCollector({
type: 'uptime',
fetch: async () => {
Expand Down
17 changes: 9 additions & 8 deletions x-pack/legacy/plugins/uptime/server/lib/compose/kibana.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,26 @@ import { UMAuthDomain } from '../domains';
import { UMDomainLibs, UMServerLibs } from '../lib';
import { ElasticsearchMonitorStatesAdapter } from '../adapters/monitor_states';
import { UMKibanaSavedObjectsAdapter } from '../adapters/saved_objects/kibana_saved_objects_adapter';
import { UptimeCorePlugins, UptimeCoreSetup } from '../adapters/framework';

export function compose(hapiServer: any): UMServerLibs {
const framework = new UMKibanaBackendFrameworkAdapter(hapiServer);
const database = new UMKibanaDatabaseAdapter(hapiServer.plugins.elasticsearch);
const authDomain = new UMAuthDomain(new UMXPackAuthAdapter(hapiServer.plugins.xpack_main), {});
export function compose(server: UptimeCoreSetup, plugins: UptimeCorePlugins): UMServerLibs {
const { elasticsearch, savedObjects, xpack } = plugins;
const framework = new UMKibanaBackendFrameworkAdapter(server, plugins);
const database = new UMKibanaDatabaseAdapter(elasticsearch);

const authDomain = new UMAuthDomain(new UMXPackAuthAdapter(xpack), {});

const domainLibs: UMDomainLibs = {
auth: authDomain,
monitors: new ElasticsearchMonitorsAdapter(database),
monitorStates: new ElasticsearchMonitorStatesAdapter(database),
pings: new ElasticsearchPingsAdapter(database),
savedObjects: new UMKibanaSavedObjectsAdapter(hapiServer),
savedObjects: new UMKibanaSavedObjectsAdapter(savedObjects, elasticsearch),
};

const libs: UMServerLibs = {
return {
framework,
database,
...domainLibs,
};

return libs;
}
19 changes: 19 additions & 0 deletions x-pack/legacy/plugins/uptime/server/plugin.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 { PluginInitializerContext } from 'src/core/server';
import { initServerWithKibana } from './kibana.index';
import { UptimeCoreSetup, UptimeCorePlugins } from './lib/adapters/framework';

export function plugin(initializerContext: PluginInitializerContext) {
return new Plugin();
}

export class Plugin {
public setup(core: UptimeCoreSetup, plugins: UptimeCorePlugins) {
initServerWithKibana(core, plugins);
}
}