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

Commit

Permalink
CNV-39781: search by mac address
Browse files Browse the repository at this point in the history
  • Loading branch information
upalatucci committed Mar 25, 2024
1 parent b2b2563 commit 5af7dab
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 8 deletions.
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;
};

0 comments on commit 5af7dab

Please sign in to comment.