forked from netbox-community/ansible_modules
/
nb_inventory.py
1416 lines (1177 loc) · 53.5 KB
/
nb_inventory.py
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
# Copyright (c) 2018 Remy Leone
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import absolute_import, division, print_function
__metaclass__ = type
DOCUMENTATION = """
name: nb_inventory
plugin_type: inventory
author:
- Remy Leone (@sieben)
- Anthony Ruhier (@Anthony25)
- Nikhil Singh Baliyan (@nikkytub)
- Sander Steffann (@steffann)
- Douglas Heriot (@DouglasHeriot)
short_description: NetBox inventory source
description:
- Get inventory hosts from NetBox
extends_documentation_fragment:
- constructed
- inventory_cache
options:
plugin:
description: token that ensures this is a source file for the 'netbox' plugin.
required: True
choices: ['netbox.netbox.nb_inventory']
api_endpoint:
description: Endpoint of the NetBox API
required: True
env:
- name: NETBOX_API
validate_certs:
description:
- Allows connection when SSL certificates are not valid. Set to C(false) when certificates are not trusted.
default: True
type: boolean
follow_redirects:
description:
- Determine how redirects are followed.
- By default, I(follow_redirects) is set to uses urllib2 default behavior.
default: urllib2
choices: ['urllib2', 'all', 'yes', 'safe', 'none']
config_context:
description:
- If True, it adds config_context in host vars.
- Config-context enables the association of arbitrary data to devices and virtual machines grouped by
region, site, role, platform, and/or tenant. Please check official netbox docs for more info.
default: False
type: boolean
flatten_config_context:
description:
- If I(config_context) is enabled, by default it's added as a host var named config_context.
- If flatten_config_context is set to True, the config context variables will be added directly to the host instead.
default: False
type: boolean
version_added: "0.2.1"
flatten_local_context_data:
description:
- If I(local_context_data) is enabled, by default it's added as a host var named local_context_data.
- If flatten_local_context_data is set to True, the config context variables will be added directly to the host instead.
default: False
type: boolean
version_added: "0.3.0"
flatten_custom_fields:
description:
- By default, host custom fields are added as a dictionary host var named custom_fields.
- If flatten_custom_fields is set to True, the fields will be added directly to the host instead.
default: False
type: boolean
version_added: "0.2.1"
token:
required: False
description:
- NetBox API token to be able to read against NetBox.
- This may not be required depending on the NetBox setup.
env:
# in order of precedence
- name: NETBOX_TOKEN
- name: NETBOX_API_KEY
plurals:
description:
- If True, all host vars are contained inside single-element arrays for legacy compatibility with old versions of this plugin.
- Group names will be plural (ie. "sites_mysite" instead of "site_mysite")
- The choices of I(group_by) will be changed by this option.
default: True
type: boolean
version_added: "0.2.1"
interfaces:
description:
- If True, it adds the device or virtual machine interface information in host vars.
default: False
type: boolean
version_added: "0.1.7"
services:
description:
- If True, it adds the device or virtual machine services information in host vars.
default: True
type: boolean
version_added: "0.2.0"
fetch_all:
description:
- By default, fetching interfaces and services will get all of the contents of NetBox regardless of query_filters applied to devices and VMs.
- When set to False, separate requests will be made fetching interfaces, services, and IP addresses for each device_id and virtual_machine_id.
- If you are using the various query_filters options to reduce the number of devices, you may find querying Netbox faster with fetch_all set to False.
- For efficiency, when False, these requests will be batched, for example /api/dcim/interfaces?limit=0&device_id=1&device_id=2&device_id=3
- These GET request URIs can become quite large for a large number of devices. If you run into HTTP 414 errors, you can adjust the max_uri_length option to suit your web server.
default: True
type: boolean
version_added: "0.2.1"
group_by:
description: Keys used to create groups. The I(plurals) option controls which of these are valid.
type: list
choices:
- sites
- site
- tenants
- tenant
- racks
- rack
- rack_group
- rack_role
- tags
- tag
- device_roles
- role
- device_types
- device_type
- manufacturers
- manufacturer
- platforms
- platform
- region
- cluster
- cluster_type
- cluster_group
- is_virtual
- services
default: []
group_names_raw:
description: Will not add the group_by choice name to the group names
default: False
type: boolean
version_added: "0.2.0"
query_filters:
description: List of parameters passed to the query string for both devices and VMs (Multiple values may be separated by commas)
type: list
default: []
device_query_filters:
description: List of parameters passed to the query string for devices (Multiple values may be separated by commas)
type: list
default: []
vm_query_filters:
description: List of parameters passed to the query string for VMs (Multiple values may be separated by commas)
type: list
default: []
timeout:
description: Timeout for Netbox requests in seconds
type: int
default: 60
max_uri_length:
description:
- When fetch_all is False, GET requests to NetBox may become quite long and return a HTTP 414 (URI Too Long).
- You can adjust this option to be smaller to avoid 414 errors, or larger for a reduced number of requests.
type: int
default: 4000
version_added: "0.2.1"
compose:
description: List of custom ansible host vars to create from the device object fetched from NetBox
default: {}
type: dict
"""
EXAMPLES = """
# netbox_inventory.yml file in YAML format
# Example command line: ansible-inventory -v --list -i netbox_inventory.yml
plugin: netbox.netbox.nb_inventory
api_endpoint: http://localhost:8000
validate_certs: True
config_context: False
group_by:
- device_roles
query_filters:
- role: network-edge-router
device_query_filters:
- has_primary_ip: 'true'
# has_primary_ip is a useful way to filter out patch panels and other passive devices
# Query filters are passed directly as an argument to the fetching queries.
# You can repeat tags in the query string.
query_filters:
- role: server
- tag: web
- tag: production
# See the NetBox documentation at https://netbox.readthedocs.io/en/latest/api/overview/
# the query_filters work as a logical **OR**
#
# Prefix any custom fields with cf_ and pass the field value with the regular NetBox query string
query_filters:
- cf_foo: bar
# NetBox inventory plugin also supports Constructable semantics
# You can fill your hosts vars using the compose option:
plugin: netbox.netbox.nb_inventory
compose:
foo: last_updated
bar: display_name
nested_variable: rack.display_name
"""
import json
import uuid
import math
from functools import partial
from sys import version as python_version
from threading import Thread
from typing import Iterable
from itertools import chain
from collections import defaultdict
from ansible.plugins.inventory import BaseInventoryPlugin, Constructable, Cacheable
from ansible.module_utils.ansible_release import __version__ as ansible_version
from ansible.errors import AnsibleError
from ansible.module_utils._text import to_text, to_native
from ansible.module_utils.urls import open_url
from ansible.module_utils.six.moves.urllib import error as urllib_error
from ansible.module_utils.six.moves.urllib.parse import urlencode
from ansible_collections.ansible.netcommon.plugins.module_utils.compat.ipaddress import (
ip_interface,
)
class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
NAME = "netbox.netbox.nb_inventory"
def _fetch_information(self, url):
results = None
cache_key = self.get_cache_key(url)
# get the user's cache option to see if we should save the cache if it is changing
user_cache_setting = self.get_option("cache")
# read if the user has caching enabled and the cache isn't being refreshed
attempt_to_read_cache = user_cache_setting and self.use_cache
# attempt to read the cache if inventory isn't being refreshed and the user has caching enabled
if attempt_to_read_cache:
try:
results = self._cache[cache_key]
need_to_fetch = False
except KeyError:
# occurs if the cache_key is not in the cache or if the cache_key expired
# we need to fetch the URL now
need_to_fetch = True
else:
# not reading from cache so do fetch
need_to_fetch = True
if need_to_fetch:
self.display.v("Fetching: " + url)
try:
response = open_url(
url,
headers=self.headers,
timeout=self.timeout,
validate_certs=self.validate_certs,
follow_redirects=self.follow_redirects,
)
except urllib_error.HTTPError as e:
"""This will return the response body when we encounter an error.
This is to help determine what might be the issue when encountering an error.
Please check issue #294 for more info.
"""
raise AnsibleError(to_native(e.fp.read()))
try:
raw_data = to_text(response.read(), errors="surrogate_or_strict")
except UnicodeError:
raise AnsibleError(
"Incorrect encoding of fetched payload from NetBox API."
)
try:
results = json.loads(raw_data)
except ValueError:
raise AnsibleError("Incorrect JSON payload: %s" % raw_data)
# put result in cache if enabled
if user_cache_setting:
self._cache[cache_key] = results
return results
def get_resource_list(self, api_url):
"""Retrieves resource list from netbox API.
Returns:
A list of all resource from netbox API.
"""
if not api_url:
raise AnsibleError("Please check API URL in script configuration file.")
resources = []
# Handle pagination
while api_url:
api_output = self._fetch_information(api_url)
resources.extend(api_output["results"])
api_url = api_output["next"]
return resources
def get_resource_list_chunked(self, api_url, query_key, query_values):
# Make an API call for multiple specific IDs, like /api/ipam/ip-addresses?limit=0&device_id=1&device_id=2&device_id=3
# Drastically cuts down HTTP requests comnpared to 1 request per host, in the case where we don't want to fetch_all
# Make sure query_values is subscriptable
if not isinstance(query_values, list):
query_values = list(query_values)
def query_string(value, separator="&"):
return separator + query_key + "=" + str(value)
# Calculate how many queries we can do per API call to stay within max_url_length
largest_value = str(max(query_values, default=0)) # values are always id ints
length_per_value = len(query_string(largest_value))
chunk_size = math.floor((self.max_uri_length - len(api_url)) / length_per_value)
# Sanity check, for case where max_uri_length < (api_url + length_per_value)
if chunk_size < 1:
chunk_size = 1
if self.api_version == "2.6":
# Issue netbox-community/netbox#3507 was fixed in v2.7.5
# If using NetBox v2.7.0-v2.7.4 will have to manually set max_uri_length to 0,
# but it's probably faster to keep fetch_all: True
# (You should really just upgrade your Netbox install)
chunk_size = 1
resources = []
for i in range(0, len(query_values), chunk_size):
chunk = query_values[i : i + chunk_size]
# process chunk of size <= chunk_size
url = api_url
for value in chunk:
url += query_string(value, "&" if "?" in url else "?")
resources.extend(self.get_resource_list(url))
return resources
@property
def group_extractors(self):
# List of group_by options and hostvars to extract
# Some keys are different depending on plurals option
extractors = {
"disk": self.extract_disk,
"memory": self.extract_memory,
"vcpus": self.extract_vcpus,
"config_context": self.extract_config_context,
"local_context_data": self.extract_local_context_data,
"custom_fields": self.extract_custom_fields,
"region": self.extract_regions,
"cluster": self.extract_cluster,
"cluster_group": self.extract_cluster_group,
"cluster_type": self.extract_cluster_type,
"is_virtual": self.extract_is_virtual,
self._pluralize_group_by("site"): self.extract_site,
self._pluralize_group_by("tenant"): self.extract_tenant,
self._pluralize_group_by("rack"): self.extract_rack,
"rack_group": self.extract_rack_group,
"rack_role": self.extract_rack_role,
self._pluralize_group_by("tag"): self.extract_tags,
self._pluralize_group_by("role"): self.extract_device_role,
self._pluralize_group_by("platform"): self.extract_platform,
self._pluralize_group_by("device_type"): self.extract_device_type,
self._pluralize_group_by("manufacturer"): self.extract_manufacturer,
}
if self.services:
extractors.update(
{"services": self.extract_services,}
)
if self.interfaces:
extractors.update(
{"interfaces": self.extract_interfaces,}
)
return extractors
def _pluralize_group_by(self, group_by):
mapping = {
"site": "sites",
"tenant": "tenants",
"rack": "racks",
"tag": "tags",
"role": "device_roles",
"platform": "platforms",
"device_type": "device_types",
"manufacturer": "manufacturers",
}
if self.plurals:
mapped = mapping.get(group_by)
return mapped or group_by
else:
return group_by
def _pluralize(self, extracted_value):
# If plurals is enabled, wrap in a single-element list for backwards compatibility
if self.plurals:
return [extracted_value]
else:
return extracted_value
def _objects_array_following_parents(
self, initial_object_id, object_lookup, object_parent_lookup
):
objects = []
object_id = initial_object_id
# Keep looping until the object has no parent
while object_id is not None:
object_slug = object_lookup[object_id]
if object_slug in objects:
# Won't ever happen - defensively guard against infinite loop
break
objects.append(object_slug)
# Get the parent of this object
object_id = object_parent_lookup[object_id]
return objects
def extract_disk(self, host):
return host.get("disk")
def extract_vcpus(self, host):
return host.get("vcpus")
def extract_memory(self, host):
return host.get("memory")
def extract_platform(self, host):
try:
return self._pluralize(self.platforms_lookup[host["platform"]["id"]])
except Exception:
return
def extract_services(self, host):
try:
services_lookup = (
self.vm_services_lookup
if host["is_virtual"]
else self.device_services_lookup
)
return list(services_lookup[host["id"]].values())
except Exception:
return
def extract_device_type(self, host):
try:
return self._pluralize(self.device_types_lookup[host["device_type"]["id"]])
except Exception:
return
def extract_rack(self, host):
try:
return self._pluralize(self.racks_lookup[host["rack"]["id"]])
except Exception:
return
def extract_rack_group(self, host):
# A host may have a rack. A rack may have a rack_group. A rack_group may have a parent rack_group.
# Produce a list of rack_groups:
# - it will be empty if the device has no rack, or the rack has no rack_group
# - it will have 1 element if the rack's group has no parent
# - it will have multiple elements if the rack's group has a parent group
rack = host.get("rack", None)
if not isinstance(rack, dict):
# Device has no rack
return None
rack_id = rack.get("id", None)
if rack_id is None:
# Device has no rack
return None
return self._objects_array_following_parents(
initial_object_id=self.racks_group_lookup[rack_id],
object_lookup=self.rack_groups_lookup,
object_parent_lookup=self.rack_group_parent_lookup,
)
def extract_rack_role(self, host):
try:
return self.racks_role_lookup[host["rack"]["id"]]
except Exception:
return
def extract_site(self, host):
try:
return self._pluralize(self.sites_lookup[host["site"]["id"]])
except Exception:
return
def extract_tenant(self, host):
try:
return self._pluralize(self.tenants_lookup[host["tenant"]["id"]])
except Exception:
return
def extract_device_role(self, host):
try:
if "device_role" in host:
return self._pluralize(
self.device_roles_lookup[host["device_role"]["id"]]
)
elif "role" in host:
return self._pluralize(self.device_roles_lookup[host["role"]["id"]])
except Exception:
return
def extract_config_context(self, host):
try:
if self.flatten_config_context:
# Don't wrap in an array if we're about to flatten it to separate host vars
return host["config_context"]
else:
return self._pluralize(host["config_context"])
except Exception:
return
def extract_local_context_data(self, host):
try:
if self.flatten_local_context_data:
# Don't wrap in an array if we're about to flatten it to separate host vars
return host["local_context_data"]
else:
return self._pluralize(host["local_context_data"])
except Exception:
return
def extract_manufacturer(self, host):
try:
return self._pluralize(
self.manufacturers_lookup[host["device_type"]["manufacturer"]["id"]]
)
except Exception:
return
def extract_primary_ip(self, host):
try:
address = host["primary_ip"]["address"]
return str(ip_interface(address).ip)
except Exception:
return
def extract_primary_ip4(self, host):
try:
address = host["primary_ip4"]["address"]
return str(ip_interface(address).ip)
except Exception:
return
def extract_primary_ip6(self, host):
try:
address = host["primary_ip6"]["address"]
return str(ip_interface(address).ip)
except Exception:
return
def extract_tags(self, host):
return host["tags"]
def extract_interfaces(self, host):
try:
interfaces_lookup = (
self.vm_interfaces_lookup
if host["is_virtual"]
else self.device_interfaces_lookup
)
interfaces = list(interfaces_lookup[host["id"]].values())
before_netbox_v29 = bool(self.ipaddresses_lookup)
# Attach IP Addresses to their interface
for interface in interfaces:
if before_netbox_v29:
interface["ip_addresses"] = list(
self.ipaddresses_lookup[interface["id"]].values()
)
else:
interface["ip_addresses"] = list(
self.vm_ipaddresses_lookup[interface["id"]].values()
if host["is_virtual"]
else self.device_ipaddresses_lookup[interface["id"]].values()
)
return interfaces
except Exception:
return
def extract_custom_fields(self, host):
try:
return host["custom_fields"]
except Exception:
return
def extract_regions(self, host):
# A host may have a site. A site may have a region. A region may have a parent region.
# Produce a list of regions:
# - it will be empty if the device has no site, or the site has no region set
# - it will have 1 element if the site's region has no parent
# - it will have multiple elements if the site's region has a parent region
site = host.get("site", None)
if not isinstance(site, dict):
# Device has no site
return []
site_id = site.get("id", None)
if site_id is None:
# Device has no site
return []
return self._objects_array_following_parents(
initial_object_id=self.sites_region_lookup[site_id],
object_lookup=self.regions_lookup,
object_parent_lookup=self.regions_parent_lookup,
)
def extract_cluster(self, host):
try:
# cluster does not have a slug
return host["cluster"]["name"]
except Exception:
return
def extract_cluster_group(self, host):
try:
return self.clusters_group_lookup[host["cluster"]["id"]]
except Exception:
return
def extract_cluster_type(self, host):
try:
return self.clusters_type_lookup[host["cluster"]["id"]]
except Exception:
return
def extract_is_virtual(self, host):
return host.get("is_virtual")
def refresh_platforms_lookup(self):
url = self.api_endpoint + "/api/dcim/platforms/?limit=0"
platforms = self.get_resource_list(api_url=url)
self.platforms_lookup = dict(
(platform["id"], platform["slug"]) for platform in platforms
)
def refresh_sites_lookup(self):
url = self.api_endpoint + "/api/dcim/sites/?limit=0"
sites = self.get_resource_list(api_url=url)
self.sites_lookup = dict((site["id"], site["slug"]) for site in sites)
def get_region_for_site(site):
# Will fail if site does not have a region defined in Netbox
try:
return (site["id"], site["region"]["id"])
except Exception:
return (site["id"], None)
# Dictionary of site id to region id
self.sites_region_lookup = dict(map(get_region_for_site, sites))
def refresh_regions_lookup(self):
url = self.api_endpoint + "/api/dcim/regions/?limit=0"
regions = self.get_resource_list(api_url=url)
self.regions_lookup = dict((region["id"], region["slug"]) for region in regions)
def get_region_parent(region):
# Will fail if region does not have a parent region
try:
return (region["id"], region["parent"]["id"])
except Exception:
return (region["id"], None)
# Dictionary of region id to parent region id
self.regions_parent_lookup = dict(
filter(lambda x: x is not None, map(get_region_parent, regions))
)
def refresh_tenants_lookup(self):
url = self.api_endpoint + "/api/tenancy/tenants/?limit=0"
tenants = self.get_resource_list(api_url=url)
self.tenants_lookup = dict((tenant["id"], tenant["slug"]) for tenant in tenants)
def refresh_racks_lookup(self):
url = self.api_endpoint + "/api/dcim/racks/?limit=0"
racks = self.get_resource_list(api_url=url)
self.racks_lookup = dict((rack["id"], rack["name"]) for rack in racks)
def get_group_for_rack(rack):
try:
return (rack["id"], rack["group"]["id"])
except Exception:
return (rack["id"], None)
def get_role_for_rack(rack):
try:
return (rack["id"], rack["role"]["slug"])
except Exception:
return (rack["id"], None)
self.racks_group_lookup = dict(map(get_group_for_rack, racks))
self.racks_role_lookup = dict(map(get_role_for_rack, racks))
def refresh_rack_groups_lookup(self):
url = self.api_endpoint + "/api/dcim/rack-groups/?limit=0"
rack_groups = self.get_resource_list(api_url=url)
self.rack_groups_lookup = dict(
(rack_group["id"], rack_group["slug"]) for rack_group in rack_groups
)
def get_rack_group_parent(rack_group):
try:
return (rack_group["id"], rack_group["parent"]["id"])
except Exception:
return (rack_group["id"], None)
# Dictionary of rack group id to parent rack group id
self.rack_group_parent_lookup = dict(map(get_rack_group_parent, rack_groups))
def refresh_device_roles_lookup(self):
url = self.api_endpoint + "/api/dcim/device-roles/?limit=0"
device_roles = self.get_resource_list(api_url=url)
self.device_roles_lookup = dict(
(device_role["id"], device_role["slug"]) for device_role in device_roles
)
def refresh_device_types_lookup(self):
url = self.api_endpoint + "/api/dcim/device-types/?limit=0"
device_types = self.get_resource_list(api_url=url)
self.device_types_lookup = dict(
(device_type["id"], device_type["slug"]) for device_type in device_types
)
def refresh_manufacturers_lookup(self):
url = self.api_endpoint + "/api/dcim/manufacturers/?limit=0"
manufacturers = self.get_resource_list(api_url=url)
self.manufacturers_lookup = dict(
(manufacturer["id"], manufacturer["slug"]) for manufacturer in manufacturers
)
def refresh_clusters_lookup(self):
url = self.api_endpoint + "/api/virtualization/clusters/?limit=0"
clusters = self.get_resource_list(api_url=url)
def get_cluster_type(cluster):
# Will fail if cluster does not have a type (required property so should always be true)
try:
return (cluster["id"], cluster["type"]["slug"])
except Exception:
return (cluster["id"], None)
def get_cluster_group(cluster):
# Will fail if cluster does not have a group (group is optional)
try:
return (cluster["id"], cluster["group"]["slug"])
except Exception:
return (cluster["id"], None)
self.clusters_type_lookup = dict(map(get_cluster_type, clusters))
self.clusters_group_lookup = dict(map(get_cluster_group, clusters))
def refresh_services(self):
url = self.api_endpoint + "/api/ipam/services/?limit=0"
services = []
if self.fetch_all:
services = self.get_resource_list(url)
else:
device_services = self.get_resource_list_chunked(
api_url=url,
query_key="device_id",
query_values=self.devices_lookup.keys(),
)
vm_services = self.get_resource_list_chunked(
api_url=url,
query_key="virtual_machine_id",
query_values=self.vms_lookup.keys(),
)
services = chain(device_services, vm_services)
# Construct a dictionary of dictionaries, separately for devices and vms.
# Allows looking up services by device id or vm id
self.device_services_lookup = defaultdict(dict)
self.vm_services_lookup = defaultdict(dict)
for service in services:
service_id = service["id"]
if service.get("device"):
self.device_services_lookup[service["device"]["id"]][
service_id
] = service
if service.get("virtual_machine"):
self.vm_services_lookup[service["virtual_machine"]["id"]][
service_id
] = service
def refresh_interfaces(self):
url_device_interfaces = self.api_endpoint + "/api/dcim/interfaces/?limit=0"
url_vm_interfaces = (
self.api_endpoint + "/api/virtualization/interfaces/?limit=0"
)
device_interfaces = []
vm_interfaces = []
if self.fetch_all:
device_interfaces = self.get_resource_list(url_device_interfaces)
vm_interfaces = self.get_resource_list(url_vm_interfaces)
else:
device_interfaces = self.get_resource_list_chunked(
api_url=url_device_interfaces,
query_key="device_id",
query_values=self.devices_lookup.keys(),
)
vm_interfaces = self.get_resource_list_chunked(
api_url=url_vm_interfaces,
query_key="virtual_machine_id",
query_values=self.vms_lookup.keys(),
)
# Construct a dictionary of dictionaries, separately for devices and vms.
# For a given device id or vm id, get a lookup of interface id to interface
# This is because interfaces may be returned multiple times when querying for virtual chassis parent and child in separate queries
self.device_interfaces_lookup = defaultdict(dict)
self.vm_interfaces_lookup = defaultdict(dict)
# /dcim/interfaces gives count_ipaddresses per interface. /virtualization/interfaces does not
self.devices_with_ips = set()
for interface in device_interfaces:
interface_id = interface["id"]
device_id = interface["device"]["id"]
# Check if device_id is actually a device we've fetched, and was not filtered out by query_filters
if device_id not in self.devices_lookup:
continue
# Check if device_id is part of a virtual chasis
# If so, treat its interfaces as actually part of the master
device = self.devices_lookup[device_id]
virtual_chassis_master = self._get_host_virtual_chassis_master(device)
if virtual_chassis_master is not None:
device_id = virtual_chassis_master
self.device_interfaces_lookup[device_id][interface_id] = interface
# Keep track of what devices have interfaces with IPs, so if fetch_all is False we can avoid unnecessary queries
if interface["count_ipaddresses"] > 0:
self.devices_with_ips.add(device_id)
for interface in vm_interfaces:
interface_id = interface["id"]
vm_id = interface["virtual_machine"]["id"]
self.vm_interfaces_lookup[vm_id][interface_id] = interface
# Note: depends on the result of refresh_interfaces for self.devices_with_ips
def refresh_ipaddresses(self):
url = (
self.api_endpoint
+ "/api/ipam/ip-addresses/?limit=0&assigned_to_interface=true"
)
ipaddresses = []
if self.fetch_all:
ipaddresses = self.get_resource_list(url)
else:
device_ips = self.get_resource_list_chunked(
api_url=url,
query_key="device_id",
query_values=list(self.devices_with_ips),
)
vm_ips = self.get_resource_list_chunked(
api_url=url,
query_key="virtual_machine_id",
query_values=self.vms_lookup.keys(),
)
ipaddresses = chain(device_ips, vm_ips)
# Construct a dictionary of lists, to allow looking up ip addresses by interface id
# Note that interface ids share the same namespace for both devices and vms so this is a single dictionary
self.ipaddresses_lookup = defaultdict(dict)
# NetBox v2.9 and onwards
self.vm_ipaddresses_lookup = defaultdict(dict)
self.device_ipaddresses_lookup = defaultdict(dict)
for ipaddress in ipaddresses:
# As of NetBox v2.9 "assigned_object_x" replaces "interface"
if ipaddress.get("assigned_object_id"):
interface_id = ipaddress["assigned_object_id"]
ip_id = ipaddress["id"]
# We need to copy the ipaddress entry to preserve the original in case caching is used.
ipaddress_copy = ipaddress.copy()
if ipaddress["assigned_object_type"] == 'virtualization.vminterface':
self.vm_ipaddresses_lookup[interface_id][ip_id] = ipaddress_copy
else:
self.device_ipaddresses_lookup[interface_id][ip_id] = ipaddress_copy # Remove "assigned_object_X" attributes, as that's redundant when ipaddress is added to an interface
del ipaddress_copy["assigned_object_id"]
del ipaddress_copy["assigned_object_type"]
del ipaddress_copy["assigned_object"]
continue
if not ipaddress.get("interface"):
continue
interface_id = ipaddress["interface"]["id"]
ip_id = ipaddress["id"]
# We need to copy the ipaddress entry to preserve the original in case caching is used.
ipaddress_copy = ipaddress.copy()
self.ipaddresses_lookup[interface_id][ip_id] = ipaddress_copy
# Remove "interface" attribute, as that's redundant when ipaddress is added to an interface
del ipaddress_copy["interface"]
@property
def lookup_processes(self):
lookups = [
self.refresh_sites_lookup,
self.refresh_regions_lookup,
self.refresh_tenants_lookup,
self.refresh_racks_lookup,
self.refresh_rack_groups_lookup,
self.refresh_device_roles_lookup,
self.refresh_platforms_lookup,
self.refresh_device_types_lookup,
self.refresh_manufacturers_lookup,
self.refresh_clusters_lookup,
]
if self.interfaces:
lookups.append(self.refresh_interfaces)
if self.services:
lookups.append(self.refresh_services)
return lookups
@property
def lookup_processes_secondary(self):
lookups = []
if self.interfaces:
lookups.append(self.refresh_ipaddresses)
return lookups
def refresh_lookups(self, lookups):
# Exceptions that occur in threads by default are printed to stderr, and ignored by the main thread
# They need to be caught, and raised in the main thread to prevent further execution of this plugin
thread_exceptions = []
def handle_thread_exceptions(lookup):
def wrapper():
try:
lookup()
except Exception as e:
# Save for the main-thread to re-raise
# Also continue to raise on this thread, so the default handler can run to print to stderr
thread_exceptions.append(e)
raise e