-
Notifications
You must be signed in to change notification settings - Fork 32
Add add_or_update to DiffSync class. #70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
dgarros
merged 19 commits into
networktocode:main
from
FragmentedPacket:50-get-or-create
Nov 12, 2021
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
b2dc459
Add add_or_update to DiffSync class.
FragmentedPacket 8161a2d
Merge branch 'main' of git://github.com/networktocode/diffsync into 5…
FragmentedPacket b172cf0
Update diffsync.add to only raise ObjectAlreadyExists if objects diff…
FragmentedPacket 182542b
Pylint to pass for tests for diffsync.add
FragmentedPacket 7ed20cb
Add get_or_create along with tests.
FragmentedPacket f4f63e4
Addressed some of the feedback from Glenn.
FragmentedPacket c4c32cf
Rename get_or_create/get_or_instantiate. Update tests.
FragmentedPacket e61b560
Add testing for update_or_create. Return object in ObjectAlreadyExist…
FragmentedPacket e917055
Update doc strings
FragmentedPacket 8f3b328
Add example of new methods.
FragmentedPacket c43f789
Updates to add device to diffsync. Update tests. Update test names an…
FragmentedPacket 95cb2f0
Few doc updates for licensing year. Remove var assignment for error t…
FragmentedPacket 37b3bca
Update diffsync/__init__.py
FragmentedPacket 93a0a74
Update tests/unit/test_diffsync.py
FragmentedPacket 025a401
Require attrs for update_or, Set existing_object as attribute in Obje…
FragmentedPacket 6c747f1
Add README.md to example-04. Add Example 04 to examples/index.rst
FragmentedPacket 4f9bf4b
Use python 3.9.7 for black.
FragmentedPacket c970516
Update to support 3.9 and black for compatibility.
FragmentedPacket 647d971
Believe I fixed the dependencies
FragmentedPacket File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# Example 4 - Using get or update helpers | ||
|
||
This example aims to expand on [Example 1](https://github.com/networktocode/diffsync/tree/main/examples/01-multiple-data-sources/README.md) that will take advantage of two new helper methods on the `DiffSync` class; `get_or_instantiate` and `update_or_instantiate`. | ||
|
||
Both methods act similar to Django's `get_or_create` function to return the object and then a boolean to identify whether the object was created or not. Let's dive into each of them. | ||
|
||
## get_or_instantiate | ||
|
||
The following arguments are supported: model (`DiffSyncModel`), ids (dictionary), and attrs (dictionary). The `model` and `ids` are used to find an existing object. If the object does not currently exist within the `DiffSync` adapter, it will then use `model`, `ids`, and `attrs` to add the object. | ||
|
||
It will then return a tuple that can be unpacked. | ||
|
||
```python | ||
obj, created = self.get_or_instantiate(Interface, {"device_name": "test100", "name": "eth0"}, {"description": "Test description"}) | ||
``` | ||
|
||
If the object already exists, `created` will be `False` or else it will return `True` if the object had to be created. | ||
|
||
## update_or_instantiate | ||
|
||
This helper is similar to `get_or_instantiate`, but it will update an existing object or add a new instance with the provided `ids` and `attrs`. The method does accept the same arguments, but requires `attrs`, whereas `get_or_instantiate` does not. | ||
|
||
```python | ||
obj, created = self.update_or_instantiate(Interface, {"device_name": "test100", "name": "eth0"}, {"description": "Test description"}) | ||
``` | ||
|
||
## Example Walkthrough | ||
|
||
We can take a look at the data we will be loading into each backend to understand why these helper methods are valuable. | ||
|
||
### Example Data | ||
|
||
```python | ||
BACKEND_DATA_A = [ | ||
{ | ||
"name": "nyc-spine1", | ||
"role": "spine", | ||
"interfaces": {"eth0": "Interface 0", "eth1": "Interface 1"}, | ||
"site": "nyc", | ||
}, | ||
{ | ||
"name": "nyc-spine2", | ||
"role": "spine", | ||
"interfaces": {"eth0": "Interface 0", "eth1": "Interface 1"}, | ||
"site": "nyc", | ||
}, | ||
] | ||
``` | ||
|
||
## Example Load | ||
|
||
```python | ||
def load(self): | ||
"""Initialize the BackendA Object by loading some site, device and interfaces from DATA.""" | ||
for device_data in BACKEND_DATA_A: | ||
device, instantiated = self.get_or_instantiate( | ||
self.device, {"name": device_data["name"]}, {"role": device_data["role"]} | ||
) | ||
|
||
site, instantiated = self.get_or_instantiate(self.site, {"name": device_data["site"]}) | ||
if instantiated: | ||
device.add_child(site) | ||
|
||
for intf_name, desc in device_data["interfaces"].items(): | ||
intf, instantiated = self.update_or_instantiate( | ||
self.interface, {"name": intf_name, "device_name": device_data["name"]}, {"description": desc} | ||
) | ||
if instantiated: | ||
device.add_child(intf) | ||
``` | ||
|
||
The new methods are helpful due to having devices that are part of the same site. As we iterate over the data and load it into the `DiffSync` adapter, we would have to account for `ObjectAlreadyExists` exceptions when we go to add each duplicate site we encounter within the data or possibly several other models depending how complex the synchronization of data is between backends. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
"""Example of a DiffSync adapter implementation using new helper methods. | ||
|
||
Copyright (c) 2021 Network To Code, LLC <info@networktocode.com> | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
""" | ||
|
||
from models import Site, Device, Interface | ||
from diffsync import DiffSync | ||
|
||
BACKEND_DATA_A = [ | ||
{ | ||
"name": "nyc-spine1", | ||
"role": "spine", | ||
"interfaces": {"eth0": "Interface 0", "eth1": "Interface 1"}, | ||
"site": "nyc", | ||
}, | ||
{ | ||
"name": "nyc-spine2", | ||
"role": "spine", | ||
"interfaces": {"eth0": "Interface 0", "eth1": "Interface 1"}, | ||
"site": "nyc", | ||
}, | ||
{ | ||
"name": "sfo-spine1", | ||
"role": "spine", | ||
"interfaces": {"eth0": "Interface 0", "eth1": "Interface 1"}, | ||
"site": "sfo", | ||
}, | ||
{ | ||
"name": "sfo-spine2", | ||
"role": "spine", | ||
"interfaces": {"eth0": "TBD", "eth1": "ddd", "eth2": "Interface 2"}, | ||
"site": "sfo", | ||
}, | ||
] | ||
BACKEND_DATA_B = [ | ||
{ | ||
"name": "atl-spine1", | ||
"role": "spine", | ||
"interfaces": {"eth0": "Interface 0", "eth1": "Interface 1"}, | ||
"site": "atl", | ||
}, | ||
{ | ||
"name": "atl-spine2", | ||
"role": "spine", | ||
"interfaces": {"eth0": "Interface 0", "eth1": "Interface 1"}, | ||
"site": "atl", | ||
}, | ||
{ | ||
"name": "nyc-spine1", | ||
"role": "spine", | ||
"interfaces": {"eth0": "Interface 0/0", "eth1": "Interface 1"}, | ||
"site": "nyc", | ||
}, | ||
{ | ||
"name": "nyc-spine2", | ||
"role": "spine", | ||
"interfaces": {"eth0": "Interface 0", "eth1": "Interface 1"}, | ||
"site": "nyc", | ||
}, | ||
{"name": "sfo-spine1", "role": "leaf", "interfaces": {"eth0": "Interface 0", "eth1": "Interface 1"}, "site": "sfo"}, | ||
{"name": "sfo-spine2", "role": "spine", "interfaces": {"eth0": "TBD", "eth1": "ddd"}, "site": "sfo"}, | ||
] | ||
|
||
|
||
class BackendA(DiffSync): | ||
"""Example of a DiffSync adapter implementation.""" | ||
|
||
site = Site | ||
device = Device | ||
interface = Interface | ||
|
||
top_level = ["device"] | ||
|
||
type = "Backend A" | ||
|
||
def load(self): | ||
"""Initialize the BackendA Object by loading some site, device and interfaces from DATA.""" | ||
for device_data in BACKEND_DATA_A: | ||
device, instantiated = self.get_or_instantiate( | ||
self.device, {"name": device_data["name"]}, {"role": device_data["role"]} | ||
) | ||
|
||
site, instantiated = self.get_or_instantiate(self.site, {"name": device_data["site"]}) | ||
if instantiated: | ||
device.add_child(site) | ||
FragmentedPacket marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
for intf_name, desc in device_data["interfaces"].items(): | ||
intf, instantiated = self.update_or_instantiate( | ||
self.interface, {"name": intf_name, "device_name": device_data["name"]}, {"description": desc} | ||
) | ||
if instantiated: | ||
device.add_child(intf) | ||
|
||
|
||
class BackendB(DiffSync): | ||
"""Example of a DiffSync adapter implementation.""" | ||
|
||
site = Site | ||
device = Device | ||
interface = Interface | ||
|
||
top_level = ["device"] | ||
|
||
type = "Backend B" | ||
|
||
def load(self): | ||
"""Initialize the BackendB Object by loading some site, device and interfaces from DATA.""" | ||
for device_data in BACKEND_DATA_B: | ||
device, instantiated = self.get_or_instantiate( | ||
self.device, {"name": device_data["name"]}, {"role": device_data["role"]} | ||
) | ||
|
||
site, instantiated = self.get_or_instantiate(self.site, {"name": device_data["site"]}) | ||
if instantiated: | ||
device.add_child(site) | ||
|
||
for intf_name, desc in device_data["interfaces"].items(): | ||
intf, instantiated = self.get_or_instantiate( | ||
self.interface, {"name": intf_name, "device_name": device_data["name"]}, {"description": desc} | ||
) | ||
if instantiated: | ||
device.add_child(intf) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.