Skip to content

Commit e12cd4e

Browse files
committed
Code cleanup + fixes and docs
1 parent 1095c31 commit e12cd4e

File tree

4 files changed

+147
-46
lines changed

4 files changed

+147
-46
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ This ensures stale objects are removed from NetBox keeping an accurate current s
3535
* requests==2.24.0
3636
* pyvmomi==6.7.3
3737
* aiodns==2.0.0
38+
* openstack
3839

3940
### Environment
4041
* 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:

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ wheel
44
requests==2.24.0
55
pyvmomi==6.7.3
66
aiodns==2.0.0
7+
setuptools==60.10.0
78
openstack

settings-example.ini

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,4 +277,138 @@ permitted_subnets = 172.16.0.0/12, 10.0.0.0/8, 192.168.0.0/16, fd00::/8
277277
# if False only data which is not preset in NetBox will be added
278278
#overwrite_interface_attributes = True
279279

280+
281+
[source/my-openstack-example]
282+
283+
# Defines if this source is enabled or not
284+
#enabled = true
285+
286+
# type of source. This defines which source handler to use.
287+
type = openstack
288+
289+
# URL to the Openstack API
290+
auth_url = https://api.openstack-instance.local:5000/v3
291+
292+
# Openstack project to connect to
293+
project = MyProject
294+
295+
# Openstack region
296+
region = RegionOne
297+
298+
# Openstack user_domain
299+
user_domain = Default
300+
301+
# Openstack project_domain
302+
project_domain = Default
303+
304+
# Enforces TLS certificate validation. If Openstack API uses a valid TLS certificate then
305+
# this option should be set to 'true' to ensure a secure connection.
306+
#validate_tls_certs = false
307+
308+
# username and password to use to log into vCenter
309+
username = vcenteruser
310+
password = supersecret
311+
312+
# IP networks eligible to be synced to NetBox.
313+
# If an IP address is not part of this networks then it WON'T be synced to NetBox
314+
permitted_subnets = 172.16.0.0/12, 10.0.0.0/8, 192.168.0.0/16, fd00::/8
315+
316+
# filters can be used to include/exclude certain objects from importing into NetBox
317+
# Include filters are checked first and exclude filters after. An object name has to
318+
# pass both filters to be synced to NetBox. If a filter is unset it will be ignored.
319+
# Filters are all treated as regex expressions!
320+
321+
# If a cluster is excluded from sync then ALL VMs and HOSTS inside the cluster will
322+
# be ignored!
323+
#cluster_exclude_filter =
324+
#cluster_include_filter =
325+
326+
# This will only include/exclude the host, not the VM if Host is part of a multi host
327+
# cluster.
328+
#host_exclude_filter =
329+
#host_include_filter =
330+
331+
# simply include/exclude VMs
332+
#vm_exclude_filter =
333+
#vm_include_filter =
334+
335+
# This option defines which Openstack Availability Zones is part of a NetBox site. This is done
336+
# with a comma separated key = value list.
337+
# key: defines the cluster name as regex
338+
# value: defines the NetBox site name (use quotes if name contains commas)
339+
# This is a quite important config setting as IP addresses, prefixes, VLANs and
340+
# VRFs are site dependent. In order to assign the correct prefix to an IP
341+
# address it is important to pick the correct site.
342+
# A VM always depends on the cluster site relation
343+
#cluster_site_relation = Cluster_NYC = New York , Cluster_FFM.* = Frankfurt
344+
345+
# Same as cluster site but on host level. If unset it will fall back
346+
# to cluster_site_relation.
347+
#host_site_relation = nyc02.* = New York, ffm01.* = Frankfurt
348+
349+
# This option defines which cluster/host/VM belongs to which tenant. This is done
350+
# with a comma separated key = value list.
351+
# key: defines a hosts/VM name as regex
352+
# value: defines the NetBox tenant name (use quotes if name contains commas)
353+
#cluster_tenant_relation = Cluster_NYC.* = Customer A
354+
#host_tenant_relation = esxi300.* = Infrastructure
355+
#vm_tenant_relation = grafana.* = Infrastructure
356+
357+
# This option defines custom platforms if the used Flavors are not suitable.
358+
# Pretty much a mapping of Openstack flavor name to your own platform name.
359+
# This is done with a comma separated key = value list.
360+
# key: defines a Openstack returned flavor name
361+
# value: defines the desired NetBox platform name
362+
#vm_platform_relation = centos-7.* = centos7, microsoft-windows-server-2016.* = Windows2016
363+
364+
# Define the NetBox device role used for hosts and VMs. The default is set to "Server". This is done
365+
# with a comma separated key = value list.
366+
# key: defines a hosts/VM name as regex
367+
# value: defines the NetBox role name (use quotes if name contains commas)
368+
#host_role_relation = .* = Server
369+
#vm_role_relation = .* = Server
370+
371+
# Define NetBox tags which are assigned to a cluster, host or VM. This is done
372+
# with a comma separated key = value list.
373+
# key: defines a hosts/VM name as regex
374+
# value: defines the NetBox tag (use quotes if name contains commas)
375+
#cluster_tag_relation = Cluster_NYC.* = Infrastructure
376+
#host_tag_relation = esxi300.* = Infrastructure
377+
#vm_tag_relation = grafana.* = Infrastructure
378+
379+
# Perform a reverse lookup for all collected IP addresses. If a dns name
380+
# was found it will be added to the IP address object in NetBox
381+
#dns_name_lookup = True
382+
383+
# use custom DNS server to do the reverse lookups
384+
#custom_dns_servers = 192.168.1.11, 192.168.1.12
385+
386+
# define how the primary IPs should be set
387+
# possible values
388+
#
389+
# always: will remove primary IP from the object where this address is
390+
# currently set as primary and moves it to new object
391+
#
392+
# when-undefined: (default)
393+
# only sets primary IP if undefined, will cause ERRORs if same IP is
394+
# assigned more then once to different hosts and IP is set as the
395+
# objects primary IP
396+
#
397+
# never: don't set any primary IPs, will cause the same ERRORs
398+
# as "when-undefined"
399+
400+
#set_primary_ip = when-undefined
401+
402+
# Do not sync flavors from a VM in Openstack to the comments field on a VM in netbox
403+
#skip_vm_platform = False
404+
405+
# Do not sync ID from a VM in Openstack to the comments field on a VM in netbox
406+
#skip_vm_comments = False
407+
408+
# strip domain part from host name before syncing device to NetBox
409+
#strip_host_domain_name = False
410+
411+
# strip domain part from VM name before syncing VM to NetBox
412+
#strip_vm_domain_name = False
413+
280414
# EOF

0 commit comments

Comments
 (0)