From 48b5d375976fe7a3ea8b426fc24910450d9dfa05 Mon Sep 17 00:00:00 2001 From: Maxim Kholod Date: Wed, 29 Nov 2023 11:49:48 +0100 Subject: [PATCH] fetch agentless policy only in serverless --- .../hooks/setup_technology.test.ts | 29 ++++++++++++++++++- .../hooks/setup_technology.ts | 8 +++-- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/create_package_policy_page/single_page_layout/hooks/setup_technology.test.ts b/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/create_package_policy_page/single_page_layout/hooks/setup_technology.test.ts index 5aa8ad0cad259..dd2a1dcc81c79 100644 --- a/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/create_package_policy_page/single_page_layout/hooks/setup_technology.test.ts +++ b/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/create_package_policy_page/single_page_layout/hooks/setup_technology.test.ts @@ -9,7 +9,7 @@ import { renderHook, act } from '@testing-library/react-hooks'; import { SetupTechnology } from '../../../../../../../../common/types'; import { ExperimentalFeaturesService } from '../../../../../services'; -import { sendGetOneAgentPolicy } from '../../../../../hooks'; +import { sendGetOneAgentPolicy, useStartServices } from '../../../../../hooks'; import { SelectedPolicyTab } from '../../components'; import { useSetupTechnology } from './setup_technology'; @@ -18,6 +18,7 @@ jest.mock('../../../../../services'); jest.mock('../../../../../hooks', () => ({ ...jest.requireActual('../../../../../hooks'), sendGetOneAgentPolicy: jest.fn(), + useStartServices: jest.fn(), })); type MockFn = jest.MockedFunction; @@ -41,6 +42,11 @@ describe('useSetupTechnology', () => { item: { id: 'agentless-policy-id' }, }, }); + (useStartServices as MockFn).mockReturnValue({ + cloud: { + isServerlessEnabled: true, + }, + }); jest.clearAllMocks(); }); @@ -78,6 +84,27 @@ describe('useSetupTechnology', () => { expect(result.current.agentlessPolicy).toEqual({ id: 'agentless-policy-id' }); }); + it('should not fetch agentless policy if agentless is enabled but serverless is disabled', async () => { + (useStartServices as MockFn).mockReturnValue({ + cloud: { + isServerlessEnabled: false, + }, + }); + + const { result } = renderHook(() => + useSetupTechnology({ + updateNewAgentPolicy: updateNewAgentPolicyMock, + newAgentPolicy: newAgentPolicyMock, + updateAgentPolicy: updateAgentPolicyMock, + setSelectedPolicyTab: setSelectedPolicyTabMock, + }) + ); + + expect(sendGetOneAgentPolicy).not.toHaveBeenCalled(); + expect(result.current.selectedSetupTechnology).toBe(SetupTechnology.AGENT_BASED); + expect(result.current.agentlessPolicy).toBeUndefined(); + }); + it('should update agent policy and selected policy tab when setup technology is agentless', async () => { const { result, waitForNextUpdate } = renderHook(() => useSetupTechnology({ diff --git a/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/create_package_policy_page/single_page_layout/hooks/setup_technology.ts b/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/create_package_policy_page/single_page_layout/hooks/setup_technology.ts index 51fd522157d8b..22972f4a9a6d4 100644 --- a/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/create_package_policy_page/single_page_layout/hooks/setup_technology.ts +++ b/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/create_package_policy_page/single_page_layout/hooks/setup_technology.ts @@ -10,7 +10,7 @@ import { useCallback, useEffect, useState } from 'react'; import { ExperimentalFeaturesService } from '../../../../../services'; import type { AgentPolicy, NewAgentPolicy } from '../../../../../types'; import { SetupTechnology } from '../../../../../types'; -import { sendGetOneAgentPolicy } from '../../../../../hooks'; +import { sendGetOneAgentPolicy, useStartServices } from '../../../../../hooks'; import { SelectedPolicyTab } from '../../components'; const AGENTLESS_POLICY_ID = 'agentless'; @@ -27,6 +27,8 @@ export function useSetupTechnology({ setSelectedPolicyTab: (tab: SelectedPolicyTab) => void; }) { const { agentless: isAgentlessEnabled } = ExperimentalFeaturesService.get(); + const { cloud } = useStartServices(); + const isServerless = cloud?.isServerlessEnabled ?? false; const [selectedSetupTechnology, setSelectedSetupTechnology] = useState( SetupTechnology.AGENT_BASED ); @@ -42,10 +44,10 @@ export function useSetupTechnology({ } }; - if (isAgentlessEnabled) { + if (isAgentlessEnabled && isServerless) { fetchAgentlessPolicy(); } - }, [isAgentlessEnabled]); + }, [isAgentlessEnabled, isServerless]); const handleSetupTechnologyChange = useCallback( (setupTechnology) => {