Skip to content

Commit 14571f1

Browse files
committed
Code cleanup + fixes and docs
1 parent f6ac7e3 commit 14571f1

3 files changed

Lines changed: 146 additions & 46 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ This ensures stale objects are removed from NetBox keeping an accurate current s
3939
* aiodns==2.0.0
4040
* setuptools>=62.00.0
4141
* pyyaml==6.0
42+
* openstack
4243

4344
### Environment
4445
* NetBox >= 2.9

module/sources/openstack/connection.py

Lines changed: 11 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,13 @@ class OpenStackHandler(SourceBase):
8585
"user_domain": None,
8686
"project_domain": None,
8787
"group_name": "Openstack",
88-
"validate_tls_certs": False,
88+
"permitted_subnets": None,
8989
"cluster_exclude_filter": None,
9090
"cluster_include_filter": None,
9191
"host_exclude_filter": None,
9292
"host_include_filter": None,
9393
"vm_exclude_filter": None,
9494
"vm_include_filter": None,
95-
"permitted_subnets": None,
96-
"collect_hardware_asset_tag": True,
97-
"match_host_by_serial": True,
9895
"cluster_site_relation": None,
9996
"cluster_tag_relation": None,
10097
"cluster_tenant_relation": None,
@@ -108,14 +105,12 @@ class OpenStackHandler(SourceBase):
108105
"vm_tenant_relation": None,
109106
"dns_name_lookup": False,
110107
"custom_dns_servers": None,
108+
"validate_tls_certs": False,
111109
"set_primary_ip": "when-undefined",
110+
"skip_vm_platform": False,
112111
"skip_vm_comments": False,
113-
"skip_vm_templates": True,
114112
"strip_host_domain_name": False,
115-
"strip_vm_domain_name": False,
116-
"sync_tags": False,
117-
"sync_parent_tags": False,
118-
"sync_custom_attributes": False
113+
"strip_vm_domain_name": False
119114
}
120115

121116
deprecated_settings = {}
@@ -652,8 +647,7 @@ def add_device_vm_to_inventory(self, object_type, object_data, pnic_data=None, v
652647
Try to find object first based on the object data, interface MAC addresses and primary IPs.
653648
1. try to find by name and cluster/site
654649
2. try to find by mac addresses interfaces
655-
3. try to find by serial number (1st) or asset tag (2nd) (ESXi host)
656-
4. try to find by primary IP
650+
3. try to find by primary IP
657651
658652
IP addresses for each interface are added here as well. First they will be checked and added
659653
if all checks pass. For each IP address a matching IP prefix will be searched for. First we
@@ -742,21 +736,6 @@ def add_device_vm_to_inventory(self, object_type, object_data, pnic_data=None, v
742736

743737
device_vm_object = self.get_object_based_on_macs(object_type, nic_macs)
744738

745-
# look for devices with same serial or asset tag
746-
if object_type == NBDevice:
747-
748-
if device_vm_object is None and object_data.get("serial") is not None and \
749-
bool(self.match_host_by_serial) is True:
750-
log.debug2(f"No match found. Trying to find {object_type.name} based on serial number")
751-
752-
device_vm_object = self.inventory.get_by_data(object_type, data={"serial": object_data.get("serial")})
753-
754-
if device_vm_object is None and object_data.get("asset_tag") is not None:
755-
log.debug2(f"No match found. Trying to find {object_type.name} based on asset tag")
756-
757-
device_vm_object = self.inventory.get_by_data(object_type,
758-
data={"asset_tag": object_data.get("asset_tag")})
759-
760739
if device_vm_object is not None:
761740
log.debug2("Found a matching %s object: %s" %
762741
(object_type.name, device_vm_object.get_display_name(including_second_key=True)))
@@ -919,13 +898,8 @@ def add_host(self, obj):
919898
does the host pass the host_include_filter and host_exclude_filter
920899
921900
Then all necessary host data will be collected.
922-
host model, manufacturer, serial, physical interfaces, virtual interfaces,
923-
virtual switches, proxy switches, host port groups, interface VLANs, IP addresses
924901
925-
Primary IPv4/6 will be determined by
926-
1. if the interface port group name contains
927-
"management" or "mngt"
928-
2. interface is the default route of this host
902+
Primary IPv4/6 will be determined by 'host_ip' value
929903
930904
Parameters
931905
----------
@@ -1005,9 +979,6 @@ def add_host(self, obj):
1005979
if get_string_or_none(obj.status) == "enabled":
1006980
status = "active"
1007981

1008-
# add asset tag if desired and present
1009-
asset_tag = None
1010-
1011982
# get host_tenant_relation
1012983
tenant_name = self.get_object_relation(name, "host_tenant_relation")
1013984

@@ -1029,8 +1000,6 @@ def add_host(self, obj):
10291000
}
10301001

10311002
# add data if present
1032-
if asset_tag is not None:
1033-
host_data["asset_tag"] = asset_tag
10341003
if platform is not None:
10351004
host_data["platform"] = {"name": platform}
10361005
if tenant_name is not None:
@@ -1078,7 +1047,6 @@ def add_virtual_machine(self, obj):
10781047
# get VM power state
10791048
status = "active" if get_string_or_none(obj.status) == "ACTIVE" else "offline"
10801049

1081-
# hypervisor_name = get_string_or_none(obj.hypervisor_hostname)
10821050
cluster_name = get_string_or_none(obj.availability_zone)
10831051

10841052
# honor strip_host_domain_name
@@ -1113,16 +1081,13 @@ def add_virtual_machine(self, obj):
11131081
# Collect data
11141082
#
11151083

1116-
# check if cluster is a Standalone ESXi
1117-
site_name = self.permitted_clusters.get(cluster_name)
1118-
if site_name is None:
1119-
site_name = self.get_site_name(NBCluster, cluster_name)
1120-
11211084
# first check against vm_platform_relation
1122-
platform = get_string_or_none(obj.flavor["original_name"])
1085+
platform = None
1086+
if bool(self.skip_vm_platform) is False:
1087+
platform = get_string_or_none(obj.flavor["original_name"])
11231088

1124-
if platform is not None:
1125-
platform = self.get_object_relation(platform, "vm_platform_relation", fallback=platform)
1089+
if platform is not None:
1090+
platform = self.get_object_relation(platform, "vm_platform_relation", fallback=platform)
11261091

11271092
disk = 0
11281093
for volume in obj.attached_volumes:

settings-example.ini

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,4 +403,138 @@ inventory_file_path = /full/path/to/inventory/files
403403
; If the device has a tenant then this one will be used. If not, the prefix tenant will be used if defined
404404
;ip_tenant_inheritance_order = device, prefix
405405

406+
407+
[source/my-openstack-example]
408+
409+
# Defines if this source is enabled or not
410+
#enabled = true
411+
412+
# type of source. This defines which source handler to use.
413+
type = openstack
414+
415+
# URL to the Openstack API
416+
auth_url = https://api.openstack-instance.local:5000/v3
417+
418+
# Openstack project to connect to
419+
project = MyProject
420+
421+
# Openstack region
422+
region = RegionOne
423+
424+
# Openstack user_domain
425+
user_domain = Default
426+
427+
# Openstack project_domain
428+
project_domain = Default
429+
430+
# Enforces TLS certificate validation. If Openstack API uses a valid TLS certificate then
431+
# this option should be set to 'true' to ensure a secure connection.
432+
#validate_tls_certs = false
433+
434+
# username and password to use to log into vCenter
435+
username = vcenteruser
436+
password = supersecret
437+
438+
# IP networks eligible to be synced to NetBox.
439+
# If an IP address is not part of this networks then it WON'T be synced to NetBox
440+
permitted_subnets = 172.16.0.0/12, 10.0.0.0/8, 192.168.0.0/16, fd00::/8
441+
442+
# filters can be used to include/exclude certain objects from importing into NetBox
443+
# Include filters are checked first and exclude filters after. An object name has to
444+
# pass both filters to be synced to NetBox. If a filter is unset it will be ignored.
445+
# Filters are all treated as regex expressions!
446+
447+
# If a cluster is excluded from sync then ALL VMs and HOSTS inside the cluster will
448+
# be ignored!
449+
#cluster_exclude_filter =
450+
#cluster_include_filter =
451+
452+
# This will only include/exclude the host, not the VM if Host is part of a multi host
453+
# cluster.
454+
#host_exclude_filter =
455+
#host_include_filter =
456+
457+
# simply include/exclude VMs
458+
#vm_exclude_filter =
459+
#vm_include_filter =
460+
461+
# This option defines which Openstack Availability Zones is part of a NetBox site. This is done
462+
# with a comma separated key = value list.
463+
# key: defines the cluster name as regex
464+
# value: defines the NetBox site name (use quotes if name contains commas)
465+
# This is a quite important config setting as IP addresses, prefixes, VLANs and
466+
# VRFs are site dependent. In order to assign the correct prefix to an IP
467+
# address it is important to pick the correct site.
468+
# A VM always depends on the cluster site relation
469+
#cluster_site_relation = Cluster_NYC = New York , Cluster_FFM.* = Frankfurt
470+
471+
# Same as cluster site but on host level. If unset it will fall back
472+
# to cluster_site_relation.
473+
#host_site_relation = nyc02.* = New York, ffm01.* = Frankfurt
474+
475+
# This option defines which cluster/host/VM belongs to which tenant. This is done
476+
# with a comma separated key = value list.
477+
# key: defines a hosts/VM name as regex
478+
# value: defines the NetBox tenant name (use quotes if name contains commas)
479+
#cluster_tenant_relation = Cluster_NYC.* = Customer A
480+
#host_tenant_relation = esxi300.* = Infrastructure
481+
#vm_tenant_relation = grafana.* = Infrastructure
482+
483+
# This option defines custom platforms if the used Flavors are not suitable.
484+
# Pretty much a mapping of Openstack flavor name to your own platform name.
485+
# This is done with a comma separated key = value list.
486+
# key: defines a Openstack returned flavor name
487+
# value: defines the desired NetBox platform name
488+
#vm_platform_relation = centos-7.* = centos7, microsoft-windows-server-2016.* = Windows2016
489+
490+
# Define the NetBox device role used for hosts and VMs. The default is set to "Server". This is done
491+
# with a comma separated key = value list.
492+
# key: defines a hosts/VM name as regex
493+
# value: defines the NetBox role name (use quotes if name contains commas)
494+
#host_role_relation = .* = Server
495+
#vm_role_relation = .* = Server
496+
497+
# Define NetBox tags which are assigned to a cluster, host or VM. This is done
498+
# with a comma separated key = value list.
499+
# key: defines a hosts/VM name as regex
500+
# value: defines the NetBox tag (use quotes if name contains commas)
501+
#cluster_tag_relation = Cluster_NYC.* = Infrastructure
502+
#host_tag_relation = esxi300.* = Infrastructure
503+
#vm_tag_relation = grafana.* = Infrastructure
504+
505+
# Perform a reverse lookup for all collected IP addresses. If a dns name
506+
# was found it will be added to the IP address object in NetBox
507+
#dns_name_lookup = True
508+
509+
# use custom DNS server to do the reverse lookups
510+
#custom_dns_servers = 192.168.1.11, 192.168.1.12
511+
512+
# define how the primary IPs should be set
513+
# possible values
514+
#
515+
# always: will remove primary IP from the object where this address is
516+
# currently set as primary and moves it to new object
517+
#
518+
# when-undefined: (default)
519+
# only sets primary IP if undefined, will cause ERRORs if same IP is
520+
# assigned more then once to different hosts and IP is set as the
521+
# objects primary IP
522+
#
523+
# never: don't set any primary IPs, will cause the same ERRORs
524+
# as "when-undefined"
525+
526+
#set_primary_ip = when-undefined
527+
528+
# Do not sync flavors from a VM in Openstack to the comments field on a VM in netbox
529+
#skip_vm_platform = False
530+
531+
# Do not sync ID from a VM in Openstack to the comments field on a VM in netbox
532+
#skip_vm_comments = False
533+
534+
# strip domain part from host name before syncing device to NetBox
535+
#strip_host_domain_name = False
536+
537+
# strip domain part from VM name before syncing VM to NetBox
538+
#strip_vm_domain_name = False
539+
406540
;EOF

0 commit comments

Comments
 (0)