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

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

Merged
merged 1 commit into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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
Loading