|
| 1 | +# Example 4 - Using get or update helpers |
| 2 | + |
| 3 | +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`. |
| 4 | + |
| 5 | +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. |
| 6 | + |
| 7 | +## get_or_instantiate |
| 8 | + |
| 9 | +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. |
| 10 | + |
| 11 | +It will then return a tuple that can be unpacked. |
| 12 | + |
| 13 | +```python |
| 14 | +obj, created = self.get_or_instantiate(Interface, {"device_name": "test100", "name": "eth0"}, {"description": "Test description"}) |
| 15 | +``` |
| 16 | + |
| 17 | +If the object already exists, `created` will be `False` or else it will return `True` if the object had to be created. |
| 18 | + |
| 19 | +## update_or_instantiate |
| 20 | + |
| 21 | +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. |
| 22 | + |
| 23 | +```python |
| 24 | +obj, created = self.update_or_instantiate(Interface, {"device_name": "test100", "name": "eth0"}, {"description": "Test description"}) |
| 25 | +``` |
| 26 | + |
| 27 | +## Example Walkthrough |
| 28 | + |
| 29 | +We can take a look at the data we will be loading into each backend to understand why these helper methods are valuable. |
| 30 | + |
| 31 | +### Example Data |
| 32 | + |
| 33 | +```python |
| 34 | +BACKEND_DATA_A = [ |
| 35 | + { |
| 36 | + "name": "nyc-spine1", |
| 37 | + "role": "spine", |
| 38 | + "interfaces": {"eth0": "Interface 0", "eth1": "Interface 1"}, |
| 39 | + "site": "nyc", |
| 40 | + }, |
| 41 | + { |
| 42 | + "name": "nyc-spine2", |
| 43 | + "role": "spine", |
| 44 | + "interfaces": {"eth0": "Interface 0", "eth1": "Interface 1"}, |
| 45 | + "site": "nyc", |
| 46 | + }, |
| 47 | +] |
| 48 | +``` |
| 49 | + |
| 50 | +## Example Load |
| 51 | + |
| 52 | +```python |
| 53 | + def load(self): |
| 54 | + """Initialize the BackendA Object by loading some site, device and interfaces from DATA.""" |
| 55 | + for device_data in BACKEND_DATA_A: |
| 56 | + device, instantiated = self.get_or_instantiate( |
| 57 | + self.device, {"name": device_data["name"]}, {"role": device_data["role"]} |
| 58 | + ) |
| 59 | + |
| 60 | + site, instantiated = self.get_or_instantiate(self.site, {"name": device_data["site"]}) |
| 61 | + if instantiated: |
| 62 | + device.add_child(site) |
| 63 | + |
| 64 | + for intf_name, desc in device_data["interfaces"].items(): |
| 65 | + intf, instantiated = self.update_or_instantiate( |
| 66 | + self.interface, {"name": intf_name, "device_name": device_data["name"]}, {"description": desc} |
| 67 | + ) |
| 68 | + if instantiated: |
| 69 | + device.add_child(intf) |
| 70 | +``` |
| 71 | + |
| 72 | +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. |
0 commit comments