Skip to content

Commit

Permalink
[8.x] [eem] _count guards against no valid sources (#204224) (#204310)
Browse files Browse the repository at this point in the history
# Backport

This will backport the following commits from `main` to `8.x`:
- [[eem] _count guards against no valid sources
(#204224)](#204224)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Kevin
Lacabane","email":"kevin.lacabane@elastic.co"},"sourceCommit":{"committedDate":"2024-12-14T10:14:29Z","message":"[eem]
_count guards against no valid sources (#204224)\n\nThe query generation
expects at least 1 source to be
passed","sha":"9a8ed0d1359a3d5848ebcbe200dff44debf6736c","branchLabelMapping":{"^v9.0.0$":"main","^v8.18.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:fix","v9.0.0","backport:prev-minor","Team:obs-entities"],"title":"[eem]
_count guards against no valid sources
","number":204224,"url":"https://github.com/elastic/kibana/pull/204224","mergeCommit":{"message":"[eem]
_count guards against no valid sources (#204224)\n\nThe query generation
expects at least 1 source to be
passed","sha":"9a8ed0d1359a3d5848ebcbe200dff44debf6736c"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/204224","number":204224,"mergeCommit":{"message":"[eem]
_count guards against no valid sources (#204224)\n\nThe query generation
expects at least 1 source to be
passed","sha":"9a8ed0d1359a3d5848ebcbe200dff44debf6736c"}}]}]
BACKPORT-->

Co-authored-by: Kevin Lacabane <kevin.lacabane@elastic.co>
  • Loading branch information
kibanamachine and klacabane authored Dec 14, 2024
1 parent 3e3c937 commit 3ef2eec
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
43 changes: 42 additions & 1 deletion x-pack/test/api_integration/apis/entity_manager/count.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
});
Expand Down Expand Up @@ -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]',
],
});
});
});
}

0 comments on commit 3ef2eec

Please sign in to comment.