Skip to content

Commit

Permalink
migrate UA to new es-js client
Browse files Browse the repository at this point in the history
  • Loading branch information
alisonelizabeth committed Jan 25, 2021
1 parent f6813b8 commit bc92fa2
Show file tree
Hide file tree
Showing 27 changed files with 292 additions and 282 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 @@ -165,6 +163,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,15 +6,14 @@

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 { MIGRATION_DEPRECATION_LEVEL } from '../../../../../common/types';

export const LEVEL_MAP: { [level: string]: number } = {
warning: 0,
critical: 1,
};

export const REVERSE_LEVEL_MAP: { [idx: number]: DeprecationInfo['level'] } = invert(
export const REVERSE_LEVEL_MAP: { [idx: number]: MIGRATION_DEPRECATION_LEVEL } = invert(
LEVEL_MAP
) as any;

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, MIGRATION_DEPRECATION_LEVEL } from '../../../../../../common/types';
import { COLOR_MAP, LEVEL_MAP, REVERSE_LEVEL_MAP } from '../constants';

const LocalizedLevels: { [level: string]: string } = {
Expand All @@ -37,7 +36,7 @@ interface DeprecationHealthProps {
single?: boolean;
}

const SingleHealth: FunctionComponent<{ level: DeprecationInfo['level']; label: string }> = ({
const SingleHealth: FunctionComponent<{ level: MIGRATION_DEPRECATION_LEVEL; label: string }> = ({
level,
label,
}) => (
Expand Down Expand Up @@ -81,7 +80,7 @@ export const DeprecationHealth: FunctionComponent<DeprecationHealthProps> = ({
.map(([numLevel, stringLevel]) => (
<SingleHealth
key={stringLevel}
level={stringLevel as DeprecationInfo['level']}
level={stringLevel as MIGRATION_DEPRECATION_LEVEL}
label={`${countByLevel[numLevel]} ${LocalizedLevels[stringLevel]}`}
/>
))}
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
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
111 changes: 55 additions & 56 deletions x-pack/plugins/upgrade_assistant/server/lib/es_migration_apis.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,95 +5,94 @@
*/

import _ from 'lodash';
import { RequestEvent } from '@elastic/elasticsearch/lib/Transport';
import { elasticsearchServiceMock } from 'src/core/server/mocks';
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import type { DeprecationAPIResponse } from '../../../../../src/core/server/elasticsearch/legacy/api_types';
import { DeprecationAPIResponse } from '../../common/types';

import { getUpgradeAssistantStatus } from './es_migration_apis';
import fakeDeprecations from './__fixtures__/fake_deprecations.json';

const fakeIndexNames = Object.keys(fakeDeprecations.index_settings);

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

describe('getUpgradeAssistantStatus', () => {
const resolvedIndices = {
indices: fakeIndexNames.map((f) => ({ name: f, attributes: ['open'] })),
};
let deprecationsResponse: DeprecationAPIResponse;

const dataClient = elasticsearchServiceMock.createLegacyScopedClusterClient();
(dataClient.callAsCurrentUser as jest.Mock).mockImplementation(async (api, { path }) => {
if (path === '/_migration/deprecations') {
return deprecationsResponse;
} else if (path === '/_resolve/index/*') {
return resolvedIndices;
} else if (api === 'indices.getMapping') {
return {};
} else {
throw new Error(`Unexpected API call: ${path}`);
}
});
// @ts-expect-error mock data is too loosely typed
const deprecationsResponse: DeprecationAPIResponse = _.cloneDeep(fakeDeprecations);

beforeEach(() => {
// @ts-expect-error mock data is too loosely typed
deprecationsResponse = _.cloneDeep(fakeDeprecations);
});
const esClient = elasticsearchServiceMock.createScopedClusterClient();

esClient.asCurrentUser.migration.deprecations.mockResolvedValue(
asApiResponse(deprecationsResponse)
);

esClient.asCurrentUser.indices.resolveIndex.mockResolvedValue(asApiResponse(resolvedIndices));

it('calls /_migration/deprecations', async () => {
await getUpgradeAssistantStatus(dataClient, false);
expect(dataClient.callAsCurrentUser).toHaveBeenCalledWith('transport.request', {
path: '/_migration/deprecations',
method: 'GET',
});
await getUpgradeAssistantStatus(esClient, false);
expect(esClient.asCurrentUser.migration.deprecations).toHaveBeenCalled();
});

it('returns the correct shape of data', async () => {
const resp = await getUpgradeAssistantStatus(dataClient, false);
const resp = await getUpgradeAssistantStatus(esClient, false);
expect(resp).toMatchSnapshot();
});

it('returns readyForUpgrade === false when critical issues found', async () => {
deprecationsResponse = {
cluster_settings: [{ level: 'critical', message: 'Do count me', url: 'https://...' }],
node_settings: [],
ml_settings: [],
index_settings: {},
};

await expect(getUpgradeAssistantStatus(dataClient, false)).resolves.toHaveProperty(
esClient.asCurrentUser.migration.deprecations.mockResolvedValue(
asApiResponse({
cluster_settings: [{ level: 'critical', message: 'Do count me', url: 'https://...' }],
node_settings: [],
ml_settings: [],
index_settings: {},
})
);

await expect(getUpgradeAssistantStatus(esClient, false)).resolves.toHaveProperty(
'readyForUpgrade',
false
);
});

it('returns readyForUpgrade === true when no critical issues found', async () => {
deprecationsResponse = {
cluster_settings: [{ level: 'warning', message: 'Do not count me', url: 'https://...' }],
node_settings: [],
ml_settings: [],
index_settings: {},
};

await expect(getUpgradeAssistantStatus(dataClient, false)).resolves.toHaveProperty(
esClient.asCurrentUser.migration.deprecations.mockResolvedValue(
asApiResponse({
cluster_settings: [{ level: 'warning', message: 'Do not count me', url: 'https://...' }],
node_settings: [],
ml_settings: [],
index_settings: {},
})
);

await expect(getUpgradeAssistantStatus(esClient, false)).resolves.toHaveProperty(
'readyForUpgrade',
true
);
});

it('filters out security realm deprecation on Cloud', async () => {
deprecationsResponse = {
cluster_settings: [
{
level: 'critical',
message: 'Security realm settings structure changed',
url: 'https://...',
},
],
node_settings: [],
ml_settings: [],
index_settings: {},
};

const result = await getUpgradeAssistantStatus(dataClient, true);
esClient.asCurrentUser.migration.deprecations.mockResolvedValue(
asApiResponse({
cluster_settings: [
{
level: 'critical',
message: 'Security realm settings structure changed',
url: 'https://...',
},
],
node_settings: [],
ml_settings: [],
index_settings: {},
})
);

const result = await getUpgradeAssistantStatus(esClient, true);

expect(result).toHaveProperty('readyForUpgrade', true);
expect(result).toHaveProperty('cluster', []);
Expand Down
Loading

0 comments on commit bc92fa2

Please sign in to comment.