11"""Diffsync adapter class for Nautobot."""
2+ # pylint: disable=import-error,no-name-in-module
23import os
34import requests
5+ from models import RegionModel , SiteModel
46from diffsync import DiffSync
57
6- from .models import RegionModel , SiteModel
78
89NAUTOBOT_URL = os .getenv ("NAUTOBOT_URL" , "https://demo.nautobot.com" )
910NAUTOBOT_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
113118class 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" ]:
0 commit comments