Skip to content

Commit b8bd2b7

Browse files
committed
Index Patterns API - Remove legacy es client usage for field caps (#80116)
* remove legacy es client usage
1 parent 25e102a commit b8bd2b7

26 files changed

Lines changed: 212 additions & 134 deletions

File tree

docs/development/plugins/data/server/kibana-plugin-plugins-data-server.indexpatternsfetcher._constructor_.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ Constructs a new instance of the `IndexPatternsFetcher` class
99
<b>Signature:</b>
1010

1111
```typescript
12-
constructor(callDataCluster: LegacyAPICaller);
12+
constructor(elasticsearchClient: ElasticsearchClient, allowNoIndices?: boolean);
1313
```
1414

1515
## Parameters
1616

1717
| Parameter | Type | Description |
1818
| --- | --- | --- |
19-
| callDataCluster | <code>LegacyAPICaller</code> | |
19+
| elasticsearchClient | <code>ElasticsearchClient</code> | |
20+
| allowNoIndices | <code>boolean</code> | |
2021

docs/development/plugins/data/server/kibana-plugin-plugins-data-server.indexpatternsfetcher.getfieldsforwildcard.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ getFieldsForWildcard(options: {
1313
pattern: string | string[];
1414
metaFields?: string[];
1515
fieldCapsOptions?: {
16-
allowNoIndices: boolean;
16+
allow_no_indices: boolean;
1717
};
1818
}): Promise<FieldDescriptor[]>;
1919
```
@@ -22,7 +22,7 @@ getFieldsForWildcard(options: {
2222

2323
| Parameter | Type | Description |
2424
| --- | --- | --- |
25-
| 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> | |
25+
| 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> | |
2626

2727
<b>Returns:</b>
2828

docs/development/plugins/data/server/kibana-plugin-plugins-data-server.indexpatternsfetcher.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export declare class IndexPatternsFetcher
1414

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

1919
## Methods
2020

src/plugins/data/server/index_patterns/fetcher/index_patterns_fetcher.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* under the License.
1818
*/
1919

20-
import { LegacyAPICaller } from 'kibana/server';
20+
import { ElasticsearchClient } from 'kibana/server';
2121

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

@@ -37,10 +37,12 @@ interface FieldSubType {
3737
}
3838

3939
export class IndexPatternsFetcher {
40-
private _callDataCluster: LegacyAPICaller;
40+
private elasticsearchClient: ElasticsearchClient;
41+
private allowNoIndices: boolean;
4142

42-
constructor(callDataCluster: LegacyAPICaller) {
43-
this._callDataCluster = callDataCluster;
43+
constructor(elasticsearchClient: ElasticsearchClient, allowNoIndices: boolean = false) {
44+
this.elasticsearchClient = elasticsearchClient;
45+
this.allowNoIndices = allowNoIndices;
4446
}
4547

4648
/**
@@ -55,10 +57,12 @@ export class IndexPatternsFetcher {
5557
async getFieldsForWildcard(options: {
5658
pattern: string | string[];
5759
metaFields?: string[];
58-
fieldCapsOptions?: { allowNoIndices: boolean };
60+
fieldCapsOptions?: { allow_no_indices: boolean };
5961
}): Promise<FieldDescriptor[]> {
6062
const { pattern, metaFields, fieldCapsOptions } = options;
61-
return await getFieldCapabilities(this._callDataCluster, pattern, metaFields, fieldCapsOptions);
63+
return await getFieldCapabilities(this.elasticsearchClient, pattern, metaFields, {
64+
allow_no_indices: fieldCapsOptions ? fieldCapsOptions.allow_no_indices : this.allowNoIndices,
65+
});
6266
}
6367

6468
/**
@@ -78,11 +82,11 @@ export class IndexPatternsFetcher {
7882
interval: string;
7983
}) {
8084
const { pattern, lookBack, metaFields } = options;
81-
const { matches } = await resolveTimePattern(this._callDataCluster, pattern);
85+
const { matches } = await resolveTimePattern(this.elasticsearchClient, pattern);
8286
const indices = matches.slice(0, lookBack);
8387
if (indices.length === 0) {
8488
throw createNoMatchingIndicesError(pattern);
8589
}
86-
return await getFieldCapabilities(this._callDataCluster, indices, metaFields);
90+
return await getFieldCapabilities(this.elasticsearchClient, indices, metaFields);
8791
}
8892
}

src/plugins/data/server/index_patterns/fetcher/lib/es_api.test.js

Lines changed: 87 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -32,36 +32,60 @@ describe('server/index_patterns/service/lib/es_api', () => {
3232
afterEach(() => sandbox.restore());
3333

3434
it('calls indices.getAlias() via callCluster', async () => {
35-
const callCluster = sinon.stub();
35+
const getAlias = sinon.stub();
36+
const callCluster = {
37+
indices: {
38+
getAlias,
39+
},
40+
fieldCaps: sinon.stub(),
41+
};
42+
3643
await callIndexAliasApi(callCluster);
37-
sinon.assert.calledOnce(callCluster);
38-
sinon.assert.calledWith(callCluster, 'indices.getAlias');
44+
sinon.assert.calledOnce(getAlias);
3945
});
4046

4147
it('passes indices directly to es api', async () => {
4248
const football = {};
43-
const callCluster = sinon.stub();
49+
const getAlias = sinon.stub();
50+
const callCluster = {
51+
indices: {
52+
getAlias,
53+
},
54+
fieldCaps: sinon.stub(),
55+
};
4456
await callIndexAliasApi(callCluster, football);
45-
sinon.assert.calledOnce(callCluster);
46-
expect(callCluster.args[0][1].index).toBe(football);
57+
sinon.assert.calledOnce(getAlias);
58+
expect(getAlias.args[0][0].index).toBe(football);
4759
});
4860

4961
it('returns the es response directly', async () => {
5062
const football = {};
51-
const callCluster = sinon.stub().returns(football);
63+
const getAlias = sinon.stub().returns(football);
64+
const callCluster = {
65+
indices: {
66+
getAlias,
67+
},
68+
fieldCaps: sinon.stub(),
69+
};
5270
const resp = await callIndexAliasApi(callCluster);
53-
sinon.assert.calledOnce(callCluster);
71+
sinon.assert.calledOnce(getAlias);
5472
expect(resp).toBe(football);
5573
});
5674

5775
it('sets ignoreUnavailable and allowNoIndices params', async () => {
58-
const callCluster = sinon.stub();
76+
const getAlias = sinon.stub();
77+
const callCluster = {
78+
indices: {
79+
getAlias,
80+
},
81+
fieldCaps: sinon.stub(),
82+
};
5983
await callIndexAliasApi(callCluster);
60-
sinon.assert.calledOnce(callCluster);
84+
sinon.assert.calledOnce(getAlias);
6185

62-
const passedOpts = callCluster.args[0][1];
63-
expect(passedOpts).toHaveProperty('ignoreUnavailable', true);
64-
expect(passedOpts).toHaveProperty('allowNoIndices', false);
86+
const passedOpts = getAlias.args[0][0];
87+
expect(passedOpts).toHaveProperty('ignore_unavailable', true);
88+
expect(passedOpts).toHaveProperty('allow_no_indices', false);
6589
});
6690

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

7296
sandbox.stub(convertEsErrorNS, 'convertEsError').throws(convertedError);
73-
const callCluster = sinon.spy(async () => {
97+
const getAlias = sinon.stub(async () => {
7498
throw esError;
7599
});
100+
const callCluster = {
101+
indices: {
102+
getAlias,
103+
},
104+
fieldCaps: sinon.stub(),
105+
};
76106
try {
77107
await callIndexAliasApi(callCluster, indices);
78108
throw new Error('expected callIndexAliasApi() to throw');
@@ -91,37 +121,60 @@ describe('server/index_patterns/service/lib/es_api', () => {
91121
afterEach(() => sandbox.restore());
92122

93123
it('calls fieldCaps() via callCluster', async () => {
94-
const callCluster = sinon.stub();
124+
const fieldCaps = sinon.stub();
125+
const callCluster = {
126+
indices: {
127+
getAlias: sinon.stub(),
128+
},
129+
fieldCaps,
130+
};
95131
await callFieldCapsApi(callCluster);
96-
sinon.assert.calledOnce(callCluster);
97-
sinon.assert.calledWith(callCluster, 'fieldCaps');
132+
sinon.assert.calledOnce(fieldCaps);
98133
});
99134

100135
it('passes indices directly to es api', async () => {
101136
const football = {};
102-
const callCluster = sinon.stub();
137+
const fieldCaps = sinon.stub();
138+
const callCluster = {
139+
indices: {
140+
getAlias: sinon.stub(),
141+
},
142+
fieldCaps,
143+
};
103144
await callFieldCapsApi(callCluster, football);
104-
sinon.assert.calledOnce(callCluster);
105-
expect(callCluster.args[0][1].index).toBe(football);
145+
sinon.assert.calledOnce(fieldCaps);
146+
expect(fieldCaps.args[0][0].index).toBe(football);
106147
});
107148

108149
it('returns the es response directly', async () => {
109150
const football = {};
110-
const callCluster = sinon.stub().returns(football);
151+
const fieldCaps = sinon.stub().returns(football);
152+
const callCluster = {
153+
indices: {
154+
getAlias: sinon.stub(),
155+
},
156+
fieldCaps,
157+
};
111158
const resp = await callFieldCapsApi(callCluster);
112-
sinon.assert.calledOnce(callCluster);
159+
sinon.assert.calledOnce(fieldCaps);
113160
expect(resp).toBe(football);
114161
});
115162

116163
it('sets ignoreUnavailable, allowNoIndices, and fields params', async () => {
117-
const callCluster = sinon.stub();
164+
const fieldCaps = sinon.stub();
165+
const callCluster = {
166+
indices: {
167+
getAlias: sinon.stub(),
168+
},
169+
fieldCaps,
170+
};
118171
await callFieldCapsApi(callCluster);
119-
sinon.assert.calledOnce(callCluster);
172+
sinon.assert.calledOnce(fieldCaps);
120173

121-
const passedOpts = callCluster.args[0][1];
174+
const passedOpts = fieldCaps.args[0][0];
122175
expect(passedOpts).toHaveProperty('fields', '*');
123-
expect(passedOpts).toHaveProperty('ignoreUnavailable', true);
124-
expect(passedOpts).toHaveProperty('allowNoIndices', false);
176+
expect(passedOpts).toHaveProperty('ignore_unavailable', true);
177+
expect(passedOpts).toHaveProperty('allow_no_indices', false);
125178
});
126179

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

132185
sandbox.stub(convertEsErrorNS, 'convertEsError').throws(convertedError);
133-
const callCluster = sinon.spy(async () => {
186+
const fieldCaps = sinon.spy(async () => {
134187
throw esError;
135188
});
189+
const callCluster = {
190+
indices: {
191+
getAlias: sinon.stub(),
192+
},
193+
fieldCaps,
194+
};
136195
try {
137196
await callFieldCapsApi(callCluster, indices);
138197
throw new Error('expected callFieldCapsApi() to throw');

src/plugins/data/server/index_patterns/fetcher/lib/es_api.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* under the License.
1818
*/
1919

20-
import { LegacyAPICaller } from 'kibana/server';
20+
import { ElasticsearchClient } from 'kibana/server';
2121
import { convertEsError } from './errors';
2222
import { FieldCapsResponse } from './field_capabilities';
2323

@@ -46,15 +46,15 @@ export interface IndexAliasResponse {
4646
* @return {Promise<IndexAliasResponse>}
4747
*/
4848
export async function callIndexAliasApi(
49-
callCluster: LegacyAPICaller,
49+
callCluster: ElasticsearchClient,
5050
indices: string[] | string
51-
): Promise<IndicesAliasResponse> {
51+
) {
5252
try {
53-
return (await callCluster('indices.getAlias', {
53+
return await callCluster.indices.getAlias({
5454
index: indices,
55-
ignoreUnavailable: true,
56-
allowNoIndices: false,
57-
})) as Promise<IndicesAliasResponse>;
55+
ignore_unavailable: true,
56+
allow_no_indices: false,
57+
});
5858
} catch (error) {
5959
throw convertEsError(indices, error);
6060
}
@@ -73,17 +73,17 @@ export async function callIndexAliasApi(
7373
* @return {Promise<FieldCapsResponse>}
7474
*/
7575
export async function callFieldCapsApi(
76-
callCluster: LegacyAPICaller,
76+
callCluster: ElasticsearchClient,
7777
indices: string[] | string,
78-
fieldCapsOptions: { allowNoIndices: boolean } = { allowNoIndices: false }
78+
fieldCapsOptions: { allow_no_indices: boolean } = { allow_no_indices: false }
7979
) {
8080
try {
81-
return (await callCluster('fieldCaps', {
81+
return await callCluster.fieldCaps<FieldCapsResponse>({
8282
index: indices,
8383
fields: '*',
84-
ignoreUnavailable: true,
84+
ignore_unavailable: true,
8585
...fieldCapsOptions,
86-
})) as FieldCapsResponse;
86+
});
8787
} catch (error) {
8888
throw convertEsError(indices, error);
8989
}

src/plugins/data/server/index_patterns/fetcher/lib/field_capabilities/field_capabilities.test.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,11 @@ describe('index_patterns/field_capabilities/field_capabilities', () => {
4848
};
4949

5050
const stubDeps = (options = {}) => {
51-
const { esResponse = {}, fieldsFromFieldCaps = [], mergeOverrides = identity } = options;
51+
const { esResponse = [], fieldsFromFieldCaps = [], mergeOverrides = identity } = options;
5252

53-
sandbox.stub(callFieldCapsApiNS, 'callFieldCapsApi').callsFake(async () => esResponse);
53+
sandbox
54+
.stub(callFieldCapsApiNS, 'callFieldCapsApi')
55+
.callsFake(async () => ({ body: esResponse }));
5456
sandbox.stub(readFieldCapsResponseNS, 'readFieldCapsResponse').returns(fieldsFromFieldCaps);
5557
sandbox.stub(mergeOverridesNS, 'mergeOverrides').callsFake(mergeOverrides);
5658
};

src/plugins/data/server/index_patterns/fetcher/lib/field_capabilities/field_capabilities.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919

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

22-
import { LegacyAPICaller } from 'kibana/server';
22+
import { ElasticsearchClient } from 'kibana/server';
2323
import { callFieldCapsApi } from '../es_api';
24-
import { FieldCapsResponse, readFieldCapsResponse } from './field_caps_response';
24+
import { readFieldCapsResponse } from './field_caps_response';
2525
import { mergeOverrides } from './overrides';
2626
import { FieldDescriptor } from '../../index_patterns_fetcher';
2727

@@ -36,17 +36,13 @@ import { FieldDescriptor } from '../../index_patterns_fetcher';
3636
* @return {Promise<Array<FieldDescriptor>>}
3737
*/
3838
export async function getFieldCapabilities(
39-
callCluster: LegacyAPICaller,
39+
callCluster: ElasticsearchClient,
4040
indices: string | string[] = [],
4141
metaFields: string[] = [],
42-
fieldCapsOptions?: { allowNoIndices: boolean }
42+
fieldCapsOptions?: { allow_no_indices: boolean }
4343
) {
44-
const esFieldCaps: FieldCapsResponse = await callFieldCapsApi(
45-
callCluster,
46-
indices,
47-
fieldCapsOptions
48-
);
49-
const fieldsFromFieldCapsByName = keyBy(readFieldCapsResponse(esFieldCaps), 'name');
44+
const esFieldCaps = await callFieldCapsApi(callCluster, indices, fieldCapsOptions);
45+
const fieldsFromFieldCapsByName = keyBy(readFieldCapsResponse(esFieldCaps.body), 'name');
5046

5147
const allFieldsUnsorted = Object.keys(fieldsFromFieldCapsByName)
5248
.filter((name) => !name.startsWith('_'))

0 commit comments

Comments
 (0)