Skip to content

Commit

Permalink
doc: Update docs+examples to show new signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
James Harr committed Nov 20, 2023
1 parent 35f8ee1 commit 00151f0
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 36 deletions.
8 changes: 4 additions & 4 deletions docs/source/getting_started/01-getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,15 +186,15 @@ class Device(DiffSyncModel):
# Call the super().create() method to create the in-memory DiffSyncModel instance
return super().create(ids=ids, diffsync=diffsync, attrs=attrs)

def update(self, attrs):
def update(self, diffsync, attrs):
## TODO add your own logic here to update the device on the remote system
# Call the super().update() method to update the in-memory DiffSyncModel instance
return super().update(attrs)
return super().update(diffsync, attrs)

def delete(self):
def delete(self, diffsync):
## TODO add your own logic here to delete the device on the remote system
# Call the super().delete() method to remove the DiffSyncModel instance from its parent DiffSync adapter
super().delete()
super().delete(diffsync)
return self
```

Expand Down
14 changes: 7 additions & 7 deletions examples/03-remote-system/nautobot_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def create(cls, diffsync: Adapter, ids: dict, attrs: dict):
item = super().create(ids=ids, diffsync=diffsync, attrs=attrs)
return item

def update(self, attrs: dict):
def update(self, diffsync: Adapter, attrs: dict):
"""Update a country object in Nautobot.
Args:
Expand All @@ -77,8 +77,8 @@ def update(self, attrs: dict):
Raises:
ObjectNotUpdated: if an error occurred.
"""
# Retrive the pynautobot object from Nautobot since we only have the UUID internally
remote = self.diffsync.nautobot.dcim.regions.get(self.remote_id)
# Retrieve the pynautobot object from Nautobot since we only have the UUID internally
remote = diffsync.nautobot.dcim.regions.get(self.remote_id)

# Convert the internal attrs to Nautobot format
if "population" in attrs:
Expand All @@ -89,17 +89,17 @@ def update(self, attrs: dict):
remote.save()
print(f"Updated Country {self.slug} | {attrs}")

return super().update(attrs)
return super().update(diffsync, attrs)

def delete(self):
def delete(self, diffsync: Adapter):
"""Delete a country object in Nautobot.
Returns:
NautobotCountry: DiffSync object
"""
# Retrieve the pynautobot object and delete the object in Nautobot
remote = self.diffsync.nautobot.dcim.regions.get(self.remote_id)
remote = diffsync.nautobot.dcim.regions.get(self.remote_id)
remote.delete()

super().delete()
super().delete(diffsync)
return self
24 changes: 13 additions & 11 deletions examples/05-nautobot-peeringdb/adapter_nautobot.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,32 +30,33 @@ def create(cls, diffsync, ids, attrs):

return super().create(diffsync, ids=ids, attrs=attrs)

def update(self, attrs):
def update(self, diffsync, attrs):
"""Update an existing Region record in remote Nautobot.
Args:
diffsync (Adapter): Adapter this model instance belongs to
attrs (dict): Updated values for this record's _attributes
"""
region = self.diffsync.nautobot_api.dcim.regions.get(name=self.name)
region = diffsync.nautobot_api.dcim.regions.get(name=self.name)
data = {}
if "slug" in attrs:
data["slug"] = attrs["slug"]
if "description" in attrs:
data["description"] = attrs["description"]
if "parent_name" in attrs:
if attrs["parent_name"]:
data["parent"] = str(self.diffsync.get(self.diffsync.region, attrs["parent_name"]).name)
data["parent"] = str(diffsync.get(diffsync.region, attrs["parent_name"]).name)
else:
data["parent"] = None

region.update(data=data)

return super().update(attrs)
return super().update(diffsync, attrs)

def delete(self): # pylint: disable= useless-super-delegation
def delete(self, diffsync): # pylint: disable= useless-super-delegation
"""Delete an existing Region record from remote Nautobot."""
# Not implemented
return super().delete()
return super().delete(diffsync)


class SiteNautobotModel(SiteModel):
Expand All @@ -81,13 +82,14 @@ def create(cls, diffsync, ids, attrs):
)
return super().create(diffsync, ids=ids, attrs=attrs)

def update(self, attrs):
def update(self, diffsync, attrs):
"""Update an existing Site record in remote Nautobot.
Args:
diffsync (Adapter): Adapter this model instance belongs to
attrs (dict): Updated values for this record's _attributes
"""
site = self.diffsync.nautobot_api.dcim.sites.get(name=self.name)
site = diffsync.nautobot_api.dcim.sites.get(name=self.name)

data = {}
if "slug" in attrs:
Expand All @@ -108,12 +110,12 @@ def update(self, attrs):

site.update(data=data)

return super().update(attrs)
return super().update(diffsync, attrs)

def delete(self): # pylint: disable= useless-super-delegation
def delete(self, diffsync): # pylint: disable= useless-super-delegation
"""Delete an existing Site record from remote Nautobot."""
# Not implemented
return super().delete()
return super().delete(diffsync)


class NautobotRemote(Adapter):
Expand Down
14 changes: 7 additions & 7 deletions examples/06-ip-prefixes/adapter_ipam_a.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ def create(cls, diffsync, ids, attrs):

return super().create(diffsync, ids=ids, attrs=attrs)

def update(self, attrs):
def update(self, diffsync, attrs):
"""Update a Prefix record in IPAM A."""
for elem in self.diffsync.data:
for elem in diffsync.data:
if elem["cidr"] == self.prefix:
if "vrf" in attrs:
elem["vrf"] = attrs["vrf"]
Expand All @@ -38,16 +38,16 @@ def update(self, attrs):
elem["customer_id"] = attrs["tenant"]
break

return super().update(attrs)
return super().update(diffsync, attrs)

def delete(self):
def delete(self, diffsync):
"""Delete a Prefix record in IPAM A."""
for index, elem in enumerate(self.diffsync.data):
for index, elem in enumerate(diffsync.data):
if elem["cidr"] == self.prefix:
del self.diffsync.data[index]
del diffsync.data[index]
break

return super().delete()
return super().delete(diffsync)


class IpamA(Adapter):
Expand Down
14 changes: 7 additions & 7 deletions examples/06-ip-prefixes/adapter_ipam_b.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ def create(cls, diffsync, ids, attrs):

return super().create(diffsync, ids=ids, attrs=attrs)

def update(self, attrs):
def update(self, diffsync, attrs):
"""Update a Prefix record in IPAM B."""
network = self.prefix.split("/")[0]
prefix_length = int(self.prefix.split("/")[1])

for elem in self.diffsync.data:
for elem in diffsync.data:
if elem["network"] == network and elem["prefix_length"] == prefix_length:
if "vrf" in attrs:
elem["vrf"] = attrs["vrf"]
Expand All @@ -40,19 +40,19 @@ def update(self, attrs):
elem["tenant"] = attrs["tenant"]
break

return super().update(attrs)
return super().update(diffsync, attrs)

def delete(self):
def delete(self, diffsync: Adapter):
"""Update a Prefix record in IPAM B."""
network = self.prefix.split("/")[0]
prefix_length = int(self.prefix.split("/")[1])

for index, elem in enumerate(self.diffsync.data):
for index, elem in enumerate(diffsync.data):
if elem["network"] == network and elem["prefix_length"] == prefix_length:
del self.diffsync.data[index]
del diffsync.data[index]
break

return super().delete()
return super().delete(diffsync)


class IpamB(Adapter):
Expand Down

0 comments on commit 00151f0

Please sign in to comment.