Skip to content

Commit

Permalink
Wizard: Properly sort arguments required by OpenSCAP
Browse files Browse the repository at this point in the history
This updates the way the arguments are sorted by whether they're required by a selected OpenSCAP profile.
  • Loading branch information
regexowl committed Jan 14, 2025
1 parent b9e60ce commit 74217f1
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ import {
import { PlusCircleIcon, TimesIcon } from '@patternfly/react-icons';

import { useAppDispatch, useAppSelector } from '../../../../../store/hooks';
import { useGetOscapCustomizationsQuery } from '../../../../../store/imageBuilderApi';
import {
addKernelArg,
removeKernelArg,
selectComplianceProfileID,
selectDistribution,
selectKernel,
} from '../../../../../store/wizardSlice';
import { isKernelArgumentValid } from '../../../validators';
Expand All @@ -25,9 +28,30 @@ const KernelArguments = () => {
const dispatch = useAppDispatch();
const kernelAppend = useAppSelector(selectKernel).append;

const release = useAppSelector(selectDistribution);
const complianceProfileID = useAppSelector(selectComplianceProfileID);

const { data: oscapProfileInfo } = useGetOscapCustomizationsQuery(
{
distribution: release,
// @ts-ignore if complianceProfileID is undefined the query is going to get skipped, so it's safe here to ignore the linter here
profile: complianceProfileID,
},
{
skip: !complianceProfileID,
}
);

const [inputValue, setInputValue] = useState('');
const [errorText, setErrorText] = useState('');

const requiredByOpenSCAP = kernelAppend.filter((arg) =>
oscapProfileInfo?.kernel?.append?.split(' ').includes(arg.name)
);
const notRequiredByOpenSCAP = kernelAppend.filter(
(arg) => !oscapProfileInfo?.kernel?.append?.split(' ').includes(arg.name)
);

const onTextInputChange = (
_event: React.FormEvent<HTMLInputElement>,
value: string
Expand All @@ -43,7 +67,7 @@ const KernelArguments = () => {
isKernelArgumentValid(value) &&
!kernelAppend.some((arg) => arg.name === value)
) {
dispatch(addKernelArg({ name: value, isRequiredByOpenSCAP: false }));
dispatch(addKernelArg({ name: value }));
setInputValue('');
setErrorText('');
}
Expand All @@ -59,7 +83,7 @@ const KernelArguments = () => {
};

const handleAddItem = (e: React.MouseEvent, value: string) => {
dispatch(addKernelArg({ name: value, isRequiredByOpenSCAP: false }));
dispatch(addKernelArg({ name: value }));
setInputValue('');
};
return (
Expand Down Expand Up @@ -95,36 +119,31 @@ const KernelArguments = () => {
<HelperTextItem variant={'error'}>{errorText}</HelperTextItem>
</HelperText>
)}
{kernelAppend.some((arg) => arg.isRequiredByOpenSCAP) && (
<ChipGroup
categoryName="Required by OpenSCAP"
numChips={20}
className="pf-v5-u-mt-sm pf-v5-u-w-100"
>
{kernelAppend
.filter((arg) => arg.isRequiredByOpenSCAP)
.map((arg) => (
<Chip
key={arg.name}
onClick={() => dispatch(removeKernelArg(arg.name))}
isReadOnly
>
{arg.name}
</Chip>
))}
</ChipGroup>
)}
<ChipGroup
categoryName="Required by OpenSCAP"
numChips={20}
className="pf-v5-u-mt-sm pf-v5-u-w-100"
>
{requiredByOpenSCAP.map((arg) => (
<Chip
key={arg.name}
onClick={() => dispatch(removeKernelArg(arg.name))}
isReadOnly
>
{arg.name}
</Chip>
))}
</ChipGroup>

<ChipGroup numChips={20} className="pf-v5-u-mt-sm pf-v5-u-w-100">
{kernelAppend
.filter((arg) => !arg.isRequiredByOpenSCAP)
.map((arg) => (
<Chip
key={arg.name}
onClick={() => dispatch(removeKernelArg(arg.name))}
>
{arg.name}
</Chip>
))}
{notRequiredByOpenSCAP.map((arg) => (
<Chip
key={arg.name}
onClick={() => dispatch(removeKernelArg(arg.name))}
>
{arg.name}
</Chip>
))}
</ChipGroup>
</FormGroup>
);
Expand Down
1 change: 0 additions & 1 deletion src/Components/CreateImageWizard/steps/Oscap/Oscap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,6 @@ const ProfileSelector = () => {
dispatch(
addKernelArg({
name: kernelArgsArray[arg],
isRequiredByOpenSCAP: true,
})
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ function commonRequestToState(
append:
request.customizations?.kernel?.append
?.split(' ')
.map((arg) => ({ name: arg, isRequiredByOpenSCAP: false })) || [],
.map((arg) => ({ name: arg })) || [],
},
timezone: {
timezone: request.customizations.timezone?.timezone || '',
Expand Down
15 changes: 4 additions & 11 deletions src/store/wizardSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,8 @@ type UserSshKeyPayload = {
sshKey: string;
};

export type KernelAppendWithAdditionalInfo = {
export type KernelArgument = {
name: string;
isRequiredByOpenSCAP: boolean;
};

export type wizardState = {
Expand Down Expand Up @@ -132,7 +131,7 @@ export type wizardState = {
disabled: string[];
};
kernel: {
append: KernelAppendWithAdditionalInfo[];
append: KernelArgument[];
};
locale: Locale;
details: {
Expand Down Expand Up @@ -795,10 +794,7 @@ export const wizardSlice = createSlice({
changeDisabledServices: (state, action: PayloadAction<string[]>) => {
state.services.disabled = action.payload;
},
addKernelArg: (
state,
action: PayloadAction<KernelAppendWithAdditionalInfo>
) => {
addKernelArg: (state, action: PayloadAction<KernelArgument>) => {
const existingArgIndex = state.kernel.append.findIndex(
(arg) => arg.name === action.payload.name
);
Expand All @@ -809,10 +805,7 @@ export const wizardSlice = createSlice({
state.kernel.append.push(action.payload);
}
},
removeKernelArg: (
state,
action: PayloadAction<KernelAppendWithAdditionalInfo['name']>
) => {
removeKernelArg: (state, action: PayloadAction<KernelArgument['name']>) => {
state.kernel.append.splice(
state.kernel.append.findIndex((arg) => arg.name === action.payload),
1
Expand Down

0 comments on commit 74217f1

Please sign in to comment.