mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-24 09:28:38 -06:00
Allow mixture of component replication and adoption
This commit is contained in:
parent
977ccb01f2
commit
8040804c75
@ -1068,41 +1068,44 @@ class Module(NetBoxModel, ConfigContextModel):
|
|||||||
adopt_components = getattr(self, '_adopt_components', False)
|
adopt_components = getattr(self, '_adopt_components', False)
|
||||||
disable_replication = getattr(self, '_disable_replication', False)
|
disable_replication = getattr(self, '_disable_replication', False)
|
||||||
|
|
||||||
# If this is a new Module and component replication has not been disabled, instantiate all its
|
# We skip adding components if the module is being edited or
|
||||||
# related components per the ModuleType definition
|
# both replication and component adoption is disabled
|
||||||
if is_new and not disable_replication:
|
if not is_new or (disable_replication and not adopt_components):
|
||||||
# Iterate all component templates
|
return
|
||||||
for templates, component_attribute, component_model in [
|
|
||||||
("consoleporttemplates", "consoleports", ConsolePort),
|
|
||||||
("consoleserverporttemplates", "consoleserverports", ConsoleServerPort),
|
|
||||||
("interfacetemplates", "interfaces", Interface),
|
|
||||||
("powerporttemplates", "powerports", PowerPort),
|
|
||||||
("poweroutlettemplates", "poweroutlets", PowerOutlet),
|
|
||||||
("rearporttemplates", "rearports", RearPort),
|
|
||||||
("frontporttemplates", "frontports", FrontPort)
|
|
||||||
]:
|
|
||||||
create_instances = []
|
|
||||||
update_instances = []
|
|
||||||
|
|
||||||
# Get the template for the module type.
|
# Iterate all component types
|
||||||
for template in getattr(self.module_type, templates).all():
|
for templates, component_attribute, component_model in [
|
||||||
template_instance = template.instantiate(device=self.device, module=self)
|
("consoleporttemplates", "consoleports", ConsolePort),
|
||||||
|
("consoleserverporttemplates", "consoleserverports", ConsoleServerPort),
|
||||||
|
("interfacetemplates", "interfaces", Interface),
|
||||||
|
("powerporttemplates", "powerports", PowerPort),
|
||||||
|
("poweroutlettemplates", "poweroutlets", PowerOutlet),
|
||||||
|
("rearporttemplates", "rearports", RearPort),
|
||||||
|
("frontporttemplates", "frontports", FrontPort)
|
||||||
|
]:
|
||||||
|
create_instances = []
|
||||||
|
update_instances = []
|
||||||
|
|
||||||
if adopt_components:
|
# Get the template for the module type.
|
||||||
existing_item = getattr(self.device, component_attribute).filter(name=template_instance.name).first()
|
for template in getattr(self.module_type, templates).all():
|
||||||
|
template_instance = template.instantiate(device=self.device, module=self)
|
||||||
|
|
||||||
# Check if there's a component with the same name already
|
if adopt_components:
|
||||||
if existing_item:
|
existing_item = getattr(self.device, component_attribute).filter(name=template_instance.name).first()
|
||||||
# Assign it to the module
|
|
||||||
existing_item.module = self
|
|
||||||
update_instances.append(existing_item)
|
|
||||||
continue
|
|
||||||
|
|
||||||
# If we are not adopting components or the component doesn't already exist
|
# Check if there's a component with the same name already
|
||||||
|
if existing_item:
|
||||||
|
# Assign it to the module
|
||||||
|
existing_item.module = self
|
||||||
|
update_instances.append(existing_item)
|
||||||
|
continue
|
||||||
|
|
||||||
|
# Only create new components if replication is enabled
|
||||||
|
if not disable_replication:
|
||||||
create_instances.append(template_instance)
|
create_instances.append(template_instance)
|
||||||
|
|
||||||
component_model.objects.bulk_create(create_instances)
|
component_model.objects.bulk_create(create_instances)
|
||||||
component_model.objects.bulk_update(update_instances, ['module'])
|
component_model.objects.bulk_update(update_instances, ['module'])
|
||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
|
Loading…
Reference in New Issue
Block a user