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

Automember > 'Host group rules' - Action buttons #630

Merged
merged 7 commits into from
Feb 12, 2025
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
164 changes: 159 additions & 5 deletions src/pages/AutoMemHostRules/AutoMemHostRules.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from "react";
// PatternFly
import {
Button,
Page,
PageSection,
PageSectionVariants,
Expand Down Expand Up @@ -34,7 +35,11 @@ import useApiError from "src/hooks/useApiError";
import GlobalErrors from "src/components/errors/GlobalErrors";
// RPC
import { GenericPayload } from "src/services/rpc";
import { useSearchHostGroupRulesEntriesMutation } from "src/services/rpcAutomember";
import {
useSearchHostGroupRulesEntriesMutation,
ChangeDefaultPayload,
useChangeDefaultGroupMutation,
} from "src/services/rpcAutomember";
// Hooks
import { useAlerts } from "src/hooks/useAlerts";
import useUpdateRoute from "src/hooks/useUpdateRoute";
Expand All @@ -48,6 +53,10 @@ import {
// Errors
import { FetchBaseQueryError } from "@reduxjs/toolkit/dist/query";
import { SerializedError } from "@reduxjs/toolkit";
// Modals
import AddRule from "src/components/modals/Automember/AddRule";
import DeleteRule from "src/components/modals/Automember/DeleteRule";
import ConfirmationModal from "src/components/modals/ConfirmationModal";

// Automembership host group rules
const AutoMemHostRules = () => {
Expand Down Expand Up @@ -80,6 +89,12 @@ const AutoMemHostRules = () => {
const [hostGroupsOptions, setHostGroupsOptions] = React.useState<
SelectOptionProps[]
>([]);
// Available elements ready to add
const [groupsAvailableToAdd, setGroupsAvailableToAdd] = React.useState<
string[]
>([]);
const [previousDefaultGroup, setPreviousDefaultGroup] =
React.useState<string>(NO_SELECTION);

// Alerts to show in the UI
const alerts = useAlerts();
Expand All @@ -100,6 +115,7 @@ const AutoMemHostRules = () => {

// API calls via custom hook
const hostGroupRulesData = useHostGroupsRulesData();
const [changeDefaultGroup] = useChangeDefaultGroupMutation();

// Show table rows
const [showTableRows, setShowTableRows] = React.useState(
Expand Down Expand Up @@ -134,12 +150,23 @@ const AutoMemHostRules = () => {
setHostGroups(hostGroupRulesData.hostGroups);
setAutomemberRules(shownPaginatedRulesList);
// If no default group is set, set it as 'No selection'
if (defaultGroup === "" || hostGroupRulesData.defaultGroup === "") {
if (hostGroupRulesData.defaultGroup === "") {
setDefaultGroup(NO_SELECTION);
setPreviousDefaultGroup(NO_SELECTION);
} else {
setDefaultGroup(hostGroupRulesData.defaultGroup);
setPreviousDefaultGroup(hostGroupRulesData.defaultGroup);
}

// Set available host groups to add
const allAutomemberIds = fullAutomemberIds.map(
(item) => item.automemberRule
);
const availableItems = hostGroupRulesData.hostGroups.filter(
(item) => !allAutomemberIds.includes(item)
);
setGroupsAvailableToAdd(availableItems);

// Set table count
setTotalCount(totalAutomembersCount);
// Show table elements
Expand Down Expand Up @@ -173,6 +200,32 @@ const AutoMemHostRules = () => {
}
}, [hostGroups]);

// On select default group
const onSelectDefaultGroup = (group: string) => {
const payload: ChangeDefaultPayload = {
defaultGroup: group,
type: "hostgroup",
};
changeDefaultGroup(payload).then((result) => {
if ("data" in result) {
setDefaultGroup(group);
setPreviousDefaultGroup(group);
alerts.addAlert(
"default-group-success",
"Default group updated",
"success"
);
onCloseConfirmationModal();
} else {
alerts.addAlert(
"default-group-failure",
"Default group not updated",
"danger"
);
}
});
};

// If some entries' status has been updated, unselect selected rows
const [isDisableEnableOp, setIsDisableEnableOp] = React.useState(false);

Expand Down Expand Up @@ -384,6 +437,61 @@ const AutoMemHostRules = () => {
updateIsDisableEnableOp,
};

// Modals functionality
const [showAddModal, setShowAddModal] = React.useState(false);
const [showDeleteModal, setShowDeleteModal] = React.useState(false);
const [showChangeConfirmationModal, setShowChangeConfirmationModal] =
React.useState(false);

const onOpenAddModal = () => {
setShowAddModal(true);
};

const onCloseAddModal = () => {
setShowAddModal(false);
};

const onAddModalToggle = () => {
setShowAddModal(!showAddModal);
};

const onOpenDeleteModal = () => {
setShowDeleteModal(true);
};

const onToggleDeleteModal = () => {
setShowDeleteModal(!showDeleteModal);
};

const onCloseConfirmationModal = () => {
setShowChangeConfirmationModal(false);
};

const onOpenConfirmationModal = () => {
setShowChangeConfirmationModal(true);
};

const onShowDefaultGroupOnModal = (group: string) => {
setDefaultGroup(group);
onOpenConfirmationModal();
};

const onCancelDefaultGroup = () => {
setDefaultGroup(previousDefaultGroup);
onCloseConfirmationModal();
};

// 'Delete automember rules data
const deleteButtonsData = {
updateIsDeleteButtonDisabled,
updateIsDeletion,
};

const selectedData = {
selectedItems: selectedAutomembers,
clearSelected: clearSelectedRules,
};

// List of Toolbar items
const toolbarItems: ToolbarItem[] = [
{
Expand Down Expand Up @@ -420,7 +528,7 @@ const AutoMemHostRules = () => {
options={hostGroupsOptions}
selected={defaultGroup}
placeholder="Default host group"
onSelectedChange={setDefaultGroup}
onSelectedChange={onShowDefaultGroupOnModal}
/>
),
},
Expand All @@ -442,15 +550,23 @@ const AutoMemHostRules = () => {
{
key: 5,
element: (
<SecondaryButton isDisabled={isDeleteButtonDisabled || !showTableRows}>
<SecondaryButton
isDisabled={isDeleteButtonDisabled || !showTableRows}
onClickHandler={onOpenDeleteModal}
>
Delete
</SecondaryButton>
),
},
{
key: 6,
element: (
<SecondaryButton isDisabled={!showTableRows}>Add</SecondaryButton>
<SecondaryButton
isDisabled={!showTableRows}
onClickHandler={onOpenAddModal}
>
Add
</SecondaryButton>
),
},
{
Expand Down Expand Up @@ -521,6 +637,44 @@ const AutoMemHostRules = () => {
className="pf-v5-u-pb-0 pf-v5-u-pr-md"
/>
</PageSection>
<AddRule
show={showAddModal}
handleModalToggle={onAddModalToggle}
onOpenAddModal={onOpenAddModal}
onCloseAddModal={onCloseAddModal}
onRefresh={refreshData}
groupsAvailableToAdd={groupsAvailableToAdd}
ruleType="hostgroup"
/>
<DeleteRule
show={showDeleteModal}
handleModalToggle={onToggleDeleteModal}
onRefresh={refreshData}
buttonsData={deleteButtonsData}
selectedData={selectedData}
ruleType="hostgroup"
/>
<ConfirmationModal
title="Default hostgroup"
isOpen={showChangeConfirmationModal}
onClose={onCloseConfirmationModal}
actions={[
<Button
variant="primary"
key="change-default"
onClick={() => {
onSelectDefaultGroup(defaultGroup);
}}
>
OK
</Button>,
<SecondaryButton key="cancel" onClickHandler={onCancelDefaultGroup}>
Cancel
</SecondaryButton>,
]}
messageText="Are you sure you want to change default group?"
messageObj={defaultGroup}
/>
</Page>
);
};
Expand Down
85 changes: 85 additions & 0 deletions tests/features/automem_hostgroup.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
Feature: Automember > Host group rules
Create and delete automember host group rules. Change default host group rule.

Background:
Given I am logged in as "Administrator"
Given I am on "host-group-rules" page

# Prep: Create a new host group to work with
Scenario: Create a new host group to work with
Given I am on "host-groups" page
When I click on "Add" button
* I type in the field "Group name" text "my_automember_hostgroup"
When in the modal dialog I click on "Add" button
* I should see "success" alert with text "New host group added"
When I close the alert
Then I should see "my_automember_hostgroup" entry in the data table

# Host group rules operations
Scenario: Add a new automember host group rule
Given I am on "host-group-rules" page
When I click on "Add" button
* I click in the "Automember" selector field
* I select "my_automember_hostgroup" option in the "Automember" selector
* in the modal dialog I click on "Add" button
* I should see "success" alert with text "Entry successfully added"
When I close the alert
Then I should see "my_automember_hostgroup" entry in the data table

Scenario: Add another automember host group rule
Given I am on "host-group-rules" page
When I click on "Add" button
* I click in the "Automember" selector field
* I select "ipaservers" option in the "Automember" selector
* in the modal dialog I click on "Add" button
* I should see "success" alert with text "Entry successfully added"
When I close the alert
Then I should see "ipaservers" entry in the data table

Scenario: Delete an automember host group rule
Given I am on "host-group-rules" page
Given I should see "my_automember_hostgroup" entry in the data table
Then I select entry "my_automember_hostgroup" in the data table
When I click on "Delete" button
* I see "Remove auto membership rules" modal
* I should see "my_automember_hostgroup" entry in the data table
When in the modal dialog I click on "Delete" button
* I should see "success" alert with text "The selected rules have been removed successfully"
When I close the alert
Then I should not see "my_automember_hostgroup" entry in the data table

Scenario: Delete another automember host group rule
Given I am on "host-group-rules" page
Given I should see "ipaservers" entry in the data table
Then I select entry "ipaservers" in the data table
When I click on "Delete" button
* I see "Remove auto membership rules" modal
* I should see "ipaservers" entry in the data table
When in the modal dialog I click on "Delete" button
* I should see "success" alert with text "The selected rules have been removed successfully"
When I close the alert
Then I should not see "ipaservers" entry in the data table

Scenario: Set default host group rule
Given I am on "host-group-rules" page
When I click in the typeahead selector field with ID "typeahead-select-input"
* I click toolbar dropdown item "ipaservers"
Then I see "Default hostgroup" modal
When I click on the "OK" button located in the footer modal dialog
Then I should see "success" alert with text "Default group updated"
When I close the alert
* in the selector with ID "typeahead-select-input" I should see option "ipaservers" selected

# Cleanup: Delete the host group
Scenario: Delete the host group
Given I am on "host-groups" page
Given I should see "my_automember_hostgroup" entry in the data table
Then I select entry "my_automember_hostgroup" in the data table
When I click on "Delete" button
* I see "Remove host groups" modal
* I should see "my_automember_hostgroup" entry in the data table
When in the modal dialog I click on "Delete" button
* I should see "success" alert with text "Host groups removed"
When I close the alert
Then I should not see "my_automember_hostgroup" entry in the data table

Loading
Loading