Skip to content

Commit 77df036

Browse files
authored
Add featureUsage API to licensing context provider (#69838)
1 parent 3b9bbdb commit 77df036

File tree

7 files changed

+66
-14
lines changed

7 files changed

+66
-14
lines changed

x-pack/mocks.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { licensingMock } from './plugins/licensing/server/mocks';
99
function createCoreRequestHandlerContextMock() {
1010
return {
1111
core: coreMock.createRequestHandlerContext(),
12-
licensing: { license: licensingMock.createLicense() },
12+
licensing: licensingMock.createRequestHandlerContext(),
1313
};
1414
}
1515

x-pack/plugins/features/server/routes/index.test.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ import { FeatureConfig } from '../../common';
1616
function createContextMock(licenseType: LicenseType = 'gold') {
1717
return {
1818
core: coreMock.createRequestHandlerContext(),
19-
licensing: {
20-
license: licensingMock.createLicense({ license: { type: licenseType } }),
21-
},
19+
licensing: licensingMock.createRequestHandlerContext({ license: { type: licenseType } }),
2220
};
2321
}
2422

x-pack/plugins/licensing/server/licensing_route_handler_context.test.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,27 @@
55
*/
66

77
import { BehaviorSubject } from 'rxjs';
8-
import { licenseMock } from '../common/licensing.mock';
98

9+
import { licenseMock } from '../common/licensing.mock';
1010
import { createRouteHandlerContext } from './licensing_route_handler_context';
11+
import { featureUsageMock } from './services/feature_usage_service.mock';
12+
import { FeatureUsageServiceStart } from './services';
13+
import { StartServicesAccessor } from 'src/core/server';
14+
import { LicensingPluginStart } from './types';
15+
16+
const createStartServices = (
17+
featureUsage: FeatureUsageServiceStart = featureUsageMock.createStart()
18+
): StartServicesAccessor<{}, LicensingPluginStart> => {
19+
return async () => [{} as any, {}, { featureUsage } as LicensingPluginStart];
20+
};
1121

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

18-
const routeHandler = createRouteHandlerContext(license$);
28+
const routeHandler = createRouteHandlerContext(license$, createStartServices());
1929

2030
const firstCtx = await routeHandler({} as any, {} as any, {} as any);
2131
license$.next(secondLicense);
@@ -24,4 +34,14 @@ describe('createRouteHandlerContext', () => {
2434
expect(firstCtx.license).toBe(firstLicense);
2535
expect(secondCtx.license).toBe(secondLicense);
2636
});
37+
38+
it('returns a the feature usage API', async () => {
39+
const license$ = new BehaviorSubject(licenseMock.createLicense());
40+
const featureUsage = featureUsageMock.createStart();
41+
42+
const routeHandler = createRouteHandlerContext(license$, createStartServices(featureUsage));
43+
const ctx = await routeHandler({} as any, {} as any, {} as any);
44+
45+
expect(ctx.featureUsage).toBe(featureUsage);
46+
});
2747
});

x-pack/plugins/licensing/server/licensing_route_handler_context.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,29 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7-
import { IContextProvider, RequestHandler } from 'src/core/server';
7+
import { IContextProvider, RequestHandler, StartServicesAccessor } from 'src/core/server';
88
import { Observable } from 'rxjs';
99
import { take } from 'rxjs/operators';
1010

1111
import { ILicense } from '../common/types';
12+
import { LicensingPluginStart } from './types';
1213

1314
/**
1415
* Create a route handler context for access to Kibana license information.
1516
* @param license$ An observable of a License instance.
1617
* @public
1718
*/
1819
export function createRouteHandlerContext(
19-
license$: Observable<ILicense>
20+
license$: Observable<ILicense>,
21+
getStartServices: StartServicesAccessor<{}, LicensingPluginStart>
2022
): IContextProvider<RequestHandler<any, any, any>, 'licensing'> {
2123
return async function licensingRouteHandlerContext() {
22-
return { license: await license$.pipe(take(1)).toPromise() };
24+
const [, , { featureUsage }] = await getStartServices();
25+
const license = await license$.pipe(take(1)).toPromise();
26+
27+
return {
28+
featureUsage,
29+
license,
30+
};
2331
};
2432
}

x-pack/plugins/licensing/server/mocks.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66
import { BehaviorSubject } from 'rxjs';
7-
import { LicensingPluginSetup, LicensingPluginStart } from './types';
7+
import {
8+
LicensingPluginSetup,
9+
LicensingPluginStart,
10+
LicensingRequestHandlerContext,
11+
} from './types';
812
import { licenseMock } from '../common/licensing.mock';
913
import { featureUsageMock } from './services/feature_usage_service.mock';
1014

@@ -43,8 +47,20 @@ const createStartMock = (): jest.Mocked<LicensingPluginStart> => {
4347
return mock;
4448
};
4549

50+
const createRequestHandlerContextMock = (
51+
...options: Parameters<typeof licenseMock.createLicense>
52+
): jest.Mocked<LicensingRequestHandlerContext> => {
53+
const mock: jest.Mocked<LicensingRequestHandlerContext> = {
54+
license: licenseMock.createLicense(...options),
55+
featureUsage: featureUsageMock.createStart(),
56+
};
57+
58+
return mock;
59+
};
60+
4661
export const licensingMock = {
4762
createSetup: createSetupMock,
4863
createStart: createStartMock,
64+
createRequestHandlerContext: createRequestHandlerContextMock,
4965
...licenseMock,
5066
};

x-pack/plugins/licensing/server/plugin.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,10 @@ export class LicensingPlugin implements Plugin<LicensingPluginSetup, LicensingPl
128128
pollingFrequency.asMilliseconds()
129129
);
130130

131-
core.http.registerRouteHandlerContext('licensing', createRouteHandlerContext(license$));
131+
core.http.registerRouteHandlerContext(
132+
'licensing',
133+
createRouteHandlerContext(license$, core.getStartServices)
134+
);
132135

133136
registerRoutes(core.http.createRouter(), core.getStartServices);
134137
core.http.registerOnPreResponse(createOnPreResponseHandler(refresh, license$));

x-pack/plugins/licensing/server/types.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,18 @@ export interface RawLicense {
4040
mode: LicenseType;
4141
}
4242

43+
/**
44+
* The APIs exposed on the `licensing` key of {@link RequestHandlerContext} for plugins that depend on licensing.
45+
* @public
46+
*/
47+
export interface LicensingRequestHandlerContext {
48+
featureUsage: FeatureUsageServiceStart;
49+
license: ILicense;
50+
}
51+
4352
declare module 'src/core/server' {
4453
interface RequestHandlerContext {
45-
licensing: {
46-
license: ILicense;
47-
};
54+
licensing: LicensingRequestHandlerContext;
4855
}
4956
}
5057

0 commit comments

Comments
 (0)