Create module components in bulk

This commit is contained in:
kkthxbye-code 2022-05-02 21:37:37 +02:00
parent a55ff798da
commit e93913f91c

View File

@ -1072,15 +1072,18 @@ class Module(NetBoxModel, ConfigContextModel):
# related components per the ModuleType definition # related components per the ModuleType definition
if is_new and not disable_replication: if is_new and not disable_replication:
# Iterate all component templates # Iterate all component templates
for templates, component_attribute in [ for templates, component_attribute, component_model in [
("consoleporttemplates", "consoleports"), ("consoleporttemplates", "consoleports", ConsolePort),
("consoleserverporttemplates", "consoleserverports"), ("consoleserverporttemplates", "consoleserverports", ConsoleServerPort),
("interfacetemplates", "interfaces"), ("interfacetemplates", "interfaces", Interface),
("powerporttemplates", "powerports"), ("powerporttemplates", "powerports", PowerPort),
("poweroutlettemplates", "poweroutlets"), ("poweroutlettemplates", "poweroutlets", PowerOutlet),
("rearporttemplates", "rearports"), ("rearporttemplates", "rearports", RearPort),
("frontporttemplates", "frontports") ("frontporttemplates", "frontports", FrontPort)
]: ]:
create_instances = []
update_instances = []
# Get the template for the module type. # Get the template for the module type.
for template in getattr(self.module_type, templates).all(): for template in getattr(self.module_type, templates).all():
template_instance = template.instantiate(device=self.device, module=self) template_instance = template.instantiate(device=self.device, module=self)
@ -1092,11 +1095,15 @@ class Module(NetBoxModel, ConfigContextModel):
if existing_item: if existing_item:
# Assign it to the module # Assign it to the module
existing_item.module = self existing_item.module = self
existing_item.save() update_instances.append(existing_item)
continue continue
# If we are not adopting components or the component doesn't already exist # If we are not adopting components or the component doesn't already exist
template_instance.save() create_instances.append(template_instance)
component_model.objects.bulk_create(create_instances)
component_model.objects.bulk_update(update_instances, ['module'])
# #
# Virtual chassis # Virtual chassis