Skip to content

Commit

Permalink
Index patterns server - throw correct error on field caps 404 (#95879) (
Browse files Browse the repository at this point in the history
#97033)

* throw correct error on field caps 404 and update tests

Co-authored-by: Matthew Kime <[email protected]>
  • Loading branch information
kibanamachine and mattkime authored Apr 13, 2021
1 parent 18d1134 commit 784270d
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
IIndexPatternsApiClient,
GetFieldsOptionsTimePattern,
} from '../../common/index_patterns/types';
import { IndexPatternMissingIndices } from '../../common/index_patterns/lib';
import { IndexPatternsFetcher } from './fetcher';

export class IndexPatternsApiServer implements IIndexPatternsApiClient {
Expand All @@ -27,12 +28,23 @@ export class IndexPatternsApiServer implements IIndexPatternsApiClient {
allowNoIndex,
}: GetFieldsOptions) {
const indexPatterns = new IndexPatternsFetcher(this.esClient, allowNoIndex);
return await indexPatterns.getFieldsForWildcard({
pattern,
metaFields,
type,
rollupIndex,
});
return await indexPatterns
.getFieldsForWildcard({
pattern,
metaFields,
type,
rollupIndex,
})
.catch((err) => {
if (
err.output.payload.statusCode === 404 &&
err.output.payload.code === 'no_matching_indices'
) {
throw new IndexPatternMissingIndices(pattern);
} else {
throw err;
}
});
}
async getFieldsForTimePattern(options: GetFieldsOptionsTimePattern) {
const indexPatterns = new IndexPatternsFetcher(this.esClient);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export const indexPatternsServiceFactory = ({
logger.error(error);
},
onNotification: ({ title, text }) => {
logger.warn(`${title} : ${text}`);
logger.warn(`${title}${text ? ` : ${text}` : ''}`);
},
onUnsupportedTimePattern: ({ index, title }) => {
logger.warn(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,8 @@ export default function ({ getService }: FtrProviderContext) {
});

it('can set field "format" on an existing field', async () => {
const title = `foo-${Date.now()}-${Math.random()}*`;
const title = indexPattern.title;
await supertest.delete(`/api/index_patterns/index_pattern/${indexPattern.id}`);
const response1 = await supertest.post('/api/index_patterns/index_pattern').send({
index_pattern: {
title,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,17 @@ import { FtrProviderContext } from '../../../../ftr_provider_context';

export default function ({ getService }: FtrProviderContext) {
const supertest = getService('supertest');
const esArchiver = getService('esArchiver');

describe('main', () => {
before(async () => {
await esArchiver.load('index_patterns/basic_index');
});

after(async () => {
await esArchiver.unload('index_patterns/basic_index');
});

it('can create a new scripted field', async () => {
const title = `foo-${Date.now()}-${Math.random()}*`;
const response1 = await supertest.post('/api/index_patterns/index_pattern').send({
Expand Down Expand Up @@ -40,7 +49,7 @@ export default function ({ getService }: FtrProviderContext) {
});

it('newly created scripted field is materialized in the index_pattern object', async () => {
const title = `foo-${Date.now()}-${Math.random()}*`;
const title = `basic_index`;
const response1 = await supertest.post('/api/index_patterns/index_pattern').send({
index_pattern: {
title,
Expand All @@ -51,7 +60,7 @@ export default function ({ getService }: FtrProviderContext) {
.post(`/api/index_patterns/index_pattern/${response1.body.index_pattern.id}/scripted_field`)
.send({
field: {
name: 'bar',
name: 'bar2',
type: 'number',
scripted: true,
script: "doc['field_name'].value",
Expand All @@ -64,12 +73,15 @@ export default function ({ getService }: FtrProviderContext) {

expect(response2.status).to.be(200);

const field = response2.body.index_pattern.fields.bar;
const field = response2.body.index_pattern.fields.bar2;

expect(field.name).to.be('bar');
expect(field.name).to.be('bar2');
expect(field.type).to.be('number');
expect(field.scripted).to.be(true);
expect(field.script).to.be("doc['field_name'].value");
await supertest.delete(
'/api/index_patterns/index_pattern/' + response1.body.index_pattern.id
);
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,25 @@ import { FtrProviderContext } from '../../../../ftr_provider_context';

export default function ({ getService }: FtrProviderContext) {
const supertest = getService('supertest');
const esArchiver = getService('esArchiver');

describe('main', () => {
before(async () => {
await esArchiver.load('index_patterns/basic_index');
});

after(async () => {
await esArchiver.unload('index_patterns/basic_index');
});

it('can remove a scripted field', async () => {
const title = `foo-${Date.now()}-${Math.random()}*`;
const title = `basic_index`;
const response1 = await supertest.post('/api/index_patterns/index_pattern').send({
index_pattern: {
title,
fields: {
bar: {
name: 'bar',
name: 'bar2',
type: 'number',
scripted: true,
script: "doc['field_name'].value",
Expand All @@ -33,10 +42,10 @@ export default function ({ getService }: FtrProviderContext) {
'/api/index_patterns/index_pattern/' + response1.body.index_pattern.id
);

expect(typeof response2.body.index_pattern.fields.bar).to.be('object');
expect(typeof response2.body.index_pattern.fields.bar2).to.be('object');

const response3 = await supertest.delete(
`/api/index_patterns/index_pattern/${response1.body.index_pattern.id}/scripted_field/bar`
`/api/index_patterns/index_pattern/${response1.body.index_pattern.id}/scripted_field/bar2`
);

expect(response3.status).to.be(200);
Expand All @@ -45,7 +54,10 @@ export default function ({ getService }: FtrProviderContext) {
'/api/index_patterns/index_pattern/' + response1.body.index_pattern.id
);

expect(typeof response4.body.index_pattern.fields.bar).to.be('undefined');
expect(typeof response4.body.index_pattern.fields.bar2).to.be('undefined');
await supertest.delete(
'/api/index_patterns/index_pattern/' + response1.body.index_pattern.id
);
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,19 @@ import { FtrProviderContext } from '../../../../ftr_provider_context';

export default function ({ getService }: FtrProviderContext) {
const supertest = getService('supertest');
const esArchiver = getService('esArchiver');

describe('main', () => {
before(async () => {
await esArchiver.load('index_patterns/basic_index');
});

after(async () => {
await esArchiver.unload('index_patterns/basic_index');
});

it('can fetch a scripted field', async () => {
const title = `foo-${Date.now()}-${Math.random()}*`;
const title = `basic_index`;
const response1 = await supertest.post('/api/index_patterns/index_pattern').send({
index_pattern: {
title,
Expand Down Expand Up @@ -47,6 +56,9 @@ export default function ({ getService }: FtrProviderContext) {
expect(response2.body.field.type).to.be('number');
expect(response2.body.field.scripted).to.be(true);
expect(response2.body.field.script).to.be("doc['field_name'].value");
await supertest.delete(
'/api/index_patterns/index_pattern/' + response1.body.index_pattern.id
);
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,19 @@ import { FtrProviderContext } from '../../../../ftr_provider_context';

export default function ({ getService }: FtrProviderContext) {
const supertest = getService('supertest');
const esArchiver = getService('esArchiver');

describe('main', () => {
before(async () => {
await esArchiver.load('index_patterns/basic_index');
});

after(async () => {
await esArchiver.unload('index_patterns/basic_index');
});

it('can overwrite an existing field', async () => {
const title = `foo-${Date.now()}-${Math.random()}*`;
const title = `basic_index`;
const response1 = await supertest.post('/api/index_patterns/index_pattern').send({
index_pattern: {
title,
Expand Down Expand Up @@ -63,10 +72,13 @@ export default function ({ getService }: FtrProviderContext) {

expect(response3.status).to.be(200);
expect(response3.body.field.type).to.be('string');
await supertest.delete(
'/api/index_patterns/index_pattern/' + response1.body.index_pattern.id
);
});

it('can add a new scripted field', async () => {
const title = `foo-${Date.now()}-${Math.random()}*`;
const title = `basic_index`;
const response1 = await supertest.post('/api/index_patterns/index_pattern').send({
index_pattern: {
title,
Expand Down Expand Up @@ -100,6 +112,9 @@ export default function ({ getService }: FtrProviderContext) {

expect(response2.status).to.be(200);
expect(response2.body.field.script).to.be("doc['bar'].value");
await supertest.delete(
'/api/index_patterns/index_pattern/' + response1.body.index_pattern.id
);
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,19 @@ import { FtrProviderContext } from '../../../../ftr_provider_context';

export default function ({ getService }: FtrProviderContext) {
const supertest = getService('supertest');
const esArchiver = getService('esArchiver');

describe('main', () => {
before(async () => {
await esArchiver.load('index_patterns/basic_index');
});

after(async () => {
await esArchiver.unload('index_patterns/basic_index');
});

it('can update an existing field', async () => {
const title = `foo-${Date.now()}-${Math.random()}*`;
const title = `basic_index`;
const response1 = await supertest.post('/api/index_patterns/index_pattern').send({
index_pattern: {
title,
Expand Down Expand Up @@ -56,6 +65,9 @@ export default function ({ getService }: FtrProviderContext) {
expect(response3.status).to.be(200);
expect(response3.body.field.type).to.be('string');
expect(response3.body.field.script).to.be("doc['bar'].value");
await supertest.delete(
'/api/index_patterns/index_pattern/' + response1.body.index_pattern.id
);
});
});
}
11 changes: 3 additions & 8 deletions x-pack/plugins/maps/server/maps_telemetry/maps_telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,7 @@ async function isFieldGeoShape(
if (!indexPattern) {
return false;
}
const fieldsForIndexPattern = await indexPatternsService.getFieldsForIndexPattern(indexPattern);
return fieldsForIndexPattern.some(
return indexPattern.fields.some(
(fieldDescriptor: IFieldType) => fieldDescriptor.name && fieldDescriptor.name === geoField!
);
}
Expand Down Expand Up @@ -192,13 +191,9 @@ async function filterIndexPatternsByField(fields: string[]) {
await Promise.all(
indexPatternIds.map(async (indexPatternId: string) => {
const indexPattern = await indexPatternsService.get(indexPatternId);
const fieldsForIndexPattern = await indexPatternsService.getFieldsForIndexPattern(
indexPattern
);
const containsField = fields.some((field: string) =>
fieldsForIndexPattern.some(
(fieldDescriptor: IFieldType) =>
fieldDescriptor.esTypes && fieldDescriptor.esTypes.includes(field)
indexPattern.fields.some(
(fieldDescriptor) => fieldDescriptor.esTypes && fieldDescriptor.esTypes.includes(field)
)
);
if (containsField) {
Expand Down

0 comments on commit 784270d

Please sign in to comment.