Skip to content
This repository was archived by the owner on Nov 28, 2024. It is now read-only.

CNV-39781: search by mac address #98

Merged
merged 1 commit into from
Mar 25, 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
1 change: 1 addition & 0 deletions locales/en/plugin__nmstate-console-plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
"Scheduling will not be possible at this state": "Scheduling will not be possible at this state",
"Search": "Search",
"Search by IP address...": "Search by IP address...",
"Search by MAC address...": "Search by MAC address...",
"selector key": "selector key",
"selector value": "selector value",
"Server": "Server",
Expand Down
9 changes: 3 additions & 6 deletions src/views/states/list/components/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { InterfaceType, NodeNetworkConfigurationInterface } from '@types';
import { isEmpty } from '@utils/helpers';

import { FILTER_TYPES, LLDP_ENABLED } from '../constants';
import { searchInterfaceByIP } from '../utilts';
import { searchInterfaceByIP, searchInterfaceByMAC } from '../utilts';

export const interfaceFilters: Record<
string,
Expand All @@ -20,17 +20,14 @@ export const interfaceFilters: Record<
if (isEmpty(selectedInput)) return true;
return selectedInput.some((ipType) => !!obj[ipType]);
},
[FILTER_TYPES.IP_ADDRESS]: (selectedInput, obj) => {
const searchIPAddress = selectedInput?.[0];

return searchInterfaceByIP(searchIPAddress, obj);
},
[FILTER_TYPES.IP_ADDRESS]: (selectedInput, obj) => searchInterfaceByIP(selectedInput?.[0], obj),
[FILTER_TYPES.LLDP]: (selectedInput, obj) => {
if (isEmpty(selectedInput)) return true;
return selectedInput.some((status) =>
status === LLDP_ENABLED ? Boolean(obj?.lldp?.enabled) : !obj?.lldp?.enabled,
);
},
[FILTER_TYPES.MAC_ADDRESS]: (selectedInput, obj) => searchInterfaceByMAC(selectedInput?.[0], obj),
} as const;

export const filterInterfaces = (
Expand Down
1 change: 1 addition & 0 deletions src/views/states/list/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { getResourceUrl } from '@utils/helpers';
export const baseListUrl = getResourceUrl({ model: NodeNetworkStateModel });

export const FILTER_TYPES = {
MAC_ADDRESS: 'mac-address',
INTERFACE_STATE: 'interface-state',
INTERFACE_TYPE: 'interface-type',
IP_FILTER: 'ip-filter',
Expand Down
16 changes: 15 additions & 1 deletion src/views/states/list/hooks/useStateFilters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { InterfaceType, NodeNetworkConfigurationInterface, V1beta1NodeNetworkSta
import { isEmpty } from '@utils/helpers';

import { FILTER_TYPES, LLDP_DISABLED, LLDP_ENABLED } from '../constants';
import { searchInterfaceByIP } from '../utilts';
import { searchInterfaceByIP, searchInterfaceByMAC } from '../utilts';

export const useStateSearchFilters = (): RowSearchFilter<V1beta1NodeNetworkState>[] => {
const { t } = useNMStateTranslation();
Expand All @@ -24,6 +24,20 @@ export const useStateSearchFilters = (): RowSearchFilter<V1beta1NodeNetworkState
filterGroupName: t('IP address'),
placeholder: t('Search by IP address...'),
},
{
type: FILTER_TYPES.MAC_ADDRESS,
filter: (searchText, obj) => {
const searchMACAddress = searchText?.selected?.[0];
if (!searchMACAddress) return true;

const interfaces = obj?.status?.currentState
?.interfaces as NodeNetworkConfigurationInterface[];

return interfaces?.some((iface) => searchInterfaceByMAC(searchMACAddress, iface));
},
filterGroupName: t('MAC address'),
placeholder: t('Search by MAC address...'),
},
];
};

Expand Down
12 changes: 11 additions & 1 deletion src/views/states/list/utilts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { NodeNetworkConfigurationInterface } from '@types';
import { isEmpty } from '@utils/helpers';
import { getIPV4Address, getIPV6Address } from '@utils/interfaces/getters';

const decimalToBinary = (decimalNumber: number) => (decimalNumber >>> 0).toString(2);
Expand Down Expand Up @@ -37,7 +38,7 @@ export const searchInterfaceByIP = (
searchIPAddress: string,
iface: NodeNetworkConfigurationInterface,
) => {
if (!searchIPAddress) return true;
if (isEmpty(searchIPAddress)) return true;

const isSearchByIpv4 = searchIPAddress.includes('.');

Expand All @@ -57,3 +58,12 @@ export const searchInterfaceByIP = (

return addresses?.some((address) => address?.toLowerCase().includes(searchIPAddress));
};

export const searchInterfaceByMAC = (
searchMACAddress: string,
iface: NodeNetworkConfigurationInterface,
) => {
if (isEmpty(searchMACAddress)) return true;

return iface?.['mac-address']?.includes(searchMACAddress) || false;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use FILTER_TYPES.MAC_ADDRESS constant
and the || false can be omitted

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i thought here to return false instead of undefined if there is no mac-address

the FILTER_TYPES.MAC_ADDRESS does not have anything to do with the mac-address property. It's the filter id :D

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you got the first if, which will return true when searchMACAddress is undefined or empty, btw I would use isEmpty there, since this is covered in the first if, the || false should be redundant, please check

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the || false is if the interface has no mac address. It's not covered by the first if. It's a different obj.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@avivtur fixed the first if using isEmpty

};
Loading