[Fleet] adding object_type for fields under a group (#162041)

## Summary

Relates https://github.com/elastic/kibana/issues/155004

Fixes a limitation that wildcard fields under a group field were not
changed to `type:object` with `object_type`.

This was discovered during testing with prometheus package where a field
was changed to `type:double` and it was not converted to object type:
c17d59c887/packages/prometheus/data_stream/collector/fields/fields.yml (L9)

To verify, upload the package
[prometheus-1.5.2.zip](12069089/prometheus-1.5.2.zip)
and verify that the resulting `collector` and `remote_write` `@package`
component template has `prometheus.metrics.*` in `dynamic_templates`
```
curl -XPOST -H 'content-type: application/zip' -H 'kbn-xsrf: true' http://localhost:5601/julia/api/fleet/epm/packages -u elastic:changeme --data-binary @prometheus-1.5.2.zip
```
<img width="499" alt="image"
src="337295d8-9e3c-40c8-8b13-bda6229e7272">

### Checklist

- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios
This commit is contained in:
Julia Bardi 2023-07-17 16:18:32 +02:00 committed by GitHub
parent 9191bd9939
commit 13b8864c48
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 60 additions and 2 deletions

View file

@ -671,6 +671,60 @@ describe('processFields', () => {
`);
});
test('handle wildcard field inside group', () => {
const wildcardFields = [
{
name: 'prometheus',
type: 'group',
fields: [
{
name: 'metrics.*',
type: 'double',
metric_type: 'gauge',
},
{
name: 'child_group',
type: 'group',
fields: [
{
name: 'child.*',
type: 'double',
metric_type: 'gauge',
},
],
},
],
},
];
expect(processFieldsWithWildcard(wildcardFields)).toEqual([
{
name: 'prometheus',
type: 'group',
fields: [
{
name: 'metrics.*',
type: 'object',
object_type: 'double',
metric_type: 'gauge',
},
{
name: 'child_group',
type: 'group',
fields: [
{
name: 'child.*',
type: 'object',
object_type: 'double',
metric_type: 'gauge',
},
],
},
],
},
]);
});
describe('processFieldsWithWildcard', () => {
const wildcardWithObjectTypeYml = `
- name: a.*.b

View file

@ -257,7 +257,11 @@ export function processFieldsWithWildcard(fields: Fields): Fields {
const newFields: Fields = [];
for (const field of fields) {
const objectTypeField = processFieldWithoutObjectType(field);
newFields.push({ ...objectTypeField });
// adding object_type for fields under a group type
if (objectTypeField.type === 'group' && objectTypeField.fields) {
objectTypeField.fields = processFieldsWithWildcard(objectTypeField.fields);
}
newFields.push(objectTypeField);
}
return newFields;
}
@ -265,7 +269,7 @@ export function processFieldsWithWildcard(fields: Fields): Fields {
export function processFieldWithoutObjectType(field: Field): Field {
const hasWildcard = field.name.includes('*');
const hasObjectType = field.object_type;
if (hasWildcard && !hasObjectType) {
if (hasWildcard && !hasObjectType && field.type !== 'object') {
return { ...field, type: 'object', object_type: field.type };
} else {
return { ...field };