Skip to content

Commit

Permalink
cover edit/upgrade flow
Browse files Browse the repository at this point in the history
  • Loading branch information
maxcold committed Dec 5, 2023
1 parent 2e6cd1b commit f9b552d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
EuiTitle,
} from '@elastic/eui';
import type { NewPackagePolicy } from '@kbn/fleet-plugin/public';
import { SetupTechnology } from '@kbn/fleet-plugin/public';
import { FormattedMessage } from '@kbn/i18n-react';
import type {
NewPackagePolicyInput,
Expand Down Expand Up @@ -531,7 +532,11 @@ export const CspPolicyTemplateForm = memo<PackagePolicyReplaceDefineStepExtensio
agentPolicy,
agentlessPolicy,
handleSetupTechnologyChange,
isEditPage,
});
const shouldRenderAgentlessSelector =
(isAgentlessAvailable && !isEditPage) ||
(isEditPage && setupTechnology === SetupTechnology.AGENTLESS);

const updatePolicy = useCallback(
(updatedPolicy: NewPackagePolicy) => {
Expand Down Expand Up @@ -590,6 +595,10 @@ export const CspPolicyTemplateForm = memo<PackagePolicyReplaceDefineStepExtensio

// TODO: get rid of useEffect in favor of directly setting aws.credentials.type
useEffect(() => {
if (isEditPage) {
return;
}

setEnabledPolicyInput(input.type);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [setupTechnology]);
Expand Down Expand Up @@ -731,8 +740,9 @@ export const CspPolicyTemplateForm = memo<PackagePolicyReplaceDefineStepExtensio
onChange={(field, value) => updatePolicy({ ...newPolicy, [field]: value })}
/>

{isAgentlessAvailable && (
{shouldRenderAgentlessSelector && (
<SetupTechnologySelector
disabled={isEditPage}
setupTechnology={setupTechnology}
onSetupTechnologyChange={setSetupTechnology}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ import {
} from '@elastic/eui';

export const SetupTechnologySelector = ({
disabled,
setupTechnology,
onSetupTechnologyChange,
}: {
disabled: boolean;
setupTechnology: SetupTechnology;
onSetupTechnologyChange: (value: SetupTechnology) => void;
}) => {
Expand Down Expand Up @@ -87,9 +89,11 @@ export const SetupTechnologySelector = ({
<>
<EuiSpacer size="l" />
<EuiAccordion
isDisabled={disabled}
initialIsOpen={disabled}
id={useGeneratedHtmlId({ prefix: 'setup-type' })}
buttonContent={
<EuiLink>
<EuiLink disabled={disabled}>
<FormattedMessage
id="xpack.csp.fleetIntegration.setupTechnology.advancedOptionsLabel"
defaultMessage="Advanced options"
Expand All @@ -108,6 +112,7 @@ export const SetupTechnologySelector = ({
}
>
<EuiSuperSelect
disabled={disabled}
options={options}
valueOfSelected={setupTechnology}
placeholder={
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,31 @@ export const useSetupTechnology = ({
agentPolicy,
agentlessPolicy,
handleSetupTechnologyChange,
isEditPage,
}: {
input: NewPackagePolicyInput;
agentPolicy?: AgentPolicy;
agentlessPolicy?: AgentPolicy;
handleSetupTechnologyChange?: (value: SetupTechnology) => void;
isEditPage: boolean;
}) => {
const [setupTechnology, setSetupTechnology] = useState<SetupTechnology>(
SetupTechnology.AGENT_BASED
);
const isCspmAws = input.type === CLOUDBEAT_AWS;
const isAgentlessAvailable = Boolean(isCspmAws && agentlessPolicy);
const agentPolicyId = agentPolicy?.id;
const agentlessPolicyId = agentlessPolicy?.id;
const [setupTechnology, setSetupTechnology] = useState<SetupTechnology>(() => {
if (isEditPage && agentPolicyId === SetupTechnology.AGENTLESS) {
return SetupTechnology.AGENTLESS;
}

return SetupTechnology.AGENT_BASED;
});

useEffect(() => {
if (isEditPage) {
return;
}

if (agentPolicyId && agentPolicyId !== agentlessPolicyId) {
/*
handle case when agent policy is coming from outside,
Expand All @@ -45,13 +55,17 @@ export const useSetupTechnology = ({
} else {
setSetupTechnology(SetupTechnology.AGENT_BASED);
}
}, [agentPolicyId, agentlessPolicyId, isAgentlessAvailable]);
}, [agentPolicyId, agentlessPolicyId, isAgentlessAvailable, isEditPage]);

useEffect(() => {
if (isEditPage) {
return;
}

if (handleSetupTechnologyChange) {
handleSetupTechnologyChange(setupTechnology);
}
}, [handleSetupTechnologyChange, setupTechnology]);
}, [handleSetupTechnologyChange, isEditPage, setupTechnology]);

return {
isAgentlessAvailable,
Expand Down

0 comments on commit f9b552d

Please sign in to comment.