[fleet] Do not create agent policy twice when there is a verification error (#137572) (#137583)

* Do not create agent policy twice when there is a verification error

* re-add setPolicyValidation call

* neaten diff

(cherry picked from commit ec15a9ce53)

Co-authored-by: Mark Hopkin <mark.hopkin@elastic.co>
This commit is contained in:
Kibana Machine 2022-07-29 10:06:52 -04:00 committed by GitHub
parent 78e5d8d1e9
commit 36244e8f43
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -228,16 +228,15 @@ export const CreatePackagePolicySinglePage: CreatePackagePolicyParams = ({
[setNewAgentPolicy, newAgentPolicy, selectedPolicyTab]
);
const updateSelectedPolicy = useCallback(
(policy) => {
setSelectedPolicyTab(policy);
setPolicyValidation(policy, newAgentPolicy);
const updateSelectedPolicyTab = useCallback(
(selectedTab) => {
setSelectedPolicyTab(selectedTab);
setPolicyValidation(selectedTab, newAgentPolicy);
},
[setSelectedPolicyTab, newAgentPolicy]
);
const hasErrors = validationResults ? validationHasErrors(validationResults) : false;
// Update package policy validation
const updatePackagePolicyValidation = useCallback(
(newPackagePolicy?: NewPackagePolicy) => {
@ -313,32 +312,31 @@ export const CreatePackagePolicySinglePage: CreatePackagePolicyParams = ({
[agentCount]
);
const createAgentPolicy = useCallback(
async ({ force }: { force?: boolean } = {}): Promise<string | undefined> => {
let policyId;
setFormState('LOADING');
// do not create agent policy with system integration if package policy already is for system package
const packagePolicyIsSystem = packagePolicy?.package?.name === FLEET_SYSTEM_PACKAGE;
const resp = await sendCreateAgentPolicy(newAgentPolicy, {
withSysMonitoring: withSysMonitoring && !packagePolicyIsSystem,
});
if (resp.error) {
setFormState('VALID');
throw resp.error;
}
if (resp.data) {
policyId = resp.data.item.id;
setAgentPolicy(resp.data.item);
setSelectedPolicyTab(SelectedPolicyTab.EXISTING);
updatePackagePolicy({ policy_id: policyId });
}
return policyId;
},
[newAgentPolicy, updatePackagePolicy, withSysMonitoring, packagePolicy]
);
const createAgentPolicy = useCallback(async (): Promise<AgentPolicy | undefined> => {
let createdAgentPolicy;
setFormState('LOADING');
// do not create agent policy with system integration if package policy already is for system package
const packagePolicyIsSystem = packagePolicy?.package?.name === FLEET_SYSTEM_PACKAGE;
const resp = await sendCreateAgentPolicy(newAgentPolicy, {
withSysMonitoring: withSysMonitoring && !packagePolicyIsSystem,
});
if (resp.error) {
setFormState('VALID');
throw resp.error;
}
if (resp.data) {
createdAgentPolicy = resp.data.item;
setAgentPolicy(createdAgentPolicy);
updatePackagePolicy({ policy_id: createdAgentPolicy.id });
}
return createdAgentPolicy;
}, [packagePolicy?.package?.name, newAgentPolicy, withSysMonitoring, updatePackagePolicy]);
const onSubmit = useCallback(
async ({ force }: { force?: boolean } = {}) => {
async ({
force,
overrideCreatedAgentPolicy,
}: { overrideCreatedAgentPolicy?: AgentPolicy; force?: boolean } = {}) => {
if (formState === 'VALID' && hasErrors) {
setFormState('INVALID');
return;
@ -347,10 +345,10 @@ export const CreatePackagePolicySinglePage: CreatePackagePolicyParams = ({
setFormState('CONFIRM');
return;
}
let policyId;
if (selectedPolicyTab === SelectedPolicyTab.NEW) {
let createdPolicy = overrideCreatedAgentPolicy;
if (selectedPolicyTab === SelectedPolicyTab.NEW && !overrideCreatedAgentPolicy) {
try {
policyId = await createAgentPolicy({ force });
createdPolicy = await createAgentPolicy();
} catch (e) {
notifications.toasts.addError(e, {
title: i18n.translate('xpack.fleet.createAgentPolicy.errorNotificationTitle', {
@ -365,7 +363,7 @@ export const CreatePackagePolicySinglePage: CreatePackagePolicyParams = ({
// passing pkgPolicy with policy_id here as setPackagePolicy doesn't propagate immediately
const { error, data } = await savePackagePolicy({
...packagePolicy,
policy_id: policyId ?? packagePolicy.policy_id,
policy_id: createdPolicy?.id ?? packagePolicy.policy_id,
force,
});
if (!error) {
@ -397,12 +395,12 @@ export const CreatePackagePolicySinglePage: CreatePackagePolicyParams = ({
});
} else {
if (isVerificationError(error)) {
setFormState('VALID'); // don't show the add agent modal
const forceInstall = await confirmForceInstall(packagePolicy.package!);
if (forceInstall) {
onSubmit({ force: true });
} else {
setFormState('VALID');
// skip creating the agent policy because it will have already been successfully created
onSubmit({ overrideCreatedAgentPolicy: createdPolicy, force: true });
}
return;
}
@ -461,7 +459,7 @@ export const CreatePackagePolicySinglePage: CreatePackagePolicyParams = ({
validation={validation}
packageInfo={packageInfo}
setHasAgentPolicyError={setHasAgentPolicyError}
updateSelectedTab={updateSelectedPolicy}
updateSelectedTab={updateSelectedPolicyTab}
selectedAgentPolicyId={queryParamsPolicyId}
/>
),
@ -473,7 +471,7 @@ export const CreatePackagePolicySinglePage: CreatePackagePolicyParams = ({
updateNewAgentPolicy,
validation,
withSysMonitoring,
updateSelectedPolicy,
updateSelectedPolicyTab,
queryParamsPolicyId,
]
);