Skip to content

Commit

Permalink
[Fleet] Support dataset with multiple level like system.process.summa…
Browse files Browse the repository at this point in the history
  • Loading branch information
nchaulet authored and kibanamachine committed May 12, 2021
1 parent 1db24e1 commit dfeb974
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 8 deletions.
65 changes: 65 additions & 0 deletions x-pack/plugins/fleet/server/services/package_policy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ paths:
},
];
}
if (dataset === 'dataset1_level1') {
return [
{
buffer: Buffer.from(`
type: log
metricset: ["dataset1.level1"]
`),
},
];
}

return [
{
buffer: Buffer.from(`
Expand Down Expand Up @@ -98,6 +109,7 @@ describe('Package policy service', () => {
type: 'logs',
dataset: 'package.dataset1',
streams: [{ input: 'log', template_path: 'some_template_path.yml' }],
path: 'dataset1',
},
],
policy_templates: [
Expand Down Expand Up @@ -151,6 +163,57 @@ describe('Package policy service', () => {
]);
});

it('should work with a two level dataset name', async () => {
const inputs = await packagePolicyService.compilePackagePolicyInputs(
({
data_streams: [
{
type: 'logs',
dataset: 'package.dataset1.level1',
streams: [{ input: 'log', template_path: 'some_template_path.yml' }],
path: 'dataset1_level1',
},
],
policy_templates: [
{
inputs: [{ type: 'log' }],
},
],
} as unknown) as PackageInfo,
[
{
type: 'log',
enabled: true,
streams: [
{
id: 'datastream01',
data_stream: { dataset: 'package.dataset1.level1', type: 'logs' },
enabled: true,
},
],
},
]
);

expect(inputs).toEqual([
{
type: 'log',
enabled: true,
streams: [
{
id: 'datastream01',
data_stream: { dataset: 'package.dataset1.level1', type: 'logs' },
enabled: true,
compiled_stream: {
metricset: ['dataset1.level1'],
type: 'log',
},
},
],
},
]);
});

it('should work with config variables at the input level', async () => {
const inputs = await packagePolicyService.compilePackagePolicyInputs(
({
Expand All @@ -159,6 +222,7 @@ describe('Package policy service', () => {
dataset: 'package.dataset1',
type: 'logs',
streams: [{ input: 'log', template_path: 'some_template_path.yml' }],
path: 'dataset1',
},
],
policy_templates: [
Expand Down Expand Up @@ -261,6 +325,7 @@ describe('Package policy service', () => {
dataset: 'package.dataset1',
type: 'logs',
streams: [{ input: 'log', template_path: 'some_template_path.yml' }],
path: 'dataset1',
},
],
policy_templates: [
Expand Down
17 changes: 9 additions & 8 deletions x-pack/plugins/fleet/server/services/package_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,6 @@ import { appContextService } from '.';

const SAVED_OBJECT_TYPE = PACKAGE_POLICY_SAVED_OBJECT_TYPE;

function getDataset(st: string) {
return st.split('.')[1];
}

class PackagePolicyService {
public async create(
soClient: SavedObjectsClientContract,
Expand Down Expand Up @@ -562,7 +558,7 @@ async function _compilePackageStream(
if (!stream.enabled) {
return { ...stream, compiled_stream: undefined };
}
const datasetPath = getDataset(stream.data_stream.dataset);

const packageDataStreams = pkgInfo.data_streams;
if (!packageDataStreams) {
throw new Error('Stream template not found, no data streams');
Expand All @@ -571,8 +567,11 @@ async function _compilePackageStream(
const packageDataStream = packageDataStreams.find(
(pkgDataStream) => pkgDataStream.dataset === stream.data_stream.dataset
);

if (!packageDataStream) {
throw new Error(`Stream template not found, unable to find dataset ${datasetPath}`);
throw new Error(
`Stream template not found, unable to find dataset ${stream.data_stream.dataset}`
);
}

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

if (!streamFromPkg.template_path) {
throw new Error(`Stream template path not found for dataset ${datasetPath}`);
throw new Error(`Stream template path not found for dataset ${stream.data_stream.dataset}`);
}

const datasetPath = packageDataStream.path;

const [pkgStreamTemplate] = await getAssetsData(
registryPkgInfo,
(path: string) => path.endsWith(streamFromPkg.template_path),
Expand All @@ -594,7 +595,7 @@ async function _compilePackageStream(

if (!pkgStreamTemplate || !pkgStreamTemplate.buffer) {
throw new Error(
`Unable to load stream template ${streamFromPkg.template_path} for dataset ${datasetPath}`
`Unable to load stream template ${streamFromPkg.template_path} for dataset ${stream.data_stream.dataset}`
);
}

Expand Down

0 comments on commit dfeb974

Please sign in to comment.