From 5af7dab5a03a7835fbedd2e2cbbf82d4f9d11949 Mon Sep 17 00:00:00 2001 From: Ugo Palatucci Date: Fri, 22 Mar 2024 12:21:59 +0100 Subject: [PATCH] CNV-39781: search by mac address --- locales/en/plugin__nmstate-console-plugin.json | 1 + src/views/states/list/components/utils.ts | 9 +++------ src/views/states/list/constants.ts | 1 + src/views/states/list/hooks/useStateFilters.ts | 16 +++++++++++++++- src/views/states/list/utilts.ts | 12 +++++++++++- 5 files changed, 31 insertions(+), 8 deletions(-) diff --git a/locales/en/plugin__nmstate-console-plugin.json b/locales/en/plugin__nmstate-console-plugin.json index bb1163f0..0fe17786 100644 --- a/locales/en/plugin__nmstate-console-plugin.json +++ b/locales/en/plugin__nmstate-console-plugin.json @@ -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", diff --git a/src/views/states/list/components/utils.ts b/src/views/states/list/components/utils.ts index 7ee22681..4e4c4df0 100644 --- a/src/views/states/list/components/utils.ts +++ b/src/views/states/list/components/utils.ts @@ -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, @@ -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 = ( diff --git a/src/views/states/list/constants.ts b/src/views/states/list/constants.ts index 299ec834..9404ac5c 100644 --- a/src/views/states/list/constants.ts +++ b/src/views/states/list/constants.ts @@ -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', diff --git a/src/views/states/list/hooks/useStateFilters.ts b/src/views/states/list/hooks/useStateFilters.ts index 168e6381..65f5edea 100644 --- a/src/views/states/list/hooks/useStateFilters.ts +++ b/src/views/states/list/hooks/useStateFilters.ts @@ -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[] => { const { t } = useNMStateTranslation(); @@ -24,6 +24,20 @@ export const useStateSearchFilters = (): RowSearchFilter { + 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...'), + }, ]; }; diff --git a/src/views/states/list/utilts.ts b/src/views/states/list/utilts.ts index dc68ce00..cbbca6af 100644 --- a/src/views/states/list/utilts.ts +++ b/src/views/states/list/utilts.ts @@ -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); @@ -37,7 +38,7 @@ export const searchInterfaceByIP = ( searchIPAddress: string, iface: NodeNetworkConfigurationInterface, ) => { - if (!searchIPAddress) return true; + if (isEmpty(searchIPAddress)) return true; const isSearchByIpv4 = searchIPAddress.includes('.'); @@ -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; +};