Skip to content

Commit

Permalink
[Fleet] Fail gracefully on agent count retrieval (elastic#200590)
Browse files Browse the repository at this point in the history
  • Loading branch information
nchaulet authored Nov 18, 2024
1 parent 2c0825f commit e9881a7
Show file tree
Hide file tree
Showing 4 changed files with 197 additions and 136 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<ConfirmDescriptionProps> = ({
downloadSource,
agentCount,
agentPolicyCount,
}) => (
<FormattedMessage
id="xpack.fleet.settings.updateDownloadSourceModal.confirmModalText"
data-test-subj="editDownloadSourcesConfirmModal.confirmModalText"
defaultMessage="This action will update {downloadSourceName} agent binary source. It will update {policies} and {agents}. This action can not be undone. Are you sure you wish to continue?"
values={{
downloadSourceName: <strong>{downloadSource.name}</strong>,
agents: (
<strong>
<FormattedMessage
id="xpack.fleet.settings.updateDownloadSourceModal.agentsCount"
defaultMessage="{agentCount, plural, one {# agent} other {# agents}}"
values={{
agentCount,
}}
/>
</strong>
),
policies: (
<strong>
<FormattedMessage
id="xpack.fleet.settings.updateDownloadSourceModal.agentPolicyCount"
defaultMessage="{agentPolicyCount, plural, one {# agent policy} other {# agent policies}}"
values={{
agentPolicyCount,
}}
/>
</strong>
),
}}
/>
);
}) =>
agentCount !== undefined && agentPolicyCount !== undefined ? (
<FormattedMessage
id="xpack.fleet.settings.updateDownloadSourceModal.confirmModalText"
data-test-subj="editDownloadSourcesConfirmModal.confirmModalText"
defaultMessage="This action will update {downloadSourceName} agent binary source. It will update {policies} and {agents}. This action can not be undone. Are you sure you wish to continue?"
values={{
downloadSourceName: <strong>{downloadSource.name}</strong>,
agents: (
<strong>
<FormattedMessage
id="xpack.fleet.settings.updateDownloadSourceModal.agentsCount"
defaultMessage="{agentCount, plural, one {# agent} other {# agents}}"
values={{
agentCount,
}}
/>
</strong>
),
policies: (
<strong>
<FormattedMessage
id="xpack.fleet.settings.updateDownloadSourceModal.agentPolicyCount"
defaultMessage="{agentPolicyCount, plural, one {# agent policy} other {# agent policies}}"
values={{
agentPolicyCount,
}}
/>
</strong>
),
}}
/>
) : (
<FormattedMessage
id="xpack.fleet.settings.updateDownloadSourceModal.confirmModalTextWithoutCount"
data-test-subj="editDownloadSourcesConfirmModal.confirmModalText"
defaultMessage="This action will update {downloadSourceName} agent binary source. It will update related policies and agents. This action can not be undone. Are you sure you wish to continue?"
values={{
downloadSourceName: <strong>{downloadSource.name}</strong>,
}}
/>
);

export async function confirmUpdate(
downloadSource: DownloadSource,
confirm: ReturnType<typeof useConfirmModal>['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(
<FormattedMessage
id="xpack.fleet.settings.updateDownloadSourceModal.confirmModalTitle"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,53 +25,65 @@ const ConfirmTitle = () => (

interface ConfirmDeleteDescriptionProps {
downloadSource: DownloadSource;
agentCount: number;
agentPolicyCount: number;
agentCount?: number;
agentPolicyCount?: number;
}

const ConfirmDeleteDescription: React.FunctionComponent<ConfirmDeleteDescriptionProps> = ({
downloadSource,
agentCount,
agentPolicyCount,
}) => (
<FormattedMessage
id="xpack.fleet.settings.deleteDowloadSource.confirmModalText"
defaultMessage="This action will delete {downloadSourceName} agent binary source. It will update {policies} and {agents}. This action can not be undone. Are you sure you wish to continue?"
values={{
downloadSourceName: <strong>{downloadSource.name}</strong>,
agents: (
<strong>
<FormattedMessage
id="xpack.fleet.settings.deleteDowloadSource.agentsCount"
defaultMessage="{agentCount, plural, one {# agent} other {# agents}}"
values={{
agentCount,
}}
/>
</strong>
),
policies: (
<strong>
<FormattedMessage
id="xpack.fleet.settings.deleteDowloadSource.agentPolicyCount"
defaultMessage="{agentPolicyCount, plural, one {# agent policy} other {# agent policies}}"
values={{
agentPolicyCount,
}}
/>
</strong>
),
}}
/>
);
}) =>
agentCount !== undefined && agentPolicyCount !== undefined ? (
<FormattedMessage
id="xpack.fleet.settings.deleteDowloadSource.confirmModalText"
defaultMessage="This action will delete {downloadSourceName} agent binary source. It will update {policies} and {agents}. This action can not be undone. Are you sure you wish to continue?"
values={{
downloadSourceName: <strong>{downloadSource.name}</strong>,
agents: (
<strong>
<FormattedMessage
id="xpack.fleet.settings.deleteDowloadSource.agentsCount"
defaultMessage="{agentCount, plural, one {# agent} other {# agents}}"
values={{
agentCount,
}}
/>
</strong>
),
policies: (
<strong>
<FormattedMessage
id="xpack.fleet.settings.deleteDowloadSource.agentPolicyCount"
defaultMessage="{agentPolicyCount, plural, one {# agent policy} other {# agent policies}}"
values={{
agentPolicyCount,
}}
/>
</strong>
),
}}
/>
) : (
<FormattedMessage
id="xpack.fleet.settings.deleteDowloadSource.confirmModalTextWithoutCount"
defaultMessage="This action will delete {downloadSourceName} agent binary source and it will update its related policies and agents. This action can not be undone. Are you sure you wish to continue?"
values={{
downloadSourceName: <strong>{downloadSource.name}</strong>,
}}
/>
);

export function useDeleteDownloadSource(onSuccess: () => void) {
const { confirm } = useConfirmModal();
const { notifications } = useStartServices();
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(
<ConfirmTitle />,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ const ConfirmTitle = () => (

interface ConfirmDescriptionProps {
output: Output;
agentCount: number;
agentPolicyCount: number;
agentCount?: number;
agentPolicyCount?: number;
}

const ConfirmDescription: React.FunctionComponent<ConfirmDescriptionProps> = ({
Expand All @@ -32,36 +32,47 @@ const ConfirmDescription: React.FunctionComponent<ConfirmDescriptionProps> = ({
agentPolicyCount,
}) => (
<>
<FormattedMessage
data-test-subj="settings.outputModal"
id="xpack.fleet.settings.updateOutput.confirmModalText"
defaultMessage="This action will update {outputName} output. It will update {policies} and {agents}. This action can not be undone. Are you sure you wish to continue?"
values={{
outputName: <strong>{output.name}</strong>,
agents: (
<strong>
<FormattedMessage
id="xpack.fleet.settings.updateOutput.agentsCount"
defaultMessage="{agentCount, plural, one {# agent} other {# agents}}"
values={{
agentCount,
}}
/>
</strong>
),
policies: (
<strong>
<FormattedMessage
id="xpack.fleet.settings.updateOutput.agentPolicyCount"
defaultMessage="{agentPolicyCount, plural, one {# agent policy} other {# agent policies}}"
values={{
agentPolicyCount,
}}
/>
</strong>
),
}}
/>
{agentCount !== undefined && agentPolicyCount !== undefined ? (
<FormattedMessage
data-test-subj="settings.outputModal"
id="xpack.fleet.settings.updateOutput.confirmModalText"
defaultMessage="This action will update {outputName} output. It will update {policies} and {agents}. This action can not be undone. Are you sure you wish to continue?"
values={{
outputName: <strong>{output.name}</strong>,
agents: (
<strong>
<FormattedMessage
id="xpack.fleet.settings.updateOutput.agentsCount"
defaultMessage="{agentCount, plural, one {# agent} other {# agents}}"
values={{
agentCount,
}}
/>
</strong>
),
policies: (
<strong>
<FormattedMessage
id="xpack.fleet.settings.updateOutput.agentPolicyCount"
defaultMessage="{agentPolicyCount, plural, one {# agent policy} other {# agent policies}}"
values={{
agentPolicyCount,
}}
/>
</strong>
),
}}
/>
) : (
<FormattedMessage
data-test-subj="settings.outputModal"
id="xpack.fleet.settings.updateOutput.confirmModalTextWithoutCount"
defaultMessage="This action will update {outputName} output. It will update related policies and agents. This action can not be undone. Are you sure you wish to continue?"
values={{
outputName: <strong>{output.name}</strong>,
}}
/>
)}

{output.type === 'logstash' ? (
<>
Expand Down Expand Up @@ -91,7 +102,13 @@ export async function confirmUpdate(
output: Output,
confirm: ReturnType<typeof useConfirmModal>['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(
<ConfirmTitle />,
<ConfirmDescription
Expand Down
Loading

0 comments on commit e9881a7

Please sign in to comment.