-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathexamples.py
More file actions
1165 lines (1000 loc) · 46 KB
/
examples.py
File metadata and controls
1165 lines (1000 loc) · 46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Sample data-source and data-target Jobs."""
# Skip colon check for multiple statements on one line.
# flake8: noqa: E701
# pylint: disable=too-many-lines
try:
from typing_extensions import TypedDict # Python<3.9
except ImportError:
from typing import TypedDict # Python>=3.9
from typing import Generator, List, Optional
import requests
from diffsync import Adapter
from diffsync.enum import DiffSyncFlags
from diffsync.exceptions import ObjectNotFound
from django.contrib.contenttypes.models import ContentType
from django.templatetags.static import static
from django.urls import reverse
from nautobot.dcim.models import Device, DeviceType, Interface, Location, LocationType, Manufacturer, Platform
from nautobot.extras.choices import SecretsGroupAccessTypeChoices, SecretsGroupSecretTypeChoices
from nautobot.extras.jobs import ObjectVar, StringVar
from nautobot.extras.models import ExternalIntegration, Role, Status
from nautobot.extras.secrets.exceptions import SecretError
from nautobot.ipam.models import IPAddress, Namespace, Prefix
from nautobot.tenancy.models import Tenant
from nautobot_ssot.contrib import NautobotAdapter, NautobotModel
from nautobot_ssot.contrib.typeddicts import TagDict
from nautobot_ssot.exceptions import MissingSecretsGroupException
from nautobot_ssot.jobs.base import DataMapping, DataSource, DataTarget
from nautobot_ssot.tests.contrib_base_classes import ContentTypeDict
# In a more complex Job, you would probably want to move the DiffSyncModel subclasses into a separate Python module(s).
name = "SSoT Examples" # pylint: disable=invalid-name
class LocationTypeModel(NautobotModel):
"""Shared data model representing a LocationType in either of the local or remote Nautobot instances."""
# Metadata about this model
_model = LocationType
_modelname = "locationtype"
_identifiers = ("name",)
# To keep this example simple, we don't include **all** attributes of a Location here. But you could!
_attributes = ("content_types", "description", "nestable", "parent__name")
# Data type declarations for all identifiers and attributes
name: str
description: str
nestable: bool
parent__name: Optional[str] = None
content_types: List[ContentTypeDict] = []
class LocationDict(TypedDict):
"""This typed dict is for M2M Locations."""
name: str
location_type__name: str
class LocationModel(NautobotModel):
"""Shared data model representing a Location in either of the local or remote Nautobot instances."""
# Metadata about this model
_model = Location
_modelname = "location"
_identifiers = ("name",)
# To keep this example simple, we don't include **all** attributes of a Location here. But you could!
_attributes = (
"location_type__name",
"status__name",
"parent__name",
"parent__location_type__name",
"tenant__name",
"description",
"tags",
)
# Data type declarations for all identifiers and attributes
name: str
location_type__name: str
status__name: str
parent__name: Optional[str] = None
parent__location_type__name: Optional[str] = None
tenant__name: Optional[str] = None
description: str
tags: List[TagDict] = []
class RoleModel(NautobotModel):
"""Shared data model representing a Role in either of the local or remote Nautobot instances."""
# Metadata about this model
_model = Role
_modelname = "role"
_identifiers = ("name",)
_attributes = ("content_types",)
name: str
content_types: List[ContentTypeDict] = []
class StatusModel(NautobotModel):
"""Shared data model representing a Status in either of the local or remote Nautobot instances."""
# Metadata about this model
_model = Status
_modelname = "status"
_identifiers = ("name",)
_attributes = ("content_types", "color")
name: str
color: str
content_types: List[ContentTypeDict] = []
class NamespaceModel(NautobotModel):
"""Shared data model representing a Namespace in either of the local or remote Nautobot instance."""
# Metadata about this model
_model = Namespace
_modelname = "namespace"
_identifiers = ("name",)
_attributes = ("description", "tags")
name: str
description: Optional[str] = ""
tags: List[TagDict] = []
class PrefixModel(NautobotModel):
"""Shared data model representing a Prefix in either of the local or remote Nautobot instances."""
# Metadata about this model
_model = Prefix
_modelname = "prefix"
_identifiers = ("network", "prefix_length", "namespace__name")
# To keep this example simple, we don't include **all** attributes of a Prefix here. But you could!
_attributes = ("description", "tenant__name", "status__name", "locations", "tags")
# Data type declarations for all identifiers and attributes
network: str
namespace__name: str
prefix_length: int
tenant__name: Optional[str] = None
status__name: str
description: str
tags: List[TagDict] = []
locations: List[LocationDict] = []
class IPAddressModel(NautobotModel):
"""Shared data model representing an IPAddress in either of the local or remote Nautobot instances."""
# Metadata about this model
_model = IPAddress
_modelname = "ipaddress"
_identifiers = ("host", "mask_length", "parent__network", "parent__prefix_length", "parent__namespace__name")
_attributes = ("status__name", "ip_version", "tenant__name", "tags")
# Data type declarations for all identifiers and attributes
host: str
mask_length: int
parent__network: str
parent__prefix_length: int
parent__namespace__name: str
status__name: str
ip_version: int
tenant__name: Optional[str]
tags: List[TagDict] = []
class TenantModel(NautobotModel):
"""Shared data model representing a Tenant in either of the local or remote Nautobot instances."""
# Metadata about this model
_model = Tenant
_modelname = "tenant"
_identifiers = ("name",)
_attributes = ("tags",)
_children = {}
name: str
prefixes: List[PrefixModel] = []
tags: List[TagDict] = []
class DeviceTypeModel(NautobotModel):
"""Shared data model representing a DeviceType in either of the local or remote Nautobot instances."""
_model = DeviceType
_modelname = "device_type"
_identifiers = ("model", "manufacturer__name")
_attributes = ("part_number", "u_height", "is_full_depth", "tags")
model: str
manufacturer__name: str
part_number: str
u_height: int
is_full_depth: bool
tags: List[TagDict] = []
class ManufacturerModel(NautobotModel):
"""Shared data model representing a Manufacturer in either of the local or remote Nautobot instances."""
_model = Manufacturer
_modelname = "manufacturer"
_identifiers = ("name",)
_attributes = ("description",)
_children = {"device_type": "device_types"}
name: str
description: str
device_types: List[DeviceTypeModel] = []
class PlatformModel(NautobotModel):
"""Shared data model representing a Platform in either of the local or remote Nautobot instances."""
_model = Platform
_modelname = "platform"
_identifiers = ("name", "manufacturer__name")
_attributes = ("description", "network_driver", "napalm_driver")
name: str
manufacturer__name: Optional[str] = None
description: str
network_driver: str
napalm_driver: str
class DeviceModel(NautobotModel):
"""Shared data model representing a Device in either of the local or remote Nautobot instances."""
# Metadata about this model
_model = Device
_modelname = "device"
_identifiers = ("name", "location__name", "location__parent__name")
_attributes = (
"location__location_type__name",
"location__parent__location_type__name",
"device_type__manufacturer__name",
"device_type__model",
"platform__name",
"role__name",
"serial",
"status__name",
"tenant__name",
"asset_tag",
"tags",
)
_children = {"interface": "interfaces"}
name: str
location__name: str
location__location_type__name: str
location__parent__name: Optional[str] = None
location__parent__location_type__name: Optional[str] = None
device_type__manufacturer__name: str
device_type__model: str
platform__name: Optional[str] = None
role__name: str
serial: str
status__name: str
tenant__name: Optional[str]
asset_tag: Optional[str]
interfaces: List["InterfaceModel"] = []
tags: List[TagDict] = []
class InterfaceModel(NautobotModel):
"""Shared data model representing an Interface in either of the local or remote Nautobot instances."""
# Metadata about this model
_model = Interface
_modelname = "interface"
_identifiers = ("name", "device__name")
_attributes = (
"device__location__name",
"device__location__parent__name",
"description",
"enabled",
"mac_address",
"mgmt_only",
"mtu",
"type",
"status__name",
"tags",
)
_children = {}
# Data type declarations for all identifiers and attributes
device__name: str
device__location__name: str
device__location__parent__name: str
description: Optional[str]
enabled: bool
mac_address: Optional[str]
mgmt_only: bool
mtu: Optional[int]
name: str
type: str
status__name: str
tags: List[TagDict] = []
class LocationRemoteModel(LocationModel):
"""Implementation of Location create/update/delete methods for updating remote Nautobot data."""
@classmethod
def create(cls, adapter, ids, attrs):
"""Create a new Location in remote Nautobot.
Args:
adapter (NautobotRemote): DiffSync adapter owning this Site
ids (dict): Initial values for this model's _identifiers
attrs (dict): Initial values for this model's _attributes
"""
adapter.post(
"/api/dcim/locations/",
{
"name": ids["name"],
"description": attrs["description"],
"status": attrs["status__name"],
"location_type": attrs["location_type__name"],
"parent": {"name": attrs["parent__name"]} if attrs.get("parent__name") else None,
"tags": attrs["tags"] if attrs.get("tags") else [],
},
)
return super().create(adapter, ids=ids, attrs=attrs)
def update(self, attrs):
"""Update an existing Site record in remote Nautobot.
Args:
attrs (dict): Updated values for this record's _attributes
"""
data = {}
if "description" in attrs:
data["description"] = attrs["description"]
if "status__name" in attrs:
data["status"] = attrs["status__name"]
if "parent__name" in attrs:
if attrs["parent__name"]:
data["parent"] = {"name": attrs["parent__name"]}
else:
data["parent"] = None
if "tags" in attrs:
data["tags"] = attrs["tags"] if attrs.get("tags") else []
self.adapter.patch(f"/api/dcim/locations/{self.pk}/", data)
return super().update(attrs)
def delete(self):
"""Delete an existing Site record from remote Nautobot."""
self.adapter.delete(f"/api/dcim/locations/{self.pk}/")
return super().delete()
class TenantRemoteModel(TenantModel):
"""Implementation of Tenant create/update/delete methods for updating remote Nautobot data."""
@classmethod
def create(cls, adapter, ids, attrs):
"""Create a new Tenant in remote Nautobot."""
adapter.post(
"/api/tenancy/tenants/",
{
"name": ids["name"],
"tags": attrs["tags"] if attrs.get("tags") else [],
},
)
return super().create(adapter, ids=ids, attrs=attrs)
def update(self, attrs):
"""Updating tenants is not supported because we don't have any attributes."""
data = {}
if "tags" in attrs:
data["tags"] = attrs["tags"] if attrs.get("tags") else []
self.adapter.patch(f"/api/tenancy/tenants/{self.pk}/", data)
return super().update(attrs)
def delete(self):
"""Delete a Tenant in remote Nautobot."""
self.adapter.delete(f"/api/tenancy/tenants/{self.pk}/")
return super().delete()
class PrefixRemoteModel(PrefixModel):
"""Implementation of Prefix create/update/delete methods for updating remote Nautobot data."""
@classmethod
def create(cls, adapter, ids, attrs):
"""Create a new Prefix in remote Nautobot.
Args:
adapter (NautobotRemote): DiffSync adapter owning this Prefix
ids (dict): Initial values for this model's _identifiers
attrs (dict): Initial values for this model's _attributes
"""
adapter.post(
"/api/ipam/prefixes/",
{
"network": ids["network"],
"prefix_length": ids["prefix_length"],
"tenant": {"name": attrs["tenant__name"]} if attrs.get("tenant__name") else None,
"namespace": {"name": ids["namespace__name"]} if ids.get("namespace__name") else None,
"description": attrs["description"],
"status": attrs["status__name"],
"tags": attrs["tags"] if attrs.get("tags") else [],
},
)
return super().create(adapter, ids=ids, attrs=attrs)
def update(self, attrs):
"""Update an existing Site record in remote Nautobot.
Args:
attrs (dict): Updated values for this record's _attributes
"""
data = {}
if "description" in attrs:
data["description"] = attrs["description"]
if "status__name" in attrs:
data["status"] = attrs["status__name"]
if "tags" in attrs:
data["tags"] = attrs["tags"] if attrs.get("tags") else []
self.adapter.patch(f"/api/dcim/locations/{self.pk}/", data)
return super().update(attrs)
def delete(self):
"""Delete an existing Site record from remote Nautobot."""
self.adapter.delete(f"/api/dcim/locations/{self.pk}/")
return super().delete()
# In a more complex Job, you would probably want to move each DiffSync subclass into a separate Python module.
class NautobotRemote(Adapter):
"""DiffSync adapter class for loading data from a remote Nautobot instance using Python requests.
In a more realistic example, you'd probably use PyNautobot here instead of raw requests,
but we didn't want to add PyNautobot as a dependency of this app just to make an example more realistic.
"""
# Model classes used by this adapter class
locationtype = LocationTypeModel
location = LocationRemoteModel
tenant = TenantRemoteModel
namespace = NamespaceModel
prefix = PrefixRemoteModel
ipaddress = IPAddressModel
manufacturer = ManufacturerModel
device_type = DeviceTypeModel
platform = PlatformModel
role = RoleModel
status = StatusModel
device = DeviceModel
interface = InterfaceModel
# Top-level class labels, i.e. those classes that are handled directly rather than as children of other models
top_level = [
"tenant",
"status",
"locationtype",
"location",
"manufacturer",
"platform",
"role",
"device",
"namespace",
"prefix",
"ipaddress",
]
def __init__(self, *args, url=None, token=None, job=None, **kwargs):
"""Instantiate this class, but do not load data immediately from the remote system.
Args:
url (str): URL of the remote Nautobot system
token (str): REST API authentication token
job (Job): The running Job instance that owns this DiffSync adapter instance
"""
super().__init__(*args, **kwargs)
if not url or not token:
raise ValueError("Both url and token must be specified!")
if not url.startswith("http"):
raise ValueError("The url must start with a schema.")
self.url = url
self.token = token
self.job = job
self.headers = {
"Accept": "application/json",
"Authorization": f"Token {self.token}",
}
def _get_api_data(self, url_path: str, **query_params) -> Generator:
"""Returns data from a url_path using pagination."""
data = requests.get(
f"{self.url}/{url_path}",
headers=self.headers,
params={"limit": 200, "exclude_m2m": "false", **query_params},
timeout=600,
).json()
yield from data["results"]
while data["next"]:
data = requests.get(
data["next"],
headers=self.headers,
timeout=600,
).json()
yield from data["results"]
def load(self):
"""Load data from the remote Nautobot instance."""
self.load_statuses()
self.load_location_types()
self.load_locations()
self.load_roles()
self.load_tenants()
self.load_namespaces()
self.load_prefixes()
self.load_ipaddresses()
self.load_manufacturers()
self.load_device_types()
self.load_platforms()
self.load_devices()
self.load_interfaces()
def load_location_types(self):
"""Load LocationType data from the remote Nautobot instance.
Ensures parent LocationTypes are loaded before their children by using
topological sorting based on parent-child relationships.
"""
# First, collect all location type entries
location_type_entries = list(self._get_api_data("api/dcim/location-types/", depth=1))
# Build a map of location type names to their entries
entries_by_name = {entry["name"]: entry for entry in location_type_entries}
# Build dependency graph: map parent name -> list of child entries
children_by_parent = {}
root_entries = [] # Entries with no parent
for entry in location_type_entries:
parent_name = entry.get("parent", {}).get("name") if entry.get("parent") else None
if parent_name:
if parent_name not in children_by_parent:
children_by_parent[parent_name] = []
children_by_parent[parent_name].append(entry)
else:
root_entries.append(entry)
# Topological sort: process parents before children
processed = set()
ordered_entries = []
def process_entry(entry):
"""Process an entry and its children recursively."""
entry_name = entry["name"]
if entry_name in processed:
return
# Process parent first if it exists and hasn't been processed
parent_name = entry.get("parent", {}).get("name") if entry.get("parent") else None
if parent_name and parent_name in entries_by_name:
parent_entry = entries_by_name[parent_name]
if parent_entry["name"] not in processed:
process_entry(parent_entry)
# Now process this entry
ordered_entries.append(entry)
processed.add(entry_name)
# Process children
if entry_name in children_by_parent:
for child_entry in children_by_parent[entry_name]:
process_entry(child_entry)
# Process all root entries (those with no parent)
for root_entry in root_entries:
process_entry(root_entry)
# Handle any remaining entries that might have been missed
# (e.g., circular references or orphaned entries)
for entry in location_type_entries:
if entry["name"] not in processed:
process_entry(entry)
# Load location types in the correct order
for lt_entry in ordered_entries:
content_types = self.get_content_types(lt_entry)
location_type = self.locationtype(
name=lt_entry["name"],
description=lt_entry["description"],
nestable=lt_entry["nestable"],
parent__name=lt_entry["parent"]["name"] if lt_entry.get("parent") else None,
content_types=content_types,
pk=lt_entry["id"],
)
self.add(location_type)
self.job.logger.debug(f"Loaded {location_type} LocationType from remote Nautobot instance")
def load_locations(self):
"""Load Locations data from the remote Nautobot instance.
Ensures parent Locations are loaded before their children by using
topological sorting based on parent-child relationships.
"""
# First, collect all location entries
location_entries = list(self._get_api_data("api/dcim/locations/", depth=3))
# Build a map of location names to their entries
entries_by_name = {entry["name"]: entry for entry in location_entries}
# Build dependency graph: map parent name -> list of child entries
children_by_parent = {}
root_entries = [] # Entries with no parent
for entry in location_entries:
parent_name = entry.get("parent", {}).get("name") if entry.get("parent") else None
if parent_name:
if parent_name not in children_by_parent:
children_by_parent[parent_name] = []
children_by_parent[parent_name].append(entry)
else:
root_entries.append(entry)
# Topological sort: process parents before children
processed = set()
ordered_entries = []
def process_entry(entry):
"""Process an entry and its children recursively."""
entry_name = entry["name"]
if entry_name in processed:
return
# Process parent first if it exists and hasn't been processed
parent_name = entry.get("parent", {}).get("name") if entry.get("parent") else None
if parent_name and parent_name in entries_by_name:
parent_entry = entries_by_name[parent_name]
if parent_entry["name"] not in processed:
process_entry(parent_entry)
# Now process this entry
ordered_entries.append(entry)
processed.add(entry_name)
# Process children
if entry_name in children_by_parent:
for child_entry in children_by_parent[entry_name]:
process_entry(child_entry)
# Process all root entries (those with no parent)
for root_entry in root_entries:
process_entry(root_entry)
# Handle any remaining entries that might have been missed
# (e.g., circular references or orphaned entries)
for entry in location_entries:
if entry["name"] not in processed:
process_entry(entry)
# Load locations in the correct order
for loc_entry in ordered_entries:
location_args = {
"name": loc_entry["name"],
"status__name": loc_entry["status"]["name"] if loc_entry["status"].get("name") else "Active",
"location_type__name": loc_entry["location_type"]["name"],
"tenant__name": loc_entry["tenant"]["name"] if loc_entry.get("tenant") else None,
"description": loc_entry["description"],
"tags": loc_entry["tags"] if loc_entry.get("tags") else [],
"pk": loc_entry["id"],
}
if loc_entry["parent"]:
location_args["parent__name"] = loc_entry["parent"]["name"]
location_args["parent__location_type__name"] = loc_entry["parent"]["location_type"]["name"]
new_location = self.location(**location_args)
self.add(new_location)
self.job.logger.debug(f"Loaded {new_location} Location from remote Nautobot instance")
def load_roles(self):
"""Load Roles data from the remote Nautobot instance."""
for role_entry in self._get_api_data("api/extras/roles/", depth=1):
content_types = self.get_content_types(role_entry)
role = self.role(
name=role_entry["name"],
content_types=content_types,
pk=role_entry["id"],
)
self.add(role)
def load_statuses(self):
"""Load Statuses data from the remote Nautobot instance."""
for status_entry in self._get_api_data("api/extras/statuses/", depth=1):
content_types = self.get_content_types(status_entry)
status = self.status(
name=status_entry["name"],
color=status_entry["color"],
content_types=content_types,
pk=status_entry["id"],
)
self.add(status)
def load_tenants(self):
"""Load Tenants data from the remote Nautobot instance."""
for tenant_entry in self._get_api_data("api/tenancy/tenants/", depth=1):
tenant = self.tenant(
name=tenant_entry["name"],
tags=tenant_entry["tags"] if tenant_entry.get("tags") else [],
pk=tenant_entry["id"],
)
self.add(tenant)
def load_namespaces(self):
"""Load Namespaces data from remote Nautobot instance."""
for namespace_entry in self._get_api_data("api/ipam/namespaces/", depth=1):
namespace = self.namespace(
name=namespace_entry["name"],
description=namespace_entry["description"],
tags=namespace_entry["tags"] if namespace_entry.get("tags") else [],
pk=namespace_entry["id"],
)
self.add(namespace)
def load_prefixes(self):
"""Load Prefixes data from the remote Nautobot instance."""
for prefix_entry in self._get_api_data("api/ipam/prefixes/", depth=2):
prefix = self.prefix(
network=prefix_entry["network"],
prefix_length=prefix_entry["prefix_length"],
namespace__name=prefix_entry["namespace"]["name"],
description=prefix_entry["description"],
locations=[
{"name": x["name"], "location_type__name": x["location_type"]["name"]}
for x in prefix_entry["locations"]
],
status__name=prefix_entry["status"]["name"] if prefix_entry["status"].get("name") else "Active",
tenant__name=prefix_entry["tenant"]["name"] if prefix_entry["tenant"] else None,
tags=prefix_entry["tags"] if prefix_entry.get("tags") else [],
pk=prefix_entry["id"],
)
self.add(prefix)
self.job.logger.debug(f"Loaded {prefix} from remote Nautobot instance")
def load_ipaddresses(self):
"""Load IPAddresses data from the remote Nautobot instance."""
for ipaddr_entry in self._get_api_data("api/ipam/ip-addresses/", depth=2):
ipaddr = self.ipaddress(
host=ipaddr_entry["host"],
mask_length=ipaddr_entry["mask_length"],
parent__network=ipaddr_entry["parent"]["network"],
parent__prefix_length=ipaddr_entry["parent"]["prefix_length"],
parent__namespace__name=ipaddr_entry["parent"]["namespace"]["name"],
status__name=ipaddr_entry["status"]["name"],
ip_version=ipaddr_entry["ip_version"],
tenant__name=ipaddr_entry["tenant"]["name"] if ipaddr_entry.get("tenant") else None,
tags=ipaddr_entry["tags"] if ipaddr_entry.get("tags") else [],
pk=ipaddr_entry["id"],
)
self.add(ipaddr)
self.job.logger.debug(f"Loaded {ipaddr} from remote Nautobot instance")
def load_manufacturers(self):
"""Load Manufacturers data from the remote Nautobot instance."""
for manufacturer in self._get_api_data("api/dcim/manufacturers/", depth=1):
manufacturer = self.manufacturer(
name=manufacturer["name"],
description=manufacturer["description"],
pk=manufacturer["id"],
)
self.add(manufacturer)
self.job.logger.debug(f"Loaded {manufacturer} from remote Nautobot instance")
def load_device_types(self):
"""Load DeviceTypes data from the remote Nautobot instance."""
for device_type in self._get_api_data("api/dcim/device-types/", depth=1):
try:
manufacturer = self.get(self.manufacturer, device_type["manufacturer"]["name"])
devicetype = self.device_type(
model=device_type["model"],
manufacturer__name=device_type["manufacturer"]["name"] if device_type.get("manufacturer") else "",
part_number=device_type["part_number"],
u_height=device_type["u_height"],
is_full_depth=device_type["is_full_depth"],
tags=device_type["tags"] if device_type.get("tags") else [],
pk=device_type["id"],
)
self.add(devicetype)
self.job.logger.debug(f"Loaded {devicetype} from remote Nautobot instance")
manufacturer.add_child(devicetype)
except ObjectNotFound:
self.job.logger.debug(f"Unable to find Manufacturer {device_type['manufacturer']['name']}")
def load_platforms(self):
"""Load Platforms data from the remote Nautobot instance."""
for platform in self._get_api_data("api/dcim/platforms/", depth=1):
platform = self.platform(
name=platform["name"],
manufacturer__name=platform["manufacturer"]["name"] if platform.get("manufacturer") else "",
description=platform["description"],
network_driver=platform["network_driver"],
napalm_driver=platform["napalm_driver"],
pk=platform["id"],
)
self.add(platform)
self.job.logger.debug(f"Loaded {platform} from remote Nautobot instance")
def load_devices(self):
"""Load Devices data from the remote Nautobot instance."""
for device in self._get_api_data("api/dcim/devices/", depth=3):
device = self.device(
name=device["name"],
location__name=device["location"]["name"],
location__parent__name=(
device["location"]["parent"]["name"] if device["location"].get("parent") else None
),
location__parent__location_type__name=(
device["location"]["parent"]["location_type"]["name"] if device["location"].get("parent") else None
),
location__location_type__name=device["location"]["location_type"]["name"],
device_type__manufacturer__name=device["device_type"]["manufacturer"]["name"],
device_type__model=device["device_type"]["model"],
platform__name=device["platform"]["name"] if device.get("platform") else None,
role__name=device["role"]["name"],
asset_tag=device["asset_tag"] if device.get("asset_tag") else None,
serial=device["serial"] if device.get("serial") else "",
status__name=device["status"]["name"],
tenant__name=device["tenant"]["name"] if device.get("tenant") else None,
tags=device["tags"] if device.get("tags") else [],
pk=device["id"],
)
self.add(device)
self.job.logger.debug(f"Loaded {device} from remote Nautobot instance")
def load_interfaces(self):
"""Load Interfaces data from the remote Nautobot instance."""
self.job.logger.info("Pulling data from remote Nautobot instance for Interfaces.")
for device in self.get_all(self.device):
for interface in self._get_api_data("api/dcim/interfaces/", depth=3, device=device.pk):
if not interface.get("device"):
self.job.logger.warning(
f"Skipping Interface {interface['name']} because it has no Device associated with it."
)
continue
new_interface = self.interface(
name=interface["name"],
device__name=interface["device"]["name"],
device__location__name=interface["device"]["location"]["name"],
device__location__parent__name=interface["device"]["location"]["parent"]["name"],
description=interface["description"],
enabled=interface["enabled"],
mac_address=interface["mac_address"],
mgmt_only=interface["mgmt_only"],
mtu=interface["mtu"],
type=interface["type"]["value"],
status__name=interface["status"]["name"],
tags=interface["tags"] if interface.get("tags") else [],
pk=interface["id"],
)
self.add(new_interface)
self.job.logger.debug(
f"Loaded {new_interface} for {interface['device']['name']} from remote Nautobot instance"
)
device.add_child(new_interface)
def get_content_types(self, entry):
"""Create list of dicts of ContentTypes.
Args:
entry (dict): Record from Nautobot.
Returns:
List[dict]: List of dictionaries of ContentTypes split into app_label and model.
"""
content_types = []
for contenttype in entry["content_types"]:
app_label, model = tuple(contenttype.split("."))
try:
ContentType.objects.get(app_label=app_label, model=model)
content_types.append({"app_label": app_label, "model": model})
except ContentType.DoesNotExist:
pass
return content_types
def post(self, path, data):
"""Send an appropriately constructed HTTP POST request."""
response = requests.post(f"{self.url}{path}", headers=self.headers, json=data, timeout=600)
response.raise_for_status()
return response
def patch(self, path, data):
"""Send an appropriately constructed HTTP PATCH request."""
response = requests.patch(f"{self.url}{path}", headers=self.headers, json=data, timeout=600)
response.raise_for_status()
return response
def delete(self, path):
"""Send an appropriately constructed HTTP DELETE request."""
response = requests.delete(f"{self.url}{path}", headers=self.headers, timeout=600)
response.raise_for_status()
return response
class NautobotLocal(NautobotAdapter):
"""DiffSync adapter class for loading data from the local Nautobot instance."""
# Model classes used by this adapter class
locationtype = LocationTypeModel
location = LocationModel
tenant = TenantModel
namespace = NamespaceModel
prefix = PrefixModel
ipaddress = IPAddressModel
manufacturer = ManufacturerModel
device_type = DeviceTypeModel
platform = PlatformModel
role = RoleModel
status = StatusModel
device = DeviceModel
interface = InterfaceModel
# Top-level class labels, i.e. those classes that are handled directly rather than as children of other models
top_level = [
"tenant",
"status",
"locationtype",
"location",
"manufacturer",
"platform",
"role",
"device",
"namespace",
"prefix",
"ipaddress",
]
# The actual Data Source and Data Target Jobs are relatively simple to implement
# once you have the above DiffSync scaffolding in place.
class ExampleDataSource(DataSource): # pylint: disable=too-many-instance-attributes
"""Sync Region and Site data from a remote Nautobot instance into the local Nautobot instance."""
source = ObjectVar(
model=ExternalIntegration,
queryset=ExternalIntegration.objects.all(),
display_field="display",
label="Nautobot Demo Instance",
required=False,
)
source_url = StringVar(
description="Remote Nautobot instance to load Sites and Regions from", default="https://demo.nautobot.com"
)
source_token = StringVar(description="REST API authentication token for remote Nautobot instance", default="a" * 40)
def __init__(self):
"""Initialize ExampleDataSource."""
super().__init__()
self.diffsync_flags = (
self.diffsync_flags | DiffSyncFlags.SKIP_UNMATCHED_DST # pylint: disable=unsupported-binary-operation
)
class Meta:
"""Metaclass attributes of ExampleDataSource."""
name = "Example Data Source"
description = 'Example "data source" Job for loading data into Nautobot from another system.'
data_source = "Nautobot (remote)"
data_source_icon = static("img/nautobot_logo.png")
@classmethod
def data_mappings(cls):
"""This Job maps Region and Site objects from the remote system to the local system."""
return (
DataMapping("LocationType (remote)", None, "LocationType (local)", reverse("dcim:locationtype_list")),
DataMapping("Location (remote)", None, "Location (local)", reverse("dcim:location_list")),
DataMapping("Role (remote)", None, "Role (local)", reverse("extras:role_list")),
DataMapping("Namespace (remote)", None, "Namespace (local)", reverse("ipam:namespace_list")),
DataMapping("Prefix (remote)", None, "Prefix (local)", reverse("ipam:prefix_list")),
DataMapping("IPAddress (remote)", None, "IPAddress (local)", reverse("ipam:ipaddress_list")),
DataMapping("Tenant (remote)", None, "Tenant (local)", reverse("tenancy:tenant_list")),
DataMapping("DeviceType (remote)", None, "DeviceType (local)", reverse("dcim:devicetype_list")),
DataMapping("Manufacturer (remote)", None, "Manufacturer (local)", reverse("dcim:manufacturer_list")),
DataMapping("Platform (remote)", None, "Platform (local)", reverse("dcim:platform_list")),
DataMapping("Device (remote)", None, "Device (local)", reverse("dcim:device_list")),
DataMapping("Interface (remote)", None, "Interface (local)", reverse("dcim:interface_list")),
)
def run(
self,
*args,