Skip to content

Commit

Permalink
Only register watcher app if we have received an indication of licens…
Browse files Browse the repository at this point in the history
…e first

Enable app to react to license updates from backend
  • Loading branch information
jloleysens committed Jan 24, 2020
1 parent fee22e8 commit d52c6e1
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 62 deletions.
13 changes: 10 additions & 3 deletions x-pack/plugins/watcher/public/application/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
* you may not use this file except in compliance with the Elastic License.
*/

import React from 'react';
import React, { useEffect, useState } from 'react';
import { Observable } from 'rxjs';
import {
ChromeStart,
DocLinksStart,
Expand Down Expand Up @@ -47,12 +48,18 @@ export interface AppDeps {
uiSettings: IUiSettingsClient;
euiUtils: any;
createTimeBuckets: () => any;
getLicenseStatus: () => LicenseStatus;
initialLicenseStatus: LicenseStatus;
licenseStatus$: Observable<LicenseStatus>;
MANAGEMENT_BREADCRUMB: any;
}

export const App = (deps: AppDeps) => {
const { valid, message } = deps.getLicenseStatus();
const [{ valid, message }, setLicenseStatus] = useState(deps.initialLicenseStatus);

useEffect(() => {
const s = deps.licenseStatus$.subscribe(setLicenseStatus);
return () => s.unsubscribe();
}, [deps.licenseStatus$]);

if (!valid) {
return (
Expand Down
117 changes: 58 additions & 59 deletions x-pack/plugins/watcher/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,81 +5,80 @@
*/
import { i18n } from '@kbn/i18n';
import { CoreSetup, Plugin, CoreStart } from 'kibana/public';
import { first, map, skip } from 'rxjs/operators';

import { FeatureCatalogueCategory } from '../../../../src/plugins/home/public';

import { LicenseStatus } from '../common/types/license_status';

import { LICENSE_CHECK_STATE } from '../../licensing/public';
import { ILicense, LICENSE_CHECK_STATE } from '../../licensing/public';
import { TimeBuckets, MANAGEMENT_BREADCRUMB } from './legacy';
import { PLUGIN } from '../common/constants';
import { Dependencies } from './types';
import { License } from '../../licensing/common/license';

export class WatcherUIPlugin implements Plugin<void, void, Dependencies, any> {
// Reference for when `mount` gets called, we don't want to render if
// we don't have a valid license. Under certain conditions the Watcher app link
// may still be present so this is a final guard.
private licenseStatus: LicenseStatus = { valid: false };
private hasRegisteredESManagementSection = false;
const licenseToLicenseStatus = (license: License): LicenseStatus => {
const { state, message } = license.check(PLUGIN.ID, PLUGIN.MINIMUM_LICENSE_REQUIRED);
return {
valid: state === LICENSE_CHECK_STATE.Valid && license.getFeature(PLUGIN.ID).isAvailable,
message,
};
};

setup(
export class WatcherUIPlugin implements Plugin<void, void, Dependencies, any> {
async setup(
{ application, notifications, http, uiSettings, getStartServices }: CoreSetup,
{ licensing, management, data, home }: Dependencies
) {
licensing.license$.subscribe(license => {
const { state, message } = license.check(PLUGIN.ID, PLUGIN.MINIMUM_LICENSE_REQUIRED);
this.licenseStatus = {
valid: state === LICENSE_CHECK_STATE.Valid && license.getFeature(PLUGIN.ID).isAvailable,
message,
};
if (this.licenseStatus.valid) {
const esSection = management.sections.getSection('elasticsearch');
if (esSection && !this.hasRegisteredESManagementSection) {
esSection.registerApp({
id: 'watcher',
title: i18n.translate(
'xpack.watcher.sections.watchList.managementSection.watcherDisplayName',
{ defaultMessage: 'Watcher' }
),
mount: async ({ element }) => {
const [core, plugins] = await getStartServices();
const { chrome, i18n: i18nDep, docLinks, savedObjects } = core;
const { eui_utils } = plugins as any;
const { boot } = await import('./application/boot');
const esSection = management.sections.getSection('elasticsearch');
const initialLicenseStatus = await licensing.license$
.pipe<ILicense>(first(), map(licenseToLicenseStatus))
.toPromise();

return boot({
getLicenseStatus: () => this.licenseStatus,
element,
toasts: notifications.toasts,
http,
uiSettings,
docLinks,
chrome,
euiUtils: eui_utils,
savedObjects: savedObjects.client,
I18nContext: i18nDep.Context,
createTimeBuckets: () => new TimeBuckets(uiSettings, data),
MANAGEMENT_BREADCRUMB,
});
},
});
if (!initialLicenseStatus.valid) {
return;
}

home.featureCatalogue.register({
id: 'watcher',
title: 'Watcher', // This is a product name so we don't translate it.
category: FeatureCatalogueCategory.ADMIN,
description: i18n.translate('xpack.watcher.watcherDescription', {
defaultMessage:
'Detect changes in your data by creating, managing, and monitoring alerts.',
}),
icon: 'watchesApp',
path: '/app/kibana#/management/elasticsearch/watcher/watches',
showOnHomePage: true,
});
esSection!.registerApp({
id: 'watcher',
title: i18n.translate(
'xpack.watcher.sections.watchList.managementSection.watcherDisplayName',
{ defaultMessage: 'Watcher' }
),
mount: async ({ element }) => {
const [core, plugins] = await getStartServices();
const { chrome, i18n: i18nDep, docLinks, savedObjects } = core;
const { eui_utils } = plugins as any;
const { boot } = await import('./application/boot');

return boot({
initialLicenseStatus,
licenseStatus$: licensing.license$.pipe(skip(1), map(licenseToLicenseStatus)),
element,
toasts: notifications.toasts,
http,
uiSettings,
docLinks,
chrome,
euiUtils: eui_utils,
savedObjects: savedObjects.client,
I18nContext: i18nDep.Context,
createTimeBuckets: () => new TimeBuckets(uiSettings, data),
MANAGEMENT_BREADCRUMB,
});
},
});

this.hasRegisteredESManagementSection = true;
}
}
home.featureCatalogue.register({
id: 'watcher',
title: 'Watcher', // This is a product name so we don't translate it.
category: FeatureCatalogueCategory.ADMIN,
description: i18n.translate('xpack.watcher.watcherDescription', {
defaultMessage: 'Detect changes in your data by creating, managing, and monitoring alerts.',
}),
icon: 'watchesApp',
path: '/app/kibana#/management/elasticsearch/watcher/watches',
showOnHomePage: true,
});
}

Expand Down

0 comments on commit d52c6e1

Please sign in to comment.