Skip to content

Commit

Permalink
Merge branch '7.x' into backport/7.x/pr-85706
Browse files Browse the repository at this point in the history
  • Loading branch information
kibanamachine authored Dec 14, 2020
2 parents 2ee0933 + 9b0ec30 commit e550b1e
Show file tree
Hide file tree
Showing 42 changed files with 651 additions and 267 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import '../../../__mocks__/enterprise_search_url.mock';
import { mockTelemetryActions, mountWithIntl } from '../../../__mocks__/';

import React from 'react';
import { EuiBasicTable, EuiPagination, EuiButtonEmpty } from '@elastic/eui';
import { EuiLinkTo } from '../../../shared/react_router_helpers';
import { EuiBasicTable, EuiPagination, EuiButtonEmpty, EuiLink } from '@elastic/eui';

import { EnginesTable } from './engines_table';

Expand Down Expand Up @@ -55,10 +54,10 @@ describe('EnginesTable', () => {
});

it('contains engine links which send telemetry', () => {
const engineLinks = wrapper.find(EuiLinkTo);
const engineLinks = wrapper.find(EuiLink);

engineLinks.forEach((link) => {
expect(link.prop('to')).toEqual('/engines/test-engine');
expect(link.prop('href')).toEqual('http://localhost:3002/as/engines/test-engine');
link.simulate('click');

expect(mockTelemetryActions.sendAppSearchTelemetry).toHaveBeenCalledWith({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

import React from 'react';
import { useActions } from 'kea';
import { EuiBasicTable, EuiBasicTableColumn } from '@elastic/eui';
import { EuiBasicTable, EuiBasicTableColumn, EuiLink } from '@elastic/eui';
import { FormattedMessage, FormattedDate, FormattedNumber } from '@kbn/i18n/react';
import { i18n } from '@kbn/i18n';

import { TelemetryLogic } from '../../../shared/telemetry';
import { EuiLinkTo } from '../../../shared/react_router_helpers';
import { getAppSearchUrl } from '../../../shared/enterprise_search_url';
import { getEngineRoute } from '../../routes';

import { ENGINES_PAGE_SIZE } from '../../../../../common/constants';
Expand Down Expand Up @@ -47,7 +47,8 @@ export const EnginesTable: React.FC<EnginesTableProps> = ({
const { sendAppSearchTelemetry } = useActions(TelemetryLogic);

const engineLinkProps = (name: string) => ({
to: getEngineRoute(name),
href: getAppSearchUrl(getEngineRoute(name)),
target: '_blank',
onClick: () =>
sendAppSearchTelemetry({
action: 'clicked',
Expand All @@ -62,9 +63,9 @@ export const EnginesTable: React.FC<EnginesTableProps> = ({
defaultMessage: 'Name',
}),
render: (name: string) => (
<EuiLinkTo data-test-subj="engineNameLink" {...engineLinkProps(name)}>
<EuiLink data-test-subj="engineNameLink" {...engineLinkProps(name)}>
{name}
</EuiLinkTo>
</EuiLink>
),
width: '30%',
truncateText: true,
Expand Down Expand Up @@ -137,12 +138,12 @@ export const EnginesTable: React.FC<EnginesTableProps> = ({
),
dataType: 'string',
render: (name: string) => (
<EuiLinkTo {...engineLinkProps(name)}>
<EuiLink {...engineLinkProps(name)}>
<FormattedMessage
id="xpack.enterpriseSearch.appSearch.enginesOverview.table.action.manage"
defaultMessage="Manage"
/>
</EuiLinkTo>
</EuiLink>
),
align: 'right',
width: '100px',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe('AppSearchConfigured', () => {
it('renders with layout', () => {
const wrapper = shallow(<AppSearchConfigured />);

expect(wrapper.find(Layout)).toHaveLength(2);
expect(wrapper.find(Layout)).toHaveLength(1);
expect(wrapper.find(Layout).last().prop('readOnlyMode')).toBeFalsy();
expect(wrapper.find(EnginesOverview)).toHaveLength(1);
expect(wrapper.find(EngineRouter)).toHaveLength(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import React, { useEffect } from 'react';
import { Route, Redirect, Switch } from 'react-router-dom';
import { useActions, useValues } from 'kea';

import { EuiPage, EuiPageBody } from '@elastic/eui';

import { getAppSearchUrl } from '../shared/enterprise_search_url';
import { KibanaLogic } from '../shared/kibana';
import { HttpLogic } from '../shared/http';
Expand Down Expand Up @@ -72,29 +74,31 @@ export const AppSearchConfigured: React.FC<InitialAppData> = (props) => {
</Layout>
</Route>
<Route>
<Layout navigation={<AppSearchNav />} readOnlyMode={readOnlyMode}>
{errorConnecting ? (
<ErrorConnecting />
) : (
<Switch>
<Route exact path={ROOT_PATH}>
<Redirect to={ENGINES_PATH} />
</Route>
<Route exact path={ENGINES_PATH}>
<EnginesOverview />
</Route>
<Route exact path={SETTINGS_PATH}>
<Settings />
</Route>
<Route exact path={CREDENTIALS_PATH}>
<Credentials />
</Route>
<Route>
<NotFound product={APP_SEARCH_PLUGIN} />
</Route>
</Switch>
)}
</Layout>
<EuiPage>
<EuiPageBody restrictWidth>
{errorConnecting ? (
<ErrorConnecting />
) : (
<Switch>
<Route exact path={ROOT_PATH}>
<Redirect to={ENGINES_PATH} />
</Route>
<Route exact path={ENGINES_PATH}>
<EnginesOverview />
</Route>
<Route exact path={SETTINGS_PATH}>
<Settings />
</Route>
<Route exact path={CREDENTIALS_PATH}>
<Credentials />
</Route>
<Route>
<NotFound product={APP_SEARCH_PLUGIN} />
</Route>
</Switch>
)}
</EuiPageBody>
</EuiPage>
</Route>
</Switch>
);
Expand Down
2 changes: 2 additions & 0 deletions x-pack/plugins/fleet/common/types/models/epm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export enum ElasticsearchAssetType {
indexTemplate = 'index_template',
ilmPolicy = 'ilm_policy',
transform = 'transform',
dataStreamIlmPolicy = 'data_stream_ilm_policy',
}

export type DataType = typeof dataTypes;
Expand Down Expand Up @@ -207,6 +208,7 @@ export type ElasticsearchAssetTypeToParts = Record<

export interface RegistryDataStream {
type: string;
ilm_policy?: string;
hidden?: boolean;
dataset: string;
title: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const AssetTitleMap: Record<AssetType, string> = {
visualization: 'Visualization',
input: 'Agent input',
map: 'Map',
data_stream_ilm_policy: 'Data Stream ILM Policy',
};

export const ServiceTitleMap: Record<Extract<ServiceName, 'kibana'>, string> = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { SavedObjectsClientContract } from 'kibana/server';
import {
ElasticsearchAssetType,
EsAssetReference,
InstallablePackage,
RegistryDataStream,
} from '../../../../../common/types/models';
import { CallESAsCurrentUser } from '../../../../types';
import { getInstallation } from '../../packages';
import { deleteIlmRefs, deleteIlms } from './remove';
import { saveInstalledEsRefs } from '../../packages/install';
import { getAsset } from '../transform/common';

interface IlmInstallation {
installationName: string;
content: string;
}

interface IlmPathDataset {
path: string;
dataStream: RegistryDataStream;
}

export const installIlmForDataStream = async (
registryPackage: InstallablePackage,
paths: string[],
callCluster: CallESAsCurrentUser,
savedObjectsClient: SavedObjectsClientContract
) => {
const installation = await getInstallation({ savedObjectsClient, pkgName: registryPackage.name });
let previousInstalledIlmEsAssets: EsAssetReference[] = [];
if (installation) {
previousInstalledIlmEsAssets = installation.installed_es.filter(
({ type, id }) => type === ElasticsearchAssetType.dataStreamIlmPolicy
);
}

// delete all previous ilm
await deleteIlms(
callCluster,
previousInstalledIlmEsAssets.map((asset) => asset.id)
);
// install the latest dataset
const dataStreams = registryPackage.data_streams;
if (!dataStreams?.length) return [];
const dataStreamIlmPaths = paths.filter((path) => isDataStreamIlm(path));
let installedIlms: EsAssetReference[] = [];
if (dataStreamIlmPaths.length > 0) {
const ilmPathDatasets = dataStreams.reduce<IlmPathDataset[]>((acc, dataStream) => {
dataStreamIlmPaths.forEach((path) => {
if (isDatasetIlm(path, dataStream.path)) {
acc.push({ path, dataStream });
}
});
return acc;
}, []);

const ilmRefs = ilmPathDatasets.reduce<EsAssetReference[]>((acc, ilmPathDataset) => {
if (ilmPathDataset) {
acc.push({
id: getIlmNameForInstallation(ilmPathDataset),
type: ElasticsearchAssetType.dataStreamIlmPolicy,
});
}
return acc;
}, []);

await saveInstalledEsRefs(savedObjectsClient, registryPackage.name, ilmRefs);

const ilmInstallations: IlmInstallation[] = ilmPathDatasets.map(
(ilmPathDataset: IlmPathDataset) => {
return {
installationName: getIlmNameForInstallation(ilmPathDataset),
content: getAsset(ilmPathDataset.path).toString('utf-8'),
};
}
);

const installationPromises = ilmInstallations.map(async (ilmInstallation) => {
return handleIlmInstall({ callCluster, ilmInstallation });
});

installedIlms = await Promise.all(installationPromises).then((results) => results.flat());
}

if (previousInstalledIlmEsAssets.length > 0) {
const currentInstallation = await getInstallation({
savedObjectsClient,
pkgName: registryPackage.name,
});

// remove the saved object reference
await deleteIlmRefs(
savedObjectsClient,
currentInstallation?.installed_es || [],
registryPackage.name,
previousInstalledIlmEsAssets.map((asset) => asset.id),
installedIlms.map((installed) => installed.id)
);
}
return installedIlms;
};

async function handleIlmInstall({
callCluster,
ilmInstallation,
}: {
callCluster: CallESAsCurrentUser;
ilmInstallation: IlmInstallation;
}): Promise<EsAssetReference> {
await callCluster('transport.request', {
method: 'PUT',
path: `/_ilm/policy/${ilmInstallation.installationName}`,
body: ilmInstallation.content,
});

return { id: ilmInstallation.installationName, type: ElasticsearchAssetType.dataStreamIlmPolicy };
}

const isDataStreamIlm = (path: string) => {
return new RegExp('(?<package>.*)/data_stream/(?<dataset>.*)/elasticsearch/ilm/*.*').test(path);
};

const isDatasetIlm = (path: string, datasetName: string) => {
return new RegExp(`(?<package>.*)/data_stream\\/${datasetName}/elasticsearch/ilm/*.*`).test(path);
};

const getIlmNameForInstallation = (ilmPathDataset: IlmPathDataset) => {
const filename = ilmPathDataset?.path.split('/')?.pop()?.split('.')[0];
return `${ilmPathDataset.dataStream.type}-${ilmPathDataset.dataStream.package}.${ilmPathDataset.dataStream.path}-${filename}`;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { SavedObjectsClientContract } from 'kibana/server';
import { CallESAsCurrentUser, ElasticsearchAssetType, EsAssetReference } from '../../../../types';
import { PACKAGES_SAVED_OBJECT_TYPE } from '../../../../../common/constants';

export const deleteIlms = async (callCluster: CallESAsCurrentUser, ilmPolicyIds: string[]) => {
await Promise.all(
ilmPolicyIds.map(async (ilmPolicyId) => {
await callCluster('transport.request', {
method: 'DELETE',
path: `_ilm/policy/${ilmPolicyId}`,
ignore: [404, 400],
});
})
);
};

export const deleteIlmRefs = async (
savedObjectsClient: SavedObjectsClientContract,
installedEsAssets: EsAssetReference[],
pkgName: string,
installedEsIdToRemove: string[],
currentInstalledEsIlmIds: string[]
) => {
const seen = new Set<string>();
const filteredAssets = installedEsAssets.filter(({ type, id }) => {
if (type !== ElasticsearchAssetType.dataStreamIlmPolicy) return true;
const add =
(currentInstalledEsIlmIds.includes(id) || !installedEsIdToRemove.includes(id)) &&
!seen.has(id);
seen.add(id);
return add;
});
return savedObjectsClient.update(PACKAGES_SAVED_OBJECT_TYPE, pkgName, {
installed_es: filteredAssets,
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ export async function installTemplate({
pipelineName,
packageName,
composedOfTemplates,
ilmPolicy: dataStream.ilm_policy,
hidden: dataStream.hidden,
});

Expand Down
Loading

0 comments on commit e550b1e

Please sign in to comment.