Skip to content

Commit

Permalink
[7.x] [License Management] Do not break when telemetry.enabled:false (
Browse files Browse the repository at this point in the history
#69711) (#70065)

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
  • Loading branch information
afharo and elasticmachine authored Jun 26, 2020
1 parent 8788928 commit 14445bb
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { ScopedHistory } from 'kibana/public';

import { CoreStart } from '../../../../../src/core/public';
import { LicensingPluginSetup, ILicense } from '../../../licensing/public';
import { TelemetryPluginSetup } from '../../../../../src/plugins/telemetry/public';
import { TelemetryPluginStart } from '../../../../../src/plugins/telemetry/public';
import { ClientConfigType } from '../types';
import { BreadcrumbService } from './breadcrumbs';

Expand All @@ -23,7 +23,7 @@ export interface AppDependencies {
};
plugins: {
licensing: LicensingPluginSetup;
telemetry?: TelemetryPluginSetup;
telemetry?: TelemetryPluginStart;
};
docLinks: {
security: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* 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 { OptInExampleFlyout } from '../../../../../../../src/plugins/telemetry_management_section/public';

// required for lazy loading
// eslint-disable-next-line import/no-default-export
export default OptInExampleFlyout;
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@
*/

import React, { Fragment } from 'react';
import { EuiLink, EuiCheckbox, EuiSpacer, EuiText, EuiTitle, EuiPopover } from '@elastic/eui';
import { FormattedMessage } from '@kbn/i18n/react';
import {
OptInExampleFlyout,
PRIVACY_STATEMENT_URL,
TelemetryPluginSetup,
} from '../../lib/telemetry';
EuiLink,
EuiCheckbox,
EuiSpacer,
EuiText,
EuiTitle,
EuiPopover,
EuiLoadingSpinner,
} from '@elastic/eui';
import { FormattedMessage } from '@kbn/i18n/react';
import { TelemetryPluginStart } from '../../lib/telemetry';

const OptInExampleFlyout = React.lazy(() => import('./opt_in_example_flyout'));

interface State {
showMoreTelemetryInfo: boolean;
Expand All @@ -22,7 +28,7 @@ interface Props {
onOptInChange: (isOptingInToTelemetry: boolean) => void;
isOptingInToTelemetry: boolean;
isStartTrial: boolean;
telemetry: TelemetryPluginSetup;
telemetry: TelemetryPluginStart;
}

export class TelemetryOptIn extends React.Component<Props, State> {
Expand Down Expand Up @@ -54,11 +60,15 @@ export class TelemetryOptIn extends React.Component<Props, State> {

let example = null;
if (showExample) {
// Using React.Suspense and lazy loading here to avoid crashing the plugin when importing
// OptInExampleFlyout but telemetryManagementSection is disabled
example = (
<OptInExampleFlyout
onClose={() => this.setState({ showExample: false })}
fetchExample={telemetry.telemetryService.fetchExample}
/>
<React.Suspense fallback={<EuiLoadingSpinner />}>
<OptInExampleFlyout
onClose={() => this.setState({ showExample: false })}
fetchExample={telemetry.telemetryService.fetchExample}
/>
</React.Suspense>
);
}

Expand Down Expand Up @@ -116,7 +126,10 @@ export class TelemetryOptIn extends React.Component<Props, State> {
</EuiLink>
),
telemetryPrivacyStatementLink: (
<EuiLink href={PRIVACY_STATEMENT_URL} target="_blank">
<EuiLink
href={telemetry.telemetryConstants.getPrivacyStatementUrl()}
target="_blank"
>
<FormattedMessage
id="xpack.licenseMgmt.telemetryOptIn.telemetryPrivacyStatementLinkText"
defaultMessage="telemetry privacy statement"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { TelemetryPluginSetup } from '../../../../../../src/plugins/telemetry/public';
import type { TelemetryPluginStart } from '../../../../../../src/plugins/telemetry/public';

export { OptInExampleFlyout } from '../../../../../../src/plugins/telemetry_management_section/public/';
export { PRIVACY_STATEMENT_URL } from '../../../../../../src/plugins/telemetry/common/constants';
export { TelemetryPluginSetup, shouldShowTelemetryOptIn };
export type { TelemetryPluginStart } from '../../../../../../src/plugins/telemetry/public';
export { shouldShowTelemetryOptIn };

function shouldShowTelemetryOptIn(
telemetry?: TelemetryPluginSetup
): telemetry is TelemetryPluginSetup {
telemetry?: TelemetryPluginStart
): telemetry is TelemetryPluginStart {
if (telemetry) {
const { telemetryService } = telemetry;
const isOptedIn = telemetryService.getIsOptedIn();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ import { FormattedMessage } from '@kbn/i18n/react';
import { TelemetryOptIn } from '../../../components/telemetry_opt_in';
import { EXTERNAL_LINKS } from '../../../../../common/constants';
import { AppContextConsumer, AppDependencies } from '../../../app_context';
import { TelemetryPluginSetup, shouldShowTelemetryOptIn } from '../../../lib/telemetry';
import { TelemetryPluginStart, shouldShowTelemetryOptIn } from '../../../lib/telemetry';

interface Props {
loadTrialStatus: () => void;
startLicenseTrial: () => void;
telemetry?: TelemetryPluginSetup;
telemetry?: TelemetryPluginStart;
shouldShowStartTrial: boolean;
}

Expand Down
18 changes: 12 additions & 6 deletions x-pack/plugins/license_management/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,21 @@
import { first } from 'rxjs/operators';
import { CoreSetup, Plugin, PluginInitializerContext } from 'src/core/public';

import { TelemetryPluginSetup } from '../../../../src/plugins/telemetry/public';
import { TelemetryPluginStart } from '../../../../src/plugins/telemetry/public';
import { ManagementSetup, ManagementSectionId } from '../../../../src/plugins/management/public';
import { LicensingPluginSetup } from '../../../plugins/licensing/public';
import { PLUGIN } from '../common/constants';
import { ClientConfigType } from './types';
import { AppDependencies } from './application';
import { BreadcrumbService } from './application/breadcrumbs';

interface PluginsDependencies {
interface PluginsDependenciesSetup {
management: ManagementSetup;
licensing: LicensingPluginSetup;
telemetry?: TelemetryPluginSetup;
}

interface PluginsDependenciesStart {
telemetry?: TelemetryPluginStart;
}

export interface LicenseManagementUIPluginSetup {
Expand All @@ -31,7 +34,10 @@ export class LicenseManagementUIPlugin

constructor(private readonly initializerContext: PluginInitializerContext) {}

setup(coreSetup: CoreSetup, plugins: PluginsDependencies): LicenseManagementUIPluginSetup {
setup(
coreSetup: CoreSetup<PluginsDependenciesStart>,
plugins: PluginsDependenciesSetup
): LicenseManagementUIPluginSetup {
const config = this.initializerContext.config.get<ClientConfigType>();

if (!config.ui.enabled) {
Expand All @@ -42,14 +48,14 @@ export class LicenseManagementUIPlugin
}

const { getStartServices } = coreSetup;
const { management, telemetry, licensing } = plugins;
const { management, licensing } = plugins;

management.sections.getSection(ManagementSectionId.Stack).registerApp({
id: PLUGIN.id,
title: PLUGIN.title,
order: 0,
mount: async ({ element, setBreadcrumbs, history }) => {
const [core] = await getStartServices();
const [core, { telemetry }] = await getStartServices();
const initialLicense = await plugins.licensing.license$.pipe(first()).toPromise();

// Setup documentation links
Expand Down

0 comments on commit 14445bb

Please sign in to comment.