1
1
"""Diffsync adapter class for Nautobot."""
2
+ # pylint: disable=import-error,no-name-in-module
2
3
import os
3
4
import requests
5
+ from models import RegionModel , SiteModel
4
6
from diffsync import DiffSync
5
7
6
- from .models import RegionModel , SiteModel
7
8
8
9
NAUTOBOT_URL = os .getenv ("NAUTOBOT_URL" , "https://demo.nautobot.com" )
9
10
NAUTOBOT_TOKEN = os .getenv ("NAUTOBOT_TOKEN" , "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" )
@@ -15,6 +16,7 @@ class RegionNautobotModel(RegionModel):
15
16
@classmethod
16
17
def create (cls , diffsync , ids , attrs ):
17
18
"""Create a new Region record in remote Nautobot.
19
+
18
20
Args:
19
21
diffsync (NautobotRemote): DiffSync adapter owning this Region
20
22
ids (dict): Initial values for this model's _identifiers
@@ -33,6 +35,7 @@ def create(cls, diffsync, ids, attrs):
33
35
34
36
def update (self , attrs ):
35
37
"""Update an existing Region record in remote Nautobot.
38
+
36
39
Args:
37
40
attrs (dict): Updated values for this record's _attributes
38
41
"""
@@ -43,13 +46,13 @@ def update(self, attrs):
43
46
data ["description" ] = attrs ["description" ]
44
47
if "parent_name" in attrs :
45
48
if attrs ["parent_name" ]:
46
- data ["parent" ] = { "name" : attrs ["parent_name" ]}
49
+ data ["parent" ] = str ( self . get ( self . region , attrs ["parent_name" ]). pk )
47
50
else :
48
51
data ["parent" ] = None
49
52
self .diffsync .patch (f"/api/dcim/regions/{ self .pk } /" , data )
50
53
return super ().update (attrs )
51
54
52
- def delete (self ):
55
+ def delete (self ): # pylint: disable= useless-super-delegation
53
56
"""Delete an existing Region record from remote Nautobot."""
54
57
# self.diffsync.delete(f"/api/dcim/regions/{self.pk}/")
55
58
return super ().delete ()
@@ -61,6 +64,7 @@ class SiteNautobotModel(SiteModel):
61
64
@classmethod
62
65
def create (cls , diffsync , ids , attrs ):
63
66
"""Create a new Site in remote Nautobot.
67
+
64
68
Args:
65
69
diffsync (NautobotRemote): DiffSync adapter owning this Site
66
70
ids (dict): Initial values for this model's _identifiers
@@ -82,6 +86,7 @@ def create(cls, diffsync, ids, attrs):
82
86
83
87
def update (self , attrs ):
84
88
"""Update an existing Site record in remote Nautobot.
89
+
85
90
Args:
86
91
attrs (dict): Updated values for this record's _attributes
87
92
"""
@@ -104,17 +109,14 @@ def update(self, attrs):
104
109
self .diffsync .patch (f"/api/dcim/sites/{ self .pk } /" , data )
105
110
return super ().update (attrs )
106
111
107
- def delete (self ):
112
+ def delete (self ): # pylint: disable= useless-super-delegation
108
113
"""Delete an existing Site record from remote Nautobot."""
109
114
# self.diffsync.delete(f"/api/dcim/sites/{self.pk}/")
110
115
return super ().delete ()
111
116
112
117
113
118
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."""
118
120
119
121
# Model classes used by this adapter class
120
122
region = RegionNautobotModel
@@ -125,6 +127,7 @@ class NautobotRemote(DiffSync):
125
127
126
128
def __init__ (self , * args , url = NAUTOBOT_URL , token = NAUTOBOT_TOKEN , ** kwargs ):
127
129
"""Instantiate this class, but do not load data immediately from the remote system.
130
+
128
131
Args:
129
132
url (str): URL of the remote Nautobot system
130
133
token (str): REST API authentication token
@@ -142,9 +145,6 @@ def __init__(self, *args, url=NAUTOBOT_URL, token=NAUTOBOT_TOKEN, **kwargs):
142
145
143
146
def load (self ):
144
147
"""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.
148
148
region_data = requests .get (f"{ self .url } /api/dcim/regions/" , headers = self .headers , params = {"limit" : 0 }).json ()
149
149
regions = region_data ["results" ]
150
150
while region_data ["next" ]:
0 commit comments