From 225bf516520d7ffb996c4cceaeb6f6071799321c Mon Sep 17 00:00:00 2001 From: CJ Cenizal Date: Mon, 3 Feb 2020 21:51:52 -0800 Subject: [PATCH] Inject NP http service into Index Management extensions. Remove dependency upon Angular's $http. --- .../index_actions_context_menu.js | 11 ++--- .../public/app/services/api.ts | 41 ------------------- .../public/app/services/notification.ts | 4 ++ .../app/store/actions/extension_action.js | 4 +- .../public/register_routes.ts | 7 +--- 5 files changed, 13 insertions(+), 54 deletions(-) diff --git a/x-pack/legacy/plugins/index_management/public/app/sections/home/index_list/index_actions_context_menu/index_actions_context_menu.js b/x-pack/legacy/plugins/index_management/public/app/sections/home/index_list/index_actions_context_menu/index_actions_context_menu.js index 5eecac55bd593..5ed651e3125cf 100644 --- a/x-pack/legacy/plugins/index_management/public/app/sections/home/index_list/index_actions_context_menu/index_actions_context_menu.js +++ b/x-pack/legacy/plugins/index_management/public/app/sections/home/index_list/index_actions_context_menu/index_actions_context_menu.js @@ -24,9 +24,10 @@ import { } from '@elastic/eui'; // We will be able to remove these after the NP migration is complete. -import { toastNotifications, fatalError } from 'ui/notify'; +import { fatalError } from 'ui/notify'; import { createUiStatsReporter } from '../../../../../../../../../../src/legacy/core_plugins/ui_metric/public'; -import { getNewPlatformCompatibleHttpClient } from '../../../../services/api'; +import { httpService } from '../../../../services/http'; +import { notificationService } from '../../../../services/notification'; import { flattenPanelTree } from '../../../../lib/flatten_panel_tree'; import { INDEX_OPEN } from '../../../../../../common/constants'; @@ -225,15 +226,15 @@ export class IndexActionsContextMenu extends Component { const actionExtensionDefinition = actionExtension({ indices, reloadIndices, - // These config options can be removed once the NP migration is complete. + // These config options can be removed once the NP migration out of legacy is complete. // They're needed for now because ILM's extensions make API calls which require these // dependencies, but they're not available unless the app's "setup" lifecycle stage occurs. // Within the old platform, "setup" only occurs once the user actually visits the app. // Once ILM and IM have been moved out of legacy this hack won't be necessary. createUiStatsReporter, - toasts: toastNotifications, + toasts: notificationService.toasts, fatalErrors: getNewPlatformCompatibleFatalErrorService(), - httpClient: getNewPlatformCompatibleHttpClient(), + httpClient: httpService.httpClient, }); if (actionExtensionDefinition) { diff --git a/x-pack/legacy/plugins/index_management/public/app/services/api.ts b/x-pack/legacy/plugins/index_management/public/app/services/api.ts index 77c86f4216070..8030a7f29d4ee 100644 --- a/x-pack/legacy/plugins/index_management/public/app/services/api.ts +++ b/x-pack/legacy/plugins/index_management/public/app/services/api.ts @@ -4,8 +4,6 @@ * you may not use this file except in compliance with the Elastic License. */ -import { npStart } from 'ui/new_platform'; - import { API_BASE_PATH, UIM_UPDATE_SETTINGS, @@ -41,45 +39,6 @@ import { useRequest, sendRequest } from './use_request'; import { httpService } from './http'; import { Template } from '../../../common/types'; -let httpClient: ng.IHttpService; - -export const setHttpClient = (client: ng.IHttpService) => { - httpClient = client; -}; - -export const getHttpClient = () => { - return httpClient; -}; - -/** - * ILM uses the NP http service internally, so we need to inject an adapater around Angular's - * $http service that provides the same interface. We can replace this with the NP http service - * once Index Management has been migrated. - */ -export const getNewPlatformCompatibleHttpClient = () => { - const prependBasePath = npStart.core.http.basePath.prepend; - - return { - post: async (path: string, payload: any): Promise => { - const parsedPayload = JSON.parse(payload.body); - const prependedPath = prependBasePath(path); - const response = await httpClient.post(prependedPath, parsedPayload); - return response.data; - }, - get: async (path: string, payload: any): Promise => { - const parsedQuery = payload && payload.query ? payload.query : ''; - const prependedPath = `${prependBasePath(path)}${parsedQuery}`; - const response = await httpClient.get(prependedPath); - return response.data; - }, - delete: async (path: string): Promise => { - const prependedPath = prependBasePath(path); - const response = await httpClient.delete(prependedPath); - return response.data; - }, - }; -}; - export async function loadIndices() { const response = await httpService.httpClient.get(`${API_BASE_PATH}/indices`); return response.data ? response.data : response; diff --git a/x-pack/legacy/plugins/index_management/public/app/services/notification.ts b/x-pack/legacy/plugins/index_management/public/app/services/notification.ts index 2e63253ccae67..b96fb3e645733 100644 --- a/x-pack/legacy/plugins/index_management/public/app/services/notification.ts +++ b/x-pack/legacy/plugins/index_management/public/app/services/notification.ts @@ -13,6 +13,10 @@ class NotificationService { this.toasts = notifications.toasts; } + public get toasts() { + return toasts; + } + private addToasts = (title: string, type: 'danger' | 'warning' | 'success', text?: string) => { this.toasts.add({ title, diff --git a/x-pack/legacy/plugins/index_management/public/app/store/actions/extension_action.js b/x-pack/legacy/plugins/index_management/public/app/store/actions/extension_action.js index 1e29e38b79c1b..277a71999492f 100644 --- a/x-pack/legacy/plugins/index_management/public/app/store/actions/extension_action.js +++ b/x-pack/legacy/plugins/index_management/public/app/store/actions/extension_action.js @@ -6,7 +6,7 @@ import { reloadIndices } from '../actions'; import { notificationService } from '../../services/notification'; -import { getNewPlatformCompatibleHttpClient } from '../../services/api'; +import { httpService } from '../../services/http'; export const performExtensionAction = ({ requestMethod, @@ -14,7 +14,7 @@ export const performExtensionAction = ({ successMessage, }) => async dispatch => { try { - await requestMethod(indexNames, getNewPlatformCompatibleHttpClient()); + await requestMethod(indexNames, httpService.httpClient); } catch (error) { notificationService.showDangerToast(error.message); return; diff --git a/x-pack/legacy/plugins/index_management/public/register_routes.ts b/x-pack/legacy/plugins/index_management/public/register_routes.ts index 778d0733d04c0..378a53cf65555 100644 --- a/x-pack/legacy/plugins/index_management/public/register_routes.ts +++ b/x-pack/legacy/plugins/index_management/public/register_routes.ts @@ -10,7 +10,6 @@ import { CoreStart } from '../../../../../src/core/public'; import { mountReactApp, unmountReactApp } from './app'; import { REACT_ROOT_ID } from './app/constants'; import { BASE_PATH } from '../common/constants'; -import { setHttpClient } from './app/services/api'; import template from './index.html'; import { manageAngularLifecycle } from './app/lib/manage_angular_lifecycle'; @@ -20,15 +19,11 @@ let elem: HTMLElement | null; export const registerRoutes = (core: CoreStart) => { routes.when(`${BASE_PATH}:view?/:action?/:id?`, { template, - controller: ($scope: any, $route: any, $http: ng.IHttpService) => { + controller: ($scope: any, $route: any) => { // clean up previously rendered React app if one exists // this happens because of React Router redirects unmountReactApp(elem); - // TODO $http is still needed for the ILM actions - // Once ILM is migrated to NP, it should be able to be removed - setHttpClient($http); - $scope.$$postDigest(() => { elem = document.getElementById(REACT_ROOT_ID); mountReactApp(elem, { core });