Skip to content

Commit

Permalink
[Fleet] Add experimental data stream features support to simplified p…
Browse files Browse the repository at this point in the history
…ackage policy API
  • Loading branch information
nchaulet committed Sep 21, 2022
1 parent f7f8fa6 commit d314a67
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,36 @@ describe('toPackagePolicy', () => {
'nginx-nginx/metrics': ['nginx.stubstatus'],
});
});

it('should to pass experimental_data_stream_features', () => {
const res = simplifiedPackagePolicytoNewPackagePolicy(
{
name: 'nginx-1',
namespace: 'default',
policy_id: 'policy123',
description: 'Test description',
},
nginxPackageInfo as unknown as PackageInfo,
{
experimental_data_stream_features: [
{
data_stream: 'logs-nginx.access',
features: {
synthetic_source: true,
},
},
],
}
);

expect(res.package?.experimental_data_stream_features).toEqual([
{
data_stream: 'logs-nginx.access',
features: {
synthetic_source: true,
},
},
]);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type {
PackagePolicyConfigRecord,
NewPackagePolicy,
PackageInfo,
ExperimentalDataStreamFeature,
} from '../types';
import { PackagePolicyValidationError } from '../errors';

Expand Down Expand Up @@ -66,7 +67,10 @@ type InputMap = Map<string, { input: NewPackagePolicyInput; streams: StreamsMap

export function simplifiedPackagePolicytoNewPackagePolicy(
data: SimplifiedPackagePolicy,
packageInfo: PackageInfo
packageInfo: PackageInfo,
options?: {
experimental_data_stream_features: ExperimentalDataStreamFeature[];
}
): NewPackagePolicy {
const {
policy_id: policyId,
Expand All @@ -77,6 +81,10 @@ export function simplifiedPackagePolicytoNewPackagePolicy(
vars: packageLevelVars,
} = data;
const packagePolicy = packageToPackagePolicy(packageInfo, policyId, namespace, name, description);
if (packagePolicy.package && options?.experimental_data_stream_features) {
packagePolicy.package.experimental_data_stream_features =
options.experimental_data_stream_features;
}

// Build a input and streams Map to easily find package policy stream
const inputMap: InputMap = new Map();
Expand Down
7 changes: 5 additions & 2 deletions x-pack/plugins/fleet/server/routes/package_policy/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,9 @@ export const createPackagePolicyHandler: FleetRequestHandler<
pkgVersion: pkg.version,
ignoreUnverified: force,
});
newPackagePolicy = simplifiedPackagePolicytoNewPackagePolicy(newPolicy, pkgInfo);
newPackagePolicy = simplifiedPackagePolicytoNewPackagePolicy(newPolicy, pkgInfo, {
experimental_data_stream_features: pkg.experimental_data_stream_features,
});
} else {
newPackagePolicy = await packagePolicyService.enrichPolicyWithDefaultsFromPackage(soClient, {
...newPolicy,
Expand Down Expand Up @@ -293,7 +295,8 @@ export const updatePackagePolicyHandler: RequestHandler<
});
newData = simplifiedPackagePolicytoNewPackagePolicy(
body as unknown as SimplifiedPackagePolicy,
pkgInfo
pkgInfo,
{ experimental_data_stream_features: pkg.experimental_data_stream_features }
);
} else {
// removed fields not recognized by schema
Expand Down
21 changes: 11 additions & 10 deletions x-pack/plugins/fleet/server/types/models/package_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@ const PackagePolicyInputsSchema = {
streams: schema.arrayOf(schema.object(PackagePolicyStreamsSchema)),
};

const ExperimentalDataStreamFeatures = schema.arrayOf(
schema.object({
data_stream: schema.string(),
features: schema.object({
synthetic_source: schema.boolean(),
}),
})
);

const PackagePolicyBaseSchema = {
name: schema.string(),
description: schema.maybe(schema.string()),
Expand All @@ -90,16 +99,7 @@ const PackagePolicyBaseSchema = {
name: schema.string(),
title: schema.string(),
version: schema.string(),
experimental_data_stream_features: schema.maybe(
schema.arrayOf(
schema.object({
data_stream: schema.string(),
features: schema.object({
synthetic_source: schema.boolean(),
}),
})
)
),
experimental_data_stream_features: schema.maybe(ExperimentalDataStreamFeatures),
})
),
// Deprecated TODO create remove issue
Expand Down Expand Up @@ -172,6 +172,7 @@ export const SimplifiedCreatePackagePolicyRequestBodySchema = schema.object({
package: schema.object({
name: schema.string(),
version: schema.string(),
experimental_data_stream_features: schema.maybe(ExperimentalDataStreamFeatures),
}),
force: schema.maybe(schema.boolean()),
vars: schema.maybe(SimplifiedVarsSchema),
Expand Down

0 comments on commit d314a67

Please sign in to comment.