From 5044575a6d44a544d0c8a5d2ac200fec140fde9e Mon Sep 17 00:00:00 2001 From: Nicolas Chaulet Date: Mon, 18 Nov 2024 14:15:00 -0500 Subject: [PATCH] [Fleet] Fail gracefully on agent count retrieval (#200590) --- .../download_source_flyout/confirm_update.tsx | 86 +++++++++++-------- .../use_delete_download_source.tsx | 80 +++++++++-------- .../edit_output_flyout/confirm_update.tsx | 83 +++++++++++------- .../settings/hooks/use_delete_output.tsx | 84 ++++++++++-------- 4 files changed, 197 insertions(+), 136 deletions(-) diff --git a/x-pack/plugins/fleet/public/applications/fleet/sections/settings/components/download_source_flyout/confirm_update.tsx b/x-pack/plugins/fleet/public/applications/fleet/sections/settings/components/download_source_flyout/confirm_update.tsx index cdc2e00736333..90a5d91407339 100644 --- a/x-pack/plugins/fleet/public/applications/fleet/sections/settings/components/download_source_flyout/confirm_update.tsx +++ b/x-pack/plugins/fleet/public/applications/fleet/sections/settings/components/download_source_flyout/confirm_update.tsx @@ -15,52 +15,68 @@ import { getCountsForDownloadSource } from './services/get_count'; interface ConfirmDescriptionProps { downloadSource: DownloadSource; - agentCount: number; - agentPolicyCount: number; + agentCount?: number; + agentPolicyCount?: number; } const ConfirmDescription: React.FunctionComponent = ({ downloadSource, agentCount, agentPolicyCount, -}) => ( - {downloadSource.name}, - agents: ( - - - - ), - policies: ( - - - - ), - }} - /> -); +}) => + agentCount !== undefined && agentPolicyCount !== undefined ? ( + {downloadSource.name}, + agents: ( + + + + ), + policies: ( + + + + ), + }} + /> + ) : ( + {downloadSource.name}, + }} + /> + ); export async function confirmUpdate( downloadSource: DownloadSource, confirm: ReturnType['confirm'] ) { - const { agentCount, agentPolicyCount } = await getCountsForDownloadSource(downloadSource); + const { agentCount, agentPolicyCount } = await getCountsForDownloadSource(downloadSource).catch( + () => ({ + // Fail gracefully when counts are not avaiable + agentCount: undefined, + agentPolicyCount: undefined, + }) + ); return confirm( ( interface ConfirmDeleteDescriptionProps { downloadSource: DownloadSource; - agentCount: number; - agentPolicyCount: number; + agentCount?: number; + agentPolicyCount?: number; } const ConfirmDeleteDescription: React.FunctionComponent = ({ downloadSource, agentCount, agentPolicyCount, -}) => ( - {downloadSource.name}, - agents: ( - - - - ), - policies: ( - - - - ), - }} - /> -); +}) => + agentCount !== undefined && agentPolicyCount !== undefined ? ( + {downloadSource.name}, + agents: ( + + + + ), + policies: ( + + + + ), + }} + /> + ) : ( + {downloadSource.name}, + }} + /> + ); export function useDeleteDownloadSource(onSuccess: () => void) { const { confirm } = useConfirmModal(); @@ -71,7 +80,10 @@ export function useDeleteDownloadSource(onSuccess: () => void) { const deleteDownloadSource = useCallback( async (downloadSource: DownloadSource) => { try { - const { agentCount, agentPolicyCount } = await getCountsForDownloadSource(downloadSource); + const { agentCount, agentPolicyCount } = await getCountsForDownloadSource( + downloadSource + // Fail gracefully when counts are not available + ).catch(() => ({ agentCount: undefined, agentPolicyCount: undefined })); const isConfirmed = await confirm( , diff --git a/x-pack/plugins/fleet/public/applications/fleet/sections/settings/components/edit_output_flyout/confirm_update.tsx b/x-pack/plugins/fleet/public/applications/fleet/sections/settings/components/edit_output_flyout/confirm_update.tsx index 4157c6964f79a..0adc4635194d1 100644 --- a/x-pack/plugins/fleet/public/applications/fleet/sections/settings/components/edit_output_flyout/confirm_update.tsx +++ b/x-pack/plugins/fleet/public/applications/fleet/sections/settings/components/edit_output_flyout/confirm_update.tsx @@ -22,8 +22,8 @@ const ConfirmTitle = () => ( interface ConfirmDescriptionProps { output: Output; - agentCount: number; - agentPolicyCount: number; + agentCount?: number; + agentPolicyCount?: number; } const ConfirmDescription: React.FunctionComponent = ({ @@ -32,36 +32,47 @@ const ConfirmDescription: React.FunctionComponent = ({ agentPolicyCount, }) => ( <> - {output.name}, - agents: ( - - - - ), - policies: ( - - - - ), - }} - /> + {agentCount !== undefined && agentPolicyCount !== undefined ? ( + {output.name}, + agents: ( + + + + ), + policies: ( + + + + ), + }} + /> + ) : ( + {output.name}, + }} + /> + )} {output.type === 'logstash' ? ( <> @@ -91,7 +102,13 @@ export async function confirmUpdate( output: Output, confirm: ReturnType['confirm'] ) { - const { agentCount, agentPolicyCount } = await getAgentAndPolicyCountForOutput(output); + const { agentCount, agentPolicyCount } = + // Fail gracefully if not agent and policy count not available + await getAgentAndPolicyCountForOutput(output).catch(() => ({ + agentCount: undefined, + agentPolicyCount: undefined, + })); + return confirm( , ( interface ConfirmDescriptionProps { output: Output; - agentCount: number; - agentPolicyCount: number; + agentCount?: number; + agentPolicyCount?: number; } const ConfirmDescription: React.FunctionComponent = ({ output, agentCount, agentPolicyCount, -}) => ( - {output.name}, - agents: ( - - - - ), - policies: ( - - - - ), - }} - /> -); +}) => + agentCount !== undefined && agentPolicyCount !== undefined ? ( + {output.name}, + agents: ( + + + + ), + policies: ( + + + + ), + }} + /> + ) : ( + {output.name}, + }} + /> + ); export function useDeleteOutput(onSuccess: () => void) { const { confirm } = useConfirmModal(); const { notifications } = useStartServices(); + const deleteOutput = useCallback( async (output: Output) => { try { - const { agentCount, agentPolicyCount } = await getAgentAndPolicyCountForOutput(output); + const { agentCount, agentPolicyCount } = await getAgentAndPolicyCountForOutput( + output + ).catch(() => ({ + // Fail gracefully if count are not available + agentCount: undefined, + agentPolicyCount: undefined, + })); const isConfirmed = await confirm( ,