Skip to content

Commit

Permalink
[Fleet] Handle when an agent policy is removed from the current space (
Browse files Browse the repository at this point in the history
  • Loading branch information
nchaulet authored Dec 5, 2024
1 parent fe56d6d commit 5061174
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
20 changes: 20 additions & 0 deletions x-pack/plugins/fleet/cypress/e2e/space_awareness/policies.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});

Expand Down Expand Up @@ -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', () => {
Expand All @@ -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);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -30,6 +31,8 @@ import {
sendGetAgentStatus,
useAgentPolicyRefresh,
useBreadcrumbs,
useFleetStatus,
useLink,
} from '../../../../../hooks';
import {
AgentPolicyForm,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 5061174

Please sign in to comment.