diff --git a/x-pack/platform/plugins/shared/entity_manager/server/lib/v2/entity_client.ts b/x-pack/platform/plugins/shared/entity_manager/server/lib/v2/entity_client.ts index 818a9b9b02df2..a2003b5fc7d11 100644 --- a/x-pack/platform/plugins/shared/entity_manager/server/lib/v2/entity_client.ts +++ b/x-pack/platform/plugins/shared/entity_manager/server/lib/v2/entity_client.ts @@ -144,6 +144,10 @@ export class EntityClient { errors: results.filter(isRejectedResult).map((result) => result.reason.message as string), })); + if (validSources.length === 0) { + return { type, value: 0, errors }; + } + const { query, filter } = getEntityCountQuery({ sources: validSources, filters, diff --git a/x-pack/test/api_integration/apis/entity_manager/count.ts b/x-pack/test/api_integration/apis/entity_manager/count.ts index a191536339163..46c60e4bd2bdc 100644 --- a/x-pack/test/api_integration/apis/entity_manager/count.ts +++ b/x-pack/test/api_integration/apis/entity_manager/count.ts @@ -361,7 +361,7 @@ export default function ({ getService }: FtrProviderContext) { }); }); - it('is resilient to invalid sources', async () => { + it('is resilient to partially valid sources', async () => { await createEntityTypeDefinition(supertest, { type: { id: 'chumble', display_name: 'chumble' }, }); @@ -417,5 +417,46 @@ export default function ({ getService }: FtrProviderContext) { ], }); }); + + it('is resilient to no valid sources', async () => { + await createEntityTypeDefinition(supertest, { + type: { id: 'chumble', display_name: 'chumble' }, + }); + await Promise.all([ + createEntitySourceDefinition(supertest, { + source: { + id: 'source1-with-chumbles', + type_id: 'chumble', + index_patterns: ['index-1-with-chumbles'], + identity_fields: ['service.name'], + metadata_fields: [], + filters: [], + }, + }), + createEntitySourceDefinition(supertest, { + source: { + id: 'source2-with-chumbles', + type_id: 'chumble', + index_patterns: ['index-2-with-chumbles'], + identity_fields: ['service.name'], + metadata_fields: [], + filters: [], + }, + }), + ]); + + const result = await countEntities(supertest, {}, 200); + + expect(result).toEqual({ + total: 0, + types: { + chumble: 0, + }, + errors: [ + 'No index found for source [source: source1-with-chumbles, type: chumble] with index patterns [index-1-with-chumbles]', + 'No index found for source [source: source2-with-chumbles, type: chumble] with index patterns [index-2-with-chumbles]', + ], + }); + }); }); }