From 240dd1c520eb6630e8aa9b9b507cf4003b2f50b2 Mon Sep 17 00:00:00 2001 From: Ahmad Bamieh Date: Thu, 5 Dec 2019 13:59:35 -0500 Subject: [PATCH] [7.x] [Cloud] move cloud plugin to np (#51789) (#52289) * merge conflicts with 7.x * resolve conflict * resolve eslint conflict --- .../kibana-plugin-public.chromestart.md | 1 + ...in-public.chromestart.sethelpsupporturl.md | 24 ++++++++ src/core/public/chrome/chrome_service.mock.ts | 1 + src/core/public/chrome/chrome_service.tsx | 12 +++- src/core/public/chrome/constants.ts | 1 - src/core/public/chrome/ui/header/header.tsx | 11 +++- .../chrome/ui/header/header_help_menu.tsx | 35 +++++------ src/core/public/public.api.md | 1 + .../kibana/public/home/components/home_app.js | 4 +- .../tutorials/apm/envs/elastic_cloud.js | 23 +++---- .../kibana/server/tutorials/apm/index.js | 3 +- .../query_string_input.test.tsx.snap | 6 ++ .../common/expression_types/datatable.ts | 2 +- x-pack/index.js | 2 - .../cloud/cloud_usage_collector.test.ts | 60 ------------------- .../plugins/cloud/cloud_usage_collector.ts | 36 ----------- x-pack/legacy/plugins/cloud/index.js | 47 --------------- x-pack/legacy/plugins/ml/index.ts | 8 ++- .../ml_telemetry/make_ml_usage_collector.ts | 6 +- .../plugins/ml/server/new_platform/plugin.ts | 6 +- .../legacy/plugins/ml/server/routes/system.js | 7 +-- .../public/components/no_data/no_data.js | 4 +- .../monitoring/public/lib/setup_mode.js | 11 ++-- .../monitoring/public/lib/setup_mode.test.js | 26 ++++---- .../public/views/no_data/controller.js | 6 +- .../__tests__/cluster.js | 15 ++++- .../lib/elasticsearch_settings/cluster.js | 9 +-- .../lib/elasticsearch_settings/find_reason.js | 4 +- .../legacy/plugins/monitoring/ui_exports.js | 2 - .../server/routes/api/repositories.test.ts | 2 +- .../server/routes/api/repositories.ts | 2 +- .../legacy/plugins/snapshot_restore/shim.ts | 16 ++--- .../legacy/plugins/transform/server/shim.ts | 12 ---- .../legacy/plugins/upgrade_assistant/index.ts | 11 ++-- .../upgrade_assistant/public/legacy.ts | 7 ++- .../public/np_ready/plugin.ts | 7 +-- .../server/np_ready/plugin.ts | 15 +++-- .../np_ready/routes/cluster_checkup.test.ts | 13 ++-- .../server/np_ready/routes/cluster_checkup.ts | 14 ++++- .../routes/deprecation_logging.test.ts | 5 -- .../server/np_ready/types.ts | 7 --- .../cloud/common}/constants.ts | 1 + .../cloud/common/is_cloud_enabled.test.ts | 33 ++++++++++ .../plugins/cloud/common/is_cloud_enabled.ts | 9 +++ x-pack/plugins/cloud/kibana.json | 9 +++ x-pack/plugins/cloud/public/index.ts | 13 ++++ x-pack/plugins/cloud/public/plugin.ts | 36 +++++++++++ .../collectors/cloud_usage_collector.test.ts | 32 ++++++++++ .../collectors/cloud_usage_collector.ts | 37 ++++++++++++ .../cloud/server/collectors/index.ts} | 6 +- x-pack/plugins/cloud/server/config.ts | 33 ++++++++++ x-pack/plugins/cloud/server/index.ts | 14 +++++ x-pack/plugins/cloud/server/plugin.ts | 54 +++++++++++++++++ x-pack/test/typings/hapi.d.ts | 2 - x-pack/typings/hapi.d.ts | 2 - 55 files changed, 471 insertions(+), 294 deletions(-) create mode 100644 docs/development/core/public/kibana-plugin-public.chromestart.sethelpsupporturl.md delete mode 100644 x-pack/legacy/plugins/cloud/cloud_usage_collector.test.ts delete mode 100644 x-pack/legacy/plugins/cloud/cloud_usage_collector.ts delete mode 100644 x-pack/legacy/plugins/cloud/index.js rename x-pack/{legacy/plugins/cloud => plugins/cloud/common}/constants.ts (81%) create mode 100644 x-pack/plugins/cloud/common/is_cloud_enabled.test.ts create mode 100644 x-pack/plugins/cloud/common/is_cloud_enabled.ts create mode 100644 x-pack/plugins/cloud/kibana.json create mode 100644 x-pack/plugins/cloud/public/index.ts create mode 100644 x-pack/plugins/cloud/public/plugin.ts create mode 100644 x-pack/plugins/cloud/server/collectors/cloud_usage_collector.test.ts create mode 100644 x-pack/plugins/cloud/server/collectors/cloud_usage_collector.ts rename x-pack/{legacy/plugins/cloud/index.d.ts => plugins/cloud/server/collectors/index.ts} (75%) create mode 100644 x-pack/plugins/cloud/server/config.ts create mode 100644 x-pack/plugins/cloud/server/index.ts create mode 100644 x-pack/plugins/cloud/server/plugin.ts diff --git a/docs/development/core/public/kibana-plugin-public.chromestart.md b/docs/development/core/public/kibana-plugin-public.chromestart.md index 153e06d591404..d5d99f3d5be65 100644 --- a/docs/development/core/public/kibana-plugin-public.chromestart.md +++ b/docs/development/core/public/kibana-plugin-public.chromestart.md @@ -39,6 +39,7 @@ export interface ChromeStart | [setBrand(brand)](./kibana-plugin-public.chromestart.setbrand.md) | Set the brand configuration. | | [setBreadcrumbs(newBreadcrumbs)](./kibana-plugin-public.chromestart.setbreadcrumbs.md) | Override the current set of breadcrumbs | | [setHelpExtension(helpExtension)](./kibana-plugin-public.chromestart.sethelpextension.md) | Override the current set of custom help content | +| [setHelpSupportUrl(url)](./kibana-plugin-public.chromestart.sethelpsupporturl.md) | Override the default support URL shown in the help menu | | [setIsCollapsed(isCollapsed)](./kibana-plugin-public.chromestart.setiscollapsed.md) | Set the collapsed state of the chrome navigation. | | [setIsVisible(isVisible)](./kibana-plugin-public.chromestart.setisvisible.md) | Set the temporary visibility for the chrome. This does nothing if the chrome is hidden by default and should be used to hide the chrome for things like full-screen modes with an exit button. | diff --git a/docs/development/core/public/kibana-plugin-public.chromestart.sethelpsupporturl.md b/docs/development/core/public/kibana-plugin-public.chromestart.sethelpsupporturl.md new file mode 100644 index 0000000000000..975283ce59cb7 --- /dev/null +++ b/docs/development/core/public/kibana-plugin-public.chromestart.sethelpsupporturl.md @@ -0,0 +1,24 @@ + + +[Home](./index.md) > [kibana-plugin-public](./kibana-plugin-public.md) > [ChromeStart](./kibana-plugin-public.chromestart.md) > [setHelpSupportUrl](./kibana-plugin-public.chromestart.sethelpsupporturl.md) + +## ChromeStart.setHelpSupportUrl() method + +Override the default support URL shown in the help menu + +Signature: + +```typescript +setHelpSupportUrl(url: string): void; +``` + +## Parameters + +| Parameter | Type | Description | +| --- | --- | --- | +| url | string | | + +Returns: + +`void` + diff --git a/src/core/public/chrome/chrome_service.mock.ts b/src/core/public/chrome/chrome_service.mock.ts index 6f61ee9dc21ba..bd932c5961eca 100644 --- a/src/core/public/chrome/chrome_service.mock.ts +++ b/src/core/public/chrome/chrome_service.mock.ts @@ -72,6 +72,7 @@ const createStartContractMock = () => { setBreadcrumbs: jest.fn(), getHelpExtension$: jest.fn(), setHelpExtension: jest.fn(), + setHelpSupportUrl: jest.fn(), }; startContract.navLinks.getAll.mockReturnValue([]); startContract.getBrand$.mockReturnValue(new BehaviorSubject({} as ChromeBrand)); diff --git a/src/core/public/chrome/chrome_service.tsx b/src/core/public/chrome/chrome_service.tsx index cc23b79e1c621..25c00836a4db7 100644 --- a/src/core/public/chrome/chrome_service.tsx +++ b/src/core/public/chrome/chrome_service.tsx @@ -37,6 +37,7 @@ import { DocTitleService, ChromeDocTitle } from './doc_title'; import { LoadingIndicator, HeaderWrapper as Header } from './ui'; import { DocLinksStart } from '../doc_links'; import { ChromeHelpExtensionMenuLink } from './ui/header/header_help_menu'; +import { KIBANA_ASK_ELASTIC_LINK } from './constants'; export { ChromeNavControls, ChromeRecentlyAccessed, ChromeDocTitle }; @@ -147,6 +148,7 @@ export class ChromeService { const helpExtension$ = new BehaviorSubject(undefined); const breadcrumbs$ = new BehaviorSubject([]); const badge$ = new BehaviorSubject(undefined); + const helpSupportUrl$ = new BehaviorSubject(KIBANA_ASK_ELASTIC_LINK); const navControls = this.navControls.start(); const navLinks = this.navLinks.start({ application, http }); @@ -172,7 +174,6 @@ export class ChromeService {
{ helpExtension$.next(helpExtension); }, + + setHelpSupportUrl: (url: string) => helpSupportUrl$.next(url), }; } @@ -401,6 +405,12 @@ export interface ChromeStart { * Override the current set of custom help content */ setHelpExtension(helpExtension?: ChromeHelpExtension): void; + + /** + * Override the default support URL shown in the help menu + * @param url The updated support URL + */ + setHelpSupportUrl(url: string): void; } /** @internal */ diff --git a/src/core/public/chrome/constants.ts b/src/core/public/chrome/constants.ts index c8e53b38c618e..8fcf3e6d62231 100644 --- a/src/core/public/chrome/constants.ts +++ b/src/core/public/chrome/constants.ts @@ -17,7 +17,6 @@ * under the License. */ -export const ELASTIC_SUPPORT_LINK = 'https://support.elastic.co/'; export const KIBANA_FEEDBACK_LINK = 'https://www.elastic.co/products/kibana/feedback'; export const KIBANA_ASK_ELASTIC_LINK = 'https://www.elastic.co/products/kibana/ask-elastic'; export const GITHUB_CREATE_ISSUE_LINK = 'https://github.com/elastic/kibana/issues/new/choose'; diff --git a/src/core/public/chrome/ui/header/header.tsx b/src/core/public/chrome/ui/header/header.tsx index 904618651201f..75f78ac8b2fa0 100644 --- a/src/core/public/chrome/ui/header/header.tsx +++ b/src/core/public/chrome/ui/header/header.tsx @@ -164,6 +164,7 @@ interface Props { recentlyAccessed$: Rx.Observable; forceAppSwitcherNavigation$: Rx.Observable; helpExtension$: Rx.Observable; + helpSupportUrl$: Rx.Observable; legacyMode: boolean; navControlsLeft$: Rx.Observable; navControlsRight$: Rx.Observable; @@ -171,7 +172,6 @@ interface Props { basePath: HttpStart['basePath']; isLocked?: boolean; onIsLockedUpdate?: (isLocked: boolean) => void; - isCloudEnabled: boolean; } interface State { @@ -283,13 +283,13 @@ class HeaderUI extends Component { basePath, breadcrumbs$, helpExtension$, + helpSupportUrl$, intl, isLocked, kibanaDocLink, kibanaVersion, onIsLockedUpdate, legacyMode, - isCloudEnabled, } = this.props; const { appTitle, @@ -389,7 +389,12 @@ class HeaderUI extends Component { diff --git a/src/core/public/chrome/ui/header/header_help_menu.tsx b/src/core/public/chrome/ui/header/header_help_menu.tsx index 50659a777eccb..80a45b4c61f07 100644 --- a/src/core/public/chrome/ui/header/header_help_menu.tsx +++ b/src/core/public/chrome/ui/header/header_help_menu.tsx @@ -36,14 +36,10 @@ import { } from '@elastic/eui'; import { ExclusiveUnion } from '@elastic/eui'; +import { combineLatest } from 'rxjs'; import { HeaderExtension } from './header_extension'; import { ChromeHelpExtension } from '../../chrome_service'; -import { - ELASTIC_SUPPORT_LINK, - GITHUB_CREATE_ISSUE_LINK, - KIBANA_ASK_ELASTIC_LINK, - KIBANA_FEEDBACK_LINK, -} from '../../constants'; +import { GITHUB_CREATE_ISSUE_LINK, KIBANA_FEEDBACK_LINK } from '../../constants'; /** @public */ export type ChromeHelpExtensionMenuGitHubLink = EuiButtonEmptyProps & { @@ -110,16 +106,17 @@ export type ChromeHelpExtensionMenuLink = ExclusiveUnion< interface Props { helpExtension$: Rx.Observable; + helpSupportUrl$: Rx.Observable; intl: InjectedIntl; kibanaVersion: string; useDefaultContent?: boolean; kibanaDocLink: string; - isCloudEnabled: boolean; } interface State { isOpen: boolean; helpExtension?: ChromeHelpExtension; + helpSupportUrl: string; } class HeaderHelpMenuUI extends Component { @@ -131,16 +128,19 @@ class HeaderHelpMenuUI extends Component { this.state = { isOpen: false, helpExtension: undefined, + helpSupportUrl: '', }; } public componentDidMount() { - this.subscription = this.props.helpExtension$.subscribe({ - next: helpExtension => { - this.setState({ - helpExtension, - }); - }, + this.subscription = combineLatest( + this.props.helpExtension$, + this.props.helpSupportUrl$ + ).subscribe(([helpExtension, helpSupportUrl]) => { + this.setState({ + helpExtension, + helpSupportUrl, + }); }); } @@ -183,7 +183,7 @@ class HeaderHelpMenuUI extends Component { public render() { const { intl, kibanaVersion, useDefaultContent, kibanaDocLink } = this.props; - const { helpExtension } = this.state; + const { helpExtension, helpSupportUrl } = this.state; const defaultContent = useDefaultContent ? ( @@ -196,12 +196,7 @@ class HeaderHelpMenuUI extends Component { - + ({ - makeUsageCollector: sinon.stub(), -}); - -const getMockServer = (cloudId?: string) => - ({ - config() { - return { - get(path: string) { - switch (path) { - case 'xpack.cloud': - return { id: cloudId }; - default: - throw Error(`server.config().get(${path}) should not be called by this collector.`); - } - }, - }; - }, - } as Server); - -describe('Cloud usage collector', () => { - describe('collector', () => { - it('returns `isCloudEnabled: false` if `xpack.cloud.id` is not defined', async () => { - const mockServer = getMockServer(); - const collector = await createCollectorFetch(mockServer)(); - expect(collector.isCloudEnabled).toBe(false); - }); - - it('returns `isCloudEnabled: true` if `xpack.cloud.id` is defined', async () => { - const stagingCollector = await createCollectorFetch(getMockServer(CLOUD_ID))(); - const collector = await createCollectorFetch(getMockServer(CLOUD_ID_STAGING))(); - expect(collector.isCloudEnabled).toBe(true); - expect(stagingCollector.isCloudEnabled).toBe(true); - }); - }); -}); - -describe('createCloudUsageCollector', () => { - it('returns calls `makeUsageCollector`', () => { - const mockServer = getMockServer(); - const usageCollection = mockUsageCollection(); - createCloudUsageCollector(usageCollection as any, mockServer); - expect(usageCollection.makeUsageCollector.calledOnce).toBe(true); - }); -}); diff --git a/x-pack/legacy/plugins/cloud/cloud_usage_collector.ts b/x-pack/legacy/plugins/cloud/cloud_usage_collector.ts deleted file mode 100644 index 7fdf32144972c..0000000000000 --- a/x-pack/legacy/plugins/cloud/cloud_usage_collector.ts +++ /dev/null @@ -1,36 +0,0 @@ -/* - * 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 { Server } from 'hapi'; -import { UsageCollectionSetup } from 'src/plugins/usage_collection/server'; -import { KIBANA_CLOUD_STATS_TYPE } from './constants'; - -export interface UsageStats { - isCloudEnabled: boolean; -} - -export function createCollectorFetch(server: Server) { - return async function fetchUsageStats(): Promise { - const { id } = server.config().get(`xpack.cloud`); - - return { - isCloudEnabled: !!id, - }; - }; -} - -export function createCloudUsageCollector(usageCollection: UsageCollectionSetup, server: Server) { - return usageCollection.makeUsageCollector({ - type: KIBANA_CLOUD_STATS_TYPE, - isReady: () => true, - fetch: createCollectorFetch(server), - }); -} - -export function registerCloudUsageCollector(usageCollection: UsageCollectionSetup, server: Server) { - const collector = createCloudUsageCollector(usageCollection, server); - usageCollection.registerCollector(collector); -} diff --git a/x-pack/legacy/plugins/cloud/index.js b/x-pack/legacy/plugins/cloud/index.js deleted file mode 100644 index c2fd35eea5292..0000000000000 --- a/x-pack/legacy/plugins/cloud/index.js +++ /dev/null @@ -1,47 +0,0 @@ -/* - * 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 { registerCloudUsageCollector } from './cloud_usage_collector'; - -export const cloud = kibana => { - return new kibana.Plugin({ - id: 'cloud', - configPrefix: 'xpack.cloud', - require: ['kibana', 'elasticsearch', 'xpack_main'], - - uiExports: { - injectDefaultVars(server, options) { - return { - isCloudEnabled: !!options.id, - cloudId: options.id - }; - }, - }, - - config(Joi) { - return Joi.object({ - enabled: Joi.boolean().default(true), - id: Joi.string(), - apm: Joi.object({ - url: Joi.string(), - secret_token: Joi.string(), - ui: Joi.object({ - url: Joi.string(), - }).default(), - }).default(), - }).default(); - }, - - init(server) { - const config = server.config().get(`xpack.cloud`); - server.expose('config', { - isCloudEnabled: !!config.id - }); - const { usageCollection } = server.newPlatform.setup.plugins; - registerCloudUsageCollector(usageCollection, server); - } - }); -}; diff --git a/x-pack/legacy/plugins/ml/index.ts b/x-pack/legacy/plugins/ml/index.ts index 90e1e748492cb..9b42998c814fd 100755 --- a/x-pack/legacy/plugins/ml/index.ts +++ b/x-pack/legacy/plugins/ml/index.ts @@ -7,7 +7,10 @@ import { resolve } from 'path'; import { i18n } from '@kbn/i18n'; import KbnServer, { Server } from 'src/legacy/server/kbn_server'; +import { UsageCollectionSetup } from 'src/plugins/usage_collection/server'; import { plugin } from './server/new_platform'; +import { CloudSetup } from '../../../plugins/cloud/server'; + import { MlInitializerContext, MlCoreSetup, @@ -80,13 +83,14 @@ export const ml = (kibana: any) => { http: mlHttpService, savedObjects: server.savedObjects, }; - + const { usageCollection, cloud } = kbnServer.newPlatform.setup.plugins; const plugins = { elasticsearch: server.plugins.elasticsearch, security: server.plugins.security, xpackMain: server.plugins.xpack_main, spaces: server.plugins.spaces, - usageCollection: kbnServer.newPlatform.setup.plugins.usageCollection, + usageCollection: usageCollection as UsageCollectionSetup, + cloud: cloud as CloudSetup, ml: this, }; diff --git a/x-pack/legacy/plugins/ml/server/lib/ml_telemetry/make_ml_usage_collector.ts b/x-pack/legacy/plugins/ml/server/lib/ml_telemetry/make_ml_usage_collector.ts index 7a9766f36a6ed..293480b2aa5dc 100644 --- a/x-pack/legacy/plugins/ml/server/lib/ml_telemetry/make_ml_usage_collector.ts +++ b/x-pack/legacy/plugins/ml/server/lib/ml_telemetry/make_ml_usage_collector.ts @@ -16,9 +16,13 @@ import { import { UsageInitialization } from '../../new_platform/plugin'; export function makeMlUsageCollector( - usageCollection: UsageCollectionSetup, + usageCollection: UsageCollectionSetup | undefined, { elasticsearchPlugin, savedObjects }: UsageInitialization ): void { + if (!usageCollection) { + return; + } + const mlUsageCollector = usageCollection.makeUsageCollector({ type: 'ml', isReady: () => true, diff --git a/x-pack/legacy/plugins/ml/server/new_platform/plugin.ts b/x-pack/legacy/plugins/ml/server/new_platform/plugin.ts index b789121beebfc..727d05605614f 100644 --- a/x-pack/legacy/plugins/ml/server/new_platform/plugin.ts +++ b/x-pack/legacy/plugins/ml/server/new_platform/plugin.ts @@ -11,6 +11,7 @@ import { KibanaConfig, SavedObjectsLegacyService } from 'src/legacy/server/kbn_s import { Logger, PluginInitializerContext, CoreSetup } from 'src/core/server'; import { ElasticsearchPlugin } from 'src/legacy/core_plugins/elasticsearch'; import { UsageCollectionSetup } from 'src/plugins/usage_collection/server'; +import { CloudSetup } from '../../../../../plugins/cloud/server'; import { XPackMainPlugin } from '../../../xpack_main/xpack_main'; import { addLinksToSampleDatasets } from '../lib/sample_data_sets'; import { checkLicense } from '../lib/check_license'; @@ -79,7 +80,8 @@ export interface PluginsSetup { xpackMain: MlXpackMainPlugin; security: any; spaces: any; - usageCollection: UsageCollectionSetup; + usageCollection?: UsageCollectionSetup; + cloud?: CloudSetup; // TODO: this is temporary for `mirrorPluginStatus` ml: any; } @@ -91,6 +93,7 @@ export interface RouteInitialization { xpackMainPlugin?: MlXpackMainPlugin; savedObjects?: SavedObjectsLegacyService; spacesPlugin: any; + cloud?: CloudSetup; } export interface UsageInitialization { elasticsearchPlugin: ElasticsearchPlugin; @@ -190,6 +193,7 @@ export class Plugin { xpackMainPlugin: plugins.xpackMain, savedObjects: core.savedObjects, spacesPlugin: plugins.spaces, + cloud: plugins.cloud, }; const usageInitializationDeps: UsageInitialization = { elasticsearchPlugin: plugins.elasticsearch, diff --git a/x-pack/legacy/plugins/ml/server/routes/system.js b/x-pack/legacy/plugins/ml/server/routes/system.js index a686971672c58..60ea6a10827e2 100644 --- a/x-pack/legacy/plugins/ml/server/routes/system.js +++ b/x-pack/legacy/plugins/ml/server/routes/system.js @@ -21,10 +21,10 @@ import { isSecurityDisabled } from '../lib/security_utils'; export function systemRoutes({ commonRouteConfig, elasticsearchPlugin, - config, route, xpackMainPlugin, - spacesPlugin + spacesPlugin, + cloud, }) { const callWithInternalUser = callWithInternalUserFactory(elasticsearchPlugin); @@ -174,8 +174,7 @@ export function systemRoutes({ try { const info = await callWithRequest('ml.info'); - const cloudIdKey = 'xpack.cloud.id'; - const cloudId = config.has(cloudIdKey) && config.get(cloudIdKey); + const cloudId = cloud && cloud.cloudId; return { ...info, cloudId }; } catch (error) { return wrapError(error); diff --git a/x-pack/legacy/plugins/monitoring/public/components/no_data/no_data.js b/x-pack/legacy/plugins/monitoring/public/components/no_data/no_data.js index f2f27499d113c..e9de0da7aabbd 100644 --- a/x-pack/legacy/plugins/monitoring/public/components/no_data/no_data.js +++ b/x-pack/legacy/plugins/monitoring/public/components/no_data/no_data.js @@ -43,7 +43,7 @@ function NoDataMessage(props) { export function NoData(props) { const [isLoading, setIsLoading] = useState(false); - const [useInternalCollection, setUseInternalCollection] = useState(props.isOnCloud); + const [useInternalCollection, setUseInternalCollection] = useState(props.isCloudEnabled); async function startSetup() { setIsLoading(true); @@ -64,7 +64,7 @@ export function NoData(props) { - { !props.isOnCloud ? ( + { !props.isCloudEnabled ? ( setUseInternalCollection(false)}> diff --git a/x-pack/legacy/plugins/monitoring/public/lib/setup_mode.js b/x-pack/legacy/plugins/monitoring/public/lib/setup_mode.js index 239c6e3fd775a..41aae01307617 100644 --- a/x-pack/legacy/plugins/monitoring/public/lib/setup_mode.js +++ b/x-pack/legacy/plugins/monitoring/public/lib/setup_mode.js @@ -12,6 +12,7 @@ import chrome from 'ui/chrome'; import { toastNotifications } from 'ui/notify'; import { i18n } from '@kbn/i18n'; import { SetupModeEnterButton } from '../components/setup_mode/enter_button'; +import { npSetup } from 'ui/new_platform'; function isOnPage(hash) { return contains(window.location.hash, hash); @@ -82,10 +83,10 @@ export const updateSetupModeData = async (uuid, fetchWithoutClusterUuid = false) const oldData = setupModeState.data; const data = await fetchCollectionData(uuid, fetchWithoutClusterUuid); setupModeState.data = data; - - const isCloud = chrome.getInjected('isOnCloud'); + const { cloud } = npSetup.plugins; + const isCloudEnabled = !!(cloud && cloud.isCloudEnabled); const hasPermissions = get(data, '_meta.hasPermissions', false); - if (isCloud || !hasPermissions) { + if (isCloudEnabled || !hasPermissions) { let text = null; if (!hasPermissions) { text = i18n.translate('xpack.monitoring.setupMode.notAvailablePermissions', { @@ -163,7 +164,9 @@ export const setSetupModeMenuItem = () => { } const globalState = angularState.injector.get('globalState'); - const enabled = !globalState.inSetupMode && !chrome.getInjected('isOnCloud'); + const { cloud } = npSetup.plugins; + const isCloudEnabled = !!(cloud && cloud.isCloudEnabled); + const enabled = !globalState.inSetupMode && !isCloudEnabled; render( , diff --git a/x-pack/legacy/plugins/monitoring/public/lib/setup_mode.test.js b/x-pack/legacy/plugins/monitoring/public/lib/setup_mode.test.js index 1a9fdfeb920da..70313d45d096b 100644 --- a/x-pack/legacy/plugins/monitoring/public/lib/setup_mode.test.js +++ b/x-pack/legacy/plugins/monitoring/public/lib/setup_mode.test.js @@ -69,12 +69,15 @@ function setModules() { describe('setup_mode', () => { beforeEach(async () => { - jest.doMock('ui/chrome', () => ({ - getInjected: key => { - if (key === 'isOnCloud') { - return false; + jest.doMock('ui/new_platform', () => ({ + npSetup: { + plugins: { + cloud: { + cloudId: undefined, + isCloudEnabled: false, + } } - }, + } })); setModules(); }); @@ -122,12 +125,15 @@ describe('setup_mode', () => { it('should not fetch data if on cloud', async done => { const addDanger = jest.fn(); - jest.doMock('ui/chrome', () => ({ - getInjected: key => { - if (key === 'isOnCloud') { - return true; + jest.doMock('ui/new_platform', () => ({ + npSetup: { + plugins: { + cloud: { + cloudId: 'test', + isCloudEnabled: true, + } } - }, + } })); data = { _meta: { diff --git a/x-pack/legacy/plugins/monitoring/public/views/no_data/controller.js b/x-pack/legacy/plugins/monitoring/public/views/no_data/controller.js index f364b70fd934f..9634f9872dd59 100644 --- a/x-pack/legacy/plugins/monitoring/public/views/no_data/controller.js +++ b/x-pack/legacy/plugins/monitoring/public/views/no_data/controller.js @@ -5,7 +5,6 @@ */ import React from 'react'; -import chrome from 'ui/chrome'; import { ClusterSettingsChecker, NodeSettingsChecker, @@ -18,6 +17,7 @@ import { I18nContext } from 'ui/i18n'; import { CODE_PATH_LICENSE } from '../../../common/constants'; import { MonitoringViewBaseController } from '../base_controller'; import { i18n } from '@kbn/i18n'; +import { npSetup } from 'ui/new_platform'; export class NoDataController extends MonitoringViewBaseController { @@ -97,6 +97,8 @@ export class NoDataController extends MonitoringViewBaseController { render(enabler) { const props = this; + const { cloud } = npSetup.plugins; + const isCloudEnabled = !!(cloud && cloud.isCloudEnabled); this.renderReact( @@ -104,7 +106,7 @@ export class NoDataController extends MonitoringViewBaseController { {...props} enabler={enabler} changePath={this.changePath} - isOnCloud={chrome.getInjected('isOnCloud')} + isCloudEnabled={isCloudEnabled} /> ); diff --git a/x-pack/legacy/plugins/monitoring/server/lib/elasticsearch_settings/__tests__/cluster.js b/x-pack/legacy/plugins/monitoring/server/lib/elasticsearch_settings/__tests__/cluster.js index 768cba0e3e350..2db348f2a7751 100644 --- a/x-pack/legacy/plugins/monitoring/server/lib/elasticsearch_settings/__tests__/cluster.js +++ b/x-pack/legacy/plugins/monitoring/server/lib/elasticsearch_settings/__tests__/cluster.js @@ -21,6 +21,15 @@ describe('Elasticsearch Cluster Settings', () => { const getReq = response => { return { server: { + newPlatform: { + setup: { + plugins: { + cloud: { + isCloudEnabled: false, + } + } + } + }, plugins: { elasticsearch: { getCluster() { @@ -49,7 +58,7 @@ describe('Elasticsearch Cluster Settings', () => { reason: { context: `cluster ${source}`, data: '-1', - isCloud: false, + isCloudEnabled: false, property: 'xpack.monitoring.collection.interval' } }); @@ -79,7 +88,7 @@ describe('Elasticsearch Cluster Settings', () => { reason: { context: `cluster ${source}`, data: 'Remote exporters indicate a possible misconfiguration: myCoolExporter', - isCloud: false, + isCloudEnabled: false, property: 'xpack.monitoring.exporters' } }); @@ -109,7 +118,7 @@ describe('Elasticsearch Cluster Settings', () => { reason: { context: `cluster ${source}`, data: 'false', - isCloud: false, + isCloudEnabled: false, property: 'xpack.monitoring.enabled' } }); diff --git a/x-pack/legacy/plugins/monitoring/server/lib/elasticsearch_settings/cluster.js b/x-pack/legacy/plugins/monitoring/server/lib/elasticsearch_settings/cluster.js index e8c508ecd5f97..596195d7ae915 100644 --- a/x-pack/legacy/plugins/monitoring/server/lib/elasticsearch_settings/cluster.js +++ b/x-pack/legacy/plugins/monitoring/server/lib/elasticsearch_settings/cluster.js @@ -7,14 +7,14 @@ import { get } from 'lodash'; import { findReason } from './find_reason'; -export function handleResponse(response, isCloud) { +export function handleResponse(response, isCloudEnabled) { const sources = ['persistent', 'transient', 'defaults']; for (const source of sources) { const monitoringSettings = get(response[source], 'xpack.monitoring'); if (monitoringSettings !== undefined) { const check = findReason(monitoringSettings, { context: `cluster ${source}`, - isCloud: isCloud + isCloudEnabled, }); if (check.found) { @@ -28,7 +28,8 @@ export function handleResponse(response, isCloud) { export async function checkClusterSettings(req) { const { callWithRequest } = req.server.plugins.elasticsearch.getCluster('admin'); - const isCloud = get(req.server.plugins, 'cloud.config.isCloudEnabled', false); + const { cloud } = req.server.newPlatform.setup.plugins; + const isCloudEnabled = !!(cloud && cloud.isCloudEnabled); const response = await callWithRequest(req, 'transport.request', { method: 'GET', path: '/_cluster/settings?include_defaults', @@ -39,5 +40,5 @@ export async function checkClusterSettings(req) { ] }); - return handleResponse(response, isCloud); + return handleResponse(response, isCloudEnabled); } diff --git a/x-pack/legacy/plugins/monitoring/server/lib/elasticsearch_settings/find_reason.js b/x-pack/legacy/plugins/monitoring/server/lib/elasticsearch_settings/find_reason.js index 07c8402b0da7a..f5aac00d8b0a5 100644 --- a/x-pack/legacy/plugins/monitoring/server/lib/elasticsearch_settings/find_reason.js +++ b/x-pack/legacy/plugins/monitoring/server/lib/elasticsearch_settings/find_reason.js @@ -14,7 +14,7 @@ const isEnabledOrDefault = property => { return property === undefined || (Boolean(property) && property !== 'false'); }; -export function findReason(settingsSource, context, isCloud) { +export function findReason(settingsSource, context, isCloudEnabled) { const iterateReasons = () => { // PluginEnabled: check for `monitoring.enabled: false` const monitoringEnabled = get(settingsSource, 'enabled'); @@ -91,7 +91,7 @@ export function findReason(settingsSource, context, isCloud) { }); if (allEnabledRemote.length > 0 && allEnabledLocal.length === 0) { let ret = {}; - if (isCloud) { + if (isCloudEnabled) { ret = { found: true, reason: { diff --git a/x-pack/legacy/plugins/monitoring/ui_exports.js b/x-pack/legacy/plugins/monitoring/ui_exports.js index 0976292be576b..f8a8c9ff51213 100644 --- a/x-pack/legacy/plugins/monitoring/ui_exports.js +++ b/x-pack/legacy/plugins/monitoring/ui_exports.js @@ -5,7 +5,6 @@ */ import { i18n } from '@kbn/i18n'; -import { get } from 'lodash'; import { resolve } from 'path'; /** @@ -30,7 +29,6 @@ export const getUiExports = () => ({ const config = server.config(); return { monitoringUiEnabled: config.get('xpack.monitoring.ui.enabled'), - isOnCloud: get(server.plugins, 'cloud.config.isCloudEnabled', false) }; }, hacks: [ 'plugins/monitoring/hacks/toggle_app_link_in_nav' ], diff --git a/x-pack/legacy/plugins/snapshot_restore/server/routes/api/repositories.test.ts b/x-pack/legacy/plugins/snapshot_restore/server/routes/api/repositories.test.ts index bbc862c747018..7801bf01016ae 100644 --- a/x-pack/legacy/plugins/snapshot_restore/server/routes/api/repositories.test.ts +++ b/x-pack/legacy/plugins/snapshot_restore/server/routes/api/repositories.test.ts @@ -39,7 +39,7 @@ describe('[Snapshot and Restore API Routes] Repositories', () => { patch: () => {}, }, { - cloud: { config: { isCloudEnabled: false } }, + cloud: { isCloudEnabled: false }, elasticsearch: { getCluster: () => ({ callWithInternalUser: mockCallWithInternalUser }) }, } ); diff --git a/x-pack/legacy/plugins/snapshot_restore/server/routes/api/repositories.ts b/x-pack/legacy/plugins/snapshot_restore/server/routes/api/repositories.ts index 486e2e7a81798..eb18a61eef366 100644 --- a/x-pack/legacy/plugins/snapshot_restore/server/routes/api/repositories.ts +++ b/x-pack/legacy/plugins/snapshot_restore/server/routes/api/repositories.ts @@ -28,7 +28,7 @@ let isCloudEnabled: boolean = false; let callWithInternalUser: any; export function registerRepositoriesRoutes(router: Router, plugins: Plugins) { - isCloudEnabled = plugins.cloud.config.isCloudEnabled; + isCloudEnabled = plugins.cloud && plugins.cloud.isCloudEnabled; callWithInternalUser = plugins.elasticsearch.getCluster('data').callWithInternalUser; router.get('repository_types', getTypesHandler); router.get('repositories', getAllHandler); diff --git a/x-pack/legacy/plugins/snapshot_restore/shim.ts b/x-pack/legacy/plugins/snapshot_restore/shim.ts index 335f7db32f0bf..ef8d65fca77d4 100644 --- a/x-pack/legacy/plugins/snapshot_restore/shim.ts +++ b/x-pack/legacy/plugins/snapshot_restore/shim.ts @@ -4,13 +4,12 @@ * you may not use this file except in compliance with the Elastic License. */ -import { get } from 'lodash'; import { i18n } from '@kbn/i18n'; import { Legacy } from 'kibana'; import { createRouter, Router } from '../../server/lib/create_router'; import { registerLicenseChecker } from '../../server/lib/register_license_checker'; import { elasticsearchJsPlugin } from './server/client/elasticsearch_slm'; - +import { CloudSetup } from '../../../plugins/cloud/server'; export interface Core { http: { createRouter(basePath: string): Router; @@ -24,11 +23,7 @@ export interface Plugins { license: { registerLicenseChecker: typeof registerLicenseChecker; }; - cloud: { - config: { - isCloudEnabled: boolean; - }; - }; + cloud: CloudSetup; settings: { config: { isSlmEnabled: boolean; @@ -42,6 +37,7 @@ export function createShim( server: Legacy.Server, pluginId: string ): { core: Core; plugins: Plugins } { + const { cloud } = server.newPlatform.setup.plugins; return { core: { http: { @@ -56,11 +52,7 @@ export function createShim( license: { registerLicenseChecker, }, - cloud: { - config: { - isCloudEnabled: get(server.plugins, 'cloud.config.isCloudEnabled', false), - }, - }, + cloud: cloud as CloudSetup, settings: { config: { isSlmEnabled: server.config() diff --git a/x-pack/legacy/plugins/transform/server/shim.ts b/x-pack/legacy/plugins/transform/server/shim.ts index 83afa788682f1..8f477d86441f4 100644 --- a/x-pack/legacy/plugins/transform/server/shim.ts +++ b/x-pack/legacy/plugins/transform/server/shim.ts @@ -4,12 +4,10 @@ * you may not use this file except in compliance with the Elastic License. */ -import { get } from 'lodash'; import { Legacy } from 'kibana'; import { createRouter, Router } from '../../../server/lib/create_router'; import { registerLicenseChecker } from '../../../server/lib/register_license_checker'; import { elasticsearchJsPlugin } from './client/elasticsearch_transform'; - export interface Core { http: { createRouter(basePath: string): Router; @@ -20,11 +18,6 @@ export interface Plugins { license: { registerLicenseChecker: typeof registerLicenseChecker; }; - cloud: { - config: { - isCloudEnabled: boolean; - }; - }; xpack_main: any; elasticsearch: any; } @@ -46,11 +39,6 @@ export function createServerShim( license: { registerLicenseChecker, }, - cloud: { - config: { - isCloudEnabled: get(server.plugins, 'cloud.config.isCloudEnabled', false), - }, - }, xpack_main: server.plugins.xpack_main, elasticsearch: server.plugins.elasticsearch, }, diff --git a/x-pack/legacy/plugins/upgrade_assistant/index.ts b/x-pack/legacy/plugins/upgrade_assistant/index.ts index 94d375e53b524..e20564c94cdaa 100644 --- a/x-pack/legacy/plugins/upgrade_assistant/index.ts +++ b/x-pack/legacy/plugins/upgrade_assistant/index.ts @@ -9,6 +9,7 @@ import { Legacy } from 'kibana'; import { resolve } from 'path'; import mappings from './mappings.json'; import { plugin } from './server/np_ready'; +import { CloudSetup } from '../../../plugins/cloud/server'; export function upgradeAssistant(kibana: any) { const publicSrc = resolve(__dirname, 'public'); @@ -43,7 +44,11 @@ export function upgradeAssistant(kibana: any) { init(server: Legacy.Server) { // Add server routes and initialize the plugin here const instance = plugin({} as any); + + const { usageCollection, cloud } = server.newPlatform.setup.plugins; instance.setup(server.newPlatform.setup.core, { + usageCollection, + cloud: cloud as CloudSetup, __LEGACY: { // Legacy objects events: server.events, @@ -58,12 +63,6 @@ export function upgradeAssistant(kibana: any) { apm_oss: server.plugins.apm_oss, elasticsearch: server.plugins.elasticsearch, xpack_main: server.plugins.xpack_main, - cloud: { - config: { - isCloudEnabled: _.get(server.plugins, 'cloud.config.isCloudEnabled', false), - }, - }, - usage: server.newPlatform.setup.plugins.usageCollection, }, } as any, }); diff --git a/x-pack/legacy/plugins/upgrade_assistant/public/legacy.ts b/x-pack/legacy/plugins/upgrade_assistant/public/legacy.ts index 3d5144249dfef..b6bc6a14de224 100644 --- a/x-pack/legacy/plugins/upgrade_assistant/public/legacy.ts +++ b/x-pack/legacy/plugins/upgrade_assistant/public/legacy.ts @@ -19,6 +19,7 @@ import chrome from 'ui/chrome'; import { NEXT_MAJOR_VERSION } from '../common/version'; import { plugin } from './np_ready'; +import { CloudSetup } from '../../../../plugins/cloud/public'; const BASE_PATH = `/management/elasticsearch/upgrade_assistant`; @@ -42,10 +43,10 @@ export interface LegacyManagementPlugin { // Based on /rfcs/text/0006_management_section_service.md export interface LegacyPlugins { + cloud?: CloudSetup; management: LegacyManagementPlugin; __LEGACY: { XSRF: string; - isCloudEnabled: boolean; }; } @@ -54,11 +55,11 @@ function startApp() { template: '', }); - + const { cloud } = npSetup.plugins as any; const legacyPluginsShim: LegacyPlugins = { + cloud: cloud as CloudSetup, __LEGACY: { XSRF: chrome.getXsrfToken(), - isCloudEnabled: chrome.getInjected('isCloudEnabled', false), }, management: { sections: { diff --git a/x-pack/legacy/plugins/upgrade_assistant/public/np_ready/plugin.ts b/x-pack/legacy/plugins/upgrade_assistant/public/np_ready/plugin.ts index 16a0c9632fb25..ed85b988c25d6 100644 --- a/x-pack/legacy/plugins/upgrade_assistant/public/np_ready/plugin.ts +++ b/x-pack/legacy/plugins/upgrade_assistant/public/np_ready/plugin.ts @@ -9,11 +9,10 @@ import { RootComponent } from './application/app'; import { LegacyPlugins } from '../legacy'; export class UpgradeAssistantUIPlugin implements Plugin { - async setup( - { http }: CoreSetup, - { management, __LEGACY: { XSRF, isCloudEnabled } }: LegacyPlugins - ) { + async setup({ http }: CoreSetup, { cloud, management, __LEGACY: { XSRF } }: LegacyPlugins) { const appRegistrar = management.sections.get('kibana'); + const isCloudEnabled = !!(cloud && cloud.isCloudEnabled); + return appRegistrar.registerApp({ mount(__, { __LEGACY: { renderToElement } }) { return renderToElement(() => RootComponent({ http, XSRF, isCloudEnabled })); diff --git a/x-pack/legacy/plugins/upgrade_assistant/server/np_ready/plugin.ts b/x-pack/legacy/plugins/upgrade_assistant/server/np_ready/plugin.ts index 0116c10e23ef6..99d9a6925aea9 100644 --- a/x-pack/legacy/plugins/upgrade_assistant/server/np_ready/plugin.ts +++ b/x-pack/legacy/plugins/upgrade_assistant/server/np_ready/plugin.ts @@ -4,21 +4,28 @@ * you may not use this file except in compliance with the Elastic License. */ import { Plugin, CoreSetup, CoreStart } from 'src/core/server'; +import { UsageCollectionSetup } from 'src/plugins/usage_collection/server'; import { ServerShim, ServerShimWithRouter } from './types'; import { credentialStoreFactory } from './lib/reindexing/credential_store'; import { registerUpgradeAssistantUsageCollector } from './lib/telemetry'; import { registerClusterCheckupRoutes } from './routes/cluster_checkup'; import { registerDeprecationLoggingRoutes } from './routes/deprecation_logging'; import { registerReindexIndicesRoutes, registerReindexWorker } from './routes/reindex_indices'; +import { CloudSetup } from '../../../../../plugins/cloud/server'; +import { registerTelemetryRoutes } from './routes/telemetry'; import { registerQueryDefaultFieldRoutes } from './routes/query_default_field'; -import { registerTelemetryRoutes } from './routes/telemetry'; +interface PluginsSetup { + __LEGACY: ServerShim; + usageCollection: UsageCollectionSetup; + cloud?: CloudSetup; +} export class UpgradeAssistantServerPlugin implements Plugin { - setup({ http }: CoreSetup, { __LEGACY }: { __LEGACY: ServerShim }) { + setup({ http }: CoreSetup, { __LEGACY, usageCollection, cloud }: PluginsSetup) { const router = http.createRouter(); const shimWithRouter: ServerShimWithRouter = { ...__LEGACY, router }; - registerClusterCheckupRoutes(shimWithRouter); + registerClusterCheckupRoutes(shimWithRouter, { cloud }); registerDeprecationLoggingRoutes(shimWithRouter); registerQueryDefaultFieldRoutes(shimWithRouter); @@ -35,7 +42,7 @@ export class UpgradeAssistantServerPlugin implements Plugin { let mockRouter: MockRouter; let serverShim: any; let ctxMock: any; + let mockPluginsSetup: any; beforeEach(() => { mockRouter = createMockRouter(); + mockPluginsSetup = { + cloud: { + isCloudEnabled: true, + }, + }; ctxMock = { core: {}, }; @@ -41,17 +47,12 @@ describe('cluster checkup API', () => { apm_oss: { indexPatterns: ['apm-*'], }, - cloud: { - config: { - isCloudEnabled: true, - }, - }, elasticsearch: { getCluster: () => ({ callWithRequest: jest.fn() } as any), } as any, }, }; - registerClusterCheckupRoutes(serverShim); + registerClusterCheckupRoutes(serverShim, mockPluginsSetup); }); describe('with cloud enabled', () => { diff --git a/x-pack/legacy/plugins/upgrade_assistant/server/np_ready/routes/cluster_checkup.ts b/x-pack/legacy/plugins/upgrade_assistant/server/np_ready/routes/cluster_checkup.ts index 6a986fba61d17..4ef4c2132a3bc 100644 --- a/x-pack/legacy/plugins/upgrade_assistant/server/np_ready/routes/cluster_checkup.ts +++ b/x-pack/legacy/plugins/upgrade_assistant/server/np_ready/routes/cluster_checkup.ts @@ -8,12 +8,20 @@ import _ from 'lodash'; import { ServerShimWithRouter } from '../types'; import { getUpgradeAssistantStatus } from '../lib/es_migration_apis'; import { versionCheckHandlerWrapper } from '../lib/es_version_precheck'; - +import { CloudSetup } from '../../../../../../plugins/cloud/server'; import { createRequestShim } from './create_request_shim'; -export function registerClusterCheckupRoutes(server: ServerShimWithRouter) { +interface PluginsSetup { + cloud?: CloudSetup; +} + +export function registerClusterCheckupRoutes( + server: ServerShimWithRouter, + pluginsSetup: PluginsSetup +) { + const { cloud } = pluginsSetup; + const isCloudEnabled = !!(cloud && cloud.isCloudEnabled); const { callWithRequest } = server.plugins.elasticsearch.getCluster('admin'); - const isCloudEnabled = _.get(server.plugins, 'cloud.config.isCloudEnabled', false); const apmIndexPatterns = _.get(server, 'plugins.apm_oss.indexPatterns', []); server.router.get( diff --git a/x-pack/legacy/plugins/upgrade_assistant/server/np_ready/routes/deprecation_logging.test.ts b/x-pack/legacy/plugins/upgrade_assistant/server/np_ready/routes/deprecation_logging.test.ts index c488f999b538e..d663361956374 100644 --- a/x-pack/legacy/plugins/upgrade_assistant/server/np_ready/routes/deprecation_logging.test.ts +++ b/x-pack/legacy/plugins/upgrade_assistant/server/np_ready/routes/deprecation_logging.test.ts @@ -31,11 +31,6 @@ describe('deprecation logging API', () => { serverShim = { router: mockRouter, plugins: { - cloud: { - config: { - isCloudEnabled: true, - }, - }, elasticsearch: { getCluster: () => ({ callWithRequest } as any), } as any, diff --git a/x-pack/legacy/plugins/upgrade_assistant/server/np_ready/types.ts b/x-pack/legacy/plugins/upgrade_assistant/server/np_ready/types.ts index da1354b002186..edee1d09cdeeb 100644 --- a/x-pack/legacy/plugins/upgrade_assistant/server/np_ready/types.ts +++ b/x-pack/legacy/plugins/upgrade_assistant/server/np_ready/types.ts @@ -5,7 +5,6 @@ */ import { Legacy } from 'kibana'; import { IRouter } from 'src/core/server'; -import { UsageCollectionSetup } from 'src/plugins/usage_collection/server'; import { ElasticsearchPlugin } from 'src/legacy/core_plugins/elasticsearch'; import { XPackMainPlugin } from '../../../xpack_main/xpack_main'; @@ -13,12 +12,6 @@ export interface ServerShim { plugins: { elasticsearch: ElasticsearchPlugin; xpack_main: XPackMainPlugin; - cloud: { - config: { - isCloudEnabled: boolean; - }; - }; - usage: UsageCollectionSetup; }; log: any; events: any; diff --git a/x-pack/legacy/plugins/cloud/constants.ts b/x-pack/plugins/cloud/common/constants.ts similarity index 81% rename from x-pack/legacy/plugins/cloud/constants.ts rename to x-pack/plugins/cloud/common/constants.ts index d2f1ad765e231..4fafafb9e4213 100644 --- a/x-pack/legacy/plugins/cloud/constants.ts +++ b/x-pack/plugins/cloud/common/constants.ts @@ -5,3 +5,4 @@ */ export const KIBANA_CLOUD_STATS_TYPE = 'cloud'; +export const ELASTIC_SUPPORT_LINK = 'https://support.elastic.co/'; diff --git a/x-pack/plugins/cloud/common/is_cloud_enabled.test.ts b/x-pack/plugins/cloud/common/is_cloud_enabled.test.ts new file mode 100644 index 0000000000000..cb804d0a2d379 --- /dev/null +++ b/x-pack/plugins/cloud/common/is_cloud_enabled.test.ts @@ -0,0 +1,33 @@ +/* + * 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 { getIsCloudEnabled } from './is_cloud_enabled'; + +const MOCK_CLOUD_ID_STAGING = + 'staging:dXMtZWFzdC0xLmF3cy5mb3VuZC5pbyRjZWM2ZjI2MWE3NGJmMjRjZTMzYmI4ODExYjg0Mjk0ZiRjNmMyY2E2ZDA0MjI0OWFmMGNjN2Q3YTllOTYyNTc0Mw=='; +const MOCK_CLOUD_ID_PROD = + 'dXMtZWFzdC0xLmF3cy5mb3VuZC5pbyRjZWM2ZjI2MWE3NGJmMjRjZTMzYmI4ODExYjg0Mjk0ZiRjNmMyY2E2ZDA0MjI0OWFmMGNjN2Q3YTllOTYyNTc0Mw=='; + +describe('getIsCloudEnabled', () => { + it('returns `false` if `id` is not undefined', async () => { + const cloudId = undefined; + const isCloudEnabled = getIsCloudEnabled(cloudId); + expect(isCloudEnabled).toBe(false); + }); + + it('returns `false` if `id` is not a string', async () => { + const cloudId = 123 as any; + const isCloudEnabled = getIsCloudEnabled(cloudId); + expect(isCloudEnabled).toBe(false); + }); + + it('returns `true` if `id` is a string', async () => { + const isCloudEnabledStaging = getIsCloudEnabled(MOCK_CLOUD_ID_STAGING); + const isCloudEnabledProd = getIsCloudEnabled(MOCK_CLOUD_ID_PROD); + expect(isCloudEnabledStaging).toBe(true); + expect(isCloudEnabledProd).toBe(true); + }); +}); diff --git a/x-pack/plugins/cloud/common/is_cloud_enabled.ts b/x-pack/plugins/cloud/common/is_cloud_enabled.ts new file mode 100644 index 0000000000000..c40a5379cd568 --- /dev/null +++ b/x-pack/plugins/cloud/common/is_cloud_enabled.ts @@ -0,0 +1,9 @@ +/* + * 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. + */ + +export function getIsCloudEnabled(cloudId?: string) { + return typeof cloudId === 'string'; +} diff --git a/x-pack/plugins/cloud/kibana.json b/x-pack/plugins/cloud/kibana.json new file mode 100644 index 0000000000000..4b8b7bbf3186c --- /dev/null +++ b/x-pack/plugins/cloud/kibana.json @@ -0,0 +1,9 @@ +{ + "id": "cloud", + "version": "8.0.0", + "kibanaVersion": "kibana", + "configPath": ["xpack", "cloud"], + "optionalPlugins": ["usageCollection"], + "server": true, + "ui": true +} diff --git a/x-pack/plugins/cloud/public/index.ts b/x-pack/plugins/cloud/public/index.ts new file mode 100644 index 0000000000000..39ef5f452c18b --- /dev/null +++ b/x-pack/plugins/cloud/public/index.ts @@ -0,0 +1,13 @@ +/* + * 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 { PluginInitializerContext } from '../../../../src/core/public'; +import { CloudPlugin } from './plugin'; + +export { CloudSetup } from './plugin'; +export function plugin(initializerContext: PluginInitializerContext) { + return new CloudPlugin(initializerContext); +} diff --git a/x-pack/plugins/cloud/public/plugin.ts b/x-pack/plugins/cloud/public/plugin.ts new file mode 100644 index 0000000000000..63f75ccd2ec94 --- /dev/null +++ b/x-pack/plugins/cloud/public/plugin.ts @@ -0,0 +1,36 @@ +/* + * 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 { CoreSetup, CoreStart, Plugin, PluginInitializerContext } from 'src/core/public'; +import { getIsCloudEnabled } from '../common/is_cloud_enabled'; +import { ELASTIC_SUPPORT_LINK } from '../common/constants'; + +interface CloudConfigType { + id?: string; +} + +export interface CloudSetup { + cloudId?: string; + isCloudEnabled: boolean; +} + +export class CloudPlugin implements Plugin { + constructor(private readonly initializerContext: PluginInitializerContext) {} + + public async setup(core: CoreSetup) { + const { id } = this.initializerContext.config.get(); + const isCloudEnabled = getIsCloudEnabled(id); + + return { + cloudId: id, + isCloudEnabled, + }; + } + + public start(coreStart: CoreStart) { + coreStart.chrome.setHelpSupportUrl(ELASTIC_SUPPORT_LINK); + } +} diff --git a/x-pack/plugins/cloud/server/collectors/cloud_usage_collector.test.ts b/x-pack/plugins/cloud/server/collectors/cloud_usage_collector.test.ts new file mode 100644 index 0000000000000..a731de341435c --- /dev/null +++ b/x-pack/plugins/cloud/server/collectors/cloud_usage_collector.test.ts @@ -0,0 +1,32 @@ +/* + * 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 { createCloudUsageCollector } from './cloud_usage_collector'; + +const mockUsageCollection = () => ({ + makeUsageCollector: jest.fn().mockImplementation((args: any) => ({ ...args })), +}); + +const getMockConfigs = (isCloudEnabled: boolean) => ({ isCloudEnabled }); + +describe('createCloudUsageCollector', () => { + it('calls `makeUsageCollector`', () => { + const mockConfigs = getMockConfigs(false); + const usageCollection = mockUsageCollection(); + createCloudUsageCollector(usageCollection as any, mockConfigs); + expect(usageCollection.makeUsageCollector).toBeCalledTimes(1); + }); + + describe('Fetched Usage data', () => { + it('return isCloudEnabled boolean', () => { + const mockConfigs = getMockConfigs(true); + const usageCollection = mockUsageCollection() as any; + const collector = createCloudUsageCollector(usageCollection, mockConfigs); + + expect(collector.fetch().isCloudEnabled).toBe(true); + }); + }); +}); diff --git a/x-pack/plugins/cloud/server/collectors/cloud_usage_collector.ts b/x-pack/plugins/cloud/server/collectors/cloud_usage_collector.ts new file mode 100644 index 0000000000000..f3eb92eeddfbe --- /dev/null +++ b/x-pack/plugins/cloud/server/collectors/cloud_usage_collector.ts @@ -0,0 +1,37 @@ +/* + * 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 { UsageCollectionSetup } from 'src/plugins/usage_collection/server'; +import { KIBANA_CLOUD_STATS_TYPE } from '../../common/constants'; + +interface Config { + isCloudEnabled: boolean; +} + +export function createCloudUsageCollector(usageCollection: UsageCollectionSetup, config: Config) { + const { isCloudEnabled } = config; + return usageCollection.makeUsageCollector({ + type: KIBANA_CLOUD_STATS_TYPE, + isReady: () => true, + fetch: () => { + return { + isCloudEnabled, + }; + }, + }); +} + +export function registerCloudUsageCollector( + usageCollection: UsageCollectionSetup | undefined, + config: Config +) { + if (!usageCollection) { + return; + } + + const collector = createCloudUsageCollector(usageCollection, config); + usageCollection.registerCollector(collector); +} diff --git a/x-pack/legacy/plugins/cloud/index.d.ts b/x-pack/plugins/cloud/server/collectors/index.ts similarity index 75% rename from x-pack/legacy/plugins/cloud/index.d.ts rename to x-pack/plugins/cloud/server/collectors/index.ts index d3cdd01f07273..c8dca0c2f792c 100644 --- a/x-pack/legacy/plugins/cloud/index.d.ts +++ b/x-pack/plugins/cloud/server/collectors/index.ts @@ -4,8 +4,4 @@ * you may not use this file except in compliance with the Elastic License. */ -export interface CloudPlugin { - config: { - isCloudEnabled: boolean; - }; -} +export { registerCloudUsageCollector } from './cloud_usage_collector'; diff --git a/x-pack/plugins/cloud/server/config.ts b/x-pack/plugins/cloud/server/config.ts new file mode 100644 index 0000000000000..77e493dc3b7dc --- /dev/null +++ b/x-pack/plugins/cloud/server/config.ts @@ -0,0 +1,33 @@ +/* + * 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 { schema, TypeOf } from '@kbn/config-schema'; +import { PluginConfigDescriptor } from 'kibana/server'; + +const apmConfigSchema = schema.object({ + url: schema.maybe(schema.string()), + secret_token: schema.maybe(schema.string()), + ui: schema.maybe( + schema.object({ + url: schema.maybe(schema.string()), + }) + ), +}); + +const configSchema = schema.object({ + enabled: schema.boolean({ defaultValue: true }), + id: schema.maybe(schema.string()), + apm: schema.maybe(apmConfigSchema), +}); + +export type CloudConfigType = TypeOf; + +export const config: PluginConfigDescriptor = { + exposeToBrowser: { + id: true, + }, + schema: configSchema, +}; diff --git a/x-pack/plugins/cloud/server/index.ts b/x-pack/plugins/cloud/server/index.ts new file mode 100644 index 0000000000000..4a2df206b3db5 --- /dev/null +++ b/x-pack/plugins/cloud/server/index.ts @@ -0,0 +1,14 @@ +/* + * 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 { PluginInitializerContext } from 'src/core/server'; +import { CloudPlugin } from './plugin'; + +export { CloudSetup } from './plugin'; +export { config } from './config'; +export const plugin = (initializerContext: PluginInitializerContext) => { + return new CloudPlugin(initializerContext); +}; diff --git a/x-pack/plugins/cloud/server/plugin.ts b/x-pack/plugins/cloud/server/plugin.ts new file mode 100644 index 0000000000000..784e8a755f921 --- /dev/null +++ b/x-pack/plugins/cloud/server/plugin.ts @@ -0,0 +1,54 @@ +/* + * 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 { first } from 'rxjs/operators'; +import { Observable } from 'rxjs'; +import { UsageCollectionSetup } from 'src/plugins/usage_collection/server'; +import { CoreSetup, Logger, Plugin, PluginInitializerContext } from 'src/core/server'; +import { CloudConfigType } from './config'; +import { registerCloudUsageCollector } from './collectors'; +import { getIsCloudEnabled } from '../common/is_cloud_enabled'; + +interface PluginsSetup { + usageCollection?: UsageCollectionSetup; +} + +export interface CloudSetup { + cloudId?: string; + isCloudEnabled: boolean; + apm: { + url?: string; + secretToken?: string; + }; +} + +export class CloudPlugin implements Plugin { + private readonly logger: Logger; + private readonly config$: Observable; + + constructor(private readonly context: PluginInitializerContext) { + this.logger = this.context.logger.get(); + this.config$ = this.context.config.create(); + } + + public async setup(core: CoreSetup, { usageCollection }: PluginsSetup) { + this.logger.debug('Setting up Cloud plugin'); + const config = await this.config$.pipe(first()).toPromise(); + const isCloudEnabled = getIsCloudEnabled(config.id); + registerCloudUsageCollector(usageCollection, { isCloudEnabled }); + + return { + cloudId: config.id, + isCloudEnabled, + apm: { + url: config.apm?.url, + secretToken: config.apm?.secret_token, + }, + }; + } + + public start() {} +} diff --git a/x-pack/test/typings/hapi.d.ts b/x-pack/test/typings/hapi.d.ts index 339819798b480..6a67c5ccee337 100644 --- a/x-pack/test/typings/hapi.d.ts +++ b/x-pack/test/typings/hapi.d.ts @@ -6,7 +6,6 @@ import 'hapi'; -import { CloudPlugin } from '../../legacy/plugins/cloud'; import { XPackMainPlugin } from '../../legacy/plugins/xpack_main/xpack_main'; import { SecurityPlugin } from '../../legacy/plugins/security'; import { ActionsPlugin, ActionsClient } from '../../legacy/plugins/actions'; @@ -19,7 +18,6 @@ declare module 'hapi' { getAlertsClient?: () => AlertsClient; } interface PluginProperties { - cloud?: CloudPlugin; xpack_main: XPackMainPlugin; security?: SecurityPlugin; actions?: ActionsPlugin; diff --git a/x-pack/typings/hapi.d.ts b/x-pack/typings/hapi.d.ts index 1c6ad3a55cb2c..d97e47f3ce0f8 100644 --- a/x-pack/typings/hapi.d.ts +++ b/x-pack/typings/hapi.d.ts @@ -6,7 +6,6 @@ import 'hapi'; -import { CloudPlugin } from '../legacy/plugins/cloud'; import { XPackMainPlugin } from '../legacy/plugins/xpack_main/xpack_main'; import { SecurityPlugin } from '../legacy/plugins/security'; import { ActionsPlugin, ActionsClient } from '../legacy/plugins/actions'; @@ -19,7 +18,6 @@ declare module 'hapi' { getAlertsClient?: () => AlertsClient; } interface PluginProperties { - cloud?: CloudPlugin; xpack_main: XPackMainPlugin; security?: SecurityPlugin; actions?: ActionsPlugin;