Skip to content

Commit

Permalink
NetBox 3.7 VPN API Changes (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
arthanson authored Jan 25, 2024
1 parent bb4ecd3 commit 3f83f61
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 73 deletions.
3 changes: 0 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ repos:
- id: python-no-log-warn
- id: python-no-eval
- id: python-use-type-annotations
- id: rst-backticks
- id: rst-directive-colons
- id: rst-inline-touching-normal

- repo: https://github.com/mgedmin/check-manifest
rev: "0.47"
Expand Down
3 changes: 2 additions & 1 deletion netbox_python/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"""Top-level package for NetBox Python."""
from importlib.metadata import version

__author__ = """Arthur Hanson"""
__email__ = "[email protected]"
__version__ = "0.1.0"
__version__ = version(__name__)

from netbox_python.exceptions import NetBoxException
from netbox_python.netbox import NetBoxClient
Expand Down
2 changes: 1 addition & 1 deletion netbox_python/api/circuits.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class _circuits(APIResource):
path = "circuits/circuits/"

class _provider_networks(APIResource):
path = "circuits/provider_networks/"
path = "circuits/provider-networks/"

class _providers(APIResource):
path = "circuits/providers/"
6 changes: 5 additions & 1 deletion netbox_python/api/dcim.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
from netbox_python.baseapi import APIResource, CreateableAPIResource, ElevationAPIResource
from netbox_python.baseapi import (
APIResource,
CreateableAPIResource,
ElevationAPIResource,
)
from netbox_python.rest import Result


Expand Down
9 changes: 1 addition & 8 deletions netbox_python/api/ipam.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@ def __init__(self, client):
self.ip_addresses = self._ip_addresses(client)
self.ip_ranges = self._ip_ranges(client)
self.ip_range = ip_range(client)
self.l2vpn_terminations = self._l2vpn_terminations(client)
self.l2vpns = self._l2vpns(client)
self.prefixes = self._prefixes(client)
self.rirs = self._rirs(client)
self.roles = self._roles(client)
self.route_targets = self._route_targets(client)
self.service_templates = self._service_templates(client)
self.services = self._services(client)
self.vlan_groups = self._vlan_groups(client)
self.vlan_group = vlan_group(client)
self.vlans = self._vlans(client)
self.vrfs = self._vrfs(client)
super().__init__()
Expand All @@ -50,12 +49,6 @@ class _ip_addresses(APIResource):
class _ip_ranges(APIResource):
path = "ipam/ip-ranges/"

class _l2vpn_terminations(APIResource):
path = "ipam/l2vpn-terminations/"

class _l2vpns(APIResource):
path = "ipam/l2vpns/"

class _prefixes(APIResource):
path = "ipam/prefixes/"

Expand Down
47 changes: 47 additions & 0 deletions netbox_python/api/vpn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from netbox_python.baseapi import APIResource


class vpn:
def __init__(self, client):
self.ike_policies = self._ike_policies(client)
self.ike_proposals = self._ike_proposals(client)
self.ipsec_policies = self._ipsec_policies(client)
self.ipsec_proposals = self._ipsec_proposals(client)
self.ipsec_profiles = self._ipsec_profiles(client)
self.tunnel_groups = self._tunnel_groups(client)
self.tunnels = self._tunnels(client)
self.tunnel_terminations = self._tunnel_terminations(client)
self.l2vpns = self._l2vpns(client)
self.l2vpn_terminations = self._l2vpn_terminations(client)

super().__init__()

class _ike_policies(APIResource):
path = "vpn/ike-policies/"

class _ike_proposals(APIResource):
path = "vpn/ike-proposals/"

class _ipsec_policies(APIResource):
path = "vpn/ipsec-policies/"

class _ipsec_proposals(APIResource):
path = "vpn/ipsec-proposals/"

class _ipsec_profiles(APIResource):
path = "vpn/ipsec-profiles/"

class _tunnel_groups(APIResource):
path = "vpn/tunnel-groups/"

class _tunnels(APIResource):
path = "vpn/tunnels/"

class _tunnel_terminations(APIResource):
path = "vpn/tunnel-terminations/"

class _l2vpns(APIResource):
path = "vpn/l2vpns/"

class _l2vpn_terminations(APIResource):
path = "vpn/l2vpn-terminations/"
34 changes: 15 additions & 19 deletions netbox_python/baseapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ def paginate(self, result: Result) -> Result:
next_token = result.pagination["next"]
yield result
while next_token:
result = self.client.get(
self.path, url_override=next_token, params=result.params
)
result = self.client.get(self.path, url_override=next_token, params=result.params)
yield result
next_token = result.pagination["next"]

Expand Down Expand Up @@ -97,31 +95,29 @@ class ROAPIResource(
pass


class AvailableAPIResource(
baseapi,
CreateableAPIResource,
ListableAPIResource,
):
def create(self, id: str | int, *args, **kwargs) -> Result:
class CreateablePathAPIResource:
def create(self, *args, **kwargs) -> Result:
path = self.path.format(id=id)
return self._create(path, *args, **kwargs)

def list(self, id: str | int, **kwargs) -> Result:

class ListablePathAPIResource:
def list(self, **kwargs) -> Result:
path = self.path.format(id=id)
return self._list(path, **kwargs)

def all(self, id: str | int, **kwargs):
def all(self, **kwargs):
path = self.path.format(id=id)
return self._all(path, **kwargs)

class ElevationAPIResource(

class AvailableAPIResource(
baseapi,
ListableAPIResource
CreateablePathAPIResource,
ListablePathAPIResource,
):
def list(self, id: str | int, **kwargs) -> Result:
path = self.path.format(id=id)
return self._list(path, **kwargs)
pass

def all(self, id: str | int, **kwargs):
path = self.path.format(id=id)
return self._all(path, **kwargs)

class ElevationAPIResource(baseapi, ListablePathAPIResource):
pass
6 changes: 3 additions & 3 deletions netbox_python/netbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from netbox_python.api.tenancy import tenancy
from netbox_python.api.users import users
from netbox_python.api.virtualization import virtualization
from netbox_python.api.vpn import vpn
from netbox_python.api.wireless import wireless
from netbox_python.baseapi import RetrievableRootAPIResource, baseapi
from netbox_python.rest import RestClient
Expand All @@ -30,9 +31,7 @@ def __init__(self, base_url: str, token: str, headers: Dict[str, str] = None):
if token:
headers["authorization"] = f"Token {token}"

url = base_url = "{}/api".format(
base_url if base_url[-1] != "/" else base_url[:-1]
)
url = base_url = "{}/api".format(base_url if base_url[-1] != "/" else base_url[:-1])

self.circuits = circuits(self)
self.core = core(self)
Expand All @@ -43,6 +42,7 @@ def __init__(self, base_url: str, token: str, headers: Dict[str, str] = None):
self.tenancy = tenancy(self)
self.users = users(self)
self.virtualization = virtualization(self)
self.vpn = vpn(self)
self.wireless = wireless(self)

super().__init__(base_url=url, headers=headers)
Expand Down
6 changes: 2 additions & 4 deletions netbox_python/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@ def request(self, method: str, path: str, **kwargs) -> Result:
response = self._session.request(method=method, url=url, **kwargs)
response.raise_for_status()
except requests.HTTPError as http_error:
raise NetBoxException(
f"Invalid request from {self.base_url}: {http_error}"
) from http_error
raise NetBoxException(f"Invalid request from {self.base_url}: {http_error}") from http_error
except requests.RequestException as err:
raise NetBoxException("Request failed") from err

Expand All @@ -73,7 +71,7 @@ def request(self, method: str, path: str, **kwargs) -> Result:

# If status_code in 200-299 range, return success Result with data, otherwise raise exception
is_success = 299 >= response.status_code >= 200 # 200 to 299 is OK
no_content_success = response.status_code == 204 # 204 is OK. Means no content
no_content_success = response.status_code == 204 # 204 is OK. Means no content
if is_success:
# check if list - fixme: should have cleaner way to do this
pagination = None
Expand Down
17 changes: 7 additions & 10 deletions requirements_dev.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
pip==23.0.1
bump2version==1.0.1
wheel==0.38.4
watchdog==2.3.0
flake8==6.0.0
tox==4.4.6
coverage==7.2.1
twine==4.0.2

pytest==7.2.1
black==23.1.0
flake8==6.0.0
pip==23.0.1
pre-commit==3.1.0

pytest==7.2.*
twine==4.0.2
watchdog==2.3.0
wheel==0.38.4
23 changes: 0 additions & 23 deletions tests/test_netbox_python.py

This file was deleted.

0 comments on commit 3f83f61

Please sign in to comment.