Skip to content

Commit

Permalink
[Fleet] Fix upgrade notice displaying in error (#122675) (#122802)
Browse files Browse the repository at this point in the history
* Fix upgrade notice displaying in error

* Move upgrade detection to a util method

(cherry picked from commit 07b64b1)

Co-authored-by: Kyle Pollich <[email protected]>
  • Loading branch information
kibanamachine and kpollich authored Jan 12, 2022
1 parent 9e5b5ee commit 1f04996
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ import type {
import type { PackagePolicyEditExtensionComponentProps } from '../../../types';
import { pkgKeyFromPackageInfo, storedPackagePoliciesToAgentInputs } from '../../../services';

import { hasUpgradeAvailable } from './utils';

export const EditPackagePolicyPage = memo(() => {
const {
params: { packagePolicyId },
Expand All @@ -84,16 +86,16 @@ export const EditPackagePolicyPage = memo(() => {
// the edit form in an "upgrade" state regardless of whether the user intended to
// "edit" their policy or "upgrade" it. This ensures the new policy generated will be
// set to use the latest version of the package, not its current version.
isUpgrade={extensionView?.useLatestPackageVersion}
forceUpgrade={extensionView?.useLatestPackageVersion}
/>
);
});

export const EditPackagePolicyForm = memo<{
packagePolicyId: string;
isUpgrade?: boolean;
forceUpgrade?: boolean;
from?: EditPackagePolicyFrom;
}>(({ packagePolicyId, isUpgrade = false, from = 'edit' }) => {
}>(({ packagePolicyId, forceUpgrade = false, from = 'edit' }) => {
const { application, notifications } = useStartServices();
const {
agents: { enabled: isFleetEnabled },
Expand All @@ -119,6 +121,14 @@ export const EditPackagePolicyForm = memo<{
useState<GetOnePackagePolicyResponse['item']>();
const [dryRunData, setDryRunData] = useState<UpgradePackagePolicyDryRunResponse>();

const [isUpgrade, setIsUpgrade] = useState<boolean>(false);

useEffect(() => {
if (forceUpgrade) {
setIsUpgrade(true);
}
}, [forceUpgrade]);

const policyId = agentPolicy?.id ?? '';

// Retrieve agent policy, package, and package policy info
Expand Down Expand Up @@ -146,11 +156,24 @@ export const EditPackagePolicyForm = memo<{
setAgentPolicy(agentPolicyData.item);
}

const { data: upgradePackagePolicyDryRunData } = await sendUpgradePackagePolicyDryRun([
packagePolicyId,
]);
const { data: upgradePackagePolicyDryRunData, error: upgradePackagePolicyDryRunError } =
await sendUpgradePackagePolicyDryRun([packagePolicyId]);

if (upgradePackagePolicyDryRunError) {
throw upgradePackagePolicyDryRunError;
}

const hasUpgrade = upgradePackagePolicyDryRunData
? hasUpgradeAvailable(upgradePackagePolicyDryRunData)
: false;

// If the dry run data doesn't indicate a difference in version numbers, flip the form back
// to its non-upgrade state, even if we were initially set to the upgrade view
if (!hasUpgrade) {
setIsUpgrade(false);
}

if (upgradePackagePolicyDryRunData) {
if (upgradePackagePolicyDryRunData && hasUpgrade) {
setDryRunData(upgradePackagePolicyDryRunData);
}

Expand Down Expand Up @@ -441,7 +464,7 @@ export const EditPackagePolicyForm = memo<{
const [selectedTab, setSelectedTab] = useState(0);

const layoutProps = {
from: extensionView?.useLatestPackageVersion ? 'upgrade-from-extension' : from,
from: extensionView?.useLatestPackageVersion && isUpgrade ? 'upgrade-from-extension' : from,
cancelUrl,
agentPolicy,
packageInfo,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import semverGt from 'semver/functions/gt';

import type { UpgradePackagePolicyDryRunResponse } from '../../../../types';

/**
* Given a dry run response, determines if a greater version exists in the "proposed"
* version of the first package policy in the response.
*/
export function hasUpgradeAvailable(dryRunData: UpgradePackagePolicyDryRunResponse) {
return (
dryRunData &&
dryRunData[0].diff &&
semverGt(
dryRunData[0].diff[1].package?.version ?? '',
dryRunData[0].diff[0].package?.version ?? ''
)
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

export * from './has_upgrade_available';
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ export const UpgradePackagePolicyPage = memo(() => {
from = 'upgrade-from-integrations-policy-list';
}

return <EditPackagePolicyForm packagePolicyId={packagePolicyId} from={from} isUpgrade />;
return <EditPackagePolicyForm packagePolicyId={packagePolicyId} from={from} forceUpgrade />;
});
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const Policy = memo(() => {
<EditPackagePolicyForm
packagePolicyId={packagePolicyId}
from="package-edit"
isUpgrade={extensionView?.useLatestPackageVersion}
forceUpgrade={extensionView?.useLatestPackageVersion}
/>
);
});

0 comments on commit 1f04996

Please sign in to comment.