From 0e928bb1365e60664056cea31eb4af521e640cd6 Mon Sep 17 00:00:00 2001 From: Yulia Cech Date: Fri, 10 Mar 2023 18:35:25 +0100 Subject: [PATCH 1/8] [License Management] Add a url locator --- .../plugins/license_management/kibana.jsonc | 3 +- .../public/application/app.js | 3 +- .../license_management/public/locator.ts | 51 +++++++++++++++++++ .../license_management/public/plugin.ts | 15 +++++- 4 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 x-pack/plugins/license_management/public/locator.ts diff --git a/x-pack/plugins/license_management/kibana.jsonc b/x-pack/plugins/license_management/kibana.jsonc index e0aef2c4aec73..b6113ad7a028c 100644 --- a/x-pack/plugins/license_management/kibana.jsonc +++ b/x-pack/plugins/license_management/kibana.jsonc @@ -14,7 +14,8 @@ "home", "licensing", "management", - "features" + "features", + "share" ], "optionalPlugins": [ "telemetry" diff --git a/x-pack/plugins/license_management/public/application/app.js b/x-pack/plugins/license_management/public/application/app.js index 6107de7c619fc..f22285e2af04c 100644 --- a/x-pack/plugins/license_management/public/application/app.js +++ b/x-pack/plugins/license_management/public/application/app.js @@ -17,6 +17,7 @@ import { EuiPageBody, EuiEmptyPrompt, } from '@elastic/eui'; +import { UPLOAD_LICENSE_ROUTE } from '../locator'; export const App = ({ hasPermission, @@ -102,7 +103,7 @@ export const App = ({ return ( - + diff --git a/x-pack/plugins/license_management/public/locator.ts b/x-pack/plugins/license_management/public/locator.ts new file mode 100644 index 0000000000000..f4edae7422165 --- /dev/null +++ b/x-pack/plugins/license_management/public/locator.ts @@ -0,0 +1,51 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import type { SerializableRecord } from '@kbn/utility-types'; +import { ManagementAppLocator } from '@kbn/management-plugin/common'; +import { LocatorDefinition, LocatorPublic} from '@kbn/share-plugin/public'; +import { PLUGIN } from '../common/constants'; + +export const LICENSE_MANAGEMENT_LOCATOR_ID = 'LICENSE_MANAGEMENT_LOCATOR'; +export const UPLOAD_LICENSE_ROUTE = 'upload_license'; + +export interface LicenseManagementLocatorParams extends SerializableRecord { + page: 'dashboard' | 'upload_license'; +} + +export type LicenseManagementLocator = LocatorPublic; + +export interface LicenseManagementLocatorDefinitionDependencies { + managementAppLocator: ManagementAppLocator; +} + +export class LicenseManagementLocatorDefinition + implements LocatorDefinition +{ + constructor(protected readonly deps: LicenseManagementLocatorDefinitionDependencies) {} + + public readonly id = LICENSE_MANAGEMENT_LOCATOR_ID; + + public readonly getLocation = async (params: LicenseManagementLocatorParams) => { + const location = await this.deps.managementAppLocator.getLocation({ + sectionId: 'stack', + appId: PLUGIN.id, + }); + + switch (params.page) { + case 'upload_license': { + return { + ...location, + path: `${location.path}/${UPLOAD_LICENSE_ROUTE}`, + }; + } + case 'dashboard': { + return location; + } + } + }; +} diff --git a/x-pack/plugins/license_management/public/plugin.ts b/x-pack/plugins/license_management/public/plugin.ts index 204fa3511ad3f..09dc79c55a5c1 100644 --- a/x-pack/plugins/license_management/public/plugin.ts +++ b/x-pack/plugins/license_management/public/plugin.ts @@ -11,14 +11,17 @@ import { CoreSetup, Plugin, PluginInitializerContext } from '@kbn/core/public'; import { TelemetryPluginStart } from '@kbn/telemetry-plugin/public'; import { ManagementSetup } from '@kbn/management-plugin/public'; import { LicensingPluginSetup } from '@kbn/licensing-plugin/public'; +import { SharePluginSetup } from '@kbn/share-plugin/public'; import { PLUGIN } from '../common/constants'; import { ClientConfigType } from './types'; import { AppDependencies } from './application'; import { BreadcrumbService } from './application/breadcrumbs'; +import { LicenseManagementLocator, LicenseManagementLocatorDefinition } from './locator'; interface PluginsDependenciesSetup { management: ManagementSetup; licensing: LicensingPluginSetup; + share: SharePluginSetup; } interface PluginsDependenciesStart { @@ -27,6 +30,7 @@ interface PluginsDependenciesStart { export interface LicenseManagementUIPluginSetup { enabled: boolean; + locator: undefined | LicenseManagementLocator; } export type LicenseManagementUIPluginStart = void; @@ -34,6 +38,7 @@ export class LicenseManagementUIPlugin implements Plugin { private breadcrumbService = new BreadcrumbService(); + private locator?: LicenseManagementLocator; constructor(private readonly initializerContext: PluginInitializerContext) {} @@ -47,11 +52,18 @@ export class LicenseManagementUIPlugin // No need to go any further return { enabled: false, + locator: this.locator, }; } const { getStartServices } = coreSetup; - const { management, licensing } = plugins; + const { management, licensing, share } = plugins; + + this.locator = share.url.locators.create( + new LicenseManagementLocatorDefinition({ + managementAppLocator: management.locator, + }) + ); management.sections.section.stack.registerApp({ id: PLUGIN.id, @@ -105,6 +117,7 @@ export class LicenseManagementUIPlugin return { enabled: true, + locator: this.locator, }; } From 2c6d696b5f72cdf7d213b1fc4b0f687ff931a2a8 Mon Sep 17 00:00:00 2001 From: Yulia Cech Date: Fri, 10 Mar 2023 18:36:53 +0100 Subject: [PATCH 2/8] [Watcher] Use a url locator for License Management --- x-pack/plugins/watcher/kibana.jsonc | 3 ++ .../watcher/public/application/app.tsx | 38 +++++++++++++------ x-pack/plugins/watcher/public/plugin.ts | 3 +- x-pack/plugins/watcher/public/types.ts | 2 + 4 files changed, 34 insertions(+), 12 deletions(-) diff --git a/x-pack/plugins/watcher/kibana.jsonc b/x-pack/plugins/watcher/kibana.jsonc index 8d70a5ecbfe40..c5010ee62ad9d 100644 --- a/x-pack/plugins/watcher/kibana.jsonc +++ b/x-pack/plugins/watcher/kibana.jsonc @@ -19,6 +19,9 @@ "data", "features" ], + "optionalPlugins": [ + "licenseManagement" + ], "requiredBundles": [ "esUiShared", "kibanaReact", diff --git a/x-pack/plugins/watcher/public/application/app.tsx b/x-pack/plugins/watcher/public/application/app.tsx index d1ebdae78955f..3035944c52302 100644 --- a/x-pack/plugins/watcher/public/application/app.tsx +++ b/x-pack/plugins/watcher/public/application/app.tsx @@ -27,6 +27,7 @@ import { FormattedMessage } from '@kbn/i18n-react'; import { RegisterManagementAppArgs, ManagementAppMountParams } from '@kbn/management-plugin/public'; import { ChartsPluginSetup } from '@kbn/charts-plugin/public'; +import { LicenseManagementLocator } from '@kbn/license-management-plugin/public/locator'; import { LicenseStatus } from '../../common/types/license_status'; import { WatchListPage, WatchEditPage, WatchStatusPage } from './sections'; import { registerRouter } from './lib/navigation'; @@ -49,6 +50,7 @@ export interface AppDeps { history: ManagementAppMountParams['history']; getUrlForApp: ApplicationStart['getUrlForApp']; executionContext: ExecutionContextStart; + licenseManagementLocator?: LicenseManagementLocator; } export const App = (deps: AppDeps) => { @@ -60,6 +62,29 @@ export const App = (deps: AppDeps) => { }, [deps.licenseStatus$]); if (!valid) { + const licenseManagementUrl = deps.licenseManagementLocator?.useUrl({ page: 'dashboard' }); + // if there is no licenseManagementUrl, the license management plugin might be disabled + const promptAction = licenseManagementUrl ? ( + + + + ) : undefined; + const promptBody = licenseManagementUrl ? ( +

{message}

+ ) : ( + <> +

{message}

+

+ +

+ + ); return ( { /> } - body={

{message}

} - actions={[ - - - , - ]} + body={promptBody} + actions={[promptAction]} />
); diff --git a/x-pack/plugins/watcher/public/plugin.ts b/x-pack/plugins/watcher/public/plugin.ts index 9a6564e6e286c..4305400a9c951 100644 --- a/x-pack/plugins/watcher/public/plugin.ts +++ b/x-pack/plugins/watcher/public/plugin.ts @@ -29,7 +29,7 @@ export class WatcherUIPlugin implements Plugin { setup( { notifications, http, uiSettings, getStartServices }: CoreSetup, - { licensing, management, data, home, charts }: Dependencies + { licensing, management, data, home, charts, licenseManagement }: Dependencies ) { const esSection = management.sections.section.insightsAndAlerting; @@ -75,6 +75,7 @@ export class WatcherUIPlugin implements Plugin { history, getUrlForApp: application.getUrlForApp, theme$, + licenseManagementLocator: licenseManagement?.locator, executionContext, }); diff --git a/x-pack/plugins/watcher/public/types.ts b/x-pack/plugins/watcher/public/types.ts index 857acdc2109a1..9a3c1ac5ae2f7 100644 --- a/x-pack/plugins/watcher/public/types.ts +++ b/x-pack/plugins/watcher/public/types.ts @@ -10,6 +10,7 @@ import { ChartsPluginStart } from '@kbn/charts-plugin/public'; import { LicensingPluginSetup } from '@kbn/licensing-plugin/public'; import { DataPublicPluginSetup } from '@kbn/data-plugin/public'; import { HomePublicPluginSetup } from '@kbn/home-plugin/public'; +import { LicenseManagementUIPluginSetup } from '@kbn/license-management-plugin/public'; export interface Dependencies { home: HomePublicPluginSetup; @@ -17,4 +18,5 @@ export interface Dependencies { licensing: LicensingPluginSetup; charts: ChartsPluginStart; data: DataPublicPluginSetup; + licenseManagement?: LicenseManagementUIPluginSetup; } From 86f6de167a67a69ce93570bf6d20a2d022c2dcc2 Mon Sep 17 00:00:00 2001 From: kibanamachine <42973632+kibanamachine@users.noreply.github.com> Date: Mon, 27 Mar 2023 16:03:11 +0000 Subject: [PATCH 3/8] [CI] Auto-commit changed files from 'node scripts/lint_ts_projects --fix' --- x-pack/plugins/license_management/tsconfig.json | 2 ++ x-pack/plugins/watcher/tsconfig.json | 1 + 2 files changed, 3 insertions(+) diff --git a/x-pack/plugins/license_management/tsconfig.json b/x-pack/plugins/license_management/tsconfig.json index c1ff3321ca87c..479f60cb75470 100644 --- a/x-pack/plugins/license_management/tsconfig.json +++ b/x-pack/plugins/license_management/tsconfig.json @@ -25,6 +25,8 @@ "@kbn/config-schema", "@kbn/test-jest-helpers", "@kbn/shared-ux-router", + "@kbn/utility-types", + "@kbn/share-plugin", ], "exclude": [ "target/**/*", diff --git a/x-pack/plugins/watcher/tsconfig.json b/x-pack/plugins/watcher/tsconfig.json index 6b40217aa0bcc..c09ca92cb70fe 100644 --- a/x-pack/plugins/watcher/tsconfig.json +++ b/x-pack/plugins/watcher/tsconfig.json @@ -33,6 +33,7 @@ "@kbn/i18n-react", "@kbn/ace", "@kbn/shared-ux-router", + "@kbn/license-management-plugin", ], "exclude": [ "target/**/*", From d1c6f865698ed1d2b28d1b196e57c098d7604ebb Mon Sep 17 00:00:00 2001 From: kibanamachine <42973632+kibanamachine@users.noreply.github.com> Date: Mon, 27 Mar 2023 16:33:31 +0000 Subject: [PATCH 4/8] [CI] Auto-commit changed files from 'node scripts/eslint --no-cache --fix' --- x-pack/plugins/license_management/public/locator.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x-pack/plugins/license_management/public/locator.ts b/x-pack/plugins/license_management/public/locator.ts index f4edae7422165..88b0ee9feb868 100644 --- a/x-pack/plugins/license_management/public/locator.ts +++ b/x-pack/plugins/license_management/public/locator.ts @@ -7,7 +7,7 @@ import type { SerializableRecord } from '@kbn/utility-types'; import { ManagementAppLocator } from '@kbn/management-plugin/common'; -import { LocatorDefinition, LocatorPublic} from '@kbn/share-plugin/public'; +import { LocatorDefinition, LocatorPublic } from '@kbn/share-plugin/public'; import { PLUGIN } from '../common/constants'; export const LICENSE_MANAGEMENT_LOCATOR_ID = 'LICENSE_MANAGEMENT_LOCATOR'; From 7d2331aa5cb09f48093d227847663a24f0bf9522 Mon Sep 17 00:00:00 2001 From: Yulia Cech Date: Tue, 28 Mar 2023 14:18:12 +0200 Subject: [PATCH 5/8] [License Management] Add unit tests for the url locator --- .../license_management/public/locator.test.ts | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 x-pack/plugins/license_management/public/locator.test.ts diff --git a/x-pack/plugins/license_management/public/locator.test.ts b/x-pack/plugins/license_management/public/locator.test.ts new file mode 100644 index 0000000000000..09f4991b0fdc8 --- /dev/null +++ b/x-pack/plugins/license_management/public/locator.test.ts @@ -0,0 +1,34 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import { sharePluginMock } from '@kbn/share-plugin/public/mocks'; +import { ManagementAppLocatorDefinition } from '@kbn/management-plugin/common/locator'; +import { LicenseManagementLocatorDefinition, LICENSE_MANAGEMENT_LOCATOR_ID } from './locator'; +describe('License Management URL locator', () => { + let locator: LicenseManagementLocatorDefinition; + beforeEach(() => { + const managementDefinition = new ManagementAppLocatorDefinition(); + locator = new LicenseManagementLocatorDefinition({ + managementAppLocator: { + ...sharePluginMock.createLocator(), + getLocation: (params) => managementDefinition.getLocation(params), + }, + }); + }); + test('locator has the right ID', () => { + expect(locator.id).toBe(LICENSE_MANAGEMENT_LOCATOR_ID); + }); + + test('locator returns the correct url for dashboard page', async () => { + const { path } = await locator.getLocation({ page: 'dashboard' }); + expect(path).toBe('/stack/license_management'); + }); + test('locator returns the correct url for upload license page', async () => { + const { path } = await locator.getLocation({ page: 'upload_license' }); + expect(path).toBe('/stack/license_management/upload_license'); + }); +}); From b595976635c21297665f0738267ec553fb8a83e3 Mon Sep 17 00:00:00 2001 From: Yulia Cech Date: Tue, 28 Mar 2023 15:52:41 +0200 Subject: [PATCH 6/8] [Watcher] Extract license prompt component --- .../watcher/public/application/app.tsx | 44 +------------- .../public/application/license_prompt.tsx | 60 +++++++++++++++++++ 2 files changed, 62 insertions(+), 42 deletions(-) create mode 100644 x-pack/plugins/watcher/public/application/license_prompt.tsx diff --git a/x-pack/plugins/watcher/public/application/app.tsx b/x-pack/plugins/watcher/public/application/app.tsx index 3035944c52302..11b2d32ebf338 100644 --- a/x-pack/plugins/watcher/public/application/app.tsx +++ b/x-pack/plugins/watcher/public/application/app.tsx @@ -20,10 +20,6 @@ import { Router, Switch, Redirect, withRouter, RouteComponentProps } from 'react import { Route } from '@kbn/shared-ux-router'; -import { EuiPageContent_Deprecated as EuiPageContent, EuiEmptyPrompt, EuiLink } from '@elastic/eui'; - -import { FormattedMessage } from '@kbn/i18n-react'; - import { RegisterManagementAppArgs, ManagementAppMountParams } from '@kbn/management-plugin/public'; import { ChartsPluginSetup } from '@kbn/charts-plugin/public'; @@ -32,6 +28,7 @@ import { LicenseStatus } from '../../common/types/license_status'; import { WatchListPage, WatchEditPage, WatchStatusPage } from './sections'; import { registerRouter } from './lib/navigation'; import { AppContextProvider } from './app_context'; +import { LicensePrompt } from './license_prompt'; const ShareRouter = withRouter(({ children, history }: RouteComponentProps & { children: any }) => { registerRouter({ history }); @@ -62,45 +59,8 @@ export const App = (deps: AppDeps) => { }, [deps.licenseStatus$]); if (!valid) { - const licenseManagementUrl = deps.licenseManagementLocator?.useUrl({ page: 'dashboard' }); - // if there is no licenseManagementUrl, the license management plugin might be disabled - const promptAction = licenseManagementUrl ? ( - - - - ) : undefined; - const promptBody = licenseManagementUrl ? ( -

{message}

- ) : ( - <> -

{message}

-

- -

- - ); return ( - - - - - } - body={promptBody} - actions={[promptAction]} - /> - + ); } return ( diff --git a/x-pack/plugins/watcher/public/application/license_prompt.tsx b/x-pack/plugins/watcher/public/application/license_prompt.tsx new file mode 100644 index 0000000000000..34ea4a3e5a9c7 --- /dev/null +++ b/x-pack/plugins/watcher/public/application/license_prompt.tsx @@ -0,0 +1,60 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import React from 'react'; +import { FormattedMessage } from '@kbn/i18n-react'; +import { EuiEmptyPrompt, EuiLink, EuiPageContent_Deprecated as EuiPageContent } from '@elastic/eui'; +import { LicenseManagementLocator } from '@kbn/license-management-plugin/public/locator'; + +export const LicensePrompt = ({ + message, + licenseManagementLocator, +}: { + message: string | undefined; + licenseManagementLocator?: LicenseManagementLocator; +}) => { + const licenseManagementUrl = licenseManagementLocator?.useUrl({ page: 'dashboard' }); + // if there is no licenseManagementUrl, the license management plugin might be disabled + const promptAction = licenseManagementUrl ? ( + + + + ) : undefined; + const promptBody = licenseManagementUrl ? ( +

{message}

+ ) : ( + <> +

{message}

+

+ +

+ + ); + return ( + + + + + } + body={promptBody} + actions={[promptAction]} + /> + + ); +}; From 880d9f3e15fb160a6ad10d47007494271824164e Mon Sep 17 00:00:00 2001 From: Yulia Cech Date: Tue, 28 Mar 2023 18:18:52 +0200 Subject: [PATCH 7/8] [Watcher] Add unit test for license prompt --- .../license_prompt.test.tsx.snap | 80 +++++++++++++++++++ .../watcher/__jest__/license_prompt.test.tsx | 35 ++++++++ 2 files changed, 115 insertions(+) create mode 100644 x-pack/plugins/watcher/__jest__/__snapshots__/license_prompt.test.tsx.snap create mode 100644 x-pack/plugins/watcher/__jest__/license_prompt.test.tsx diff --git a/x-pack/plugins/watcher/__jest__/__snapshots__/license_prompt.test.tsx.snap b/x-pack/plugins/watcher/__jest__/__snapshots__/license_prompt.test.tsx.snap new file mode 100644 index 0000000000000..7d3768c237574 --- /dev/null +++ b/x-pack/plugins/watcher/__jest__/__snapshots__/license_prompt.test.tsx.snap @@ -0,0 +1,80 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`License prompt renders a prompt with a link to License Management 1`] = ` + + + + , + ] + } + body={ +

+ License error +

+ } + iconType="warning" + title={ +

+ +

+ } + /> +
+`; + +exports[`License prompt renders a prompt without a link to License Management 1`] = ` + + +

+ License error +

+

+ +

+ + } + iconType="warning" + title={ +

+ +

+ } + /> +
+`; diff --git a/x-pack/plugins/watcher/__jest__/license_prompt.test.tsx b/x-pack/plugins/watcher/__jest__/license_prompt.test.tsx new file mode 100644 index 0000000000000..e17f163feba49 --- /dev/null +++ b/x-pack/plugins/watcher/__jest__/license_prompt.test.tsx @@ -0,0 +1,35 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import React from 'react'; +import { shallow } from 'enzyme'; +import { sharePluginMock } from '@kbn/share-plugin/public/mocks'; +import { + LicenseManagementLocator, + LicenseManagementLocatorParams, +} from '@kbn/license-management-plugin/public/locator'; +import { LicensePrompt } from '../public/application/license_prompt'; + +describe('License prompt', () => { + test('renders a prompt with a link to License Management', () => { + const locator = { + ...sharePluginMock.createLocator(), + useUrl: (params: LicenseManagementLocatorParams) => '/license_management', + } as LicenseManagementLocator; + const component = shallow( + + ); + + expect(component).toMatchSnapshot(); + }); + + test('renders a prompt without a link to License Management', () => { + const component = shallow(); + + expect(component).toMatchSnapshot(); + }); +}); From 57b10a4c07bb99180f71e147d4aaee497b25f9a8 Mon Sep 17 00:00:00 2001 From: kibanamachine <42973632+kibanamachine@users.noreply.github.com> Date: Tue, 28 Mar 2023 16:24:08 +0000 Subject: [PATCH 8/8] [CI] Auto-commit changed files from 'node scripts/lint_ts_projects --fix' --- x-pack/plugins/watcher/tsconfig.json | 1 + 1 file changed, 1 insertion(+) diff --git a/x-pack/plugins/watcher/tsconfig.json b/x-pack/plugins/watcher/tsconfig.json index c09ca92cb70fe..0762abab662de 100644 --- a/x-pack/plugins/watcher/tsconfig.json +++ b/x-pack/plugins/watcher/tsconfig.json @@ -34,6 +34,7 @@ "@kbn/ace", "@kbn/shared-ux-router", "@kbn/license-management-plugin", + "@kbn/share-plugin", ], "exclude": [ "target/**/*",