Skip to content
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
23 changes: 20 additions & 3 deletions cosmo/clients/netbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,32 @@ def __init__(self, url, token):
base_version_match = re.search(r"[\d.]+", version)
self.base_version = Version(base_version_match.group(0))

if self.base_version > Version("4.2.0"):
if self.base_version > Version("4.3.0"):
log.info("Using version 4.3.x strategy...")
self.child_client = NetboxV4Strategy(
url,
token,
multiple_mac_addresses=True,
netbox_43_query_syntax=True,
feature_flags=feature_flags,
)
elif self.base_version > Version("4.2.0"):
log.info("Using version 4.2.x strategy...")
self.child_client = NetboxV4Strategy(
url, token, multiple_mac_addresses=True, feature_flags=feature_flags
url,
token,
multiple_mac_addresses=True,
netbox_43_query_syntax=False,
feature_flags=feature_flags,
)
elif self.base_version > Version("4.0.0"):
log.info("Using version 4.0.x strategy...")
self.child_client = NetboxV4Strategy(
url, token, multiple_mac_addresses=False, feature_flags=feature_flags
url,
token,
multiple_mac_addresses=False,
netbox_43_query_syntax=False,
feature_flags=feature_flags,
)
else:
raise Exception("Unknown Version")
Expand Down
26 changes: 22 additions & 4 deletions cosmo/clients/netbox_v4.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,20 @@ def _merge_into(self, data: dict, query_result):


class ConnectedDevicesDataQuery(ParallelQuery):
def __init__(self, *args, netbox_43_query_syntax=False, **kwargs):
super().__init__(*args, **kwargs)
self.netbox_43_query_syntax = netbox_43_query_syntax

def _fetch_data(self, kwargs, pool):
tag_filter = (
'tags: { name: { exact: "bgp_cpe" }}'
if self.netbox_43_query_syntax
else 'tags: "bgp_cpe"'
)
query_template = Template(
"""
query {
interface_list(filters: { tag: "bgp_cpe" }) {
interface_list(filters: { $tag_filter }) {
__typename
id,
parent {
Expand Down Expand Up @@ -71,7 +80,9 @@ def _fetch_data(self, kwargs, pool):
"""
)

return self.client.query(query_template.substitute())["data"]
return self.client.query(query_template.substitute(tag_filter=tag_filter))[
"data"
]

def _merge_into(self, data: dict, query_data):

Expand Down Expand Up @@ -628,10 +639,13 @@ def _merge_into(self, data: dict, query_data):

class NetboxV4Strategy:

def __init__(self, url, token, multiple_mac_addresses, feature_flags):
def __init__(
self, url, token, multiple_mac_addresses, netbox_43_query_syntax, feature_flags
):
self.url = url
self.token = token
self.multiple_mac_addresses = multiple_mac_addresses
self.netbox_43_query_syntax = netbox_43_query_syntax
self.feature_flags = feature_flags

def get_data(self, device_config):
Expand Down Expand Up @@ -673,7 +687,11 @@ def get_data(self, device_config):
else StaticRouteDummyQuery(client, device_list=device_list)
),
DeviceMACQuery(client, device_list=device_list),
ConnectedDevicesDataQuery(client, device_list=device_list),
ConnectedDevicesDataQuery(
client,
device_list=device_list,
netbox_43_query_syntax=self.netbox_43_query_syntax,
),
LoopbackDataQuery(client, device_list=device_list),
(
IPPoolDataQuery(client, device_list=device_list)
Expand Down