Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fleet] Update package policy id when agent id changes #151886

Merged
merged 5 commits into from
Feb 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import React, { memo, useEffect, useState } from 'react';
import React, { memo, useState } from 'react';
import { i18n } from '@kbn/i18n';
import { FormattedMessage } from '@kbn/i18n-react';
import {
Expand Down Expand Up @@ -86,18 +86,6 @@ export const StepDefinePackagePolicy: React.FunctionComponent<{
});
}

// Update package policy's package and agent policy info
useEffect(() => {
// TODO move this to parent hook
// If agent policy has changed, update package policy's agent policy ID and namespace
if (agentPolicy && packagePolicy.policy_id !== agentPolicy.id) {
updatePackagePolicy({
policy_id: agentPolicy.id,
namespace: agentPolicy.namespace,
});
}
}, [packagePolicy, agentPolicy, packageInfo, updatePackagePolicy]);

Comment on lines -89 to -100
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this effect was lifted to a single parent hook - onSubmit, which is 1 of 3 usages. the other two are not relevant:

  • edit screen doesn't allow changing agent policy
  • multi page layout doesn't use package-policy-replace-define-step

alternatively, we can instead keep it as is and duplicate it in our custom extension component.

const isManaged = packagePolicy.is_managed;

return validationResults ? (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type { RenderHookResult } from '@testing-library/react-hooks';

import type { TestRenderer } from '../../../../../../../mock';
import { createFleetTestRendererMock } from '../../../../../../../mock';
import type { PackageInfo } from '../../../../../types';
import type { AgentPolicy, PackageInfo } from '../../../../../types';

import { sendGetPackagePolicies } from '../../../../../hooks';

Expand Down Expand Up @@ -94,6 +94,43 @@ describe('useOnSubmit', () => {
});
});

it('should set package policy id and namespace when agent policy changes', () => {
act(() => {
renderResult.result.current.updateAgentPolicy({
id: 'some-id',
namespace: 'default',
} as AgentPolicy);
});

expect(renderResult.result.current.packagePolicy).toEqual({
policy_id: 'some-id',
namespace: 'default',
description: '',
enabled: true,
inputs: [],
name: 'apache-1',
package: {
name: 'apache',
title: 'Apache',
version: '1.0.0',
},
vars: {
'Advanced var': {
type: 'bool',
value: true,
},
'Required var': {
type: 'bool',
value: undefined,
},
'Show user var': {
type: 'string',
value: 'showUserVarVal',
},
},
});
});

it('should set index 1 name to package policy on init if no package policies exist for this package', () => {
// waitFor(() => {
// expect(renderResult.getByDisplayValue('apache-1')).toBeInTheDocument();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,15 @@ export function useOnSubmit({
init();
}, [packageInfo, agentPolicy, updatePackagePolicy, integrationToEnable, isInitialized]);

useEffect(() => {
if (agentPolicy && packagePolicy.policy_id !== agentPolicy.id) {
updatePackagePolicy({
policy_id: agentPolicy.id,
namespace: agentPolicy.namespace,
});
}
}, [packagePolicy, agentPolicy, updatePackagePolicy]);

const onSaveNavigate = useOnSaveNavigate({
packagePolicy,
queryParamsPolicyId,
Expand Down