Skip to content

Commit eb39cb0

Browse files
committed
enforces custom attributes name constrains
refs: #186
1 parent d7d0560 commit eb39cb0

2 files changed

Lines changed: 9 additions & 5 deletions

File tree

module/sources/common/source_base.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
# For a copy, see file LICENSE.txt included in this
88
# repository or visit: <https://opensource.org/licenses/MIT>.
99

10+
import re
11+
1012
from ipaddress import ip_interface, ip_address, IPv6Address, IPv4Address, IPv6Network, IPv4Network
1113

1214
from module.netbox.inventory import (
@@ -630,6 +632,10 @@ def add_update_custom_field(self, data):
630632
new or updated NBCustomField
631633
"""
632634

635+
# enforce NetBox name constrains
636+
data["name"] = NetBoxObject.format_slug(
637+
re.sub('-+', '-', data.get("name").replace("_", "-")).strip("-"), 100)[0:50].replace("-", "_")
638+
633639
custom_field = self.inventory.get_by_data(NBCustomField, data={"name": data.get("name")})
634640

635641
if custom_field is None:

module/sources/vmware/connection.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -824,17 +824,15 @@ def get_object_custom_fields(self, obj):
824824
if label is None:
825825
continue
826826

827-
name = NetBoxObject.format_slug(f"vcsa_{label}", 50).replace("-", "_").strip("-_")
828-
829-
self.add_update_custom_field({
830-
"name": name,
827+
custom_field = self.add_update_custom_field({
828+
"name": f"vcsa_{label}",
831829
"label": label,
832830
"content_types": [content_type],
833831
"type": "text",
834832
"description": f"vCenter '{self.name}' synced custom attribute '{label}'"
835833
})
836834

837-
return_custom_fields[name] = value
835+
return_custom_fields[grab(custom_field, "data.name")] = value
838836

839837
return return_custom_fields
840838

0 commit comments

Comments
 (0)