Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NP Licensing plugin route handler context #46586

Merged
merged 5 commits into from
Sep 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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 { BehaviorSubject } from 'rxjs';
import { ILicense } from './types';
import { setup } from './__fixtures__/setup';
import { createRouteHandlerContext } from './licensing_route_handler_context';

describe('licensingRouteHandlerContext', () => {
it('provides the initial license value', async () => {
const { license$, license } = await setup();

const context = createRouteHandlerContext(license$);

const { license: contextResult } = await context({});

expect(contextResult).toBe(license);
});

it('provides the latest license value', async () => {
const { license } = await setup();
const license$ = new BehaviorSubject<ILicense>(license);

const context = createRouteHandlerContext(license$);

const latestLicense = (Symbol() as unknown) as ILicense;
license$.next(latestLicense);

const { license: contextResult } = await context({});

expect(contextResult).toBe(latestLicense);
});
});
19 changes: 19 additions & 0 deletions x-pack/plugins/licensing/server/licensing_route_handler_context.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 { RequestHandlerContext, IContextProvider } from 'src/core/server';
import { Observable } from 'rxjs';
import { take } from 'rxjs/operators';
import { ILicense } from './types';

export function createRouteHandlerContext(
license$: Observable<ILicense>
): IContextProvider<RequestHandlerContext, 'licensing'> {
return async function licensingRouteHandlerContext() {
const license = await license$.pipe(take(1)).toPromise();
return { license };
};
}
17 changes: 17 additions & 0 deletions x-pack/plugins/licensing/server/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,21 @@ describe('licensing plugin', () => {

expect(licenseTypes).toEqual(['basic', 'gold', 'platinum']);
});

test('provides a licensing context to http routes', async () => {
Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not sure of the best way to test the new context. This test is basically useless, so I'm open to ideas. I could write a functional test that stands up another plugin for the sole purpose of testing this, but that felt like overkill...

Copy link
Contributor

Choose a reason for hiding this comment

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

It'd probably be easier to test if you export the context provider function separately from registering it. That way you could have this test to ensure it gets registered, and a separate test that verifies the behavior of the provider function

Copy link
Member Author

Choose a reason for hiding this comment

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

Sounds good to me! Done in 477bd5b

const { coreSetup, plugin: _plugin } = await setupOnly();

plugin = _plugin;

await plugin.setup(coreSetup);

expect(coreSetup.http.registerRouteHandlerContext.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
"licensing",
[Function],
],
]
`);
});
});
6 changes: 5 additions & 1 deletion x-pack/plugins/licensing/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { Poller } from '../../../../src/core/utils/poller';
import { LicensingConfigType, LicensingPluginSetup, ILicense } from './types';
import { LicensingConfig } from './licensing_config';
import { License } from './license';
import { createRouteHandlerContext } from './licensing_route_handler_context';

export class Plugin implements CorePlugin<LicensingPluginSetup> {
private readonly logger: Logger;
Expand Down Expand Up @@ -120,9 +121,12 @@ export class Plugin implements CorePlugin<LicensingPluginSetup> {
public async setup(core: CoreSetup) {
const config = await this.config$.pipe(first()).toPromise();
const poller = this.create(config, core);
const license$ = poller.subject$.asObservable();
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Remove blank line before this declaration.


core.http.registerRouteHandlerContext('licensing', createRouteHandlerContext(license$));

return {
license$: poller.subject$.asObservable(),
license$,
};
}

Expand Down
11 changes: 11 additions & 0 deletions x-pack/plugins/licensing/server/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,14 @@ export type LicensingConfigType = TypeOf<typeof schema>;
export type LicenseType = keyof typeof LICENSE_TYPE;
/** @public */
export type LicenseFeatureSerializer = (licensing: ILicense) => any;

/** @public */
export interface LicensingRequestContext {
license: ILicense;
}

declare module 'src/core/server' {
interface RequestHandlerContext {
licensing: LicensingRequestContext;
}
}