From 1619b2ad64ee029b85b37409481fb3b98a2b82d8 Mon Sep 17 00:00:00 2001 From: Justin Jeffery <92743314+justinjeffery-ipf@users.noreply.github.com> Date: Wed, 9 Feb 2022 11:58:12 -0500 Subject: [PATCH] Case insensitive hostname searching (#76) * Case insensitive hostname searching using regex until IP Fabric implements 'ieq' and 'nieq' functions * Fix doc --- nautobot_chatops_ipfabric/ipfabric.py | 21 ++++++++++++++++----- nautobot_chatops_ipfabric/worker.py | 4 +--- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/nautobot_chatops_ipfabric/ipfabric.py b/nautobot_chatops_ipfabric/ipfabric.py index 28fa2df..d438180 100644 --- a/nautobot_chatops_ipfabric/ipfabric.py +++ b/nautobot_chatops_ipfabric/ipfabric.py @@ -13,6 +13,17 @@ logger = logging.getLogger("rq.worker") +def create_regex(string: str) -> str: + """Takes a string and returns a case insensitive regex.""" + regex = "^" + for i in string.upper(): + if i.isalpha(): + regex += f"[{i}{i.lower()}]" + else: + regex += i + return regex + "$" + + # pylint: disable=R0904 class IpFabric: """IpFabric will contain all the necessary API methods.""" @@ -97,7 +108,7 @@ def get_interfaces_load_info(self, device, snapshot_id=LAST, limit=DEFAULT_PAGE_ # columns and snapshot required payload = { "columns": ["intName", "inBytes", "outBytes"], - "filters": {"hostname": ["eq", device]}, + "filters": {"hostname": ["reg", create_regex(device)]}, "pagination": {"limit": limit, "start": 0}, "snapshot": snapshot_id, "sort": {"order": "desc", "column": "intName"}, @@ -226,7 +237,7 @@ def get_interfaces_errors_info(self, device, snapshot_id=LAST, limit=DEFAULT_PAG # columns and snapshot required payload = { "columns": ["intName", "errPktsPct", "errRate"], - "filters": {"hostname": ["eq", device]}, + "filters": {"hostname": ["reg", create_regex(device)]}, "pagination": {"limit": limit, "start": 0}, "snapshot": snapshot_id, "sort": {"order": "desc", "column": "intName"}, @@ -241,7 +252,7 @@ def get_interfaces_drops_info(self, device, snapshot_id=LAST, limit=DEFAULT_PAGE # columns and snapshot required payload = { "columns": ["intName", "dropsPktsPct", "dropsRate"], - "filters": {"hostname": ["eq", device]}, + "filters": {"hostname": ["reg", create_regex(device)]}, "pagination": {"limit": limit, "start": 0}, "snapshot": snapshot_id, "sort": {"order": "desc", "column": "intName"}, @@ -267,12 +278,12 @@ def get_bgp_neighbors(self, device, state, snapshot_id=LAST, limit=DEFAULT_PAGE_ "totalReceivedPrefixes", ], "snapshot": snapshot_id, - "filters": {"hostname": ["eq", device]}, + "filters": {"hostname": ["reg", create_regex(device)]}, "pagination": {"limit": limit, "start": 0}, } if state != "any": - payload["filters"] = {"and": [{"hostname": ["eq", device]}, {"state": ["eq", state]}]} + payload["filters"] = {"and": [{"hostname": ["reg", create_regex(device)]}, {"state": ["eq", state]}]} return self.get_response("/api/v1/tables/routing/protocols/bgp/neighbors", payload) def get_parsed_path_simulation( diff --git a/nautobot_chatops_ipfabric/worker.py b/nautobot_chatops_ipfabric/worker.py index 83077b4..dae6d15 100644 --- a/nautobot_chatops_ipfabric/worker.py +++ b/nautobot_chatops_ipfabric/worker.py @@ -546,9 +546,7 @@ def routing(dispatcher, device=None, protocol=None, filter_opt=None): """Get routing information for a device.""" snapshot_id = get_user_snapshot(dispatcher) logger.debug("Getting devices") - devices = [ - (device["hostname"], device["hostname"].lower()) for device in ipfabric_api.get_devices_info(snapshot_id) - ] + devices = [(device["hostname"], device["hostname"]) for device in ipfabric_api.get_devices_info(snapshot_id)] if not devices: dispatcher.send_blocks(