Skip to content

Commit

Permalink
Merge branch 'field_caps_remove_legacy_es_client_usage' of github.com…
Browse files Browse the repository at this point in the history
…:mattkime/kibana into field_caps_remove_legacy_es_client_usage
  • Loading branch information
mattkime committed Oct 13, 2020
2 parents 795e0a6 + f828af1 commit 66b5002
Show file tree
Hide file tree
Showing 13 changed files with 140 additions and 67 deletions.
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 @@ -40,8 +40,8 @@ export class IndexPatternsFetcher {
private elasticsearchClient: ElasticsearchClient;
private allowNoIndices: boolean;

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

Expand All @@ -57,11 +57,11 @@ 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.elasticsearchClient, pattern, metaFields, {
allowNoIndices: fieldCapsOptions ? fieldCapsOptions.allowNoIndices : this.allowNoIndices,
allow_no_indices: fieldCapsOptions ? fieldCapsOptions.allow_no_indices : this.allowNoIndices,
});
}

Expand Down
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
5 changes: 3 additions & 2 deletions src/plugins/data/server/index_patterns/fetcher/lib/es_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

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

export interface IndicesAliasResponse {
[index: string]: IndexAliasResponse;
Expand Down Expand Up @@ -74,10 +75,10 @@ export async function callIndexAliasApi(
export async function callFieldCapsApi(
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: '*',
ignore_unavailable: true,
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 @@ -39,10 +39,10 @@ export async function getFieldCapabilities(
callCluster: ElasticsearchClient,
indices: string | string[] = [],
metaFields: string[] = [],
fieldCapsOptions?: { allowNoIndices: boolean }
fieldCapsOptions?: { allow_no_indices: boolean }
) {
const esFieldCaps = await callFieldCapsApi(callCluster, indices, fieldCapsOptions);
const fieldsFromFieldCapsByName = keyBy(readFieldCapsResponse(esFieldCaps.body.fields), 'name');
const fieldsFromFieldCapsByName = keyBy(readFieldCapsResponse(esFieldCaps.body), 'name');

const allFieldsUnsorted = Object.keys(fieldsFromFieldCapsByName)
.filter((name) => !name.startsWith('_'))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,19 @@ const TIME_PATTERN = '[logs-]dddd-YYYY.w';

describe('server/index_patterns/service/lib/resolve_time_pattern', () => {
let sandbox;
const esClientMock = {
indices: {
getAlias: () => ({}),
},
};
beforeEach(() => (sandbox = sinon.createSandbox()));
afterEach(() => sandbox.restore());

describe('resolveTimePattern()', () => {
describe('pre request', () => {
it('uses callIndexAliasApi() fn', async () => {
sandbox.stub(callIndexAliasApiNS, 'callIndexAliasApi').returns({});
await resolveTimePattern(noop, TIME_PATTERN);
await resolveTimePattern(esClientMock, TIME_PATTERN);
sinon.assert.calledOnce(callIndexAliasApi);
});

Expand All @@ -49,7 +54,7 @@ describe('server/index_patterns/service/lib/resolve_time_pattern', () => {

sandbox.stub(timePatternToWildcardNS, 'timePatternToWildcard').returns(wildcard);

await resolveTimePattern(noop, timePattern);
await resolveTimePattern(esClientMock, timePattern);
sinon.assert.calledOnce(timePatternToWildcard);
expect(timePatternToWildcard.firstCall.args).toEqual([timePattern]);
});
Expand All @@ -61,7 +66,7 @@ describe('server/index_patterns/service/lib/resolve_time_pattern', () => {
sandbox.stub(callIndexAliasApiNS, 'callIndexAliasApi').returns({});
sandbox.stub(timePatternToWildcardNS, 'timePatternToWildcard').returns(wildcard);

await resolveTimePattern(noop, timePattern);
await resolveTimePattern(esClientMock, timePattern);
sinon.assert.calledOnce(callIndexAliasApi);
expect(callIndexAliasApi.firstCall.args[1]).toBe(wildcard);
});
Expand All @@ -70,13 +75,15 @@ describe('server/index_patterns/service/lib/resolve_time_pattern', () => {
describe('read response', () => {
it('returns all aliases names in result.all, ordered by time desc', async () => {
sandbox.stub(callIndexAliasApiNS, 'callIndexAliasApi').returns({
'logs-2016.2': {},
'logs-Saturday-2017.1': {},
'logs-2016.1': {},
'logs-Sunday-2017.1': {},
'logs-2015': {},
'logs-2016.3': {},
'logs-Friday-2017.1': {},
body: {
'logs-2016.2': {},
'logs-Saturday-2017.1': {},
'logs-2016.1': {},
'logs-Sunday-2017.1': {},
'logs-2015': {},
'logs-2016.3': {},
'logs-Friday-2017.1': {},
},
});

const resp = await resolveTimePattern(noop, TIME_PATTERN);
Expand All @@ -94,13 +101,15 @@ describe('server/index_patterns/service/lib/resolve_time_pattern', () => {

it('returns all indices matching the time pattern in matches, ordered by time desc', async () => {
sandbox.stub(callIndexAliasApiNS, 'callIndexAliasApi').returns({
'logs-2016.2': {},
'logs-Saturday-2017.1': {},
'logs-2016.1': {},
'logs-Sunday-2017.1': {},
'logs-2015': {},
'logs-2016.3': {},
'logs-Friday-2017.1': {},
body: {
'logs-2016.2': {},
'logs-Saturday-2017.1': {},
'logs-2016.1': {},
'logs-Sunday-2017.1': {},
'logs-2015': {},
'logs-2016.3': {},
'logs-Friday-2017.1': {},
},
});

const resp = await resolveTimePattern(noop, TIME_PATTERN);
Expand Down
Loading

0 comments on commit 66b5002

Please sign in to comment.