From b23698b2cc4614a8982f90afe7f6160f3f83af65 Mon Sep 17 00:00:00 2001 From: Alison Goryachev Date: Wed, 17 Feb 2021 21:00:00 -0500 Subject: [PATCH] add version-specific logic --- .../upgrade_assistant/common/constants.ts | 15 ++++++ .../application/components/tabs.test.tsx | 9 ++-- .../tabs/checkup/checkup_tab.test.tsx | 10 ++-- .../tabs/checkup/deprecations/index_table.tsx | 4 +- .../__snapshots__/warning_step.test.tsx.snap | 36 ------------- .../reindex/flyout/warning_step.test.tsx | 38 +++++++------ .../reindex/flyout/warnings_step.tsx | 4 +- .../server/lib/__fixtures__/version.ts | 11 ++-- .../server/lib/es_version_precheck.test.ts | 2 +- .../lib/reindexing/index_settings.test.ts | 41 +++++++------- .../server/lib/reindexing/index_settings.ts | 31 +++++------ .../lib/reindexing/reindex_actions.test.ts | 2 +- .../server/lib/reindexing/reindex_actions.ts | 54 +++++++++---------- .../lib/reindexing/reindex_service.test.ts | 7 ++- .../server/lib/reindexing/reindex_service.ts | 2 +- .../server/lib/reindexing/types.ts | 1 + 16 files changed, 123 insertions(+), 144 deletions(-) create mode 100644 x-pack/plugins/upgrade_assistant/common/constants.ts diff --git a/x-pack/plugins/upgrade_assistant/common/constants.ts b/x-pack/plugins/upgrade_assistant/common/constants.ts new file mode 100644 index 000000000000000..654a1a333173328 --- /dev/null +++ b/x-pack/plugins/upgrade_assistant/common/constants.ts @@ -0,0 +1,15 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ +import SemVer from 'semver/classes/semver'; + +/* + * These constants are used only in tests to add conditional logic based on Kibana version + * On master, the version should represent the next major version (e.g., master --> 8.0.0) + * The release branch should match the release version (e.g., 7.x --> 7.0.0) + */ +export const mockKibanaVersion = '8.0.0'; +export const mockKibanaSemverVersion = new SemVer(mockKibanaVersion); diff --git a/x-pack/plugins/upgrade_assistant/public/application/components/tabs.test.tsx b/x-pack/plugins/upgrade_assistant/public/application/components/tabs.test.tsx index ee722a39372169e..b732f6806a388b8 100644 --- a/x-pack/plugins/upgrade_assistant/public/application/components/tabs.test.tsx +++ b/x-pack/plugins/upgrade_assistant/public/application/components/tabs.test.tsx @@ -6,9 +6,9 @@ */ import React from 'react'; -import SemVer from 'semver/classes/semver'; import { mountWithIntl } from '@kbn/test/jest'; import { httpServiceMock } from 'src/core/public/mocks'; +import { mockKibanaSemverVersion } from '../../../common/constants'; import { UpgradeAssistantTabs } from './tabs'; import { LoadingState } from './types'; @@ -18,7 +18,6 @@ import { OverviewTab } from './tabs/overview'; const promisesToResolve = () => new Promise((resolve) => setTimeout(resolve, 0)); const mockHttp = httpServiceMock.createSetupContract(); -const mockKibanaVersion = new SemVer('8.0.0'); jest.mock('../app_context', () => { return { @@ -29,9 +28,9 @@ jest.mock('../app_context', () => { ELASTIC_WEBSITE_URL: 'https://www.elastic.co/', }, kibanaVersionInfo: { - currentMajor: mockKibanaVersion.major, - prevMajor: mockKibanaVersion.major - 1, - nextMajor: mockKibanaVersion.major + 1, + currentMajor: mockKibanaSemverVersion.major, + prevMajor: mockKibanaSemverVersion.major - 1, + nextMajor: mockKibanaSemverVersion.major + 1, }, }; }, diff --git a/x-pack/plugins/upgrade_assistant/public/application/components/tabs/checkup/checkup_tab.test.tsx b/x-pack/plugins/upgrade_assistant/public/application/components/tabs/checkup/checkup_tab.test.tsx index 1ed1e0b01f65b42..bf890c856239e81 100644 --- a/x-pack/plugins/upgrade_assistant/public/application/components/tabs/checkup/checkup_tab.test.tsx +++ b/x-pack/plugins/upgrade_assistant/public/application/components/tabs/checkup/checkup_tab.test.tsx @@ -7,7 +7,7 @@ import { shallow } from 'enzyme'; import React from 'react'; -import SemVer from 'semver/classes/semver'; +import { mockKibanaSemverVersion } from '../../../../../common/constants'; import { LoadingState } from '../../types'; import AssistanceData from '../__fixtures__/checkup_api_response.json'; @@ -22,8 +22,6 @@ const defaultProps = { setSelectedTabIndex: jest.fn(), }; -const mockKibanaVersion = new SemVer('8.0.0'); - jest.mock('../../../app_context', () => { return { useAppContext: () => { @@ -33,9 +31,9 @@ jest.mock('../../../app_context', () => { ELASTIC_WEBSITE_URL: 'https://www.elastic.co/', }, kibanaVersionInfo: { - currentMajor: mockKibanaVersion.major, - prevMajor: mockKibanaVersion.major - 1, - nextMajor: mockKibanaVersion.major + 1, + currentMajor: mockKibanaSemverVersion.major, + prevMajor: mockKibanaSemverVersion.major - 1, + nextMajor: mockKibanaSemverVersion.major + 1, }, }; }, diff --git a/x-pack/plugins/upgrade_assistant/public/application/components/tabs/checkup/deprecations/index_table.tsx b/x-pack/plugins/upgrade_assistant/public/application/components/tabs/checkup/deprecations/index_table.tsx index 418ab12d9c9157c..292887853e4b3b3 100644 --- a/x-pack/plugins/upgrade_assistant/public/application/components/tabs/checkup/deprecations/index_table.tsx +++ b/x-pack/plugins/upgrade_assistant/public/application/components/tabs/checkup/deprecations/index_table.tsx @@ -145,9 +145,9 @@ export class IndexDeprecationTable extends React.Component< // NOTE: this naive implementation assumes all indices in the table are // should show the reindex button. This should work for known use cases. const { indices } = this.props; - const showActionsColumn = Boolean(indices.find((i) => i.reindex === true)); + const hasActionsColumn = Boolean(indices.find((i) => i.reindex === true)); - if (showActionsColumn === false) { + if (hasActionsColumn === false) { return null; } diff --git a/x-pack/plugins/upgrade_assistant/public/application/components/tabs/checkup/deprecations/reindex/flyout/__snapshots__/warning_step.test.tsx.snap b/x-pack/plugins/upgrade_assistant/public/application/components/tabs/checkup/deprecations/reindex/flyout/__snapshots__/warning_step.test.tsx.snap index 3c91a6be19c83ef..dba019550f2a1b7 100644 --- a/x-pack/plugins/upgrade_assistant/public/application/components/tabs/checkup/deprecations/reindex/flyout/__snapshots__/warning_step.test.tsx.snap +++ b/x-pack/plugins/upgrade_assistant/public/application/components/tabs/checkup/deprecations/reindex/flyout/__snapshots__/warning_step.test.tsx.snap @@ -23,42 +23,6 @@ exports[`WarningsFlyoutStep renders 1`] = `

- - _doc - , - } - } - /> - } - documentationUrl="https://www.elastic.co/guide/en/elasticsearch/reference/current/removal-of-types.html" - label={ - - _doc - , - } - } - /> - } - onChange={[Function]} - warning={0} - /> { DOC_LINK_VERSION: 'current', ELASTIC_WEBSITE_URL: 'https://www.elastic.co/', }, + kibanaVersionInfo: { + currentMajor: mockKibanaSemverVersion.major, + prevMajor: mockKibanaSemverVersion.major - 1, + nextMajor: mockKibanaSemverVersion.major + 1, + }, }; }, }; @@ -37,19 +43,21 @@ describe('WarningsFlyoutStep', () => { expect(shallow()).toMatchSnapshot(); }); - it('does not allow proceeding until all are checked', () => { - const wrapper = mount( - - - - ); - const button = wrapper.find('EuiButton'); - - button.simulate('click'); - expect(defaultProps.advanceNextStep).not.toHaveBeenCalled(); - - wrapper.find(`input#${idForWarning(ReindexWarning.customTypeName)}`).simulate('change'); - button.simulate('click'); - expect(defaultProps.advanceNextStep).toHaveBeenCalled(); - }); + if (mockKibanaSemverVersion.major === 7) { + it('does not allow proceeding until all are checked', () => { + const wrapper = mount( + + + + ); + const button = wrapper.find('EuiButton'); + + button.simulate('click'); + expect(defaultProps.advanceNextStep).not.toHaveBeenCalled(); + + wrapper.find(`input#${idForWarning(ReindexWarning.customTypeName)}`).simulate('change'); + button.simulate('click'); + expect(defaultProps.advanceNextStep).toHaveBeenCalled(); + }); + } }); diff --git a/x-pack/plugins/upgrade_assistant/public/application/components/tabs/checkup/deprecations/reindex/flyout/warnings_step.tsx b/x-pack/plugins/upgrade_assistant/public/application/components/tabs/checkup/deprecations/reindex/flyout/warnings_step.tsx index fed481ef8bdab47..92fee8740045fa6 100644 --- a/x-pack/plugins/upgrade_assistant/public/application/components/tabs/checkup/deprecations/reindex/flyout/warnings_step.tsx +++ b/x-pack/plugins/upgrade_assistant/public/application/components/tabs/checkup/deprecations/reindex/flyout/warnings_step.tsx @@ -101,7 +101,7 @@ export const WarningsFlyoutStep: React.FunctionComponent - {warnings.includes(ReindexWarning.customTypeName) && ( + {kibanaVersionInfo.currentMajor === 7 && warnings.includes(ReindexWarning.customTypeName) && ( { - const currentVersion = new SemVer(versionString); - const currentMajor = currentVersion.major; +export const getMockVersionInfo = () => { + const currentMajor = mockKibanaSemverVersion.major; return { - currentVersion, + currentVersion: mockKibanaSemverVersion, currentMajor, prevMajor: currentMajor - 1, nextMajor: currentMajor + 1, diff --git a/x-pack/plugins/upgrade_assistant/server/lib/es_version_precheck.test.ts b/x-pack/plugins/upgrade_assistant/server/lib/es_version_precheck.test.ts index 25dcd2521525dfc..720ba133f2773d8 100644 --- a/x-pack/plugins/upgrade_assistant/server/lib/es_version_precheck.test.ts +++ b/x-pack/plugins/upgrade_assistant/server/lib/es_version_precheck.test.ts @@ -9,7 +9,7 @@ import { SemVer } from 'semver'; import { IScopedClusterClient, kibanaResponseFactory } from 'src/core/server'; import { coreMock } from 'src/core/server/mocks'; import { licensingMock } from '../../../../plugins/licensing/server/mocks'; -import { MOCK_VERSION_STRING, getMockVersionInfo } from './__fixtures__/version'; +import { getMockVersionInfo } from './__fixtures__/version'; import { esVersionCheck, diff --git a/x-pack/plugins/upgrade_assistant/server/lib/reindexing/index_settings.test.ts b/x-pack/plugins/upgrade_assistant/server/lib/reindexing/index_settings.test.ts index dd441594ca039a0..7a6db67ea745e8f 100644 --- a/x-pack/plugins/upgrade_assistant/server/lib/reindexing/index_settings.test.ts +++ b/x-pack/plugins/upgrade_assistant/server/lib/reindexing/index_settings.test.ts @@ -5,9 +5,10 @@ * 2.0. */ +import { mockKibanaSemverVersion, mockKibanaVersion } from '../../../common/constants'; import { ReindexWarning } from '../../../common/types'; import { versionService } from '../version'; -import { MOCK_VERSION_STRING, getMockVersionInfo } from '../__fixtures__/version'; +import { getMockVersionInfo } from '../__fixtures__/version'; import { generateNewIndexName, @@ -63,7 +64,7 @@ describe('transformFlatSettings', () => { describe('sourceNameForIndex', () => { beforeEach(() => { - versionService.setup(MOCK_VERSION_STRING); + versionService.setup(mockKibanaVersion); }); it('parses internal indices', () => { @@ -89,7 +90,7 @@ describe('sourceNameForIndex', () => { describe('generateNewIndexName', () => { beforeEach(() => { - versionService.setup(MOCK_VERSION_STRING); + versionService.setup(mockKibanaVersion); }); it('parses internal indices', () => { @@ -133,21 +134,23 @@ describe('getReindexWarnings', () => { ).toEqual([]); }); - it('returns customTypeName for non-_doc mapping types', () => { - expect( - getReindexWarnings({ - settings: {}, - mappings: { doc: {} }, - }) - ).toEqual([ReindexWarning.customTypeName]); - }); + if (mockKibanaSemverVersion.major === 7) { + it('returns customTypeName for non-_doc mapping types', () => { + expect( + getReindexWarnings({ + settings: {}, + mappings: { doc: {} }, + }) + ).toEqual([ReindexWarning.customTypeName]); + }); - it('does not return customTypeName for _doc mapping types', () => { - expect( - getReindexWarnings({ - settings: {}, - mappings: { _doc: {} }, - }) - ).toEqual([]); - }); + it('does not return customTypeName for _doc mapping types', () => { + expect( + getReindexWarnings({ + settings: {}, + mappings: { _doc: {} }, + }) + ).toEqual([]); + }); + } }); diff --git a/x-pack/plugins/upgrade_assistant/server/lib/reindexing/index_settings.ts b/x-pack/plugins/upgrade_assistant/server/lib/reindexing/index_settings.ts index fade0f2ba31a16d..0b351f6aeff8c13 100644 --- a/x-pack/plugins/upgrade_assistant/server/lib/reindexing/index_settings.ts +++ b/x-pack/plugins/upgrade_assistant/server/lib/reindexing/index_settings.ts @@ -9,9 +9,6 @@ import { flow, omit } from 'lodash'; import { ReindexWarning } from '../../../common/types'; import { versionService } from '../version'; import { FlatSettings, FlatSettingsWithTypeName } from './types'; - -export const DEFAULT_TYPE_NAME = '_doc'; - export interface ParsedIndexName { cleanIndexName: string; baseName: string; @@ -75,21 +72,25 @@ export const generateNewIndexName = (indexName: string): string => { * Returns an array of warnings that should be displayed to user before reindexing begins. * @param flatSettings */ -export const getReindexWarnings = (flatSettings: FlatSettingsWithTypeName): ReindexWarning[] => { - const typeNameWarning = usesCustomTypeName(flatSettings); +export const getReindexWarnings = ( + flatSettings: FlatSettingsWithTypeName | FlatSettings +): ReindexWarning[] => { + const warnings = [ + // No warnings yet for 8.0 -> 9.0 + ] as Array<[ReindexWarning, boolean]>; - const warnings = [[ReindexWarning.customTypeName, typeNameWarning]] as Array< - [ReindexWarning, boolean] - >; + if (versionService.getMajorVersion() === 7) { + const DEFAULT_TYPE_NAME = '_doc'; + // In 7+ it's not possible to have more than one type anyways, so always grab the first + // (and only) key. + const typeName = Object.getOwnPropertyNames(flatSettings.mappings)[0]; - return warnings.filter(([_, applies]) => applies).map(([warning, _]) => warning); -}; + const typeNameWarning = Boolean(typeName && typeName !== DEFAULT_TYPE_NAME); + + warnings.push([ReindexWarning.customTypeName, typeNameWarning]); + } -const usesCustomTypeName = (flatSettings: FlatSettingsWithTypeName) => { - // In 7+ it's not possible to have more than one type anyways, so always grab the first - // (and only) key. - const typeName = Object.getOwnPropertyNames(flatSettings.mappings)[0]; - return typeName && typeName !== DEFAULT_TYPE_NAME; + return warnings.filter(([_, applies]) => applies).map(([warning, _]) => warning); }; const removeUnsettableSettings = (settings: FlatSettings['settings']) => diff --git a/x-pack/plugins/upgrade_assistant/server/lib/reindexing/reindex_actions.test.ts b/x-pack/plugins/upgrade_assistant/server/lib/reindexing/reindex_actions.test.ts index 9d8f9c4c3253dd3..18c2978be6f90da 100644 --- a/x-pack/plugins/upgrade_assistant/server/lib/reindexing/reindex_actions.test.ts +++ b/x-pack/plugins/upgrade_assistant/server/lib/reindexing/reindex_actions.test.ts @@ -21,7 +21,7 @@ import { } from '../../../common/types'; import { versionService } from '../version'; import { LOCK_WINDOW, ReindexActions, reindexActionsFactory } from './reindex_actions'; -import { MOCK_VERSION_STRING, getMockVersionInfo } from '../__fixtures__/version'; +import { getMockVersionInfo } from '../__fixtures__/version'; const { currentMajor, prevMajor } = getMockVersionInfo(); diff --git a/x-pack/plugins/upgrade_assistant/server/lib/reindexing/reindex_actions.ts b/x-pack/plugins/upgrade_assistant/server/lib/reindexing/reindex_actions.ts index c4b15b145375407..fe8844b28e37a62 100644 --- a/x-pack/plugins/upgrade_assistant/server/lib/reindexing/reindex_actions.ts +++ b/x-pack/plugins/upgrade_assistant/server/lib/reindexing/reindex_actions.ts @@ -21,6 +21,7 @@ import { ReindexStatus, ReindexStep, } from '../../../common/types'; +import { versionService } from '../version'; import { generateNewIndexName } from './index_settings'; import { FlatSettings, FlatSettingsWithTypeName } from './types'; @@ -85,13 +86,7 @@ export interface ReindexActions { * Retrieve index settings (in flat, dot-notation style) and mappings. * @param indexName */ - getFlatSettings(indexName: string): Promise; - - /** - * Retrieve index settings (in flat, dot-notation style) and mappings with the type name included. - * @param indexName - */ - getFlatSettingsWithTypeName(indexName: string): Promise; + getFlatSettings(indexName: string): Promise; // ----- Functions below are for enforcing locks around groups of indices like ML or Watcher @@ -243,34 +238,33 @@ export const reindexActionsFactory = ( }, async getFlatSettings(indexName: string) { - const { body: flatSettings } = await esClient.indices.get<{ - [indexName: string]: FlatSettings; - }>({ - index: indexName, - flat_settings: true, - }); - - if (!flatSettings[indexName]) { - return null; + let flatSettings; + + if (versionService.getMajorVersion() === 7) { + // On 7.x, we need to get index settings with mapping type + flatSettings = await esClient.indices.get<{ + [indexName: string]: FlatSettingsWithTypeName; + }>({ + index: indexName, + flat_settings: true, + // This @ts-ignore is needed on master since the flag is deprecated on >7.x + // @ts-ignore + include_type_name: true, + }); + } else { + flatSettings = await esClient.indices.get<{ + [indexName: string]: FlatSettings; + }>({ + index: indexName, + flat_settings: true, + }); } - return flatSettings[indexName]; - }, - - async getFlatSettingsWithTypeName(indexName: string) { - const { body: flatSettings } = await esClient.indices.get<{ - [indexName: string]: FlatSettingsWithTypeName; - }>({ - index: indexName, - flat_settings: true, - include_type_name: true, - }); - - if (!flatSettings[indexName]) { + if (!flatSettings.body[indexName]) { return null; } - return flatSettings[indexName]; + return flatSettings.body[indexName]; }, async _fetchAndLockIndexGroupDoc(indexGroup) { diff --git a/x-pack/plugins/upgrade_assistant/server/lib/reindexing/reindex_service.test.ts b/x-pack/plugins/upgrade_assistant/server/lib/reindexing/reindex_service.test.ts index 6823c3227ef1f34..45c061abfe1c82f 100644 --- a/x-pack/plugins/upgrade_assistant/server/lib/reindexing/reindex_service.test.ts +++ b/x-pack/plugins/upgrade_assistant/server/lib/reindexing/reindex_service.test.ts @@ -23,7 +23,7 @@ import { import { licensingMock } from '../../../../licensing/server/mocks'; import { LicensingPluginSetup } from '../../../../licensing/server'; -import { MOCK_VERSION_STRING, getMockVersionInfo } from '../__fixtures__/version'; +import { getMockVersionInfo } from '../__fixtures__/version'; import { esIndicesStateCheck } from '../es_indices_state_check'; import { versionService } from '../version'; @@ -67,7 +67,6 @@ describe('reindexService', () => { findReindexOperations: jest.fn(unimplemented('findReindexOperations')), findAllByStatus: jest.fn(unimplemented('findAllInProgressOperations')), getFlatSettings: jest.fn(unimplemented('getFlatSettings')), - getFlatSettingsWithTypeName: jest.fn(unimplemented('getFlatSettingsWithTypeName')), cleanupChanges: jest.fn(), incrementIndexGroupReindexes: jest.fn(unimplemented('incrementIndexGroupReindexes')), decrementIndexGroupReindexes: jest.fn(unimplemented('decrementIndexGroupReindexes')), @@ -211,7 +210,7 @@ describe('reindexService', () => { describe('detectReindexWarnings', () => { it('fetches reindex warnings from flat settings', async () => { const indexName = 'myIndex'; - actions.getFlatSettingsWithTypeName.mockResolvedValueOnce({ + actions.getFlatSettings.mockResolvedValueOnce({ settings: { 'index.provided_name': indexName, }, @@ -225,7 +224,7 @@ describe('reindexService', () => { }); it('returns null if index does not exist', async () => { - actions.getFlatSettingsWithTypeName.mockResolvedValueOnce(null); + actions.getFlatSettings.mockResolvedValueOnce(null); const reindexWarnings = await service.detectReindexWarnings('myIndex'); expect(reindexWarnings).toBeNull(); }); diff --git a/x-pack/plugins/upgrade_assistant/server/lib/reindexing/reindex_service.ts b/x-pack/plugins/upgrade_assistant/server/lib/reindexing/reindex_service.ts index 297c672df6f144c..284d275cf82c6d9 100644 --- a/x-pack/plugins/upgrade_assistant/server/lib/reindexing/reindex_service.ts +++ b/x-pack/plugins/upgrade_assistant/server/lib/reindexing/reindex_service.ts @@ -550,7 +550,7 @@ export const reindexServiceFactory = ( }, async detectReindexWarnings(indexName: string) { - const flatSettings = await actions.getFlatSettingsWithTypeName(indexName); + const flatSettings = await actions.getFlatSettings(indexName); if (!flatSettings) { return null; } else { diff --git a/x-pack/plugins/upgrade_assistant/server/lib/reindexing/types.ts b/x-pack/plugins/upgrade_assistant/server/lib/reindexing/types.ts index f96eeec36963bfd..569316e276e430c 100644 --- a/x-pack/plugins/upgrade_assistant/server/lib/reindexing/types.ts +++ b/x-pack/plugins/upgrade_assistant/server/lib/reindexing/types.ts @@ -28,6 +28,7 @@ export interface FlatSettings { }; } +// Specific to 7.x-8 upgrade export interface FlatSettingsWithTypeName { settings: { [key: string]: string;