forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Fleet] delete unenrolled agents task (elastic#195544)
Closes elastic#189506 Testing steps: - enable deleting unenrolled agents by adding `xpack.fleet.enableDeleteUnenrolledAgents: true` to `kibana.dev.yml` or turn it on on the UI - add some unenroll agents with the helper script ``` cd x-pack/plugins/fleet node scripts/create_agents/index.js --status unenrolled --count 10 info Creating 10 agents with statuses: info unenrolled: 10 info Batch complete, created 10 agent docs, took 0, errors: false info All batches complete. Created 10 agents in total. Goodbye! ``` - restart kibana or wait for the task to run and verify that the unenrolled agents were deleted ``` [2024-10-08T16:14:45.152+02:00][DEBUG][plugins.fleet.fleet:delete-unenrolled-agents-task:0.0.5] [DeleteUnenrolledAgentsTask] Executed deletion of 10 unenrolled agents [2024-10-08T16:14:45.153+02:00][INFO ][plugins.fleet.fleet:delete-unenrolled-agents-task:0.0.5] [DeleteUnenrolledAgentsTask] runTask ended: success ``` Added to UI settings: <img width="1057" alt="image" src="https://github.com/user-attachments/assets/2c9279f9-86a8-4630-a6cd-5aaa42e05fe7"> If the flag is preconfigured, disabled update on the UI with a tooltip: <img width="1009" alt="image" src="https://github.com/user-attachments/assets/45041020-6447-4295-995e-6848f0238f88"> The update is also prevented from the API: <img width="2522" alt="image" src="https://github.com/user-attachments/assets/cfbc8e21-e062-4e7f-9d08-9767fa387752"> Once the preconfiguration is removed, the UI update is allowed again. - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios --------- Co-authored-by: kibanamachine <[email protected]>
- Loading branch information
1 parent
29c591b
commit 643bcad
Showing
23 changed files
with
791 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
...public/applications/fleet/sections/settings/components/settings_page/advanced_section.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import React, { useCallback, useEffect } from 'react'; | ||
|
||
import { | ||
EuiTitle, | ||
EuiLink, | ||
EuiSpacer, | ||
EuiDescribedFormGroup, | ||
EuiSwitch, | ||
EuiForm, | ||
EuiFormRow, | ||
EuiToolTip, | ||
} from '@elastic/eui'; | ||
import { FormattedMessage } from '@kbn/i18n-react'; | ||
|
||
import { i18n } from '@kbn/i18n'; | ||
|
||
import { | ||
useAuthz, | ||
useGetSettings, | ||
usePutSettingsMutation, | ||
useStartServices, | ||
} from '../../../../hooks'; | ||
|
||
export const AdvancedSection: React.FunctionComponent<{}> = ({}) => { | ||
const authz = useAuthz(); | ||
const { docLinks, notifications } = useStartServices(); | ||
const deleteUnenrolledAgents = | ||
useGetSettings().data?.item?.delete_unenrolled_agents?.enabled ?? false; | ||
const isPreconfigured = | ||
useGetSettings().data?.item?.delete_unenrolled_agents?.is_preconfigured ?? false; | ||
const [deleteUnenrolledAgentsChecked, setDeleteUnenrolledAgentsChecked] = | ||
React.useState<boolean>(deleteUnenrolledAgents); | ||
const { mutateAsync: mutateSettingsAsync } = usePutSettingsMutation(); | ||
|
||
useEffect(() => { | ||
if (deleteUnenrolledAgents) { | ||
setDeleteUnenrolledAgentsChecked(deleteUnenrolledAgents); | ||
} | ||
}, [deleteUnenrolledAgents]); | ||
|
||
const updateSettings = useCallback( | ||
async (deleteFlag: boolean) => { | ||
try { | ||
setDeleteUnenrolledAgentsChecked(deleteFlag); | ||
const res = await mutateSettingsAsync({ | ||
delete_unenrolled_agents: { | ||
enabled: deleteFlag, | ||
is_preconfigured: false, | ||
}, | ||
}); | ||
|
||
if (res.error) { | ||
throw res.error; | ||
} | ||
} catch (error) { | ||
setDeleteUnenrolledAgentsChecked(!deleteFlag); | ||
notifications.toasts.addError(error, { | ||
title: i18n.translate('xpack.fleet.errorUpdatingSettings', { | ||
defaultMessage: 'Error updating settings', | ||
}), | ||
}); | ||
} | ||
}, | ||
[mutateSettingsAsync, notifications.toasts] | ||
); | ||
|
||
return ( | ||
<> | ||
<EuiTitle size="s"> | ||
<h4 data-test-subj="advancedHeader"> | ||
<FormattedMessage | ||
id="xpack.fleet.settings.advancedSectionTitle" | ||
defaultMessage="Advanced Settings" | ||
/> | ||
</h4> | ||
</EuiTitle> | ||
<EuiSpacer size="m" /> | ||
<EuiForm component="form"> | ||
<EuiDescribedFormGroup | ||
title={ | ||
<h3> | ||
<FormattedMessage | ||
id="xpack.fleet.settings.deleteUnenrolledAgentsLabel" | ||
defaultMessage="Delete unenrolled agents" | ||
/> | ||
</h3> | ||
} | ||
description={ | ||
<p> | ||
<FormattedMessage | ||
id="xpack.fleet.settings.advancedSection.switchLabel" | ||
defaultMessage="Switching on this setting will enable auto deletion of unenrolled agents. For more information see our {docLink}." | ||
values={{ | ||
docLink: ( | ||
<EuiLink target="_blank" external href={docLinks.links.fleet.settings}> | ||
<FormattedMessage | ||
id="xpack.fleet.settings.advancedSection.link" | ||
defaultMessage="docs" | ||
/> | ||
</EuiLink> | ||
), | ||
}} | ||
/> | ||
</p> | ||
} | ||
> | ||
<EuiFormRow label=""> | ||
<EuiToolTip | ||
content={ | ||
isPreconfigured | ||
? i18n.translate('xpack.fleet.settings.advancedSection.preconfiguredTitle', { | ||
defaultMessage: 'This setting is preconfigured and cannot be updated.', | ||
}) | ||
: undefined | ||
} | ||
> | ||
<EuiSwitch | ||
label={ | ||
<FormattedMessage | ||
id="xpack.fleet.settings.deleteUnenrolledAgentsLabel" | ||
defaultMessage="Delete unenrolled agents" | ||
/> | ||
} | ||
checked={deleteUnenrolledAgentsChecked} | ||
onChange={(e) => updateSettings(e.target.checked)} | ||
disabled={!authz.fleet.allSettings || isPreconfigured} | ||
/> | ||
</EuiToolTip> | ||
</EuiFormRow> | ||
</EuiDescribedFormGroup> | ||
</EuiForm> | ||
|
||
<EuiSpacer size="m" /> | ||
</> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.