Skip to content

Commit a512f9b

Browse files
committed
Add PR review + fix linting tests
1 parent e96a7af commit a512f9b

File tree

3 files changed

+31
-15
lines changed

3 files changed

+31
-15
lines changed

examples/05-nautobot-peeringdb/adapter_nautobot.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
"""Diffsync adapter class for Nautobot."""
2+
# pylint: disable=import-error,no-name-in-module
23
import os
34
import requests
5+
from models import RegionModel, SiteModel
46
from diffsync import DiffSync
57

6-
from .models import RegionModel, SiteModel
78

89
NAUTOBOT_URL = os.getenv("NAUTOBOT_URL", "https://demo.nautobot.com")
910
NAUTOBOT_TOKEN = os.getenv("NAUTOBOT_TOKEN", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
@@ -15,6 +16,7 @@ class RegionNautobotModel(RegionModel):
1516
@classmethod
1617
def create(cls, diffsync, ids, attrs):
1718
"""Create a new Region record in remote Nautobot.
19+
1820
Args:
1921
diffsync (NautobotRemote): DiffSync adapter owning this Region
2022
ids (dict): Initial values for this model's _identifiers
@@ -33,6 +35,7 @@ def create(cls, diffsync, ids, attrs):
3335

3436
def update(self, attrs):
3537
"""Update an existing Region record in remote Nautobot.
38+
3639
Args:
3740
attrs (dict): Updated values for this record's _attributes
3841
"""
@@ -43,13 +46,13 @@ def update(self, attrs):
4346
data["description"] = attrs["description"]
4447
if "parent_name" in attrs:
4548
if attrs["parent_name"]:
46-
data["parent"] = {"name": attrs["parent_name"]}
49+
data["parent"] = str(self.get(self.region, attrs["parent_name"]).pk)
4750
else:
4851
data["parent"] = None
4952
self.diffsync.patch(f"/api/dcim/regions/{self.pk}/", data)
5053
return super().update(attrs)
5154

52-
def delete(self):
55+
def delete(self): # pylint: disable= useless-super-delegation
5356
"""Delete an existing Region record from remote Nautobot."""
5457
# self.diffsync.delete(f"/api/dcim/regions/{self.pk}/")
5558
return super().delete()
@@ -61,6 +64,7 @@ class SiteNautobotModel(SiteModel):
6164
@classmethod
6265
def create(cls, diffsync, ids, attrs):
6366
"""Create a new Site in remote Nautobot.
67+
6468
Args:
6569
diffsync (NautobotRemote): DiffSync adapter owning this Site
6670
ids (dict): Initial values for this model's _identifiers
@@ -82,6 +86,7 @@ def create(cls, diffsync, ids, attrs):
8286

8387
def update(self, attrs):
8488
"""Update an existing Site record in remote Nautobot.
89+
8590
Args:
8691
attrs (dict): Updated values for this record's _attributes
8792
"""
@@ -104,17 +109,14 @@ def update(self, attrs):
104109
self.diffsync.patch(f"/api/dcim/sites/{self.pk}/", data)
105110
return super().update(attrs)
106111

107-
def delete(self):
112+
def delete(self): # pylint: disable= useless-super-delegation
108113
"""Delete an existing Site record from remote Nautobot."""
109114
# self.diffsync.delete(f"/api/dcim/sites/{self.pk}/")
110115
return super().delete()
111116

112117

113118
class NautobotRemote(DiffSync):
114-
"""DiffSync adapter class for loading data from a remote Nautobot instance using Python requests.
115-
In a more realistic example, you'd probably use PyNautobot here instead of raw requests,
116-
but we didn't want to add PyNautobot as a dependency of this plugin just to make an example more realistic.
117-
"""
119+
"""DiffSync adapter class for loading data from a remote Nautobot instance using Python requests."""
118120

119121
# Model classes used by this adapter class
120122
region = RegionNautobotModel
@@ -125,6 +127,7 @@ class NautobotRemote(DiffSync):
125127

126128
def __init__(self, *args, url=NAUTOBOT_URL, token=NAUTOBOT_TOKEN, **kwargs):
127129
"""Instantiate this class, but do not load data immediately from the remote system.
130+
128131
Args:
129132
url (str): URL of the remote Nautobot system
130133
token (str): REST API authentication token
@@ -142,9 +145,6 @@ def __init__(self, *args, url=NAUTOBOT_URL, token=NAUTOBOT_TOKEN, **kwargs):
142145

143146
def load(self):
144147
"""Load Region and Site data from the remote Nautobot instance."""
145-
# To keep the example simple, we disable REST API pagination of results.
146-
# In a real job, especially when expecting a large number of results,
147-
# we'd want to handle pagination, or use something like PyNautobot.
148148
region_data = requests.get(f"{self.url}/api/dcim/regions/", headers=self.headers, params={"limit": 0}).json()
149149
regions = region_data["results"]
150150
while region_data["next"]:

examples/05-nautobot-peeringdb/adapter_peeringdb.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
"""Diffsync adapter class for PeeringDB."""
2+
# pylint: disable=import-error,no-name-in-module
23
import requests
34
from slugify import slugify
45
import pycountry
5-
6+
from models import RegionModel, SiteModel
67
from diffsync import DiffSync
78
from diffsync.exceptions import ObjectNotFound
89

9-
from .models import RegionModel, SiteModel
1010

1111
PEERINGDB_URL = "https://peeringdb.com/"
1212

1313

1414
class PeeringDB(DiffSync):
15-
"""DiffSync adapter using pysnow to communicate with PeeringDB."""
15+
"""DiffSync adapter using requests to communicate with PeeringDB."""
1616

1717
# Model classes used by this adapter class
1818
region = RegionModel
@@ -35,10 +35,21 @@ def load(self):
3535
try:
3636
self.get(self.region, fac["city"])
3737
except ObjectNotFound:
38+
# Adding the parent region if necessary
39+
parent_name = pycountry.countries.get(alpha_2=fac["country"]).name
40+
try:
41+
self.get(self.region, parent_name)
42+
except ObjectNotFound:
43+
parent_region = self.region(
44+
name=parent_name,
45+
slug=slugify(parent_name),
46+
)
47+
self.add(parent_region)
48+
3849
region = self.region(
3950
name=fac["city"],
4051
slug=slugify(fac["city"]),
41-
parent_name=pycountry.countries.get(alpha_2=fac["country"]).name,
52+
parent_name=parent_name,
4253
)
4354
self.add(region)
4455

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
diffsync
2+
python-slugify
3+
pycountry
4+
requests
5+
IPython

0 commit comments

Comments
 (0)