Skip to content

Commit

Permalink
[7.x] [Upgrade Assistant] Migrate server to new es-js client (#89207) (
Browse files Browse the repository at this point in the history
  • Loading branch information
alisonelizabeth committed Jan 29, 2021
1 parent a5c55bf commit e194a76
Show file tree
Hide file tree
Showing 42 changed files with 666 additions and 611 deletions.
19 changes: 17 additions & 2 deletions x-pack/plugins/upgrade_assistant/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
*/

import { SavedObject, SavedObjectAttributes } from 'src/core/public';
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import type { DeprecationInfo } from '../../../../src/core/server/elasticsearch/legacy/api_types';

export enum ReindexStep {
// Enum values are spaced out by 10 to give us room to insert steps in between.
Expand Down Expand Up @@ -164,6 +162,23 @@ export interface UpgradeAssistantTelemetrySavedObjectAttributes {
[key: string]: any;
}

export type MIGRATION_DEPRECATION_LEVEL = 'none' | 'info' | 'warning' | 'critical';
export interface DeprecationInfo {
level: MIGRATION_DEPRECATION_LEVEL;
message: string;
url: string;
details?: string;
}

export interface IndexSettingsDeprecationInfo {
[indexName: string]: DeprecationInfo[];
}
export interface DeprecationAPIResponse {
cluster_settings: DeprecationInfo[];
ml_settings: DeprecationInfo[];
node_settings: DeprecationInfo[];
index_settings: IndexSettingsDeprecationInfo;
}
export interface EnrichedDeprecationInfo extends DeprecationInfo {
index?: string;
node?: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@

import { IconColor } from '@elastic/eui';
import { invert } from 'lodash';
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import type { DeprecationInfo } from '../../../../../../../../src/core/server/elasticsearch/legacy/api_types';
import { DeprecationInfo } from '../../../../../common/types';

export const LEVEL_MAP: { [level: string]: number } = {
warning: 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import React, { FunctionComponent, useState } from 'react';
import { i18n } from '@kbn/i18n';
import { EuiButton, EuiFieldSearch, EuiFlexGroup, EuiFlexItem, EuiCallOut } from '@elastic/eui';
import { FormattedMessage } from '@kbn/i18n/react';
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import type { DeprecationInfo } from '../../../../../../../../src/core/server/elasticsearch/legacy/api_types';
import { DeprecationInfo } from '../../../../../common/types';
import { GroupByOption, LevelFilterOption, LoadingState } from '../../types';
import { FilterBar } from './filter_bar';
import { GroupByBar } from './group_by_bar';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ import React from 'react';
import { mountWithIntl, shallowWithIntl } from '@kbn/test/jest';
import { EuiBadge, EuiPagination } from '@elastic/eui';

// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import type { DeprecationInfo } from '../../../../../../../../../src/core/server/elasticsearch/legacy/api_types';
import { EnrichedDeprecationInfo } from '../../../../../../common/types';
import { DeprecationInfo, EnrichedDeprecationInfo } from '../../../../../../common/types';
import { GroupByOption, LevelFilterOption } from '../../../types';
import { DeprecationAccordion, filterDeps, GroupedDeprecations } from './grouped';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ import {
} from '@elastic/eui';
import { FormattedMessage } from '@kbn/i18n/react';

// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import type { DeprecationInfo } from '../../../../../../../../../src/core/server/elasticsearch/legacy/api_types';
import { EnrichedDeprecationInfo } from '../../../../../../common/types';
import { DeprecationInfo, EnrichedDeprecationInfo } from '../../../../../../common/types';
import { GroupByOption, LevelFilterOption } from '../../../types';

import { DeprecationCountSummary } from './count_summary';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import React, { FunctionComponent } from 'react';
import { EuiBadge, EuiToolTip } from '@elastic/eui';
import { i18n } from '@kbn/i18n';

// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import type { DeprecationInfo } from '../../../../../../../../../src/core/server/elasticsearch/legacy/api_types';
import { DeprecationInfo } from '../../../../../../common/types';
import { COLOR_MAP, LEVEL_MAP, REVERSE_LEVEL_MAP } from '../constants';

const LocalizedLevels: { [level: string]: string } = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@

import React, { FunctionComponent } from 'react';

// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import type { DeprecationInfo } from '../../../../../../../../../src/core/server/elasticsearch/legacy/api_types';
import { EnrichedDeprecationInfo } from '../../../../../../common/types';
import { DeprecationInfo, EnrichedDeprecationInfo } from '../../../../../../common/types';
import { GroupByOption } from '../../../types';

import { COLOR_MAP, LEVEL_MAP } from '../constants';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@

import { mount, shallow } from 'enzyme';
import React from 'react';
import { DeprecationInfo } from '../../../../../common/types';

// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import type { DeprecationInfo } from '../../../../../../../../src/core/server/elasticsearch/legacy/api_types';
import { LevelFilterOption } from '../../types';
import { FilterBar } from './filter_bar';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import React from 'react';
import { EuiFilterButton, EuiFilterGroup, EuiFlexItem } from '@elastic/eui';
import { i18n } from '@kbn/i18n';

// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import type { DeprecationInfo } from '../../../../../../../../src/core/server/elasticsearch/legacy/api_types';
import { DeprecationInfo } from '../../../../../common/types';
import { LevelFilterOption } from '../../types';

const LocalizedOptions: { [option: string]: string } = {
Expand Down
46 changes: 25 additions & 21 deletions x-pack/plugins/upgrade_assistant/server/lib/apm/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,25 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { RequestEvent } from '@elastic/elasticsearch/lib/Transport';
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import { ScopedClusterClientMock } from 'src/core/server/elasticsearch/client/mocks';
import { elasticsearchServiceMock } from 'src/core/server/mocks';
import { getDeprecatedApmIndices, isLegacyApmIndex } from './index';

function mockedCallWithRequest() {
const createMock = () =>
jest.fn().mockImplementation(async () => {
return {
describe('getDeprecatedApmIndices', () => {
let clusterClient: ScopedClusterClientMock;

const asApiResponse = <T>(body: T): RequestEvent<T> =>
({
body,
} as RequestEvent<T>);

beforeEach(() => {
clusterClient = elasticsearchServiceMock.createScopedClusterClient();

clusterClient.asCurrentUser.indices.getMapping.mockResolvedValueOnce(
asApiResponse({
'foo-1': {
mappings: {},
},
Expand All @@ -34,38 +46,30 @@ function mockedCallWithRequest() {
},
},
},
};
});
return {
callAsInternalUser: createMock(),
callAsCurrentUser: createMock(),
};
}
})
);
});

describe('getDeprecatedApmIndices', () => {
it('calls indices.getMapping', async () => {
const callWithRequest = mockedCallWithRequest();
await getDeprecatedApmIndices(callWithRequest, ['foo-*', 'bar-*']);
await getDeprecatedApmIndices(clusterClient, ['foo-*', 'bar-*']);

expect(callWithRequest.callAsCurrentUser).toHaveBeenCalledWith('indices.getMapping', {
expect(clusterClient.asCurrentUser.indices.getMapping).toHaveBeenCalledWith({
index: 'foo-*,bar-*',
filterPath: '*.mappings._meta.version,*.mappings.properties.@timestamp',
filter_path: '*.mappings._meta.version,*.mappings.properties.@timestamp',
});
});

it('includes mappings not yet at 7.0.0', async () => {
const callWithRequest = mockedCallWithRequest();
const deprecations = await getDeprecatedApmIndices(callWithRequest, ['foo-*']);
const deprecations = await getDeprecatedApmIndices(clusterClient, ['foo-*']);

expect(deprecations).toHaveLength(2);
expect(deprecations[0].index).toEqual('foo-1');
expect(deprecations[1].index).toEqual('foo-2');
});

it('formats the deprecations', async () => {
const callWithRequest = mockedCallWithRequest();
// @ts-ignore
const [deprecation, _] = await getDeprecatedApmIndices(callWithRequest, ['foo-*']);
const [deprecation, _] = await getDeprecatedApmIndices(clusterClient, ['foo-*']);

expect(deprecation.level).toEqual('warning');
expect(deprecation.message).toEqual('APM index requires conversion to 7.x format');
Expand Down
8 changes: 4 additions & 4 deletions x-pack/plugins/upgrade_assistant/server/lib/apm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@
import { get } from 'lodash';
import minimatch from 'minimatch';
import { SemVer, valid } from 'semver';
import { ILegacyScopedClusterClient } from 'src/core/server';
import { IScopedClusterClient } from 'src/core/server';

import { EnrichedDeprecationInfo } from '../../../common/types';
import { FlatSettings } from '../reindexing/types';

export async function getDeprecatedApmIndices(
clusterClient: ILegacyScopedClusterClient,
clusterClient: IScopedClusterClient,
indexPatterns: string[] = []
): Promise<EnrichedDeprecationInfo[]> {
const indices = await clusterClient.callAsCurrentUser('indices.getMapping', {
const { body: indices } = await clusterClient.asCurrentUser.indices.getMapping({
index: indexPatterns.join(','),
// we include @timestamp to prevent filtering mappings without a version
// since @timestamp is expected to always exist
filterPath: '*.mappings._meta.version,*.mappings.properties.@timestamp',
filter_path: '*.mappings._meta.version,*.mappings.properties.@timestamp',
});

return Object.keys(indices).reduce((deprecations: EnrichedDeprecationInfo[], index) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,30 @@ import {

describe('getDeprecationLoggingStatus', () => {
it('calls cluster.getSettings', async () => {
const dataClient = elasticsearchServiceMock.createLegacyScopedClusterClient();
const dataClient = elasticsearchServiceMock.createScopedClusterClient();
await getDeprecationLoggingStatus(dataClient);
expect(dataClient.callAsCurrentUser).toHaveBeenCalledWith('cluster.getSettings', {
includeDefaults: true,
expect(dataClient.asCurrentUser.cluster.getSettings).toHaveBeenCalledWith({
include_defaults: true,
});
});
});

describe('setDeprecationLogging', () => {
describe('isEnabled = true', () => {
it('calls cluster.putSettings with logger.deprecation = WARN', async () => {
const dataClient = elasticsearchServiceMock.createLegacyScopedClusterClient();
const dataClient = elasticsearchServiceMock.createScopedClusterClient();
await setDeprecationLogging(dataClient, true);
expect(dataClient.callAsCurrentUser).toHaveBeenCalledWith('cluster.putSettings', {
expect(dataClient.asCurrentUser.cluster.putSettings).toHaveBeenCalledWith({
body: { transient: { 'logger.deprecation': 'WARN' } },
});
});
});

describe('isEnabled = false', () => {
it('calls cluster.putSettings with logger.deprecation = ERROR', async () => {
const dataClient = elasticsearchServiceMock.createLegacyScopedClusterClient();
const dataClient = elasticsearchServiceMock.createScopedClusterClient();
await setDeprecationLogging(dataClient, false);
expect(dataClient.callAsCurrentUser).toHaveBeenCalledWith('cluster.putSettings', {
expect(dataClient.asCurrentUser.cluster.putSettings).toHaveBeenCalledWith({
body: { transient: { 'logger.deprecation': 'ERROR' } },
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
* you may not use this file except in compliance with the Elastic License.
*/
import { get } from 'lodash';
import { ILegacyScopedClusterClient } from 'src/core/server';
import { IScopedClusterClient } from 'src/core/server';

interface DeprecationLoggingStatus {
isEnabled: boolean;
}

export async function getDeprecationLoggingStatus(
dataClient: ILegacyScopedClusterClient
dataClient: IScopedClusterClient
): Promise<DeprecationLoggingStatus> {
const response = await dataClient.callAsCurrentUser('cluster.getSettings', {
includeDefaults: true,
const { body: response } = await dataClient.asCurrentUser.cluster.getSettings({
include_defaults: true,
});

return {
Expand All @@ -23,10 +23,10 @@ export async function getDeprecationLoggingStatus(
}

export async function setDeprecationLogging(
dataClient: ILegacyScopedClusterClient,
dataClient: IScopedClusterClient,
isEnabled: boolean
): Promise<DeprecationLoggingStatus> {
const response = await dataClient.callAsCurrentUser('cluster.putSettings', {
const { body: response } = await dataClient.asCurrentUser.cluster.putSettings({
body: {
transient: {
'logger.deprecation': isEnabled ? 'WARN' : 'ERROR',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,19 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { LegacyAPICaller } from 'kibana/server';
import { ElasticsearchClient } from 'kibana/server';
import { getIndexState } from '../../common/get_index_state';
import { ResolveIndexResponseFromES } from '../../common/types';

type StatusCheckResult = Record<string, 'open' | 'closed'>;

export const esIndicesStateCheck = async (
callAsUser: LegacyAPICaller,
asCurrentUser: ElasticsearchClient,
indices: string[]
): Promise<StatusCheckResult> => {
const response: ResolveIndexResponseFromES = await callAsUser('transport.request', {
method: 'GET',
path: `/_resolve/index/*`,
query: {
expand_wildcards: 'all',
},
const { body: response } = await asCurrentUser.indices.resolveIndex<ResolveIndexResponseFromES>({
name: '*',
expand_wildcards: 'all',
});

const result: StatusCheckResult = {};
Expand Down
Loading

0 comments on commit e194a76

Please sign in to comment.