[Fleet] Handle when an agent policy is removed from the current space (#202991)

This commit is contained in:
Nicolas Chaulet 2024-12-05 09:13:53 -05:00 committed by GitHub
parent fe56d6d90a
commit 5061174117
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 36 additions and 3 deletions

View file

@ -28,6 +28,7 @@ describe('Space aware policies creation', { testIsolation: false }, () => {
beforeEach(() => {
cy.intercept('GET', /\/api\/fleet\/agent_policies/).as('getAgentPolicies');
cy.intercept('PUT', /\/api\/fleet\/agent_policies\/.*/).as('putAgentPolicy');
cy.intercept('GET', /\/internal\/fleet\/agent_policies_spaces/).as('getAgentPoliciesSpaces');
});
@ -59,6 +60,7 @@ describe('Space aware policies creation', { testIsolation: false }, () => {
cy.getBySel(AGENT_POLICY_DETAILS_PAGE.SPACE_SELECTOR_COMBOBOX).click().type('default{enter}');
cy.getBySel(AGENT_POLICY_DETAILS_PAGE.SAVE_BUTTON).click();
cy.wait('@putAgentPolicy');
});
it('the policy should be visible in the test space', () => {
@ -72,4 +74,22 @@ describe('Space aware policies creation', { testIsolation: false }, () => {
cy.wait('@getAgentPolicies');
cy.getBySel(AGENT_POLICIES_TABLE).contains(POLICY_NAME);
});
it('should redirect to the agent policies list when removing the current space from a policy', () => {
cy.visit('/s/test/app/fleet/policies');
cy.getBySel(AGENT_POLICIES_TABLE).contains(POLICY_NAME).click();
cy.getBySel(AGENT_POLICY_DETAILS_PAGE.SETTINGS_TAB).click();
cy.wait('@getAgentPoliciesSpaces');
cy.get('[title="Remove Test from selection in this group"]').click();
cy.getBySel(AGENT_POLICY_DETAILS_PAGE.SAVE_BUTTON).click();
cy.wait('@putAgentPolicy');
cy.wait('@getAgentPolicies');
cy.location('pathname').should('eq', '/s/test/app/fleet/policies');
cy.getBySel(AGENT_POLICIES_TABLE).contains(NO_AGENT_POLICIES);
});
});

View file

@ -18,9 +18,10 @@ import {
} from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import { FormattedMessage } from '@kbn/i18n-react';
import { DEFAULT_SPACE_ID } from '@kbn/spaces-plugin/common';
import { useHistory } from 'react-router-dom';
import { useSpaceSettingsContext } from '../../../../../../../hooks/use_space_settings_context';
import type { AgentPolicy } from '../../../../../types';
import {
useStartServices,
@ -30,6 +31,8 @@ import {
sendGetAgentStatus,
useAgentPolicyRefresh,
useBreadcrumbs,
useFleetStatus,
useLink,
} from '../../../../../hooks';
import {
AgentPolicyForm,
@ -79,14 +82,17 @@ export const SettingsView = memo<{ agentPolicy: AgentPolicy }>(
({ agentPolicy: originalAgentPolicy }) => {
useBreadcrumbs('policy_details', { policyName: originalAgentPolicy.name });
const { notifications } = useStartServices();
const { spaceId } = useFleetStatus();
const {
agents: { enabled: isFleetEnabled },
} = useConfig();
const { getPath } = useLink();
const hasAllAgentPoliciesPrivileges = useAuthz().fleet.allAgentPolicies;
const refreshAgentPolicy = useAgentPolicyRefresh();
const [agentPolicy, setAgentPolicy] = useState<AgentPolicy>({
...originalAgentPolicy,
});
const history = useHistory();
const spaceSettings = useSpaceSettingsContext();
const [isLoading, setIsLoading] = useState<boolean>(false);
@ -121,8 +127,15 @@ export const SettingsView = memo<{ agentPolicy: AgentPolicy }>(
values: { name: agentPolicy.name },
})
);
refreshAgentPolicy();
setHasChanges(false);
if (
agentPolicy.space_ids &&
!agentPolicy.space_ids.includes(spaceId ?? DEFAULT_SPACE_ID)
) {
history.replace(getPath('policies_list'));
} else {
refreshAgentPolicy();
setHasChanges(false);
}
} else {
notifications.toasts.addDanger(
error