From b55e964fd2f0b6a5bd7eeb8b56df5a5575d4e29d Mon Sep 17 00:00:00 2001 From: Ignacio Rivas Date: Mon, 11 Oct 2021 18:17:52 +0100 Subject: [PATCH] [Upgrade Assistant] Delete deprecation log cache (#114113) --- .../helpers/http_requests.ts | 11 +++++ .../fix_logs_step/fix_logs_step.test.tsx | 43 +++++++++++++++++++ .../deprecations_count_checkpoint.tsx | 32 ++++++++++++-- .../public/application/lib/api.ts | 7 +++ .../server/routes/deprecation_logging.test.ts | 38 ++++++++++++++++ .../server/routes/deprecation_logging.ts | 29 +++++++++++++ 6 files changed, 156 insertions(+), 4 deletions(-) diff --git a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/helpers/http_requests.ts b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/helpers/http_requests.ts index 448eb79152894..4f0e048da0792 100644 --- a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/helpers/http_requests.ts +++ b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/helpers/http_requests.ts @@ -69,6 +69,16 @@ const registerHttpRequestMockHelpers = (server: SinonFakeServer) => { ]); }; + const setDeleteLogsCacheResponse = (response?: string, error?: ResponseError) => { + const status = error ? error.statusCode || 400 : 200; + const body = error ? error : response; + server.respondWith('DELETE', `${API_BASE_PATH}/deprecation_logging/cache`, [ + status, + { 'Content-Type': 'application/json' }, + JSON.stringify(body), + ]); + }; + const setUpdateDeprecationLoggingResponse = ( response?: DeprecationLoggingStatus, error?: ResponseError @@ -169,6 +179,7 @@ const registerHttpRequestMockHelpers = (server: SinonFakeServer) => { setDeleteMlSnapshotResponse, setUpgradeMlSnapshotStatusResponse, setLoadDeprecationLogsCountResponse, + setDeleteLogsCacheResponse, setStartReindexingResponse, setReindexStatusResponse, setLoadMlUpgradeModeResponse, diff --git a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/overview/fix_logs_step/fix_logs_step.test.tsx b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/overview/fix_logs_step/fix_logs_step.test.tsx index 7f58b36d29c36..7d4b6b8518dff 100644 --- a/x-pack/plugins/upgrade_assistant/__jest__/client_integration/overview/fix_logs_step/fix_logs_step.test.tsx +++ b/x-pack/plugins/upgrade_assistant/__jest__/client_integration/overview/fix_logs_step/fix_logs_step.test.tsx @@ -248,6 +248,7 @@ describe('Overview - Fix deprecation logs step', () => { describe('Step 3 - Resolve log issues', () => { beforeEach(async () => { httpRequestsMockHelpers.setLoadDeprecationLoggingResponse(getLoggingResponse(true)); + httpRequestsMockHelpers.setDeleteLogsCacheResponse('ok'); }); test('With deprecation warnings', async () => { @@ -337,6 +338,48 @@ describe('Overview - Fix deprecation logs step', () => { expect(exists('noWarningsCallout')).toBe(true); }); + test('Shows a toast if deleting cache fails', async () => { + const error = { + statusCode: 500, + error: 'Internal server error', + message: 'Internal server error', + }; + + httpRequestsMockHelpers.setDeleteLogsCacheResponse(undefined, error); + // Initially we want to have the callout to have a warning state + httpRequestsMockHelpers.setLoadDeprecationLogsCountResponse({ count: 10 }); + + const addDanger = jest.fn(); + await act(async () => { + testBed = await setupOverviewPage({ + services: { + core: { + notifications: { + toasts: { + addDanger, + }, + }, + }, + }, + }); + }); + + const { exists, actions, component } = testBed; + + component.update(); + + httpRequestsMockHelpers.setLoadDeprecationLogsCountResponse({ count: 0 }); + + await actions.clickResetButton(); + + // The toast should always be shown if the delete logs cache fails. + expect(addDanger).toHaveBeenCalled(); + // Even though we changed the response of the getLogsCountResponse, when the + // deleteLogsCache fails the getLogsCount api should not be called and the + // status of the callout should remain the same it initially was. + expect(exists('hasWarningsCallout')).toBe(true); + }); + describe('Poll for logs count', () => { beforeEach(async () => { jest.useFakeTimers(); diff --git a/x-pack/plugins/upgrade_assistant/public/application/components/overview/fix_logs_step/deprecations_count_checkpoint/deprecations_count_checkpoint.tsx b/x-pack/plugins/upgrade_assistant/public/application/components/overview/fix_logs_step/deprecations_count_checkpoint/deprecations_count_checkpoint.tsx index 9717918e25a6d..c200884e94699 100644 --- a/x-pack/plugins/upgrade_assistant/public/application/components/overview/fix_logs_step/deprecations_count_checkpoint/deprecations_count_checkpoint.tsx +++ b/x-pack/plugins/upgrade_assistant/public/application/components/overview/fix_logs_step/deprecations_count_checkpoint/deprecations_count_checkpoint.tsx @@ -5,7 +5,7 @@ * 2.0. */ -import React, { FunctionComponent, useEffect } from 'react'; +import React, { FunctionComponent, useEffect, useState } from 'react'; import moment from 'moment-timezone'; import { FormattedDate, FormattedTime, FormattedMessage } from '@kbn/i18n/react'; @@ -44,6 +44,9 @@ const i18nTexts = { defaultMessage: 'Reset counter', } ), + errorToastTitle: i18n.translate('xpack.upgradeAssistant.overview.verifyChanges.errorToastTitle', { + defaultMessage: 'Could not delete deprecation logs cache', + }), }; interface Props { @@ -57,8 +60,12 @@ export const DeprecationsCountCheckpoint: FunctionComponent = ({ setCheckpoint, setHasNoDeprecationLogs, }) => { + const [isDeletingCache, setIsDeletingCache] = useState(false); const { - services: { api }, + services: { + api, + core: { notifications }, + }, } = useAppContext(); const { data, error, isLoading, resendRequest, isInitialRequest } = api.getDeprecationLogsCount( checkpoint @@ -70,7 +77,19 @@ export const DeprecationsCountCheckpoint: FunctionComponent = ({ const calloutIcon = hasLogs ? 'alert' : 'check'; const calloutTestId = hasLogs ? 'hasWarningsCallout' : 'noWarningsCallout'; - const onResetClick = () => { + const onResetClick = async () => { + setIsDeletingCache(true); + const { error: deleteLogsCacheError } = await api.deleteDeprecationLogsCache(); + setIsDeletingCache(false); + + if (deleteLogsCacheError) { + notifications.toasts.addDanger({ + title: i18nTexts.errorToastTitle, + text: deleteLogsCacheError.message.toString(), + }); + return; + } + const now = moment().toISOString(); setCheckpoint(now); }; @@ -115,7 +134,12 @@ export const DeprecationsCountCheckpoint: FunctionComponent = ({ data-test-subj={calloutTestId} >

{i18nTexts.calloutBody}

- + {i18nTexts.resetCounterButton} diff --git a/x-pack/plugins/upgrade_assistant/public/application/lib/api.ts b/x-pack/plugins/upgrade_assistant/public/application/lib/api.ts index da4f87f497467..b701a5a5eff2c 100644 --- a/x-pack/plugins/upgrade_assistant/public/application/lib/api.ts +++ b/x-pack/plugins/upgrade_assistant/public/application/lib/api.ts @@ -106,6 +106,13 @@ export class ApiService { }); } + public deleteDeprecationLogsCache() { + return this.sendRequest({ + path: `${API_BASE_PATH}/deprecation_logging/cache`, + method: 'delete', + }); + } + public async updateIndexSettings(indexName: string, settings: string[]) { const result = await this.sendRequest({ path: `${API_BASE_PATH}/${indexName}/index_settings`, diff --git a/x-pack/plugins/upgrade_assistant/server/routes/deprecation_logging.test.ts b/x-pack/plugins/upgrade_assistant/server/routes/deprecation_logging.test.ts index b94e75d1aedab..1315e503732e3 100644 --- a/x-pack/plugins/upgrade_assistant/server/routes/deprecation_logging.test.ts +++ b/x-pack/plugins/upgrade_assistant/server/routes/deprecation_logging.test.ts @@ -168,4 +168,42 @@ describe('deprecation logging API', () => { ).rejects.toThrow('scary error!'); }); }); + + describe('DELETE /api/upgrade_assistant/deprecation_logging/cache', () => { + it('returns ok if if the cache was deleted', async () => { + ( + routeHandlerContextMock.core.elasticsearch.client.asCurrentUser.transport + .request as jest.Mock + ).mockResolvedValue({ + body: 'ok', + }); + + const resp = await routeDependencies.router.getHandler({ + method: 'delete', + pathPattern: '/api/upgrade_assistant/deprecation_logging/cache', + })(routeHandlerContextMock, createRequestMock(), kibanaResponseFactory); + + expect(resp.status).toEqual(200); + expect( + routeHandlerContextMock.core.elasticsearch.client.asCurrentUser.transport.request + ).toHaveBeenCalledWith({ + method: 'DELETE', + path: '/_logging/deprecation_cache', + }); + expect(resp.payload).toEqual('ok'); + }); + + it('returns an error if it throws', async () => { + ( + routeHandlerContextMock.core.elasticsearch.client.asCurrentUser.transport + .request as jest.Mock + ).mockRejectedValue(new Error('scary error!')); + await expect( + routeDependencies.router.getHandler({ + method: 'delete', + pathPattern: '/api/upgrade_assistant/deprecation_logging/cache', + })(routeHandlerContextMock, createRequestMock(), kibanaResponseFactory) + ).rejects.toThrow('scary error!'); + }); + }); }); diff --git a/x-pack/plugins/upgrade_assistant/server/routes/deprecation_logging.ts b/x-pack/plugins/upgrade_assistant/server/routes/deprecation_logging.ts index 00116ae24a3fb..5d7f0f67b0ca9 100644 --- a/x-pack/plugins/upgrade_assistant/server/routes/deprecation_logging.ts +++ b/x-pack/plugins/upgrade_assistant/server/routes/deprecation_logging.ts @@ -124,4 +124,33 @@ export function registerDeprecationLoggingRoutes({ } ) ); + + router.delete( + { + path: `${API_BASE_PATH}/deprecation_logging/cache`, + validate: false, + }, + versionCheckHandlerWrapper( + async ( + { + core: { + elasticsearch: { client }, + }, + }, + request, + response + ) => { + try { + await client.asCurrentUser.transport.request({ + method: 'DELETE', + path: '/_logging/deprecation_cache', + }); + + return response.ok({ body: 'ok' }); + } catch (error) { + return handleEsError({ error, response }); + } + } + ) + ); }