Skip to content

Commit 75b2441

Browse files
OmripresentOmri Abu
andauthored
Adding missing field processing for custom_fields and object_permissions (#9)
Co-authored-by: Omri Abu <[email protected]>
1 parent 9b2f24a commit 75b2441

File tree

4 files changed

+102
-1
lines changed

4 files changed

+102
-1
lines changed

src/netbox_initializers/initializers/custom_fields.py

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,69 @@ def load_data(self):
4949
if cf_details.get("weight", -1) >= 0:
5050
custom_field.weight = cf_details["weight"]
5151

52-
if cf_details.get("choices", False):
52+
# object_type should only be applied when type is object, multiobject
53+
if cf_details.get("object_type"):
54+
if cf_details.get("type") not in (
55+
"object",
56+
"multiobject",
57+
):
58+
print(
59+
f"⚠️ Unable to create Custom Field '{cf_name}': object_type is "
60+
+ "supported only for object and multiobject types"
61+
)
62+
custom_field.delete()
63+
continue
64+
custom_field.object_type = get_class_for_class_path(cf_details["object_type"])
65+
66+
# validation_regex should only be applied when type is text, longtext, url
67+
if cf_details.get("validation_regex"):
68+
if cf_details.get("type") not in (
69+
"text",
70+
"longtext",
71+
"url",
72+
):
73+
print(
74+
f"⚠️ Unable to create Custom Field '{cf_name}': validation_regex is "
75+
+ "supported only for text, longtext and, url types"
76+
)
77+
custom_field.delete()
78+
continue
79+
custom_field.validation_regex = cf_details["validation_regex"]
80+
81+
# validation_minimum should only be applied when type is integer
82+
if cf_details.get("validation_minimum"):
83+
if cf_details.get("type") not in ("integer",):
84+
print(
85+
f"⚠️ Unable to create Custom Field '{cf_name}': validation_minimum is "
86+
+ "supported only for integer type"
87+
)
88+
custom_field.delete()
89+
continue
90+
custom_field.validation_minimum = cf_details["validation_minimum"]
91+
92+
# validation_maximum should only be applied when type is integer
93+
if cf_details.get("validation_maximum"):
94+
if cf_details.get("type") not in ("integer",):
95+
print(
96+
f"⚠️ Unable to create Custom Field '{cf_name}': validation_maximum is "
97+
+ "supported only for integer type"
98+
)
99+
custom_field.delete()
100+
continue
101+
custom_field.validation_maximum = cf_details["validation_maximum"]
102+
103+
# choices should only be applied when type is select, multiselect
104+
if cf_details.get("choices"):
105+
if cf_details.get("type") not in (
106+
"select",
107+
"multiselect",
108+
):
109+
print(
110+
f"⚠️ Unable to create Custom Field '{cf_name}': choices is supported only "
111+
+ "for select and multiselect types"
112+
)
113+
custom_field.delete()
114+
continue
53115
custom_field.choices = []
54116

55117
for choice_detail in cf_details.get("choices", []):

src/netbox_initializers/initializers/object_permissions.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ def load_data(self):
2222
},
2323
)
2424

25+
if permission_details.get("constraints", 0):
26+
object_permission.constraints = permission_details["constraints"]
27+
2528
if permission_details.get("object_types", 0):
2629
object_types = permission_details["object_types"]
2730

src/netbox_initializers/initializers/yaml/custom_fields.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
## - date
77
## - url
88
## - select
9+
## - multiselect
10+
## - object
11+
## - multiobject
912
## filter_logic:
1013
## - disabled
1114
## - loose
@@ -34,6 +37,8 @@
3437
# description: Enter numbers into an integer field.
3538
# required: true
3639
# filter_logic: loose
40+
# validation_minimum: 0
41+
# validation_maximum: 255
3742
# weight: 10
3843
# on_objects:
3944
# - tenancy.models.Tenant
@@ -82,6 +87,7 @@
8287
# description: Link to something nice.
8388
# required: true
8489
# filter_logic: disabled
90+
# validation_regex: ^https://
8591
# on_objects:
8692
# - tenancy.models.Tenant
8793
# date_field:
@@ -91,3 +97,21 @@
9197
# filter_logic: disabled
9298
# on_objects:
9399
# - dcim.models.Device
100+
# multiobject_field:
101+
# type: multiobject
102+
# label: Related Objects
103+
# description: IP addresses that belong to this location
104+
# required: true
105+
# filter_logic: loose
106+
# on_objects:
107+
# - dcim.models.Location
108+
# object_type: ipam.models.IPAddress
109+
# object_field:
110+
# type: object
111+
# label: ASN
112+
# description: This device has an ASN now
113+
# required: false
114+
# filter_logic: loose
115+
# on_objects:
116+
# - dcim.models.Device
117+
# object_type: ipam.models.ASN

src/netbox_initializers/initializers/yaml/object_permissions.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,15 @@
4646
# - vlan
4747
# - vlangroup
4848
# - vrf
49+
# vips.change:
50+
# actions:
51+
# - change
52+
# description: "Update VIP object permission"
53+
# enabled: true
54+
# object_types:
55+
# ipam:
56+
# - ipaddress
57+
# groups:
58+
# - devops
59+
# constraints:
60+
# role: vip

0 commit comments

Comments
 (0)