Skip to content

Commit

Permalink
Increment name globally instead than per policy
Browse files Browse the repository at this point in the history
  • Loading branch information
criamico committed Oct 20, 2021
1 parent 96dc012 commit da8863e
Showing 1 changed file with 33 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,11 @@ import {

import styled from 'styled-components';

import type {
AgentPolicy,
PackageInfo,
PackagePolicy,
NewPackagePolicy,
RegistryVarsEntry,
} from '../../../types';
import type { AgentPolicy, PackageInfo, NewPackagePolicy, RegistryVarsEntry } from '../../../types';
import { packageToPackagePolicy, pkgKeyFromPackageInfo } from '../../../services';
import { Loading } from '../../../components';
import { useStartServices } from '../../../hooks';
import { useStartServices, useGetPackagePolicies } from '../../../hooks';
import { PACKAGE_POLICY_SAVED_OBJECT_TYPE } from '../../../../../constants';

import { isAdvancedVar } from './services';
import type { PackagePolicyValidationResults } from './services';
Expand Down Expand Up @@ -65,6 +60,14 @@ export const StepDefinePackagePolicy: React.FunctionComponent<{
submitAttempted,
}) => {
const { docLinks } = useStartServices();

// Fetch all packagePolicies having the package name
const { data: packagePolicyData, isLoading: isLoadingPackagePolicies } = useGetPackagePolicies({
perPage: 1000,
page: 1,
kuery: `${PACKAGE_POLICY_SAVED_OBJECT_TYPE}.package.name:${packageInfo.name}`,
});

// Form show/hide states
const [isShowingAdvanced, setIsShowingAdvanced] = useState<boolean>(false);

Expand All @@ -84,32 +87,37 @@ export const StepDefinePackagePolicy: React.FunctionComponent<{

// Update package policy's package and agent policy info
useEffect(() => {
if (isLoadingPackagePolicies) {
return;
}
const pkg = packagePolicy.package;
const currentPkgKey = pkg ? pkgKeyFromPackageInfo(pkg) : '';
const pkgKey = pkgKeyFromPackageInfo(packageInfo);

// If package has changed, create shell package policy with input&stream values based on package info
if (currentPkgKey !== pkgKey) {
// Existing package policies on the agent policy using the package name, retrieve highest number appended to package policy name
// Retrieve highest number appended to package policy name and increment it by one
const pkgPoliciesNamePattern = new RegExp(`${packageInfo.name}-(\\d+)`);
const pkgPoliciesWithMatchingNames = agentPolicy
? (agentPolicy.package_policies as PackagePolicy[])
const pkgPoliciesWithMatchingNames = packagePolicyData?.items
? packagePolicyData.items
.filter((ds) => Boolean(ds.name.match(pkgPoliciesNamePattern)))
.map((ds) => parseInt(ds.name.match(pkgPoliciesNamePattern)![1], 10))
.sort((a, b) => a - b)
: [];

const incrementedName = `${packageInfo.name}-${
pkgPoliciesWithMatchingNames.length
? pkgPoliciesWithMatchingNames[pkgPoliciesWithMatchingNames.length - 1] + 1
: 1
}`;

updatePackagePolicy(
packageToPackagePolicy(
packageInfo,
agentPolicy?.id || '',
packagePolicy.output_id,
packagePolicy.namespace,
`${packageInfo.name}-${
pkgPoliciesWithMatchingNames.length
? pkgPoliciesWithMatchingNames[pkgPoliciesWithMatchingNames.length - 1] + 1
: 1
}`,
incrementedName,
packagePolicy.description,
integrationToEnable
)
Expand All @@ -123,7 +131,15 @@ export const StepDefinePackagePolicy: React.FunctionComponent<{
namespace: agentPolicy.namespace,
});
}
}, [packagePolicy, agentPolicy, packageInfo, updatePackagePolicy, integrationToEnable]);
}, [
packagePolicy,
agentPolicy,
packageInfo,
updatePackagePolicy,
integrationToEnable,
packagePolicyData,
isLoadingPackagePolicies,
]);

return validationResults ? (
<FormGroupResponsiveFields
Expand Down

0 comments on commit da8863e

Please sign in to comment.