Skip to content

Commit

Permalink
Clean up license service
Browse files Browse the repository at this point in the history
  • Loading branch information
jen-huang committed Sep 16, 2020
1 parent d39767c commit 1062d93
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 81 deletions.
1 change: 1 addition & 0 deletions x-pack/plugins/ingest_manager/common/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ export { isPackageLimited, doesAgentPolicyAlreadyIncludePackage } from './limite
export { decodeCloudId } from './decode_cloud_id';
export { isValidNamespace } from './is_valid_namespace';
export { isDiffPathProtocol } from './is_diff_path_protocol';
export { LicenseService } from './license';
46 changes: 46 additions & 0 deletions x-pack/plugins/ingest_manager/common/services/license.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { Observable, Subscription } from 'rxjs';
import { ILicense } from '../../../licensing/common/types';

// Generic license service class that works with the license observable
// Both server and client plugins instancates a singleton version of this class
export class LicenseService {
private observable: Observable<ILicense> | null = null;
private subscription: Subscription | null = null;
private licenseInformation: ILicense | null = null;

private updateInformation(licenseInformation: ILicense) {
this.licenseInformation = licenseInformation;
}

public start(license$: Observable<ILicense>) {
this.observable = license$;
this.subscription = this.observable.subscribe(this.updateInformation.bind(this));
}

public stop() {
if (this.subscription) {
this.subscription.unsubscribe();
}
}

public getLicenseInformation() {
return this.licenseInformation;
}

public getLicenseInformation$() {
return this.observable;
}

public isGoldPlus() {
return (
this.licenseInformation?.isAvailable &&
this.licenseInformation?.isActive &&
this.licenseInformation?.hasAtLeast('gold')
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export { useCapabilities } from './use_capabilities';
export { useCore } from './use_core';
export { useConfig, ConfigContext } from './use_config';
export { useSetupDeps, useStartDeps, DepsContext } from './use_deps';
export { useLicense } from './use_license';
export { licenseService, useLicense } from './use_license';
export { useBreadcrumbs } from './use_breadcrumbs';
export { useLink } from './use_link';
export { useKibanaLink } from './use_kibana_link';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,10 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { Observable, Subscription } from 'rxjs';
import { ILicense } from '../../../../../licensing/public';
import { useSetupDeps } from './use_deps';
import { LicenseService } from '../services';

// TODO: Make this better lol
class LicenseService {
private observable: Observable<ILicense> | null = null;
private subscription: Subscription | null = null;
private licenseInformation: ILicense | null = null;

private updateInformation(licenseInformation: ILicense) {
this.licenseInformation = licenseInformation;
}

public start(license$: Observable<ILicense>) {
this.observable = license$;
this.subscription = this.observable.subscribe(this.updateInformation.bind(this));
}

public stop() {
if (this.subscription) {
this.subscription.unsubscribe();
}
}

public getLicenseInformation() {
return this.licenseInformation;
}

public getLicenseInformation$() {
return this.observable;
}
}
export const licenseService = new LicenseService();

export function useLicense() {
const { licensing } = useSetupDeps();
const licenseService = new LicenseService();
licenseService.start(licensing.license$);
return licenseService;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,16 @@ import { PAGE_ROUTING_PATHS } from './constants';
import { DefaultLayout, WithoutHeaderLayout } from './layouts';
import { Loading, Error } from './components';
import { IngestManagerOverview, EPMApp, AgentPolicyApp, FleetApp, DataStreamApp } from './sections';
import { DepsContext, ConfigContext, useConfig } from './hooks';
import {
DepsContext,
ConfigContext,
useConfig,
useCore,
sendSetup,
sendGetPermissionsCheck,
licenseService,
} from './hooks';
import { PackageInstallProvider } from './sections/epm/hooks';
import { useCore, sendSetup, sendGetPermissionsCheck } from './hooks';
import { FleetStatusProvider } from './hooks/use_fleet_status';
import './index.scss';
import { KibanaContextProvider } from '../../../../../../src/plugins/kibana_react/public';
Expand Down Expand Up @@ -279,4 +286,5 @@ export function renderApp(
export const teardownIngestManager = (coreStart: CoreStart) => {
coreStart.chrome.docTitle.reset();
coreStart.chrome.setBreadcrumbs([]);
licenseService.stop();
};
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,7 @@ export const AgentListPage: React.FunctionComponent<{}> = () => {
const { getHref } = useLink();
const defaultKuery: string = (useUrlParams().urlParams.kuery as string) || '';
const hasWriteCapabilites = useCapabilities().write;
const licenseService = useLicense();
const license = licenseService.getLicenseInformation();
const isGoldPlus = license?.isAvailable && license?.isActive && license?.hasAtLeast('gold');
const isGoldPlus = useLicense().isGoldPlus();

// Agent data states
const [showInactive, setShowInactive] = useState<boolean>(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ export {
isPackageLimited,
doesAgentPolicyAlreadyIncludePackage,
isValidNamespace,
LicenseService,
} from '../../../../common';
5 changes: 4 additions & 1 deletion x-pack/plugins/ingest_manager/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { BASE_PATH } from './applications/ingest_manager/constants';

import { IngestManagerConfigType } from '../common/types';
import { setupRouteService, appRoutesService } from '../common';
import { setHttpClient } from './applications/ingest_manager/hooks';
import { setHttpClient, licenseService } from './applications/ingest_manager/hooks';
import {
TutorialDirectoryNotice,
TutorialDirectoryHeaderLink,
Expand Down Expand Up @@ -71,6 +71,9 @@ export class IngestManagerPlugin
// Set up http client
setHttpClient(core.http);

// Set up license service
licenseService.start(deps.licensing.license$);

// Register main Ingest Manager app
core.application.register({
id: PLUGIN_ID,
Expand Down
40 changes: 1 addition & 39 deletions x-pack/plugins/ingest_manager/server/services/license.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,6 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { Observable, Subscription } from 'rxjs';
import { ILicense } from '../../../licensing/server';

class LicenseService {
private observable: Observable<ILicense> | null = null;
private subscription: Subscription | null = null;
private licenseInformation: ILicense | null = null;

private updateInformation(licenseInformation: ILicense) {
this.licenseInformation = licenseInformation;
}

public start(license$: Observable<ILicense>) {
this.observable = license$;
this.subscription = this.observable.subscribe(this.updateInformation.bind(this));
}

public stop() {
if (this.subscription) {
this.subscription.unsubscribe();
}
}

public getLicenseInformation() {
return this.licenseInformation;
}

public getLicenseInformation$() {
return this.observable;
}

public isGoldPlus() {
return (
this.licenseInformation?.isAvailable &&
this.licenseInformation?.isActive &&
this.licenseInformation?.hasAtLeast('gold')
);
}
}
import { LicenseService } from '../../common';

export const licenseService = new LicenseService();

0 comments on commit 1062d93

Please sign in to comment.