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] Fail gracefully on agent count retrieval #200590

Merged
merged 4 commits into from
Nov 18, 2024
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 @@ -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?"
Copy link
Contributor

Choose a reason for hiding this comment

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

I think that repeating the will update part twice sounds a bit strange here. Could we change to something like this instead?
This action will update {downloadSourceName} agent binary source, 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 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. It will update related policies and agents. This action can not be undone. Are you sure you wish to continue?"
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd suggest:
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?

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks, this sounds better I will made the change

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