mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
Add ability to adopt components when adding a module
This commit is contained in:
@ -633,12 +633,18 @@ class ModuleForm(NetBoxModelForm):
|
|||||||
help_text="Automatically populate components associated with this module type"
|
help_text="Automatically populate components associated with this module type"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
adopt_components = forms.BooleanField(
|
||||||
|
required=False,
|
||||||
|
initial=False,
|
||||||
|
help_text="Adopt already existing components"
|
||||||
|
)
|
||||||
|
|
||||||
fieldsets = (
|
fieldsets = (
|
||||||
('Module', (
|
('Module', (
|
||||||
'device', 'module_bay', 'manufacturer', 'module_type', 'tags',
|
'device', 'module_bay', 'manufacturer', 'module_type', 'tags',
|
||||||
)),
|
)),
|
||||||
('Hardware', (
|
('Hardware', (
|
||||||
'serial', 'asset_tag', 'replicate_components',
|
'serial', 'asset_tag', 'replicate_components', 'adopt_components',
|
||||||
)),
|
)),
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -646,7 +652,7 @@ class ModuleForm(NetBoxModelForm):
|
|||||||
model = Module
|
model = Module
|
||||||
fields = [
|
fields = [
|
||||||
'device', 'module_bay', 'manufacturer', 'module_type', 'serial', 'asset_tag', 'tags',
|
'device', 'module_bay', 'manufacturer', 'module_type', 'serial', 'asset_tag', 'tags',
|
||||||
'replicate_components', 'comments',
|
'replicate_components', 'adopt_components', 'comments',
|
||||||
]
|
]
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
@ -655,6 +661,8 @@ class ModuleForm(NetBoxModelForm):
|
|||||||
if self.instance.pk:
|
if self.instance.pk:
|
||||||
self.fields['replicate_components'].initial = False
|
self.fields['replicate_components'].initial = False
|
||||||
self.fields['replicate_components'].disabled = True
|
self.fields['replicate_components'].disabled = True
|
||||||
|
self.fields['adopt_components'].initial = False
|
||||||
|
self.fields['adopt_components'].disabled = True
|
||||||
|
|
||||||
def save(self, *args, **kwargs):
|
def save(self, *args, **kwargs):
|
||||||
|
|
||||||
@ -662,6 +670,9 @@ class ModuleForm(NetBoxModelForm):
|
|||||||
if self.instance.pk or not self.cleaned_data['replicate_components']:
|
if self.instance.pk or not self.cleaned_data['replicate_components']:
|
||||||
self.instance._disable_replication = True
|
self.instance._disable_replication = True
|
||||||
|
|
||||||
|
if self.cleaned_data['adopt_components']:
|
||||||
|
self.instance._adopt_components = True
|
||||||
|
|
||||||
return super().save(*args, **kwargs)
|
return super().save(*args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
@ -1065,31 +1065,38 @@ class Module(NetBoxModel, ConfigContextModel):
|
|||||||
|
|
||||||
super().save(*args, **kwargs)
|
super().save(*args, **kwargs)
|
||||||
|
|
||||||
|
adopt_components = getattr(self, '_adopt_components', False)
|
||||||
|
disable_replication = getattr(self, '_disable_replication', False)
|
||||||
|
|
||||||
# If this is a new Module and component replication has not been disabled, instantiate all its
|
# If this is a new Module and component replication has not been disabled, instantiate all its
|
||||||
# related components per the ModuleType definition
|
# related components per the ModuleType definition
|
||||||
if is_new and not getattr(self, '_disable_replication', False):
|
if is_new and not disable_replication:
|
||||||
ConsolePort.objects.bulk_create(
|
# Iterate all component templates
|
||||||
[x.instantiate(device=self.device, module=self) for x in self.module_type.consoleporttemplates.all()]
|
for templates, component_attribute in [
|
||||||
)
|
("consoleporttemplates", "consoleports"),
|
||||||
ConsoleServerPort.objects.bulk_create(
|
("consoleserverporttemplates", "consoleserverports"),
|
||||||
[x.instantiate(device=self.device, module=self) for x in self.module_type.consoleserverporttemplates.all()]
|
("interfacetemplates", "interfaces"),
|
||||||
)
|
("powerporttemplates", "powerports"),
|
||||||
PowerPort.objects.bulk_create(
|
("poweroutlettemplates", "poweroutlets"),
|
||||||
[x.instantiate(device=self.device, module=self) for x in self.module_type.powerporttemplates.all()]
|
("rearporttemplates", "rearports"),
|
||||||
)
|
("frontporttemplates", "frontports")
|
||||||
PowerOutlet.objects.bulk_create(
|
]:
|
||||||
[x.instantiate(device=self.device, module=self) for x in self.module_type.poweroutlettemplates.all()]
|
# Get the template for the module type.
|
||||||
)
|
for template in getattr(self.module_type, templates).all():
|
||||||
Interface.objects.bulk_create(
|
template_instance = template.instantiate(device=self.device, module=self)
|
||||||
[x.instantiate(device=self.device, module=self) for x in self.module_type.interfacetemplates.all()]
|
|
||||||
)
|
|
||||||
RearPort.objects.bulk_create(
|
|
||||||
[x.instantiate(device=self.device, module=self) for x in self.module_type.rearporttemplates.all()]
|
|
||||||
)
|
|
||||||
FrontPort.objects.bulk_create(
|
|
||||||
[x.instantiate(device=self.device, module=self) for x in self.module_type.frontporttemplates.all()]
|
|
||||||
)
|
|
||||||
|
|
||||||
|
if adopt_components:
|
||||||
|
existing_item = getattr(self.device, component_attribute).filter(name=template_instance.name).first()
|
||||||
|
|
||||||
|
# Check if there's a component with the same name already
|
||||||
|
if existing_item:
|
||||||
|
# Assign it to the module
|
||||||
|
existing_item.module = self
|
||||||
|
existing_item.save()
|
||||||
|
continue
|
||||||
|
|
||||||
|
# If we are not adopting components or the component doesn't already exist
|
||||||
|
template_instance.save()
|
||||||
|
|
||||||
#
|
#
|
||||||
# Virtual chassis
|
# Virtual chassis
|
||||||
|
Reference in New Issue
Block a user