Skip to content

Commit

Permalink
adds support for NetBox 4.0 #387
Browse files Browse the repository at this point in the history
  • Loading branch information
bb-Ricardo committed May 8, 2024
1 parent 7299929 commit 42fc943
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 31 deletions.
5 changes: 4 additions & 1 deletion module/netbox/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,10 @@ def query_current_data(self, netbox_objects_to_query=None):

# request a brief list of existing objects
log.debug(f"Requesting a brief list of {nb_object_class.name}s from NetBox")
brief_nb_data = self.request(nb_object_class, params={"brief": 1, "limit": 500})
brief_params = {"brief": 1, "limit": 500}
if version.parse(self.inventory.netbox_api_version) >= version.parse("4.0"):
brief_params["fields"] = "id"
brief_nb_data = self.request(nb_object_class, params=brief_params)
log.debug("NetBox returned %d results." % len(brief_nb_data.get("results", list())))

log.debug(f"Requesting the last updates since {latest_update} of {nb_object_class.name}s from NetBox")
Expand Down
38 changes: 25 additions & 13 deletions module/netbox/object_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1152,7 +1152,7 @@ class NBCustomField(NetBoxObject):
primary_key = "name"
prune = False
# used by this software
valid_content_types = [
valid_object_types = [
"dcim.device",
"dcim.interface",
"dcim.inventoryitem",
Expand All @@ -1164,6 +1164,8 @@ class NBCustomField(NetBoxObject):

def __init__(self, *args, **kwargs):
self.data_model = {
"object_types": list,
# field name (object_types) for NetBox < 4.0.0
"content_types": list,
"type": ["text", "longtext", "integer", "boolean", "date", "url", "json", "select", "multiselect"],
"name": 50,
Expand All @@ -1178,29 +1180,39 @@ def __init__(self, *args, **kwargs):
def update(self, data=None, read_from_netbox=False, source=None):
"""
handle content types properly
append to existing content_types and don't delete any
append to existing object_types and don't delete any
"""

# get current content types
current_content_types = list()
for content_type in grab(self, "data.content_types", fallback=list()):
current_content_types.append(content_type)
current_object_types = list()
for object_type in grab(self, "data.object_types", fallback=list()):
current_object_types.append(object_type)

if isinstance(data.get("content_types"), str):
data["content_types"] = [data.get("content_types")]
if isinstance(data.get("object_types"), str):
data["object_types"] = [data.get("object_types")]

for content_type in data.get("content_types"):
if content_type not in self.valid_content_types and read_from_netbox is False:
log.error(f"Invalid content type '{content_type}' for {self.name}")
for object_type in data.get("object_types"):
if object_type not in self.valid_object_types and read_from_netbox is False:
log.error(f"Invalid content type '{object_type}' for {self.name}")
continue

if content_type not in current_content_types:
current_content_types.append(content_type)
if object_type not in current_object_types:
current_object_types.append(object_type)

data["content_types"] = current_content_types
data["object_types"] = current_object_types

# Keep support for NetBox < 4.0
if version.parse(self.inventory.netbox_api_version) < version.parse("4.0.0"):
if data.get("object_types") is not None:
data["content_types"] = data.get("object_types")
del data["object_types"]

super().update(data=data, read_from_netbox=read_from_netbox, source=source)

if isinstance(grab(self, "data.object_types"), str):
self.data["object_types"] = [grab(self, "data.object_types")]

# Keep support for NetBox < 4.0
if isinstance(grab(self, "data.content_types"), str):
self.data["content_types"] = [grab(self, "data.content_types")]

Expand Down
12 changes: 6 additions & 6 deletions module/sources/check_redfish/import_inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ def update_device(self):
self.add_update_custom_field({
"name": "service_tag",
"label": "Service Tag",
"content_types": [
"object_types": [
"dcim.device"
],
"type": "text",
Expand Down Expand Up @@ -1040,7 +1040,7 @@ def add_necessary_base_objects(self):
self.add_update_custom_field({
"name": "firmware",
"label": "Firmware",
"content_types": [
"object_types": [
"dcim.inventoryitem",
"dcim.powerport"
],
Expand All @@ -1052,7 +1052,7 @@ def add_necessary_base_objects(self):
self.add_update_custom_field({
"name": "inventory_type",
"label": "Type",
"content_types": ["dcim.inventoryitem"],
"object_types": ["dcim.inventoryitem"],
"type": "text",
"description": "Describes the type of inventory item"
})
Expand All @@ -1061,7 +1061,7 @@ def add_necessary_base_objects(self):
self.add_update_custom_field({
"name": "inventory_size",
"label": "Size",
"content_types": ["dcim.inventoryitem"],
"object_types": ["dcim.inventoryitem"],
"type": "text",
"description": "Describes the size of the inventory item if applicable"
})
Expand All @@ -1070,7 +1070,7 @@ def add_necessary_base_objects(self):
self.add_update_custom_field({
"name": "inventory_speed",
"label": "Speed",
"content_types": ["dcim.inventoryitem"],
"object_types": ["dcim.inventoryitem"],
"type": "text",
"description": "Describes the speed of the inventory item if applicable"
})
Expand All @@ -1079,7 +1079,7 @@ def add_necessary_base_objects(self):
self.add_update_custom_field({
"name": "health",
"label": "Health",
"content_types": [
"object_types": [
"dcim.inventoryitem",
"dcim.powerport",
"dcim.device"
Expand Down
4 changes: 2 additions & 2 deletions module/sources/common/source_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,7 @@ def add_vlan_object_to_netbox(self, vlan_data, site_name=None):
def add_update_custom_field(self, data) -> NBCustomField:
"""
Adds/updates a NBCustomField object with data.
Update will only update the 'content_types' attribute.
Update will only update the 'object_types' attribute.
Parameters
----------
Expand All @@ -792,7 +792,7 @@ def add_update_custom_field(self, data) -> NBCustomField:
if custom_field is None:
custom_field = self.inventory.add_object(NBCustomField, data=data, source=self)
else:
custom_field.update(data={"content_types": data.get("content_types")}, source=self)
custom_field.update(data={"object_types": data.get("object_types")}, source=self)

return custom_field

Expand Down
16 changes: 8 additions & 8 deletions module/sources/vmware/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -779,16 +779,16 @@ def get_object_custom_fields(self, obj):
custom_value = grab(obj, "customValue", fallback=list())

if grab(obj, "_wsdlName") == "VirtualMachine":
content_type = "virtualization.virtualmachine"
object_type = "virtualization.virtualmachine"
custom_object_attributes = self.settings.vm_custom_object_attributes or list()
object_attribute_prefix = "vm"
else:
content_type = "dcim.device"
object_type = "dcim.device"
custom_object_attributes = self.settings.host_custom_object_attributes or list()
object_attribute_prefix = "host"

# add basic host data to device
if content_type == "dcim.device":
if object_type == "dcim.device":
num_cpu_cores = grab(obj, "summary.hardware.numCpuCores")
cpu_model = grab(obj, "summary.hardware.cpuModel")
memory_size = grab(obj, "summary.hardware.memorySize")
Expand All @@ -797,7 +797,7 @@ def get_object_custom_fields(self, obj):
custom_field = self.add_update_custom_field({
"name": "vcsa_host_cpu_cores",
"label": "Physical CPU Cores",
"content_types": [content_type],
"object_types": [object_type],
"type": "text",
"description": f"vCenter '{self.name}' reported Host CPU cores"
})
Expand All @@ -808,7 +808,7 @@ def get_object_custom_fields(self, obj):
custom_field = self.add_update_custom_field({
"name": "vcsa_host_memory",
"label": "Memory",
"content_types": [content_type],
"object_types": [object_type],
"type": "text",
"description": f"vCenter '{self.name}' reported Memory"
})
Expand Down Expand Up @@ -846,7 +846,7 @@ def get_object_custom_fields(self, obj):
custom_field = self.add_update_custom_field({
"name": f"vcsa_{label}",
"label": label,
"content_types": [content_type],
"object_types": [object_type],
"type": "text",
"description": f"vCenter '{self.name}' synced custom attribute '{label}'"
})
Expand Down Expand Up @@ -883,7 +883,7 @@ def get_object_custom_fields(self, obj):
custom_field = self.add_update_custom_field({
"name": f"vcsa_{object_attribute_prefix}_{custom_object_attribute}",
"label": custom_object_attribute,
"content_types": [content_type],
"object_types": [object_type],
"type": custom_field_type,
"description": f"vCenter '{self.name}' synced object attribute '{custom_object_attribute}'"
})
Expand Down Expand Up @@ -1321,7 +1321,7 @@ def add_datacenter(self, obj):
custom_field = self.add_update_custom_field({
"name": f"vcsa_{label}",
"label": label,
"content_types": ["virtualization.clustergroup"],
"object_types": ["virtualization.clustergroup"],
"type": "text",
"description": f"vCenter '{self.name}' synced custom attribute '{label}'"
})
Expand Down
2 changes: 1 addition & 1 deletion scripts/publi.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ unset DOCKER_TLS_VERIFY
unset DOCKER_HOST
unset DOCKER_CERT_PATH

find . -name "__pycache__" -delete
find module -type d -name "__pycache__" -exec rm -rf {} \;
docker --config ./docker-tmp login
docker --config ./docker-tmp buildx create --use
if [[ $FINAL == true ]]; then
Expand Down

0 comments on commit 42fc943

Please sign in to comment.