Skip to content

Commit

Permalink
Add a log sources setting migration to the upgrade assistant
Browse files Browse the repository at this point in the history
  • Loading branch information
Kerry350 committed Aug 7, 2024
1 parent 45274e0 commit 114f3aa
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* 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.
*/

export const MIGRATE_LOG_VIEW_SETTINGS_URL =
'/api/logs_shared/deprecations/migrate_log_view_settings';
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* 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 { LogsSharedPluginCoreSetup } from '../types';
import { getLogSourcesSettingDeprecationInfo } from './log_sources_setting';

export const registerDeprecations = ({ core }: { core: LogsSharedPluginCoreSetup }) => {
core.deprecations.registerDeprecations({
getDeprecations: async (context) => {
return [
...(await getLogSourcesSettingDeprecationInfo({
context,
getStartServices: core.getStartServices,
})),
];
},
});
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* 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 { DeprecationsDetails } from '@kbn/core-deprecations-common';
import { GetDeprecationsContext } from '@kbn/core-deprecations-server';
import { i18n } from '@kbn/i18n';
import { MIGRATE_LOG_VIEW_SETTINGS_URL } from '../../common/http_api/deprecations';
import { logSourcesKibanaAdvancedSettingRT } from '../../common';
import { LogsSharedPluginStartServicesAccessor } from '../types';

export const getLogSourcesSettingDeprecationInfo = async ({
getStartServices,
context,
}: {
context: GetDeprecationsContext;
getStartServices: LogsSharedPluginStartServicesAccessor;
}): Promise<DeprecationsDetails[]> => {
const [_, pluginStartDeps, pluginStart] = await getStartServices();
const logSourcesService =
pluginStartDeps.logsDataAccess.services.logSourcesServiceFactory.getLogSourcesService(
context.savedObjectsClient
);
const logViewsClient = pluginStart.logViews.getClient(
context.savedObjectsClient,
context.esClient.asCurrentUser,
logSourcesService
);

const logView = await logViewsClient.getLogView('default');

if (logView && !logSourcesKibanaAdvancedSettingRT.is(logView.attributes.logIndices)) {
return [
{
title: i18n.translate('xpack.logsShared.deprecations.migrateLogSourcesSettingTitle', {
defaultMessage: 'Log sources setting',
}),
level: 'warning',
deprecationType: 'config',
configPath: '',
message: i18n.translate('xpack.logsShared.deprecations.migrateLogSourcesSettingMessage', {
defaultMessage:
'Indices and Data view options previously provided via the Logs UI settings page are now deprecated. Please migrate to using the Kibana log sources advanced setting.',
}),
correctiveActions: {
manualSteps: [
i18n.translate(
'xpack.logsShared.deprecations.migrateLogSourcesSettingMessage.manualStepMessage',
{
defaultMessage:
'Update the Log sources Kibana advanced setting (via Management > Advanced Settings) to match the setting previously provided via the Logs UI settings page. Then via the Logs UI settings page use the Kibana log sources advanced setting option.',
}
),
],
api: {
method: 'PUT',
path: MIGRATE_LOG_VIEW_SETTINGS_URL,
},
},
},
];
} else {
return [];
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ import {
initLogEntriesSummaryRoute,
} from './routes/log_entries';
import { initLogViewRoutes } from './routes/log_views';
import { initMigrateLogViewSettingsRoute } from './routes/deprecations';

export const initLogsSharedServer = (libs: LogsSharedBackendLibs) => {
initLogEntriesHighlightsRoute(libs);
initLogEntriesSummaryRoute(libs);
initLogEntriesSummaryHighlightsRoute(libs);
initLogViewRoutes(libs);
initMigrateLogViewSettingsRoute(libs);
};
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { LogsSharedLogEntriesDomain } from './lib/domains/log_entries_domain';
import { LogsSharedKibanaLogEntriesAdapter } from './lib/adapters/log_entries/kibana_log_entries_adapter';
import { LogEntriesService } from './services/log_entries';
import { LogsSharedConfig } from '../common/plugin_config';
import { registerDeprecations } from './deprecations';

export class LogsSharedPlugin
implements
Expand Down Expand Up @@ -84,6 +85,8 @@ export class LogsSharedPlugin
const logEntriesService = new LogEntriesService();
logEntriesService.setup(core, plugins);

registerDeprecations({ core });

return {
...domainLibs,
logViews,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
* 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.
*/

export * from './migrate_log_view_settings';
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* 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 { MIGRATE_LOG_VIEW_SETTINGS_URL } from '../../../common/http_api/deprecations';
import { logSourcesKibanaAdvancedSettingRT } from '../../../common';
import { LogsSharedBackendLibs } from '../../lib/logs_shared_types';

// This route facilitates automated one-click handling of updating log view's to use the
// Kibana advanced setting as part of the upgrade assistant.
// First, it will gather the indices currently set on the log view.
// Secondly, it will update the advanced setting to use these indices.
// Lastly, it will update the log view to use the kibana advanced setting.
export const initMigrateLogViewSettingsRoute = ({
framework,
getStartServices,
}: LogsSharedBackendLibs) => {
framework.router.put(
{ path: MIGRATE_LOG_VIEW_SETTINGS_URL, validate: false },
async (context, request, response) => {
const [_, pluginStartDeps, pluginStart] = await getStartServices();

const logSourcesService =
await pluginStartDeps.logsDataAccess.services.logSourcesServiceFactory.getScopedLogSourcesService(
request
);
const logViewsClient = pluginStart.logViews.getScopedClient(request);

const logView = await logViewsClient.getLogView('default');

if (!logView || logSourcesKibanaAdvancedSettingRT.is(logView.attributes.logIndices)) {
throw new Error(
"Unable to migrate log view settings. A log view either doesn't exist or is already using the Kibana advanced setting."
);
}

const indices = (
await logViewsClient.getResolvedLogView({
type: 'log-view-reference',
logViewId: 'default',
})
).indices;

await logSourcesService.setLogSources([{ indexPattern: indices }]);
await logViewsClient.putLogView('default', {
logIndices: { type: 'kibana_advanced_setting' },
});
return response.ok();
}
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,7 @@
"@kbn/test-jest-helpers",
"@kbn/router-utils",
"@kbn/logs-data-access-plugin",
"@kbn/core-deprecations-common",
"@kbn/core-deprecations-server",
]
}

0 comments on commit 114f3aa

Please sign in to comment.