From 470400744377bf8e23bdd812e18fd252eeef0aff Mon Sep 17 00:00:00 2001
From: Julia Bardi <90178898+juliaElastic@users.noreply.github.com>
Date: Wed, 24 Jan 2024 09:15:36 +0100
Subject: [PATCH] [Fleet] allow clearing version combo (#175322)
## Summary
Closes https://github.com/elastic/kibana/issues/175294
Allow clearing upgrade version combo, added validation to highlight that
version is required.
Clearing is allowed with backspace or clear button.
Invalid state goes away when selecting a version from the list or typing
a custom version.
Single agent upgrade:
Bulk agent upgrade:
### Checklist
- [x] Any text added follows [EUI's writing
guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses
sentence case text and includes [i18n
support](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md)
- [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
---
.../agent_upgrade_modal/index.test.tsx | 32 +++++++++++++++
.../components/agent_upgrade_modal/index.tsx | 40 ++++++++++++++-----
2 files changed, 63 insertions(+), 9 deletions(-)
diff --git a/x-pack/plugins/fleet/public/applications/fleet/sections/agents/components/agent_upgrade_modal/index.test.tsx b/x-pack/plugins/fleet/public/applications/fleet/sections/agents/components/agent_upgrade_modal/index.test.tsx
index 3073420f4dc58..919a500477f0c 100644
--- a/x-pack/plugins/fleet/public/applications/fleet/sections/agents/components/agent_upgrade_modal/index.test.tsx
+++ b/x-pack/plugins/fleet/public/applications/fleet/sections/agents/components/agent_upgrade_modal/index.test.tsx
@@ -109,6 +109,38 @@ describe('AgentUpgradeAgentModal', () => {
});
});
+ it('should make combo invalid on clearing version', async () => {
+ const { utils } = renderAgentUpgradeAgentModal({
+ agents: [{ id: 'agent1', local_metadata: { host: 'abc' } }] as any,
+ agentCount: 1,
+ });
+
+ await waitFor(() => {
+ fireEvent.click(utils.getByTestId('comboBoxClearButton'));
+ const container = utils.getByTestId('agentUpgradeModal.VersionCombobox');
+ const input = within(container).getByRole('combobox');
+ expect(input?.value).toEqual('');
+ expect(utils.getByText('Version is required')).toBeInTheDocument();
+ expect(utils.getByTestId('confirmModalConfirmButton')).toBeDisabled();
+ });
+ });
+
+ it('should make combo invalid on clearing version - bulk action', async () => {
+ const { utils } = renderAgentUpgradeAgentModal({
+ agents: '*',
+ agentCount: 1,
+ });
+
+ await waitFor(() => {
+ fireEvent.click(utils.getByTestId('comboBoxClearButton'));
+ const container = utils.getByTestId('agentUpgradeModal.VersionCombobox');
+ const input = within(container).getByRole('combobox');
+ expect(input?.value).toEqual('');
+ expect(utils.getByText('Version is required')).toBeInTheDocument();
+ expect(utils.getByTestId('confirmModalConfirmButton')).toBeDisabled();
+ });
+ });
+
it('should display the custom version text input if no versions', async () => {
const { utils } = renderAgentUpgradeAgentModal({
agents: [
diff --git a/x-pack/plugins/fleet/public/applications/fleet/sections/agents/components/agent_upgrade_modal/index.tsx b/x-pack/plugins/fleet/public/applications/fleet/sections/agents/components/agent_upgrade_modal/index.tsx
index 57a23cbe412f8..e333efb152ea3 100644
--- a/x-pack/plugins/fleet/public/applications/fleet/sections/agents/components/agent_upgrade_modal/index.tsx
+++ b/x-pack/plugins/fleet/public/applications/fleet/sections/agents/components/agent_upgrade_modal/index.tsx
@@ -101,6 +101,8 @@ export const AgentUpgradeAgentModal: React.FunctionComponent('');
const QUERY_STUCK_UPDATING = `status:updating AND upgrade_started_at:* AND NOT upgraded_at:* AND upgrade_started_at < now-${AGENT_UPDATING_TIMEOUT_HOURS}h`;
+ const EMPTY_VALUE = useMemo(() => ({ label: '', value: '' }), []);
+ const [isInvalid, setIsInvalid] = useState(false);
useEffect(() => {
const getStuckUpdatingAgentCount = async (agentsOrQuery: Agent[] | string) => {
@@ -179,10 +181,10 @@ export const AgentUpgradeAgentModal: React.FunctionComponent> = MAINTENANCE_VALUES.map(
@@ -292,6 +294,7 @@ export const AgentUpgradeAgentModal: React.FunctionComponent
) : isSingleAgent ? (
+ selectedVersion[0].value &&
!isAgentUpgradeableToVersion(agents[0], selectedVersion[0].value) ? (
@@ -433,8 +440,10 @@ export const AgentUpgradeAgentModal: React.FunctionComponent
)}
@@ -443,6 +452,15 @@ export const AgentUpgradeAgentModal: React.FunctionComponent
+ ) : undefined
+ }
>
{noVersions ? (
>) => {
if (!selected.length) {
- return;
+ setSelectedVersion([EMPTY_VALUE]);
+ setIsInvalid(true);
+ } else {
+ setSelectedVersion(selected);
+ setIsInvalid(false);
}
- setSelectedVersion(selected);
}}
onCreateOption={
config?.internal?.onlyAllowAgentUpgradeToKnownVersions ? undefined : onCreateOption
}
customOptionText="Use custom agent version {searchValue} (not recommended)"
+ isInvalid={isInvalid}
/>
)}