Skip to content

Commit

Permalink
Inject NP http service into Index Management extensions. Remove depen…
Browse files Browse the repository at this point in the history
…dency upon Angular's $http.
  • Loading branch information
cjcenizal committed Feb 4, 2020
1 parent e46dad1 commit 225bf51
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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) {
Expand Down
41 changes: 0 additions & 41 deletions x-pack/legacy/plugins/index_management/public/app/services/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<any> => {
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<any> => {
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<any> => {
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@

import { reloadIndices } from '../actions';
import { notificationService } from '../../services/notification';
import { getNewPlatformCompatibleHttpClient } from '../../services/api';
import { httpService } from '../../services/http';

export const performExtensionAction = ({
requestMethod,
indexNames,
successMessage,
}) => async dispatch => {
try {
await requestMethod(indexNames, getNewPlatformCompatibleHttpClient());
await requestMethod(indexNames, httpService.httpClient);
} catch (error) {
notificationService.showDangerToast(error.message);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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 });
Expand Down

0 comments on commit 225bf51

Please sign in to comment.