|
| 1 | +"""DiffSync Adapter for Nautobot to manage regions.""" |
1 | 2 | import os |
2 | 3 | import pynautobot |
3 | 4 |
|
| 5 | +from nautobot_models import NautobotCountry, NautobotRegion |
| 6 | + |
4 | 7 | from diffsync import DiffSync |
5 | 8 |
|
6 | | -from nautobot_models import NautobotCountry, NautobotRegion |
7 | 9 |
|
8 | 10 | NAUTOBOT_URL = os.getenv("NAUTOBOT_URL", "https://demo.nautobot.com") |
9 | 11 | NAUTOBOT_TOKEN = os.getenv("NAUTOBOT_TOKEN", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa") |
@@ -40,10 +42,11 @@ class NautobotAdapter(DiffSync): |
40 | 42 |
|
41 | 43 | def load(self): |
42 | 44 | """Load all data from Nautobot into the internal cache after transformation.""" |
43 | | - |
44 | 45 | # Initialize pynautobot to interact with Nautobot and store it within the adapter |
45 | 46 | # to reuse it later |
46 | | - self.nautobot = pynautobot.api(url=NAUTOBOT_URL, token=NAUTOBOT_TOKEN) |
| 47 | + self.nautobot = pynautobot.api( |
| 48 | + url=NAUTOBOT_URL, token=NAUTOBOT_TOKEN |
| 49 | + ) # pylint: disable=attribute-defined-outside-init |
47 | 50 |
|
48 | 51 | # Pull all regions from Nautobot, which includes all regions and all countries |
49 | 52 | regions = self.nautobot.dcim.regions.all() |
@@ -75,10 +78,9 @@ def load(self): |
75 | 78 |
|
76 | 79 | def sync_from(self, *args, **kwargs): |
77 | 80 | """Sync the data with Nautobot but first ensure that all the required Custom fields are present in Nautobot.""" |
78 | | - |
79 | 81 | # Check if all required custom fields exist, create them if they don't |
80 | 82 | for custom_field in CUSTOM_FIELDS: |
81 | | - nb_cfs = self.cfs = self.nautobot.extras.custom_fields.filter(name=custom_field.get("name")) |
| 83 | + nb_cfs = self.nautobot.extras.custom_fields.filter(name=custom_field.get("name")) |
82 | 84 | if not nb_cfs: |
83 | 85 | self.nautobot.extras.custom_fields.create(**custom_field) |
84 | 86 |
|
|
0 commit comments