Skip to content

Commit dfeb974

Browse files
nchauletkibanamachine
authored andcommitted
[Fleet] Support dataset with multiple level like system.process.summary (#99852)
1 parent 1db24e1 commit dfeb974

2 files changed

Lines changed: 74 additions & 8 deletions

File tree

x-pack/plugins/fleet/server/services/package_policy.test.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,17 @@ paths:
3838
},
3939
];
4040
}
41+
if (dataset === 'dataset1_level1') {
42+
return [
43+
{
44+
buffer: Buffer.from(`
45+
type: log
46+
metricset: ["dataset1.level1"]
47+
`),
48+
},
49+
];
50+
}
51+
4152
return [
4253
{
4354
buffer: Buffer.from(`
@@ -98,6 +109,7 @@ describe('Package policy service', () => {
98109
type: 'logs',
99110
dataset: 'package.dataset1',
100111
streams: [{ input: 'log', template_path: 'some_template_path.yml' }],
112+
path: 'dataset1',
101113
},
102114
],
103115
policy_templates: [
@@ -151,6 +163,57 @@ describe('Package policy service', () => {
151163
]);
152164
});
153165

166+
it('should work with a two level dataset name', async () => {
167+
const inputs = await packagePolicyService.compilePackagePolicyInputs(
168+
({
169+
data_streams: [
170+
{
171+
type: 'logs',
172+
dataset: 'package.dataset1.level1',
173+
streams: [{ input: 'log', template_path: 'some_template_path.yml' }],
174+
path: 'dataset1_level1',
175+
},
176+
],
177+
policy_templates: [
178+
{
179+
inputs: [{ type: 'log' }],
180+
},
181+
],
182+
} as unknown) as PackageInfo,
183+
[
184+
{
185+
type: 'log',
186+
enabled: true,
187+
streams: [
188+
{
189+
id: 'datastream01',
190+
data_stream: { dataset: 'package.dataset1.level1', type: 'logs' },
191+
enabled: true,
192+
},
193+
],
194+
},
195+
]
196+
);
197+
198+
expect(inputs).toEqual([
199+
{
200+
type: 'log',
201+
enabled: true,
202+
streams: [
203+
{
204+
id: 'datastream01',
205+
data_stream: { dataset: 'package.dataset1.level1', type: 'logs' },
206+
enabled: true,
207+
compiled_stream: {
208+
metricset: ['dataset1.level1'],
209+
type: 'log',
210+
},
211+
},
212+
],
213+
},
214+
]);
215+
});
216+
154217
it('should work with config variables at the input level', async () => {
155218
const inputs = await packagePolicyService.compilePackagePolicyInputs(
156219
({
@@ -159,6 +222,7 @@ describe('Package policy service', () => {
159222
dataset: 'package.dataset1',
160223
type: 'logs',
161224
streams: [{ input: 'log', template_path: 'some_template_path.yml' }],
225+
path: 'dataset1',
162226
},
163227
],
164228
policy_templates: [
@@ -261,6 +325,7 @@ describe('Package policy service', () => {
261325
dataset: 'package.dataset1',
262326
type: 'logs',
263327
streams: [{ input: 'log', template_path: 'some_template_path.yml' }],
328+
path: 'dataset1',
264329
},
265330
],
266331
policy_templates: [

x-pack/plugins/fleet/server/services/package_policy.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,6 @@ import { appContextService } from '.';
5656

5757
const SAVED_OBJECT_TYPE = PACKAGE_POLICY_SAVED_OBJECT_TYPE;
5858

59-
function getDataset(st: string) {
60-
return st.split('.')[1];
61-
}
62-
6359
class PackagePolicyService {
6460
public async create(
6561
soClient: SavedObjectsClientContract,
@@ -562,7 +558,7 @@ async function _compilePackageStream(
562558
if (!stream.enabled) {
563559
return { ...stream, compiled_stream: undefined };
564560
}
565-
const datasetPath = getDataset(stream.data_stream.dataset);
561+
566562
const packageDataStreams = pkgInfo.data_streams;
567563
if (!packageDataStreams) {
568564
throw new Error('Stream template not found, no data streams');
@@ -571,8 +567,11 @@ async function _compilePackageStream(
571567
const packageDataStream = packageDataStreams.find(
572568
(pkgDataStream) => pkgDataStream.dataset === stream.data_stream.dataset
573569
);
570+
574571
if (!packageDataStream) {
575-
throw new Error(`Stream template not found, unable to find dataset ${datasetPath}`);
572+
throw new Error(
573+
`Stream template not found, unable to find dataset ${stream.data_stream.dataset}`
574+
);
576575
}
577576

578577
const streamFromPkg = (packageDataStream.streams || []).find(
@@ -583,9 +582,11 @@ async function _compilePackageStream(
583582
}
584583

585584
if (!streamFromPkg.template_path) {
586-
throw new Error(`Stream template path not found for dataset ${datasetPath}`);
585+
throw new Error(`Stream template path not found for dataset ${stream.data_stream.dataset}`);
587586
}
588587

588+
const datasetPath = packageDataStream.path;
589+
589590
const [pkgStreamTemplate] = await getAssetsData(
590591
registryPkgInfo,
591592
(path: string) => path.endsWith(streamFromPkg.template_path),
@@ -594,7 +595,7 @@ async function _compilePackageStream(
594595

595596
if (!pkgStreamTemplate || !pkgStreamTemplate.buffer) {
596597
throw new Error(
597-
`Unable to load stream template ${streamFromPkg.template_path} for dataset ${datasetPath}`
598+
`Unable to load stream template ${streamFromPkg.template_path} for dataset ${stream.data_stream.dataset}`
598599
);
599600
}
600601

0 commit comments

Comments
 (0)