Add featureUsage API to licensing context provider (#69838)

This commit is contained in:
Josh Dover 2020-06-25 15:28:48 -06:00 committed by GitHub
parent 3b9bbdb1a0
commit 77df036558
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 66 additions and 14 deletions

View file

@ -9,7 +9,7 @@ import { licensingMock } from './plugins/licensing/server/mocks';
function createCoreRequestHandlerContextMock() {
return {
core: coreMock.createRequestHandlerContext(),
licensing: { license: licensingMock.createLicense() },
licensing: licensingMock.createRequestHandlerContext(),
};
}

View file

@ -16,9 +16,7 @@ import { FeatureConfig } from '../../common';
function createContextMock(licenseType: LicenseType = 'gold') {
return {
core: coreMock.createRequestHandlerContext(),
licensing: {
license: licensingMock.createLicense({ license: { type: licenseType } }),
},
licensing: licensingMock.createRequestHandlerContext({ license: { type: licenseType } }),
};
}

View file

@ -5,9 +5,19 @@
*/
import { BehaviorSubject } from 'rxjs';
import { licenseMock } from '../common/licensing.mock';
import { licenseMock } from '../common/licensing.mock';
import { createRouteHandlerContext } from './licensing_route_handler_context';
import { featureUsageMock } from './services/feature_usage_service.mock';
import { FeatureUsageServiceStart } from './services';
import { StartServicesAccessor } from 'src/core/server';
import { LicensingPluginStart } from './types';
const createStartServices = (
featureUsage: FeatureUsageServiceStart = featureUsageMock.createStart()
): StartServicesAccessor<{}, LicensingPluginStart> => {
return async () => [{} as any, {}, { featureUsage } as LicensingPluginStart];
};
describe('createRouteHandlerContext', () => {
it('returns a function providing the last license value', async () => {
@ -15,7 +25,7 @@ describe('createRouteHandlerContext', () => {
const secondLicense = licenseMock.createLicense();
const license$ = new BehaviorSubject(firstLicense);
const routeHandler = createRouteHandlerContext(license$);
const routeHandler = createRouteHandlerContext(license$, createStartServices());
const firstCtx = await routeHandler({} as any, {} as any, {} as any);
license$.next(secondLicense);
@ -24,4 +34,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$, createStartServices(featureUsage));
const ctx = await routeHandler({} as any, {} as any, {} as any);
expect(ctx.featureUsage).toBe(featureUsage);
});
});

View file

@ -4,11 +4,12 @@
* you may not use this file except in compliance with the Elastic License.
*/
import { IContextProvider, RequestHandler } from 'src/core/server';
import { IContextProvider, RequestHandler, StartServicesAccessor } from 'src/core/server';
import { Observable } from 'rxjs';
import { take } from 'rxjs/operators';
import { ILicense } from '../common/types';
import { LicensingPluginStart } from './types';
/**
* Create a route handler context for access to Kibana license information.
@ -16,9 +17,16 @@ import { ILicense } from '../common/types';
* @public
*/
export function createRouteHandlerContext(
license$: Observable<ILicense>
license$: Observable<ILicense>,
getStartServices: StartServicesAccessor<{}, LicensingPluginStart>
): IContextProvider<RequestHandler<any, any, any>, 'licensing'> {
return async function licensingRouteHandlerContext() {
return { license: await license$.pipe(take(1)).toPromise() };
const [, , { featureUsage }] = await getStartServices();
const license = await license$.pipe(take(1)).toPromise();
return {
featureUsage,
license,
};
};
}

View file

@ -4,7 +4,11 @@
* you may not use this file except in compliance with the Elastic License.
*/
import { BehaviorSubject } from 'rxjs';
import { LicensingPluginSetup, LicensingPluginStart } from './types';
import {
LicensingPluginSetup,
LicensingPluginStart,
LicensingRequestHandlerContext,
} from './types';
import { licenseMock } from '../common/licensing.mock';
import { featureUsageMock } from './services/feature_usage_service.mock';
@ -43,8 +47,20 @@ const createStartMock = (): jest.Mocked<LicensingPluginStart> => {
return mock;
};
const createRequestHandlerContextMock = (
...options: Parameters<typeof licenseMock.createLicense>
): jest.Mocked<LicensingRequestHandlerContext> => {
const mock: jest.Mocked<LicensingRequestHandlerContext> = {
license: licenseMock.createLicense(...options),
featureUsage: featureUsageMock.createStart(),
};
return mock;
};
export const licensingMock = {
createSetup: createSetupMock,
createStart: createStartMock,
createRequestHandlerContext: createRequestHandlerContextMock,
...licenseMock,
};

View file

@ -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$, core.getStartServices)
);
registerRoutes(core.http.createRouter(), core.getStartServices);
core.http.registerOnPreResponse(createOnPreResponseHandler(refresh, license$));

View file

@ -40,11 +40,18 @@ export interface RawLicense {
mode: LicenseType;
}
/**
* The APIs exposed on the `licensing` key of {@link RequestHandlerContext} for plugins that depend on licensing.
* @public
*/
export interface LicensingRequestHandlerContext {
featureUsage: FeatureUsageServiceStart;
license: ILicense;
}
declare module 'src/core/server' {
interface RequestHandlerContext {
licensing: {
license: ILicense;
};
licensing: LicensingRequestHandlerContext;
}
}