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

[7.10] backport - use two methods of retrieving index list for pre 7.9 compatiblity (#80006) #80112

Merged
merged 1 commit into from
Oct 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -36,8 +36,8 @@ const mockIndexPatternCreationType = new IndexPatternCreationConfig({
});

jest.mock('../../lib/get_indices', () => ({
getIndices: ({}, {}, query: string) => {
if (query.startsWith('e')) {
getIndices: ({ pattern }: { pattern: string }) => {
if (pattern.startsWith('e')) {
return [{ name: 'es', item: {} }];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ export class StepIndexPattern extends Component<StepIndexPatternProps, StepIndex
fetchIndices = async (query: string) => {
const { indexPatternCreationType } = this.props;
const { existingIndexPatterns } = this.state;
const { http } = this.context.services;
const getIndexTags = (indexName: string) => indexPatternCreationType.getIndexTags(indexName);
const searchClient = this.context.services.data.search.search;
const showAllIndices = this.state.isIncludingSystemIndices;

if ((existingIndexPatterns as string[]).includes(query)) {
this.setState({ indexPatternExists: true });
Expand All @@ -157,12 +161,7 @@ export class StepIndexPattern extends Component<StepIndexPatternProps, StepIndex

if (query.endsWith('*')) {
const exactMatchedIndices = await ensureMinimumTime(
getIndices(
this.context.services.http,
(indexName: string) => indexPatternCreationType.getIndexTags(indexName),
query,
this.state.isIncludingSystemIndices
)
getIndices({ http, getIndexTags, pattern: query, showAllIndices, searchClient })
);
// If the search changed, discard this state
if (query !== this.lastQuery) {
Expand All @@ -173,18 +172,8 @@ export class StepIndexPattern extends Component<StepIndexPatternProps, StepIndex
}

const [partialMatchedIndices, exactMatchedIndices] = await ensureMinimumTime([
getIndices(
this.context.services.http,
(indexName: string) => indexPatternCreationType.getIndexTags(indexName),
`${query}*`,
this.state.isIncludingSystemIndices
),
getIndices(
this.context.services.http,
(indexName: string) => indexPatternCreationType.getIndexTags(indexName),
query,
this.state.isIncludingSystemIndices
),
getIndices({ http, getIndexTags, pattern: `${query}*`, showAllIndices, searchClient }),
getIndices({ http, getIndexTags, pattern: query, showAllIndices, searchClient }),
]);

// If the search changed, discard this state
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ export class CreateIndexPatternWizard extends Component<
};

fetchData = async () => {
const { http } = this.context.services;
const getIndexTags = (indexName: string) =>
this.state.indexPatternCreationType.getIndexTags(indexName);
const searchClient = this.context.services.data.search.search;

const indicesFailMsg = (
<FormattedMessage
id="indexPatternManagement.createIndexPattern.loadIndicesFailMsg"
Expand All @@ -125,12 +130,7 @@ export class CreateIndexPatternWizard extends Component<
// query local and remote indices, updating state independently
ensureMinimumTime(
this.catchAndWarn(
getIndices(
this.context.services.http,
(indexName: string) => this.state.indexPatternCreationType.getIndexTags(indexName),
`*`,
false
),
getIndices({ http, getIndexTags, pattern: '*', searchClient }),

[],
indicesFailMsg
Expand All @@ -142,12 +142,7 @@ export class CreateIndexPatternWizard extends Component<
this.catchAndWarn(
// if we get an error from remote cluster query, supply fallback value that allows user entry.
// ['a'] is fallback value
getIndices(
this.context.services.http,
(indexName: string) => this.state.indexPatternCreationType.getIndexTags(indexName),
`*:*`,
false
),
getIndices({ http, getIndexTags, pattern: '*:*', searchClient }),

['a'],
clustersFailMsg
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@
* under the License.
*/

import { getIndices, responseToItemArray } from './get_indices';
import { getIndices, responseToItemArray, dedupeMatchedItems } from './get_indices';
import { httpServiceMock } from '../../../../../../core/public/mocks';
import { ResolveIndexResponseItemIndexAttrs } from '../types';
import { ResolveIndexResponseItemIndexAttrs, MatchedItem } from '../types';
import { Observable } from 'rxjs';

export const successfulResponse = {
export const successfulResolveResponse = {
indices: [
{
name: 'remoteCluster1:bar-01',
Expand All @@ -43,27 +44,64 @@ export const successfulResponse = {
],
};

const mockGetTags = () => [];
const successfulSearchResponse = {
rawResponse: {
aggregations: {
indices: {
buckets: [{ key: 'kibana_sample_data_ecommerce' }, { key: '.kibana_1' }],
},
},
},
};

const getIndexTags = () => [];
const searchClient = () =>
new Observable((observer) => {
observer.next(successfulSearchResponse);
observer.complete();
}) as any;

const http = httpServiceMock.createStartContract();
http.get.mockResolvedValue(successfulResponse);
http.get.mockResolvedValue(successfulResolveResponse);

describe('getIndices', () => {
it('should work in a basic case', async () => {
const result = await getIndices(http, mockGetTags, 'kibana', false);
const uncalledSearchClient = jest.fn();
const result = await getIndices({
http,
getIndexTags,
pattern: 'kibana',
searchClient: uncalledSearchClient,
});
expect(http.get).toHaveBeenCalled();
expect(uncalledSearchClient).not.toHaveBeenCalled();
expect(result.length).toBe(3);
expect(result[0].name).toBe('f-alias');
expect(result[1].name).toBe('foo');
});

it('should make two calls in cross cluser case', async () => {
http.get.mockResolvedValue(successfulResolveResponse);
const result = await getIndices({ http, getIndexTags, pattern: '*:kibana', searchClient });

expect(http.get).toHaveBeenCalled();
expect(result.length).toBe(4);
expect(result[0].name).toBe('f-alias');
expect(result[1].name).toBe('foo');
expect(result[2].name).toBe('kibana_sample_data_ecommerce');
expect(result[3].name).toBe('remoteCluster1:bar-01');
});

it('should ignore ccs query-all', async () => {
expect((await getIndices(http, mockGetTags, '*:', false)).length).toBe(0);
expect((await getIndices({ http, getIndexTags, pattern: '*:', searchClient })).length).toBe(0);
});

it('should ignore a single comma', async () => {
expect((await getIndices(http, mockGetTags, ',', false)).length).toBe(0);
expect((await getIndices(http, mockGetTags, ',*', false)).length).toBe(0);
expect((await getIndices(http, mockGetTags, ',foobar', false)).length).toBe(0);
expect((await getIndices({ http, getIndexTags, pattern: ',', searchClient })).length).toBe(0);
expect((await getIndices({ http, getIndexTags, pattern: ',*', searchClient })).length).toBe(0);
expect(
(await getIndices({ http, getIndexTags, pattern: ',foobar', searchClient })).length
).toBe(0);
});

it('response object to item array', () => {
Expand Down Expand Up @@ -91,16 +129,22 @@ describe('getIndices', () => {
},
],
};
expect(responseToItemArray(result, mockGetTags)).toMatchSnapshot();
expect(responseToItemArray({}, mockGetTags)).toEqual([]);
expect(responseToItemArray(result, getIndexTags)).toMatchSnapshot();
expect(responseToItemArray({}, getIndexTags)).toEqual([]);
});

it('matched items are deduped', () => {
const setA = [{ name: 'a' }, { name: 'b' }] as MatchedItem[];
const setB = [{ name: 'b' }, { name: 'c' }] as MatchedItem[];
expect(dedupeMatchedItems(setA, setB)).toHaveLength(3);
});

describe('errors', () => {
it('should handle errors gracefully', async () => {
http.get.mockImplementationOnce(() => {
throw new Error('Test error');
});
const result = await getIndices(http, mockGetTags, 'kibana', false);
const result = await getIndices({ http, getIndexTags, pattern: 'kibana', searchClient });
expect(result.length).toBe(0);
});
});
Expand Down
Loading