Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Index Patterns API - Remove legacy es client usage for field caps #80116

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
0ef83ad
index pattern server api simpler dependency
mattkime Oct 12, 2020
a663b12
fix plugin functional
mattkime Oct 12, 2020
04cb1de
remove legacy es client usage
mattkime Oct 12, 2020
96c9e02
Update index_patterns_service.ts
mattkime Oct 12, 2020
6e341fa
Merge branch 'index_pattern_server_simpler_deps' into field_caps_remo…
mattkime Oct 12, 2020
85bd623
Merge branch 'master' into field_caps_remove_legacy_es_client_usage
mattkime Oct 12, 2020
6b98e25
fix index pattern field list loading
mattkime Oct 12, 2020
3c8e547
fix tests
mattkime Oct 12, 2020
8e05312
fix tests and update docs
mattkime Oct 13, 2020
f828af1
fix hybrid index pattern support
mattkime Oct 13, 2020
e7d316b
Merge branch 'master' into field_caps_remove_legacy_es_client_usage
mattkime Oct 13, 2020
795e0a6
uptime type fixes
mattkime Oct 13, 2020
66b5002
Merge branch 'field_caps_remove_legacy_es_client_usage' of github.com…
mattkime Oct 13, 2020
ce643a6
Add scoped cluster client to alerts and actions services.
justinkambic Oct 16, 2020
87d72f7
Merge branch 'master' into alerting_scoped-cluster-client
justinkambic Oct 19, 2020
9e284d0
Modify functional test to use new ES client.
justinkambic Oct 19, 2020
be76dd7
Merge branch 'master' into field_caps_remove_legacy_es_client_usage
mattkime Oct 20, 2020
f3c2115
Merge branch 'alerting_scoped-cluster-client' into field_caps_remove_…
mattkime Oct 20, 2020
c7c9041
correct dependency from alert service
mattkime Oct 20, 2020
502181b
Merge branch 'master' into field_caps_remove_legacy_es_client_usage
mattkime Oct 20, 2020
375b577
remove dupe alert service
mattkime Oct 21, 2020
593c8c8
Merge branch 'master' into field_caps_remove_legacy_es_client_usage
kibanamachine Oct 23, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ Constructs a new instance of the `IndexPatternsFetcher` class
<b>Signature:</b>

```typescript
constructor(callDataCluster: LegacyAPICaller);
constructor(elasticsearchClient: ElasticsearchClient, allowNoIndices?: boolean);
```

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| callDataCluster | <code>LegacyAPICaller</code> | |
| elasticsearchClient | <code>ElasticsearchClient</code> | |
| allowNoIndices | <code>boolean</code> | |

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ getFieldsForWildcard(options: {
pattern: string | string[];
metaFields?: string[];
fieldCapsOptions?: {
allowNoIndices: boolean;
allow_no_indices: boolean;
};
}): Promise<FieldDescriptor[]>;
```
Expand All @@ -22,7 +22,7 @@ getFieldsForWildcard(options: {

| Parameter | Type | Description |
| --- | --- | --- |
| options | <code>{</code><br/><code> pattern: string &#124; string[];</code><br/><code> metaFields?: string[];</code><br/><code> fieldCapsOptions?: {</code><br/><code> allowNoIndices: boolean;</code><br/><code> };</code><br/><code> }</code> | |
| options | <code>{</code><br/><code> pattern: string &#124; string[];</code><br/><code> metaFields?: string[];</code><br/><code> fieldCapsOptions?: {</code><br/><code> allow_no_indices: boolean;</code><br/><code> };</code><br/><code> }</code> | |

<b>Returns:</b>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export declare class IndexPatternsFetcher

| Constructor | Modifiers | Description |
| --- | --- | --- |
| [(constructor)(callDataCluster)](./kibana-plugin-plugins-data-server.indexpatternsfetcher._constructor_.md) | | Constructs a new instance of the <code>IndexPatternsFetcher</code> class |
| [(constructor)(elasticsearchClient, allowNoIndices)](./kibana-plugin-plugins-data-server.indexpatternsfetcher._constructor_.md) | | Constructs a new instance of the <code>IndexPatternsFetcher</code> class |

## Methods

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

import { LegacyAPICaller } from 'kibana/server';
import { ElasticsearchClient } from 'kibana/server';

import { getFieldCapabilities, resolveTimePattern, createNoMatchingIndicesError } from './lib';

Expand All @@ -37,10 +37,12 @@ interface FieldSubType {
}

export class IndexPatternsFetcher {
private _callDataCluster: LegacyAPICaller;
private elasticsearchClient: ElasticsearchClient;
private allowNoIndices: boolean;

constructor(callDataCluster: LegacyAPICaller) {
this._callDataCluster = callDataCluster;
constructor(elasticsearchClient: ElasticsearchClient, allowNoIndices: boolean = false) {
this.elasticsearchClient = elasticsearchClient;
this.allowNoIndices = allowNoIndices;
}

/**
Expand All @@ -55,10 +57,12 @@ export class IndexPatternsFetcher {
async getFieldsForWildcard(options: {
pattern: string | string[];
metaFields?: string[];
fieldCapsOptions?: { allowNoIndices: boolean };
fieldCapsOptions?: { allow_no_indices: boolean };
}): Promise<FieldDescriptor[]> {
const { pattern, metaFields, fieldCapsOptions } = options;
return await getFieldCapabilities(this._callDataCluster, pattern, metaFields, fieldCapsOptions);
return await getFieldCapabilities(this.elasticsearchClient, pattern, metaFields, {
allow_no_indices: fieldCapsOptions ? fieldCapsOptions.allow_no_indices : this.allowNoIndices,
});
}

/**
Expand All @@ -78,11 +82,11 @@ export class IndexPatternsFetcher {
interval: string;
}) {
const { pattern, lookBack, metaFields } = options;
const { matches } = await resolveTimePattern(this._callDataCluster, pattern);
const { matches } = await resolveTimePattern(this.elasticsearchClient, pattern);
const indices = matches.slice(0, lookBack);
if (indices.length === 0) {
throw createNoMatchingIndicesError(pattern);
}
return await getFieldCapabilities(this._callDataCluster, indices, metaFields);
return await getFieldCapabilities(this.elasticsearchClient, indices, metaFields);
}
}
115 changes: 87 additions & 28 deletions src/plugins/data/server/index_patterns/fetcher/lib/es_api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,36 +32,60 @@ describe('server/index_patterns/service/lib/es_api', () => {
afterEach(() => sandbox.restore());

it('calls indices.getAlias() via callCluster', async () => {
const callCluster = sinon.stub();
const getAlias = sinon.stub();
const callCluster = {
indices: {
getAlias,
},
fieldCaps: sinon.stub(),
};

await callIndexAliasApi(callCluster);
sinon.assert.calledOnce(callCluster);
sinon.assert.calledWith(callCluster, 'indices.getAlias');
sinon.assert.calledOnce(getAlias);
});

it('passes indices directly to es api', async () => {
const football = {};
const callCluster = sinon.stub();
const getAlias = sinon.stub();
const callCluster = {
indices: {
getAlias,
},
fieldCaps: sinon.stub(),
};
await callIndexAliasApi(callCluster, football);
sinon.assert.calledOnce(callCluster);
expect(callCluster.args[0][1].index).toBe(football);
sinon.assert.calledOnce(getAlias);
expect(getAlias.args[0][0].index).toBe(football);
});

it('returns the es response directly', async () => {
const football = {};
const callCluster = sinon.stub().returns(football);
const getAlias = sinon.stub().returns(football);
const callCluster = {
indices: {
getAlias,
},
fieldCaps: sinon.stub(),
};
const resp = await callIndexAliasApi(callCluster);
sinon.assert.calledOnce(callCluster);
sinon.assert.calledOnce(getAlias);
expect(resp).toBe(football);
});

it('sets ignoreUnavailable and allowNoIndices params', async () => {
const callCluster = sinon.stub();
const getAlias = sinon.stub();
const callCluster = {
indices: {
getAlias,
},
fieldCaps: sinon.stub(),
};
await callIndexAliasApi(callCluster);
sinon.assert.calledOnce(callCluster);
sinon.assert.calledOnce(getAlias);

const passedOpts = callCluster.args[0][1];
expect(passedOpts).toHaveProperty('ignoreUnavailable', true);
expect(passedOpts).toHaveProperty('allowNoIndices', false);
const passedOpts = getAlias.args[0][0];
expect(passedOpts).toHaveProperty('ignore_unavailable', true);
expect(passedOpts).toHaveProperty('allow_no_indices', false);
});

it('handles errors with convertEsError()', async () => {
Expand All @@ -70,9 +94,15 @@ describe('server/index_patterns/service/lib/es_api', () => {
const convertedError = new Error('convertedError');

sandbox.stub(convertEsErrorNS, 'convertEsError').throws(convertedError);
const callCluster = sinon.spy(async () => {
const getAlias = sinon.stub(async () => {
throw esError;
});
const callCluster = {
indices: {
getAlias,
},
fieldCaps: sinon.stub(),
};
try {
await callIndexAliasApi(callCluster, indices);
throw new Error('expected callIndexAliasApi() to throw');
Expand All @@ -91,37 +121,60 @@ describe('server/index_patterns/service/lib/es_api', () => {
afterEach(() => sandbox.restore());

it('calls fieldCaps() via callCluster', async () => {
const callCluster = sinon.stub();
const fieldCaps = sinon.stub();
const callCluster = {
indices: {
getAlias: sinon.stub(),
},
fieldCaps,
};
await callFieldCapsApi(callCluster);
sinon.assert.calledOnce(callCluster);
sinon.assert.calledWith(callCluster, 'fieldCaps');
sinon.assert.calledOnce(fieldCaps);
});

it('passes indices directly to es api', async () => {
const football = {};
const callCluster = sinon.stub();
const fieldCaps = sinon.stub();
const callCluster = {
indices: {
getAlias: sinon.stub(),
},
fieldCaps,
};
await callFieldCapsApi(callCluster, football);
sinon.assert.calledOnce(callCluster);
expect(callCluster.args[0][1].index).toBe(football);
sinon.assert.calledOnce(fieldCaps);
expect(fieldCaps.args[0][0].index).toBe(football);
});

it('returns the es response directly', async () => {
const football = {};
const callCluster = sinon.stub().returns(football);
const fieldCaps = sinon.stub().returns(football);
const callCluster = {
indices: {
getAlias: sinon.stub(),
},
fieldCaps,
};
const resp = await callFieldCapsApi(callCluster);
sinon.assert.calledOnce(callCluster);
sinon.assert.calledOnce(fieldCaps);
expect(resp).toBe(football);
});

it('sets ignoreUnavailable, allowNoIndices, and fields params', async () => {
const callCluster = sinon.stub();
const fieldCaps = sinon.stub();
const callCluster = {
indices: {
getAlias: sinon.stub(),
},
fieldCaps,
};
await callFieldCapsApi(callCluster);
sinon.assert.calledOnce(callCluster);
sinon.assert.calledOnce(fieldCaps);

const passedOpts = callCluster.args[0][1];
const passedOpts = fieldCaps.args[0][0];
expect(passedOpts).toHaveProperty('fields', '*');
expect(passedOpts).toHaveProperty('ignoreUnavailable', true);
expect(passedOpts).toHaveProperty('allowNoIndices', false);
expect(passedOpts).toHaveProperty('ignore_unavailable', true);
expect(passedOpts).toHaveProperty('allow_no_indices', false);
});

it('handles errors with convertEsError()', async () => {
Expand All @@ -130,9 +183,15 @@ describe('server/index_patterns/service/lib/es_api', () => {
const convertedError = new Error('convertedError');

sandbox.stub(convertEsErrorNS, 'convertEsError').throws(convertedError);
const callCluster = sinon.spy(async () => {
const fieldCaps = sinon.spy(async () => {
throw esError;
});
const callCluster = {
indices: {
getAlias: sinon.stub(),
},
fieldCaps,
};
try {
await callFieldCapsApi(callCluster, indices);
throw new Error('expected callFieldCapsApi() to throw');
Expand Down
24 changes: 12 additions & 12 deletions src/plugins/data/server/index_patterns/fetcher/lib/es_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

import { LegacyAPICaller } from 'kibana/server';
import { ElasticsearchClient } from 'kibana/server';
import { convertEsError } from './errors';
import { FieldCapsResponse } from './field_capabilities';

Expand Down Expand Up @@ -46,15 +46,15 @@ export interface IndexAliasResponse {
* @return {Promise<IndexAliasResponse>}
*/
export async function callIndexAliasApi(
callCluster: LegacyAPICaller,
callCluster: ElasticsearchClient,
indices: string[] | string
): Promise<IndicesAliasResponse> {
) {
try {
return (await callCluster('indices.getAlias', {
return await callCluster.indices.getAlias({
index: indices,
ignoreUnavailable: true,
allowNoIndices: false,
})) as Promise<IndicesAliasResponse>;
ignore_unavailable: true,
allow_no_indices: false,
});
} catch (error) {
throw convertEsError(indices, error);
}
Expand All @@ -73,17 +73,17 @@ export async function callIndexAliasApi(
* @return {Promise<FieldCapsResponse>}
*/
export async function callFieldCapsApi(
callCluster: LegacyAPICaller,
callCluster: ElasticsearchClient,
indices: string[] | string,
fieldCapsOptions: { allowNoIndices: boolean } = { allowNoIndices: false }
fieldCapsOptions: { allow_no_indices: boolean } = { allow_no_indices: false }
) {
try {
return (await callCluster('fieldCaps', {
return await callCluster.fieldCaps<FieldCapsResponse>({
index: indices,
fields: '*',
ignoreUnavailable: true,
ignore_unavailable: true,
...fieldCapsOptions,
})) as FieldCapsResponse;
});
} catch (error) {
throw convertEsError(indices, error);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@ describe('index_patterns/field_capabilities/field_capabilities', () => {
};

const stubDeps = (options = {}) => {
const { esResponse = {}, fieldsFromFieldCaps = [], mergeOverrides = identity } = options;
const { esResponse = [], fieldsFromFieldCaps = [], mergeOverrides = identity } = options;

sandbox.stub(callFieldCapsApiNS, 'callFieldCapsApi').callsFake(async () => esResponse);
sandbox
.stub(callFieldCapsApiNS, 'callFieldCapsApi')
.callsFake(async () => ({ body: esResponse }));
sandbox.stub(readFieldCapsResponseNS, 'readFieldCapsResponse').returns(fieldsFromFieldCaps);
sandbox.stub(mergeOverridesNS, 'mergeOverrides').callsFake(mergeOverrides);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@

import { defaults, keyBy, sortBy } from 'lodash';

import { LegacyAPICaller } from 'kibana/server';
import { ElasticsearchClient } from 'kibana/server';
import { callFieldCapsApi } from '../es_api';
import { FieldCapsResponse, readFieldCapsResponse } from './field_caps_response';
import { readFieldCapsResponse } from './field_caps_response';
import { mergeOverrides } from './overrides';
import { FieldDescriptor } from '../../index_patterns_fetcher';

Expand All @@ -36,17 +36,13 @@ import { FieldDescriptor } from '../../index_patterns_fetcher';
* @return {Promise<Array<FieldDescriptor>>}
*/
export async function getFieldCapabilities(
callCluster: LegacyAPICaller,
callCluster: ElasticsearchClient,
indices: string | string[] = [],
metaFields: string[] = [],
fieldCapsOptions?: { allowNoIndices: boolean }
fieldCapsOptions?: { allow_no_indices: boolean }
) {
const esFieldCaps: FieldCapsResponse = await callFieldCapsApi(
callCluster,
indices,
fieldCapsOptions
);
const fieldsFromFieldCapsByName = keyBy(readFieldCapsResponse(esFieldCaps), 'name');
const esFieldCaps = await callFieldCapsApi(callCluster, indices, fieldCapsOptions);
const fieldsFromFieldCapsByName = keyBy(readFieldCapsResponse(esFieldCaps.body), 'name');

const allFieldsUnsorted = Object.keys(fieldsFromFieldCapsByName)
.filter((name) => !name.startsWith('_'))
Expand Down
Loading