Skip to content
This repository has been archived by the owner on Sep 7, 2023. It is now read-only.

Commit

Permalink
Case insensitive hostname searching (#76)
Browse files Browse the repository at this point in the history
* Case insensitive hostname searching using regex until IP Fabric implements 'ieq' and 'nieq' functions

* Fix doc
  • Loading branch information
Justin Jeffery authored Feb 9, 2022
1 parent be6ed28 commit 1619b2a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
21 changes: 16 additions & 5 deletions nautobot_chatops_ipfabric/ipfabric.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down Expand Up @@ -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"},
Expand Down Expand Up @@ -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"},
Expand All @@ -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"},
Expand All @@ -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(
Expand Down
4 changes: 1 addition & 3 deletions nautobot_chatops_ipfabric/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 1619b2a

Please sign in to comment.