+ name = user.username + "'s Copy of '" + old_template.name + "'"
+ num_copies = ResourceTemplate.objects.filter(name__startswith=name).count()
+ template = ResourceTemplate.objects.create(
+ name=name if num_copies == 0 else name + " (" + str(num_copies) + ")",
+ xml=old_template.xml,
+ owner=user,
+ lab=old_template.lab,
+ description=old_template.description,
+ public=False,
+ temporary=True,
+ private_vlan_pool=old_template.private_vlan_pool,
+ public_vlan_pool=old_template.public_vlan_pool,
+ copy_of=old_template
+ )
+
+ for old_network in old_template.networks.all():
+ Network.objects.create(
+ name=old_network.name,
+ bundle=template,
+ is_public=old_network.is_public
+ )
+ # We are assuming there is only one opnfv config per public resource template
+ old_opnfv = template.opnfv_config.first()
+ if old_opnfv:
+ opnfv_config = OPNFVConfig.objects.create(
+ installer=old_opnfv.installer,
+ scenario=old_opnfv.installer,
+ template=template,
+ name=old_opnfv.installer,
+ )
+ # I am explicitly leaving opnfv_config.networks empty to avoid
+ # problems with duplicated / shared networks. In the quick deploy,
+ # there is never multiple networks anyway. This may have to change in the future
+
+ for old_config in old_template.getConfigs():
+ image_to_set = image
+ if not image:
+ image_to_set = old_config.image
+
+ config = ResourceConfiguration.objects.create(
+ profile=old_config.profile,
+ image=image_to_set,
+ template=template,
+ is_head_node=old_config.is_head_node,
+ name=hostname if len(old_template.getConfigs()) == 1 else old_config.name,
+ # cloud_init_files=old_config.cloud_init_files.set()
+ )
+
+ for file in old_config.cloud_init_files.all():
+ config.cloud_init_files.add(file)
+
+ if global_cloud_config:
+ config.cloud_init_files.add(global_cloud_config)
+ config.save()
+
+ for old_iface_config in old_config.interface_configs.all():
+ iface_config = InterfaceConfiguration.objects.create(
+ profile=old_iface_config.profile,
+ resource_config=config
+ )
+
+ for old_connection in old_iface_config.connections.all():
+ iface_config.connections.add(NetworkConnection.objects.create(
+ network=template.networks.get(name=old_connection.network.name),
+ vlan_is_tagged=old_connection.vlan_is_tagged
+ ))
+
+ for old_res_opnfv in old_config.resource_opnfv_config.all():
+ if old_opnfv:
+ ResourceOPNFVConfig.objects.create(
+ role=old_opnfv.role,
+ resource_config=config,
+ opnfv_config=opnfv_config
+ )
+ return template