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 1 commit
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
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],
],
]
`);
});
});
11 changes: 9 additions & 2 deletions x-pack/plugins/licensing/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { Observable } from 'rxjs';
import { first, map } from 'rxjs/operators';
import { first, map, take } from 'rxjs/operators';
import moment from 'moment';
import {
CoreSetup,
Expand Down Expand Up @@ -121,8 +121,15 @@ export class Plugin implements CorePlugin<LicensingPluginSetup> {
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', async () => {
const license = await license$.pipe(take(1)).toPromise();
return { 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 'kibana/server' {
interface RequestHandlerContext {
licensing: LicensingRequestContext;
}
}