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] refresh policies on create and flyout open #125499

Merged
merged 3 commits into from
Feb 14, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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, { useEffect, useState } from 'react';
import React, { useEffect, useState, useMemo } from 'react';
import {
EuiFlyout,
EuiFlyoutBody,
Expand All @@ -22,7 +22,12 @@ import {
} from '@elastic/eui';
import { FormattedMessage } from '@kbn/i18n-react';

import { useGetSettings, sendGetOneAgentPolicy, useFleetStatus } from '../../hooks';
import {
useGetSettings,
sendGetOneAgentPolicy,
useFleetStatus,
useGetAgentPolicies,
} from '../../hooks';
import { FLEET_SERVER_PACKAGE } from '../../constants';
import type { PackagePolicy } from '../../types';

Expand All @@ -47,7 +52,6 @@ export * from './steps';
export const AgentEnrollmentFlyout: React.FunctionComponent<Props> = ({
onClose,
agentPolicy,
agentPolicies,
viewDataStep,
defaultMode = 'managed',
}) => {
Expand All @@ -60,6 +64,24 @@ export const AgentEnrollmentFlyout: React.FunctionComponent<Props> = ({
const [policyId, setSelectedPolicyId] = useState(agentPolicy?.id);
const [isFleetServerPolicySelected, setIsFleetServerPolicySelected] = useState<boolean>(false);

// loading the latest agentPolicies for add agent flyout
const {
data: agentPoliciesData,
isLoading: isLoadingAgentPolicies,
resendRequest: refreshAgentPolicies,
} = useGetAgentPolicies({
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I decided to load/refresh agent policies here in AgentEnrollmentFlyout.
The reason is that AgentEnrollmentFlyout is being called from 7 places, from which 4 are used without agentPolicies, 3 with agentPolicies. It would be too much duplication to reload agentPolicies from all those caller sites.
AgentEnrollmentFlyout Props should be cleaned up so that agentPolicies is not passed as a prop.

Copy link
Member

Choose a reason for hiding this comment

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

What do you think of creating a hook like useAgentEnrollmentFlyoutData that contains this and the useMemo below, to start separating more business and presentation in this component?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm going to create a hook in a next pr, also clean up some unused props

page: 1,
perPage: 1000,
full: true,
});

const agentPolicies = useMemo(() => {
if (!isLoadingAgentPolicies) {
return agentPoliciesData?.items;
}
return [];
}, [isLoadingAgentPolicies, agentPoliciesData?.items]);

useEffect(() => {
async function checkPolicyIsFleetServer() {
if (policyId && setIsFleetServerPolicySelected) {
Expand Down Expand Up @@ -143,9 +165,14 @@ export const AgentEnrollmentFlyout: React.FunctionComponent<Props> = ({
agentPolicies={agentPolicies}
viewDataStep={viewDataStep}
isFleetServerPolicySelected={isFleetServerPolicySelected}
refreshAgentPolicies={refreshAgentPolicies}
/>
) : (
<StandaloneInstructions agentPolicy={agentPolicy} agentPolicies={agentPolicies} />
<StandaloneInstructions
agentPolicy={agentPolicy}
agentPolicies={agentPolicies}
refreshAgentPolicies={refreshAgentPolicies}
/>
)}
</EuiFlyoutBody>
<EuiFlyoutFooter>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,7 @@ import type { EuiContainedStepProps } from '@elastic/eui/src/components/steps/st
import { i18n } from '@kbn/i18n';
import { FormattedMessage } from '@kbn/i18n-react';

import {
useGetOneEnrollmentAPIKey,
useLink,
useFleetStatus,
useGetAgents,
useGetAgentPolicies,
} from '../../hooks';
import { useGetOneEnrollmentAPIKey, useLink, useFleetStatus, useGetAgents } from '../../hooks';

import { ManualInstructions } from '../../components/enrollment_instructions';
import {
Expand All @@ -34,9 +28,7 @@ import { policyHasFleetServer } from '../../applications/fleet/sections/agents/s
import { FLEET_SERVER_PACKAGE } from '../../constants';

import { DownloadStep, AgentPolicySelectionStep, AgentEnrollmentKeySelectionStep } from './steps';
import type { BaseProps } from './types';

type Props = BaseProps;
import type { InstructionProps } from './types';

const DefaultMissingRequirements = () => {
const { getHref } = useLink();
Expand Down Expand Up @@ -65,14 +57,15 @@ const FleetServerMissingRequirements = () => {
return <FleetServerRequirementPage />;
};

export const ManagedInstructions = React.memo<Props>(
export const ManagedInstructions = React.memo<InstructionProps>(
({
agentPolicy,
agentPolicies,
viewDataStep,
setSelectedPolicyId,
isFleetServerPolicySelected,
settings,
refreshAgentPolicies,
}) => {
const fleetStatus = useFleetStatus();

Expand All @@ -87,24 +80,15 @@ export const ManagedInstructions = React.memo<Props>(
showInactive: false,
});

const { data: agentPoliciesData, isLoading: isLoadingAgentPolicies } = useGetAgentPolicies({
page: 1,
perPage: 1000,
full: true,
});

const fleetServers = useMemo(() => {
let policies = agentPolicies;
if (!agentPolicies && !isLoadingAgentPolicies) {
policies = agentPoliciesData?.items;
}
const policies = agentPolicies;
const fleetServerAgentPolicies: string[] = (policies ?? [])
.filter((pol) => policyHasFleetServer(pol))
.map((pol) => pol.id);
return (agents?.items ?? []).filter((agent) =>
fleetServerAgentPolicies.includes(agent.policy_id ?? '')
);
}, [agents, agentPolicies, agentPoliciesData, isLoadingAgentPolicies]);
}, [agents, agentPolicies]);

const fleetServerSteps = useMemo(() => {
const {
Expand Down Expand Up @@ -137,6 +121,7 @@ export const ManagedInstructions = React.memo<Props>(
setSelectedAPIKeyId,
setSelectedPolicyId,
excludeFleetServer: true,
refreshAgentPolicies,
})
: AgentEnrollmentKeySelectionStep({ agentPolicy, selectedApiKeyId, setSelectedAPIKeyId }),
DownloadStep(isFleetServerPolicySelected || false),
Expand Down Expand Up @@ -165,6 +150,7 @@ export const ManagedInstructions = React.memo<Props>(
setSelectedPolicyId,
setSelectedAPIKeyId,
agentPolicies,
refreshAgentPolicies,
apiKey.data,
fleetServerSteps,
isFleetServerPolicySelected,
Expand Down
Loading