Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def get_netbox_inventory(self, device_id, tag):
try:
items = nb.dcim.inventory_items.filter(
device_id=device_id,
tag=tag
)
except pynetbox.core.query.RequestError:
logging.info('Tag {tag} is missing, returning empty array.'.format(tag=tag))
items = []
return items
endpoint = getattr(getattr(self._api, nb_app), nb_type)
collection = []
try:
if "filters" in args:
seen_id = []
for filter_args in args['filters']:
for elt in endpoint.filter(**filter_args):
# Add the element to collection only if it is not
# already in
if elt.id not in seen_id:
seen_id.append(elt.id)
collection.append(dict(elt))
else:
collection = [dict(e) for e in endpoint.all()]
except pynetbox.core.query.RequestError as err:
raise YaaniError(
"An error occured while requesting Netbox: {}\n"
.format(str(err))
)
except requests.exceptions.ConnectionError as err:
raise YaaniError(
"An error occured while requesting Netbox: {}\n"
.format(str(err))
)
return collection