Skip to content

Commit 1dc6949

Browse files
Closes: #282 - Add handling for max_custom_object_types config option (and configuration.md) (#300)
* Add handling for max_cots config option (and configuration.md) * Rename to max_custom_object_types * Add default_settings entry * Lower default maximum to 50 --------- Co-authored-by: Jeremy Stretch <jstretch@netboxlabs.com>
1 parent c75c126 commit 1dc6949

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

docs/configuration.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Configuration Parameters
2+
3+
## `max_custom_object_types`
4+
5+
Default: 50
6+
7+
The maximum number of Custom Object Types that may be created.

netbox_custom_objects/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ class CustomObjectsPluginConfig(PluginConfig):
5353
author_email = 'support@netboxlabs.com'
5454
base_url = "custom-objects"
5555
min_version = "4.4.0"
56-
default_settings = {}
56+
default_settings = {
57+
# The maximum number of Custom Object Types that may be created
58+
'max_custom_object_types': 50,
59+
}
5760
required_settings = []
5861
template_extensions = "template_content.template_extensions"
5962

netbox_custom_objects/models.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
TagsMixin,
4242
get_model_features,
4343
)
44+
from netbox.plugins import get_plugin_config
4445
from netbox.registry import registry
4546
from netbox.search import SearchIndex
4647
from utilities import filters
@@ -207,6 +208,7 @@ class CustomObjectType(NetBoxModel):
207208
blank=True,
208209
editable=False
209210
)
211+
210212
class Meta:
211213
verbose_name = "Custom Object Type"
212214
ordering = ("name",)
@@ -223,6 +225,18 @@ class Meta:
223225
def __str__(self):
224226
return self.display_name
225227

228+
def clean(self):
229+
super().clean()
230+
231+
# Enforce max number of COTs that may be created (max_custom_object_types)
232+
if not self.pk:
233+
max_cots = get_plugin_config("netbox_custom_objects", "max_custom_object_types")
234+
if max_cots and CustomObjectType.objects.count() > max_cots:
235+
raise ValidationError(_(
236+
f"Maximum number of Custom Object Types ({max_cots}) "
237+
"exceeded; adjust max_custom_object_types to raise this limit"
238+
))
239+
226240
@classmethod
227241
def clear_model_cache(cls, custom_object_type_id=None):
228242
"""

0 commit comments

Comments
 (0)