Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Added HTTP retries for Nautobot API #241

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
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
14 changes: 12 additions & 2 deletions network_importer/adapters/nautobot_api/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import warnings

import pynautobot
from diffsync.exceptions import ObjectAlreadyExists, ObjectNotFound
from packaging.version import Version, InvalidVersion
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry

from diffsync.exceptions import ObjectAlreadyExists, ObjectNotFound
import network_importer.config as config # pylint: disable=import-error
from network_importer.adapters.base import BaseAdapter # pylint: disable=import-error
from network_importer.adapters.nautobot_api.models import ( # pylint: disable=import-error
Expand All @@ -17,8 +19,8 @@
NautobotPrefix,
NautobotVlan,
)
from network_importer.adapters.nautobot_api.tasks import query_device_info_from_nautobot
from network_importer.adapters.nautobot_api.settings import InventorySettings, AdapterSettings
from network_importer.adapters.nautobot_api.tasks import query_device_info_from_nautobot

warnings.filterwarnings("ignore", category=DeprecationWarning)

Expand Down Expand Up @@ -104,6 +106,14 @@ def load(self):
else:
self.nautobot.http_session.verify = True

if inventory_settings.http_retries:
retries = Retry(total=inventory_settings.http_retries,
backoff_factor=0.5,
status_forcelist=[429, 500, 502, 503, 504, ],
allowed_methods=False
)
self.nautobot.http_session.mount(self.nautobot.base_url, HTTPAdapter(max_retries=retries))

self._check_nautobot_version()

sites = {}
Expand Down
17 changes: 14 additions & 3 deletions network_importer/adapters/nautobot_api/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@

import sys
from typing import Any, List
import pynautobot
from pydantic import ValidationError

import pynautobot
from nornir.core.inventory import Defaults, Groups, Hosts, Inventory, ParentGroups, ConnectionOptions
from nornir.core.plugins.inventory import InventoryPluginRegister
from pydantic import ValidationError
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry

from network_importer.adapters.nautobot_api.settings import InventorySettings
from network_importer.inventory import NetworkImporterInventory, NetworkImporterHost
from network_importer.utils import build_filter_params
from network_importer.adapters.nautobot_api.settings import InventorySettings


class NautobotAPIInventory(NetworkImporterInventory):
Expand Down Expand Up @@ -51,6 +54,14 @@ def __init__(self, *args, **kwargs: Any,) -> None:
if not self.settings.verify_ssl:
self.session.http_session.verify = False

if self.settings.http_retries:
retries = Retry(total=self.settings.http_retries,
backoff_factor=0.5,
status_forcelist=[429, 500, 502, 503, 504, ],
allowed_methods=False
)
self.session.http_session.mount(self.session.base_url, HTTPAdapter(max_retries=retries))

def load(self):
"""Load inventory by fetching devices from nautobot."""
if self.filter_parameters:
Expand Down
2 changes: 2 additions & 0 deletions network_importer/adapters/nautobot_api/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class InventorySettings(BaseSettings):
address: Optional[str] = "http://localhost"
token: Optional[str] = None
verify_ssl: Union[bool, str] = True
http_retries: Optional[int] = 0

use_primary_ip: Optional[bool] = True
fqdn: Optional[str] = None
Expand All @@ -30,4 +31,5 @@ class Config:
"address": {"env": "NAUTOBOT_ADDRESS"},
"token": {"env": "NAUTOBOT_TOKEN"},
"verify_ssl": {"env": "NAUTOBOT_VERIFY_SSL"},
"http_retries": {"env": "NAUTOBOT_HTTP_RETRY"},
}
10 changes: 10 additions & 0 deletions network_importer/adapters/nautobot_api/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

import pynautobot
from nornir.core.task import Result, Task
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry

import network_importer.config as config # pylint: disable=import-error
from network_importer.adapters.nautobot_api.settings import InventorySettings
Expand Down Expand Up @@ -34,6 +36,14 @@ def query_device_info_from_nautobot(task: Task) -> Result:
else:
nautobot.http_session.verify = True

if inventory_settings.http_retries:
retries = Retry(total=inventory_settings.http_retries,
backoff_factor=0.5,
status_forcelist=[429, 500, 502, 503, 504, ],
allowed_methods=False
)
nautobot.http_session.mount(nautobot.base_url, HTTPAdapter(max_retries=retries))

# Set a Results dictionary
results = {
"device": None,
Expand Down