Skip to content

Commit

Permalink
Make packageToPackagePolicy params an object, fix types
Browse files Browse the repository at this point in the history
  • Loading branch information
jen-huang committed Apr 6, 2021
1 parent 5dad3d7 commit 5fc6aa8
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -325,9 +325,11 @@ describe('Fleet - packageToPackagePolicy', () => {

describe('packageToPackagePolicy', () => {
it('returns package policy with default name', () => {
expect(packageToPackagePolicy(mockPackage, '1', '2')).toEqual({
expect(
packageToPackagePolicy({ packageInfo: mockPackage, agentPolicyId: '1', outputId: '2' })
).toEqual({
policy_id: '1',
namespace: '',
namespace: 'default',
enabled: true,
inputs: [],
name: 'mock-package-1',
Expand All @@ -341,7 +343,14 @@ describe('Fleet - packageToPackagePolicy', () => {
});

it('returns package policy with custom name', () => {
expect(packageToPackagePolicy(mockPackage, '1', '2', 'default', 'pkgPolicy-1')).toEqual({
expect(
packageToPackagePolicy({
name: 'pkgPolicy-1',
packageInfo: mockPackage,
agentPolicyId: '1',
outputId: '2',
})
).toEqual({
policy_id: '1',
namespace: 'default',
enabled: true,
Expand All @@ -358,14 +367,14 @@ describe('Fleet - packageToPackagePolicy', () => {

it('returns package policy with namespace and description', () => {
expect(
packageToPackagePolicy(
mockPackage,
'1',
'2',
'mock-namespace',
'pkgPolicy-1',
'Test description'
)
packageToPackagePolicy({
name: 'pkgPolicy-1',
namespace: 'mock-namespace',
description: 'Test description',
packageInfo: mockPackage,
agentPolicyId: '1',
outputId: '2',
})
).toEqual({
policy_id: '1',
enabled: true,
Expand All @@ -383,14 +392,19 @@ describe('Fleet - packageToPackagePolicy', () => {
});

it('returns package policy with inputs and package-level vars', () => {
const mockPackageWithPolicyTemplates = ({
const mockPackageWithPackageVars = ({
...mockPackage,
policy_templates: [{ inputs: [{ type: 'foo' }] }],
vars: [{ default: 'foo-var-value', name: 'var-name', type: 'text' }],
} as unknown) as PackageInfo;

expect(
packageToPackagePolicy(mockPackageWithPolicyTemplates, '1', '2', 'default', 'pkgPolicy-1')
packageToPackagePolicy({
name: 'pkgPolicy-1',
packageInfo: mockPackageWithPackageVars,
agentPolicyId: '1',
outputId: '2',
})
).toEqual({
policy_id: '1',
namespace: 'default',
Expand All @@ -409,12 +423,12 @@ describe('Fleet - packageToPackagePolicy', () => {

it('returns package policy with multiple policy templates correctly', () => {
expect(
packageToPackagePolicy(
packageWithPolicyTemplates,
'some-policy-id',
'some-output-id',
'default'
)
packageToPackagePolicy({
name: 'aws-1',
packageInfo: packageWithPolicyTemplates,
agentPolicyId: 'some-policy-id',
outputId: 'some-output-id',
})
).toMatchSnapshot();
});
});
Expand Down
29 changes: 19 additions & 10 deletions x-pack/plugins/fleet/common/services/package_to_package_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import type {
PackagePolicyConfigRecordEntry,
} from '../types';

import { doesPackageHaveIntegrations, groupInputs } from './';
import { groupInputs } from './';

export const findDataStreamsByNames = (
names: string[] = [],
Expand Down Expand Up @@ -112,16 +112,25 @@ export const packageToPackagePolicyInputs = (
* @param outputId
* @param packagePolicyName
*/
export const packageToPackagePolicy = (
packageInfo: PackageInfo,
agentPolicyId: string,
outputId: string,
namespace: string = '',
packagePolicyName?: string,
description?: string
): NewPackagePolicy => {
export const packageToPackagePolicy = (options: {
name?: string;
description?: string;
namespace?: string;
packageInfo: PackageInfo;
agentPolicyId: string;
outputId: string;
}): NewPackagePolicy => {
const {
name,
packageInfo,
agentPolicyId,
outputId,
namespace = 'default',
description,
} = options;

const packagePolicy: NewPackagePolicy = {
name: packagePolicyName || `${packageInfo.name}-1`,
name: name || `${packageInfo.name}-1`,
namespace,
description,
package: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
* 2.0.
*/

import React, { memo, useEffect, useState, useMemo } from 'react';
import React, { memo, useEffect, useState } from 'react';
import { FormattedMessage } from '@kbn/i18n/react';
import {
EuiFormRow,
EuiFieldText,
EuiButtonEmpty,
EuiSpacer,
EuiText,
EuiComboBox,
EuiDescribedFormGroup,
Expand Down Expand Up @@ -74,19 +73,18 @@ export const StepDefinePackagePolicy: React.FunctionComponent<{
.sort((a, b) => a - b);

updatePackagePolicy(
packageToPackagePolicy(
packageInfo,
agentPolicy.id,
packagePolicy.output_id,
packagePolicy.namespace,
// FIXME: Improve package policies name uniqueness - https://github.com/elastic/kibana/issues/72948
`${packageInfo.name}-${
packageToPackagePolicy({
name: `${packageInfo.name}-${
pkgPoliciesWithMatchingNames.length
? pkgPoliciesWithMatchingNames[pkgPoliciesWithMatchingNames.length - 1] + 1
: 1
}`,
packagePolicy.description
)
description: packagePolicy.description,
namespace: packagePolicy.namespace,
packageInfo,
agentPolicyId: agentPolicy.id,
outputId: packagePolicy.output_id,
})
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ jest.mock('../../services/package_policy', (): {
} => {
return {
packagePolicyService: {
compilePackagePolicyInputs: jest.fn((packageInfo, dataInputs) => Promise.resolve(dataInputs)),
compilePackagePolicyInputs: jest.fn((packageInfo, vars, dataInputs) =>
Promise.resolve(dataInputs)
),
buildPackagePolicyFromPackage: jest.fn(),
bulkCreate: jest.fn(),
create: jest.fn((soClient, esClient, newData) =>
Expand Down
14 changes: 7 additions & 7 deletions x-pack/plugins/fleet/server/services/agent_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -802,14 +802,14 @@ export async function addPackageToAgentPolicy(
pkgVersion: packageToInstall.version,
});

const basePackagePolicy = packageToPackagePolicy(
const basePackagePolicy = packageToPackagePolicy({
name: packagePolicyName,
description: packagePolicyDescription,
namespace: agentPolicy.namespace ?? 'default',
packageInfo,
agentPolicy.id,
defaultOutput.id,
agentPolicy.namespace ?? 'default',
packagePolicyName,
packagePolicyDescription
);
agentPolicyId: agentPolicy.id,
outputId: defaultOutput.id,
});

const newPackagePolicy = transformPackagePolicy
? transformPackagePolicy(basePackagePolicy)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ test('getPipelineNameForInstallation gets correct name', () => {
const packageVersion = '1.0.1';
const pipelineRefName = 'pipeline-json';
const pipelineEntryNameForInstallation = getPipelineNameForInstallation({
pipelineName: dataStream.ingest_pipeline,
pipelineName: dataStream.ingest_pipeline!,
dataStream,
packageVersion,
});
Expand Down
10 changes: 7 additions & 3 deletions x-pack/plugins/fleet/server/services/package_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -416,19 +416,23 @@ class PackagePolicyService {
): Promise<NewPackagePolicy | undefined> {
const pkgInstall = await getInstallation({ savedObjectsClient: soClient, pkgName });
if (pkgInstall) {
const [pkgInfo, defaultOutputId] = await Promise.all([
const [packageInfo, defaultOutputId] = await Promise.all([
getPackageInfo({
savedObjectsClient: soClient,
pkgName: pkgInstall.name,
pkgVersion: pkgInstall.version,
}),
outputService.getDefaultOutputId(soClient),
]);
if (pkgInfo) {
if (packageInfo) {
if (!defaultOutputId) {
throw new Error('Default output is not set');
}
return packageToPackagePolicy(pkgInfo, '', defaultOutputId);
return packageToPackagePolicy({
packageInfo,
agentPolicyId: '',
outputId: defaultOutputId,
});
}
}
}
Expand Down

0 comments on commit 5fc6aa8

Please sign in to comment.