Skip to content

Commit

Permalink
adds config option 'disable_vlan_sync' to vmware source
Browse files Browse the repository at this point in the history
refs: #205
  • Loading branch information
bb-Ricardo committed Jun 27, 2022
1 parent 8e410a4 commit 9cfa9b1
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
9 changes: 9 additions & 0 deletions module/netbox/object_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1046,6 +1046,15 @@ def unset_attribute(self, attribute_name=None):
log.error(f"Found undefined data model key '{attribute_name}' for object '{self.__class__.__name__}'")
return

data_type = self.data_model.get(attribute_name)
current_value = self.data.get(attribute_name)

if (data_type in [NBTagList, NBVLANList] or isinstance(data_type, (list, dict))) and len(current_value) == 0:
return

if current_value is None:
return

# mark attribute to unset, this way it will be deleted in NetBox before any other updates are performed
log.info(f"Setting attribute '{attribute_name}' for '{self.get_display_name()}' to None")
self.unset_items.append(attribute_name)
Expand Down
15 changes: 14 additions & 1 deletion module/sources/common/source_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,8 @@ def return_longest_matching_prefix_for_ip(self, ip_to_match=None, site_name=None

return current_longest_matching_prefix

def add_update_interface(self, interface_object, device_object, interface_data, interface_ips=None):
def add_update_interface(self, interface_object, device_object, interface_data, interface_ips=None,
disable_vlan_sync=False):
"""
Adds/Updates an interface to/of a NBVM or NBDevice including IP addresses.
Validates/enriches data in following order:
Expand All @@ -237,6 +238,8 @@ def add_update_interface(self, interface_object, device_object, interface_data,
dictionary with interface attributes to add to this interface
interface_ips: list
list of ip addresses which are assigned to this interface
disable_vlan_sync: bool
if True, VLAN information will be removed from interface before creating/updating interface
Returns
-------
Expand Down Expand Up @@ -277,6 +280,11 @@ def add_update_interface(self, interface_object, device_object, interface_data,
if len(tagged_vlans) > 0:
del (interface_data["tagged_vlans"])

# delete information about any vlans if sync is disable.
if disable_vlan_sync is True:
untagged_vlan = None
tagged_vlans = list()

# get device tenant
device_tenant = grab(device_object, "data.tenant")

Expand All @@ -289,6 +297,11 @@ def add_update_interface(self, interface_object, device_object, interface_data,
else:
interface_object.update(data=interface_data, source=self)

# delete information about any vlans if sync is disable.
if disable_vlan_sync is True:
interface_object.unset_attribute("untagged_vlan")
interface_object.unset_attribute("tagged_vlans")

ip_address_objects = list()
matching_ip_prefixes = list()
# add all interface IPs
Expand Down
6 changes: 4 additions & 2 deletions module/sources/vmware/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ class VMWareHandler(SourceBase):
"host_custom_object_attributes": None,
"vm_custom_object_attributes": None,
"set_source_name_as_cluster_group": False,
"sync_vm_dummy_interfaces": False
"sync_vm_dummy_interfaces": False,
"disable_vlan_sync": False
}

deprecated_settings = {}
Expand Down Expand Up @@ -1205,7 +1206,8 @@ def add_device_vm_to_inventory(self, object_type, object_data, pnic_data=None, v

# add/update interface with retrieved data
nic_object, ip_address_objects = self.add_update_interface(nic_object_dict.get(int_name), device_vm_object,
int_data, nic_ips.get(int_name, list()))
int_data, nic_ips.get(int_name, list()),
self.disable_vlan_sync)

# add all interface IPs
for ip_object in ip_address_objects:
Expand Down
3 changes: 3 additions & 0 deletions settings-example.ini
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,9 @@ permitted_subnets = 172.16.0.0/12, 10.0.0.0/8, 192.168.0.0/16, fd00::/8
# and are exposed through VM guest tools.
#sync_vm_dummy_interfaces = False

# disables syncing of any VLANs visible in vCenter to NetBox
#disable_vlan_sync = False

[source/my-redfish-example]

# Defines if this source is enabled or not
Expand Down

0 comments on commit 9cfa9b1

Please sign in to comment.