Skip to content

Commit

Permalink
Assorted server cleanup.
Browse files Browse the repository at this point in the history
* Fix simulate endpoint and move elasticsearch/issues/59152 workaround to server.
* Remove commented-out code and unnecessary types from fetch_indices.
* Convert to IScopedClusterClient in a few places.
* Delete wrapEsError helpers.
* Remove unnecessary calls to encodeURIComponent on the server.
  • Loading branch information
cjcenizal committed Jul 20, 2021
1 parent 8386a36 commit 0d53fc5
Show file tree
Hide file tree
Showing 14 changed files with 63 additions and 145 deletions.
13 changes: 13 additions & 0 deletions x-pack/plugins/index_management/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,17 @@ POST ds/_doc
{
"@timestamp": "2020-01-27"
}
```

## Index templates tab

### Quick steps for texting

By default, **legacy index templates** are not shown in the UI. Make them appear by creating one in Console:

```
PUT _template/template_1
{
"index_patterns": ["foo*"]
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@ export const SimulateTemplate = React.memo(({ template, filters }: Props) => {
}

const indexTemplate = serializeTemplate(stripEmptyFields(template) as TemplateDeserialized);

// Until ES fixes a bug on their side we will send a random index pattern to the simulate API.
// Issue: https://github.com/elastic/elasticsearch/issues/59152
indexTemplate.index_patterns = [uuid.v4()];

const { data, error } = await simulateIndexTemplate(indexTemplate);
let filteredTemplate = data;

Expand Down
54 changes: 3 additions & 51 deletions x-pack/plugins/index_management/server/lib/fetch_indices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,75 +5,27 @@
* 2.0.
*/

import { CatIndicesParams } from 'elasticsearch';
import { IScopedClusterClient } from 'kibana/server';
import { IndexDataEnricher } from '../services';
import { Index } from '../index';

interface Hit {
health: string;
status: string;
index: string;
uuid: string;
pri: string;
rep: string;
'docs.count': any;
'store.size': any;
sth: 'true' | 'false';
hidden: boolean;
}

interface IndexInfo {
aliases: { [aliasName: string]: unknown };
mappings: unknown;
data_stream?: string;
settings: {
index: {
hidden: 'true' | 'false';
};
};
}

interface GetIndicesResponse {
body: {
[indexName: string]: IndexInfo;
};
}

async function fetchIndicesCall(
client: IScopedClusterClient,
indexNames?: string[]
): Promise<Index[]> {
const indexNamesString = indexNames && indexNames.length ? indexNames.join(',') : '*';

// This call retrieves alias and settings (incl. hidden status) information about indices
// const indices: GetIndicesResponse = await callAsCurrentUser('transport.request', {
// method: 'GET',
// // transport.request doesn't do any URI encoding, unlike other JS client APIs. This enables
// // working with Logstash indices with names like %{[@metadata][beat]}-%{[@metadata][version]}.
// path: `/${encodeURIComponent(indexNamesString)}`,
// query: {
// expand_wildcards: 'hidden,all',
// },
// });

const { body: indices }: GetIndicesResponse = await client.asCurrentUser.indices.get({
index: encodeURIComponent(indexNamesString),
const { body: indices } = await client.asCurrentUser.indices.get({
index: indexNamesString,
expand_wildcards: 'hidden,all',
});

if (!Object.keys(indices).length) {
return [];
}

// This call retrieves health and other high-level information about indices.
// const catHits: Hit[] = await callAsCurrentUser('transport.request', {
// method: 'GET',
// path: '/_cat/indices',
// query: catQuery,
// });

const { body: catHits }: Hit[] = await client.asCurrentUser.cat.indices({
const { body: catHits } = await client.asCurrentUser.cat.indices({
format: 'json',
h: 'health,status,index,uuid,pri,rep,docs.count,sth,store.size',
expand_wildcards: 'hidden,all',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
* 2.0.
*/

import type { ElasticsearchClient } from 'kibana/server';
import { IScopedClusterClient } from 'kibana/server';

// Cloud has its own system for managing templates and we want to make
// this clear in the UI when a template is used in a Cloud deployment.
export const getCloudManagedTemplatePrefix = async (
client: ElasticsearchClient
client: IScopedClusterClient
): Promise<string | undefined> => {
try {
const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import { RouteDependencies } from '../../../types';
import { addBasePath } from '../index';
import { componentTemplateSchema } from './schema_validation';

export const registerCreateRoute = ({ router, lib: { handleEsError } }: RouteDependencies): void => {
export const registerCreateRoute = ({
router,
lib: { handleEsError },
}: RouteDependencies): void => {
router.post(
{
path: addBasePath('/component_templates'),
Expand All @@ -29,7 +32,9 @@ export const registerCreateRoute = ({ router, lib: { handleEsError } }: RouteDep

try {
// Check that a component template with the same name doesn't already exist
const { body : { component_templates: componentTemplates } } = await client.asCurrentUser.cluster.getComponentTemplate({
const {
body: { component_templates: componentTemplates },
} = await client.asCurrentUser.cluster.getComponentTemplate({
name,
});

Expand Down Expand Up @@ -57,7 +62,7 @@ export const registerCreateRoute = ({ router, lib: { handleEsError } }: RouteDep

return response.ok({ body: responseBody });
} catch (error) {
return handleEsError({ error, response });;
return handleEsError({ error, response });
}
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ const paramsSchema = schema.object({
names: schema.string(),
});

export const registerDeleteRoute = ({ router, lib: { handleEsError } }: RouteDependencies): void => {
export const registerDeleteRoute = ({
router,
lib: { handleEsError },
}: RouteDependencies): void => {
router.delete(
{
path: addBasePath('/component_templates/{names}'),
Expand All @@ -33,17 +36,19 @@ export const registerDeleteRoute = ({ router, lib: { handleEsError } }: RouteDep
};

await Promise.all(
componentNames.map((componentName) => {
return client.asCurrentUser.cluster.deleteComponentTemplate({
name: componentName,
})
.then(() => responseBody.itemsDeleted.push(componentName))
.catch((error) =>
responseBody.errors.push({
name: componentName,
error: handleEsError({ error, response }),
})
);
componentNames.map(async (componentName) => {
try {
await client.asCurrentUser.cluster.deleteComponentTemplate({
name: componentName,
});

return responseBody.itemsDeleted.push(componentName);
} catch (error) {
return responseBody.errors.push({
name: componentName,
error: handleEsError({ error, response }),
});
}
})
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ export const registerPrivilegesRoute = ({
}

privilegesResult.hasAllPrivileges = hasAllPrivileges;

return response.ok({ body: privilegesResult });
} catch (error) {
return handleEsError({ error, response });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@ import { schema, TypeOf } from '@kbn/config-schema';

import { RouteDependencies } from '../../../types';
import { addBasePath } from '../index';
import { wrapEsError } from '../../helpers';

const bodySchema = schema.object({
dataStreams: schema.arrayOf(schema.string()),
});

export function registerDeleteRoute({ router }: RouteDependencies) {
export function registerDeleteRoute({ router, lib: { handleEsError } }: RouteDependencies) {
router.post(
{
path: addBasePath('/delete_data_streams'),
Expand All @@ -38,10 +37,10 @@ export function registerDeleteRoute({ router }: RouteDependencies) {
});

return responseBody.dataStreamsDeleted.push(name);
} catch (e) {
} catch (error) {
return responseBody.errors.push({
name,
error: wrapEsError(e),
error: handleEsError({ error, response }),
});
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function registerFreezeRoute({ router, lib: { handleEsError } }: RouteDep

try {
await client.asCurrentUser.indices.freeze({
index: encodeURIComponent(indices.join(',')),
index: indices.join(','),
});
return response.ok();
} catch (error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function registerUnfreezeRoute({ router, lib: { handleEsError } }: RouteD

try {
await client.asCurrentUser.indices.unfreeze({
index: encodeURIComponent(indices.join(',')),
index: indices.join(','),
});
return response.ok();
} catch (error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { schema, TypeOf } from '@kbn/config-schema';

import { RouteDependencies } from '../../../types';
import { addBasePath } from '../index';
import { wrapEsError } from '../../helpers';

import { TemplateDeserialized } from '../../../../common';

Expand Down Expand Up @@ -54,7 +53,10 @@ export function registerDeleteRoute({ router, lib: { handleEsError } }: RouteDep

return responseBody.templatesDeleted.push(name);
} catch (error) {
return response.errors.push(handleEsError({ error, response: res }));
return responseBody.errors.push({
name,
error: handleEsError({ error, response }),
});
}
})
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,14 @@ export function registerSimulateRoute({ router, lib: { handleEsError } }: RouteD
const template = request.body as TypeOf<typeof bodySchema>;

try {
const { body: templatePreview } = await client.asCurrentUser.indices.simulateIndexTemplate({
body: template,
const { body: templatePreview } = await client.asCurrentUser.indices.simulateTemplate({
body: {
...template,
// Until ES fixes a bug on their side we need to send a fake index pattern
// that won't match any indices.
// Issue: https://github.com/elastic/elasticsearch/issues/59152
index_patterns: ['a_fake_index_pattern_that_wont_match_any_indices'],
},
});

return response.ok({ body: templatePreview });
Expand Down
58 changes: 0 additions & 58 deletions x-pack/plugins/index_management/server/routes/helpers.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
* 2.0.
*/

import type { ElasticsearchClient } from 'kibana/server';
import { IScopedClusterClient } from 'kibana/server';
import { Index } from '../index';

export type Enricher = (indices: Index[], client: ElasticsearchClient) => Promise<Index[]>;
export type Enricher = (indices: Index[], client: IScopedClusterClient) => Promise<Index[]>;

export class IndexDataEnricher {
private readonly _enrichers: Enricher[] = [];
Expand All @@ -19,7 +19,7 @@ export class IndexDataEnricher {

public enrichIndices = async (
indices: Index[],
client: ElasticsearchClient
client: IScopedClusterClient
): Promise<Index[]> => {
let enrichedIndices = indices;

Expand Down

0 comments on commit 0d53fc5

Please sign in to comment.