Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import { BehaviorSubject } from 'rxjs';
import { licenseMock } from '../common/licensing.mock';

import { createRouteHandlerContext } from './licensing_route_handler_context';
import { featureUsageMock } from './services/feature_usage_service.mock';

describe('createRouteHandlerContext', () => {
it('returns a function providing the last license value', async () => {
const firstLicense = licenseMock.createLicense();
const secondLicense = licenseMock.createLicense();
const license$ = new BehaviorSubject(firstLicense);

const routeHandler = createRouteHandlerContext(license$);
const routeHandler = createRouteHandlerContext(license$, featureUsageMock.createStart());

const firstCtx = await routeHandler({} as any, {} as any, {} as any);
license$.next(secondLicense);
Expand All @@ -24,4 +25,14 @@ describe('createRouteHandlerContext', () => {
expect(firstCtx.license).toBe(firstLicense);
expect(secondCtx.license).toBe(secondLicense);
});

it('returns a the feature usage API', async () => {
const license$ = new BehaviorSubject(licenseMock.createLicense());
const featureUsage = featureUsageMock.createStart();

const routeHandler = createRouteHandlerContext(license$, featureUsage);
const ctx = await routeHandler({} as any, {} as any, {} as any);

expect(ctx.featureUsage).toBe(featureUsage);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,21 @@ import { Observable } from 'rxjs';
import { take } from 'rxjs/operators';

import { ILicense } from '../common/types';
import { FeatureUsageServiceStart } from './services';

/**
* Create a route handler context for access to Kibana license information.
* @param license$ An observable of a License instance.
* @public
*/
export function createRouteHandlerContext(
license$: Observable<ILicense>
license$: Observable<ILicense>,
featureUsage: FeatureUsageServiceStart
): IContextProvider<RequestHandler<any, any, any>, 'licensing'> {
return async function licensingRouteHandlerContext() {
return { license: await license$.pipe(take(1)).toPromise() };
return {
license: await license$.pipe(take(1)).toPromise(),
featureUsage,
};
};
}
5 changes: 4 additions & 1 deletion x-pack/plugins/licensing/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,10 @@ export class LicensingPlugin implements Plugin<LicensingPluginSetup, LicensingPl
pollingFrequency.asMilliseconds()
);

core.http.registerRouteHandlerContext('licensing', createRouteHandlerContext(license$));
core.http.registerRouteHandlerContext(
'licensing',
createRouteHandlerContext(license$, this.featureUsage.start())
Comment thread
joshdover marked this conversation as resolved.
Outdated
);

registerRoutes(core.http.createRouter(), core.getStartServices);
core.http.registerOnPreResponse(createOnPreResponseHandler(refresh, license$));
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/licensing/server/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export interface RawLicense {

declare module 'src/core/server' {
interface RequestHandlerContext {
featureUsage: FeatureUsageServiceStart;
licensing: {
license: ILicense;
};
Expand Down