Skip to content

Commit 1062d93

Browse files
committed
Clean up license service
1 parent d39767c commit 1062d93

9 files changed

Lines changed: 67 additions & 81 deletions

File tree

x-pack/plugins/ingest_manager/common/services/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ export { isPackageLimited, doesAgentPolicyAlreadyIncludePackage } from './limite
1212
export { decodeCloudId } from './decode_cloud_id';
1313
export { isValidNamespace } from './is_valid_namespace';
1414
export { isDiffPathProtocol } from './is_diff_path_protocol';
15+
export { LicenseService } from './license';
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
import { Observable, Subscription } from 'rxjs';
7+
import { ILicense } from '../../../licensing/common/types';
8+
9+
// Generic license service class that works with the license observable
10+
// Both server and client plugins instancates a singleton version of this class
11+
export class LicenseService {
12+
private observable: Observable<ILicense> | null = null;
13+
private subscription: Subscription | null = null;
14+
private licenseInformation: ILicense | null = null;
15+
16+
private updateInformation(licenseInformation: ILicense) {
17+
this.licenseInformation = licenseInformation;
18+
}
19+
20+
public start(license$: Observable<ILicense>) {
21+
this.observable = license$;
22+
this.subscription = this.observable.subscribe(this.updateInformation.bind(this));
23+
}
24+
25+
public stop() {
26+
if (this.subscription) {
27+
this.subscription.unsubscribe();
28+
}
29+
}
30+
31+
public getLicenseInformation() {
32+
return this.licenseInformation;
33+
}
34+
35+
public getLicenseInformation$() {
36+
return this.observable;
37+
}
38+
39+
public isGoldPlus() {
40+
return (
41+
this.licenseInformation?.isAvailable &&
42+
this.licenseInformation?.isActive &&
43+
this.licenseInformation?.hasAtLeast('gold')
44+
);
45+
}
46+
}

x-pack/plugins/ingest_manager/public/applications/ingest_manager/hooks/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export { useCapabilities } from './use_capabilities';
88
export { useCore } from './use_core';
99
export { useConfig, ConfigContext } from './use_config';
1010
export { useSetupDeps, useStartDeps, DepsContext } from './use_deps';
11-
export { useLicense } from './use_license';
11+
export { licenseService, useLicense } from './use_license';
1212
export { useBreadcrumbs } from './use_breadcrumbs';
1313
export { useLink } from './use_link';
1414
export { useKibanaLink } from './use_kibana_link';

x-pack/plugins/ingest_manager/public/applications/ingest_manager/hooks/use_license.ts

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,10 @@
33
* or more contributor license agreements. Licensed under the Elastic License;
44
* you may not use this file except in compliance with the Elastic License.
55
*/
6-
import { Observable, Subscription } from 'rxjs';
7-
import { ILicense } from '../../../../../licensing/public';
8-
import { useSetupDeps } from './use_deps';
6+
import { LicenseService } from '../services';
97

10-
// TODO: Make this better lol
11-
class LicenseService {
12-
private observable: Observable<ILicense> | null = null;
13-
private subscription: Subscription | null = null;
14-
private licenseInformation: ILicense | null = null;
15-
16-
private updateInformation(licenseInformation: ILicense) {
17-
this.licenseInformation = licenseInformation;
18-
}
19-
20-
public start(license$: Observable<ILicense>) {
21-
this.observable = license$;
22-
this.subscription = this.observable.subscribe(this.updateInformation.bind(this));
23-
}
24-
25-
public stop() {
26-
if (this.subscription) {
27-
this.subscription.unsubscribe();
28-
}
29-
}
30-
31-
public getLicenseInformation() {
32-
return this.licenseInformation;
33-
}
34-
35-
public getLicenseInformation$() {
36-
return this.observable;
37-
}
38-
}
8+
export const licenseService = new LicenseService();
399

4010
export function useLicense() {
41-
const { licensing } = useSetupDeps();
42-
const licenseService = new LicenseService();
43-
licenseService.start(licensing.license$);
4411
return licenseService;
4512
}

x-pack/plugins/ingest_manager/public/applications/ingest_manager/index.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,16 @@ import { PAGE_ROUTING_PATHS } from './constants';
2222
import { DefaultLayout, WithoutHeaderLayout } from './layouts';
2323
import { Loading, Error } from './components';
2424
import { IngestManagerOverview, EPMApp, AgentPolicyApp, FleetApp, DataStreamApp } from './sections';
25-
import { DepsContext, ConfigContext, useConfig } from './hooks';
25+
import {
26+
DepsContext,
27+
ConfigContext,
28+
useConfig,
29+
useCore,
30+
sendSetup,
31+
sendGetPermissionsCheck,
32+
licenseService,
33+
} from './hooks';
2634
import { PackageInstallProvider } from './sections/epm/hooks';
27-
import { useCore, sendSetup, sendGetPermissionsCheck } from './hooks';
2835
import { FleetStatusProvider } from './hooks/use_fleet_status';
2936
import './index.scss';
3037
import { KibanaContextProvider } from '../../../../../../src/plugins/kibana_react/public';
@@ -279,4 +286,5 @@ export function renderApp(
279286
export const teardownIngestManager = (coreStart: CoreStart) => {
280287
coreStart.chrome.docTitle.reset();
281288
coreStart.chrome.setBreadcrumbs([]);
289+
licenseService.stop();
282290
};

x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/fleet/agent_list_page/index.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,7 @@ export const AgentListPage: React.FunctionComponent<{}> = () => {
145145
const { getHref } = useLink();
146146
const defaultKuery: string = (useUrlParams().urlParams.kuery as string) || '';
147147
const hasWriteCapabilites = useCapabilities().write;
148-
const licenseService = useLicense();
149-
const license = licenseService.getLicenseInformation();
150-
const isGoldPlus = license?.isAvailable && license?.isActive && license?.hasAtLeast('gold');
148+
const isGoldPlus = useLicense().isGoldPlus();
151149

152150
// Agent data states
153151
const [showInactive, setShowInactive] = useState<boolean>(false);

x-pack/plugins/ingest_manager/public/applications/ingest_manager/services/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ export {
2525
isPackageLimited,
2626
doesAgentPolicyAlreadyIncludePackage,
2727
isValidNamespace,
28+
LicenseService,
2829
} from '../../../../common';

x-pack/plugins/ingest_manager/public/plugin.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { BASE_PATH } from './applications/ingest_manager/constants';
2323

2424
import { IngestManagerConfigType } from '../common/types';
2525
import { setupRouteService, appRoutesService } from '../common';
26-
import { setHttpClient } from './applications/ingest_manager/hooks';
26+
import { setHttpClient, licenseService } from './applications/ingest_manager/hooks';
2727
import {
2828
TutorialDirectoryNotice,
2929
TutorialDirectoryHeaderLink,
@@ -71,6 +71,9 @@ export class IngestManagerPlugin
7171
// Set up http client
7272
setHttpClient(core.http);
7373

74+
// Set up license service
75+
licenseService.start(deps.licensing.license$);
76+
7477
// Register main Ingest Manager app
7578
core.application.register({
7679
id: PLUGIN_ID,

x-pack/plugins/ingest_manager/server/services/license.ts

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,6 @@
33
* or more contributor license agreements. Licensed under the Elastic License;
44
* you may not use this file except in compliance with the Elastic License.
55
*/
6-
import { Observable, Subscription } from 'rxjs';
7-
import { ILicense } from '../../../licensing/server';
8-
9-
class LicenseService {
10-
private observable: Observable<ILicense> | null = null;
11-
private subscription: Subscription | null = null;
12-
private licenseInformation: ILicense | null = null;
13-
14-
private updateInformation(licenseInformation: ILicense) {
15-
this.licenseInformation = licenseInformation;
16-
}
17-
18-
public start(license$: Observable<ILicense>) {
19-
this.observable = license$;
20-
this.subscription = this.observable.subscribe(this.updateInformation.bind(this));
21-
}
22-
23-
public stop() {
24-
if (this.subscription) {
25-
this.subscription.unsubscribe();
26-
}
27-
}
28-
29-
public getLicenseInformation() {
30-
return this.licenseInformation;
31-
}
32-
33-
public getLicenseInformation$() {
34-
return this.observable;
35-
}
36-
37-
public isGoldPlus() {
38-
return (
39-
this.licenseInformation?.isAvailable &&
40-
this.licenseInformation?.isActive &&
41-
this.licenseInformation?.hasAtLeast('gold')
42-
);
43-
}
44-
}
6+
import { LicenseService } from '../../common';
457

468
export const licenseService = new LicenseService();

0 commit comments

Comments
 (0)