Skip to content

Commit

Permalink
[8.x] [Fleet] Fail gracefully on agent count retrieval (#200590) (#20…
Browse files Browse the repository at this point in the history
…0641)

# Backport

This will backport the following commits from `main` to `8.x`:
- [[Fleet] Fail gracefully on agent count retrieval
(#200590)](#200590)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Nicolas
Chaulet","email":"nicolas.chaulet@elastic.co"},"sourceCommit":{"committedDate":"2024-11-18T19:15:00Z","message":"[Fleet]
Fail gracefully on agent count retrieval
(#200590)","sha":"e9881a7add765f532ab500b7d74f9c988727d2b0","branchLabelMapping":{"^v9.0.0$":"main","^v8.17.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","Team:Fleet","v9.0.0","backport:prev-minor"],"title":"[Fleet]
Fail gracefully on agent count
retrieval","number":200590,"url":"https://github.com/elastic/kibana/pull/200590","mergeCommit":{"message":"[Fleet]
Fail gracefully on agent count retrieval
(#200590)","sha":"e9881a7add765f532ab500b7d74f9c988727d2b0"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/200590","number":200590,"mergeCommit":{"message":"[Fleet]
Fail gracefully on agent count retrieval
(#200590)","sha":"e9881a7add765f532ab500b7d74f9c988727d2b0"}}]}]
BACKPORT-->

Co-authored-by: Nicolas Chaulet <nicolas.chaulet@elastic.co>
  • Loading branch information
kibanamachine and nchaulet authored Nov 18, 2024
1 parent 31144f5 commit fe0ca23
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 fe0ca23

Please sign in to comment.