From 00151f00e18fe3ba4fbff23fc29f764c0a4f5dd7 Mon Sep 17 00:00:00 2001 From: James Harr Date: Mon, 20 Nov 2023 16:10:01 +0000 Subject: [PATCH] doc: Update docs+examples to show new signatures --- .../getting_started/01-getting-started.md | 8 +++---- examples/03-remote-system/nautobot_models.py | 14 +++++------ .../05-nautobot-peeringdb/adapter_nautobot.py | 24 ++++++++++--------- examples/06-ip-prefixes/adapter_ipam_a.py | 14 +++++------ examples/06-ip-prefixes/adapter_ipam_b.py | 14 +++++------ 5 files changed, 38 insertions(+), 36 deletions(-) diff --git a/docs/source/getting_started/01-getting-started.md b/docs/source/getting_started/01-getting-started.md index 9b99cc35..a2d48832 100644 --- a/docs/source/getting_started/01-getting-started.md +++ b/docs/source/getting_started/01-getting-started.md @@ -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 ``` diff --git a/examples/03-remote-system/nautobot_models.py b/examples/03-remote-system/nautobot_models.py index 2baba778..66522069 100644 --- a/examples/03-remote-system/nautobot_models.py +++ b/examples/03-remote-system/nautobot_models.py @@ -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: @@ -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: @@ -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 diff --git a/examples/05-nautobot-peeringdb/adapter_nautobot.py b/examples/05-nautobot-peeringdb/adapter_nautobot.py index 84be2d69..1f5dafcc 100644 --- a/examples/05-nautobot-peeringdb/adapter_nautobot.py +++ b/examples/05-nautobot-peeringdb/adapter_nautobot.py @@ -30,13 +30,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 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"] @@ -44,18 +45,18 @@ def update(self, 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): @@ -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: @@ -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): diff --git a/examples/06-ip-prefixes/adapter_ipam_a.py b/examples/06-ip-prefixes/adapter_ipam_a.py index 5463cd46..386786d2 100644 --- a/examples/06-ip-prefixes/adapter_ipam_a.py +++ b/examples/06-ip-prefixes/adapter_ipam_a.py @@ -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"] @@ -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): diff --git a/examples/06-ip-prefixes/adapter_ipam_b.py b/examples/06-ip-prefixes/adapter_ipam_b.py index 7a6eadb3..08efcd7c 100644 --- a/examples/06-ip-prefixes/adapter_ipam_b.py +++ b/examples/06-ip-prefixes/adapter_ipam_b.py @@ -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"] @@ -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):