mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
Initial work on #4721 (WIP)
This commit is contained in:
43
netbox/virtualization/migrations/0015_interface.py
Normal file
43
netbox/virtualization/migrations/0015_interface.py
Normal file
@@ -0,0 +1,43 @@
|
||||
# Generated by Django 3.0.6 on 2020-06-18 20:21
|
||||
|
||||
import dcim.fields
|
||||
import django.core.validators
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
import taggit.managers
|
||||
import utilities.fields
|
||||
import utilities.ordering
|
||||
import utilities.query_functions
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('ipam', '0036_standardize_description'),
|
||||
('extras', '0042_customfield_manager'),
|
||||
('virtualization', '0014_standardize_description'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Interface',
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False)),
|
||||
('name', models.CharField(max_length=64)),
|
||||
('_name', utilities.fields.NaturalOrderingField('name', blank=True, max_length=100, naturalize_function=utilities.ordering.naturalize_interface)),
|
||||
('enabled', models.BooleanField(default=True)),
|
||||
('mac_address', dcim.fields.MACAddressField(blank=True, null=True)),
|
||||
('mtu', models.PositiveIntegerField(blank=True, null=True, validators=[django.core.validators.MinValueValidator(1), django.core.validators.MaxValueValidator(65536)])),
|
||||
('mode', models.CharField(blank=True, max_length=50)),
|
||||
('description', models.CharField(blank=True, max_length=200)),
|
||||
('tagged_vlans', models.ManyToManyField(blank=True, related_name='vm_interfaces_as_tagged', to='ipam.VLAN')),
|
||||
('tags', taggit.managers.TaggableManager(related_name='vm_interface', through='extras.TaggedItem', to='extras.Tag')),
|
||||
('untagged_vlan', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='vm_interfaces_as_untagged', to='ipam.VLAN')),
|
||||
('virtual_machine', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='interfaces', to='virtualization.VirtualMachine')),
|
||||
],
|
||||
options={
|
||||
'ordering': ('virtual_machine', utilities.query_functions.CollateAsChar('_name')),
|
||||
'unique_together': {('virtual_machine', 'name')},
|
||||
},
|
||||
),
|
||||
]
|
@@ -0,0 +1,69 @@
|
||||
import sys
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
def replicate_interfaces(apps, schema_editor):
|
||||
ContentType = apps.get_model('contenttypes', 'ContentType')
|
||||
TaggedItem = apps.get_model('taggit', 'TaggedItem')
|
||||
Interface = apps.get_model('dcim', 'Interface')
|
||||
IPAddress = apps.get_model('ipam', 'IPAddress')
|
||||
VMInterface = apps.get_model('virtualization', 'Interface')
|
||||
|
||||
interface_ct = ContentType.objects.get_for_model(Interface)
|
||||
vm_interface_ct = ContentType.objects.get_for_model(VMInterface)
|
||||
|
||||
# Replicate dcim.Interface instances assigned to VirtualMachines
|
||||
original_interfaces = Interface.objects.filter(virtual_machine__isnull=False)
|
||||
for interface in original_interfaces:
|
||||
vm_interface = VMInterface(
|
||||
virtual_machine=interface.virtual_machine,
|
||||
name=interface.name,
|
||||
enabled=interface.enabled,
|
||||
mac_address=interface.mac_address,
|
||||
mtu=interface.mtu,
|
||||
mode=interface.mode,
|
||||
description=interface.description,
|
||||
untagged_vlan=interface.untagged_vlan,
|
||||
)
|
||||
vm_interface.save()
|
||||
|
||||
# Copy tagged VLANs
|
||||
vm_interface.tagged_vlans.set(interface.tagged_vlans.all())
|
||||
|
||||
# Reassign tags to the new instance
|
||||
TaggedItem.objects.filter(
|
||||
content_type=interface_ct, object_id=interface.pk
|
||||
).update(
|
||||
content_type=vm_interface_ct, object_id=vm_interface.pk
|
||||
)
|
||||
|
||||
# Update any assigned IPAddresses
|
||||
IPAddress.objects.filter(assigned_object_id=interface.pk).update(
|
||||
assigned_object_type=vm_interface_ct,
|
||||
assigned_object_id=vm_interface.pk
|
||||
)
|
||||
|
||||
replicated_count = VMInterface.objects.count()
|
||||
if 'test' not in sys.argv:
|
||||
print(f"\n Replicated {replicated_count} interfaces ", end='', flush=True)
|
||||
|
||||
# Verify that all interfaces have been replicated
|
||||
assert replicated_count == original_interfaces.count(), "Replicated interfaces count does not match original count!"
|
||||
|
||||
# Delete original VM interfaces
|
||||
original_interfaces.delete()
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('ipam', '0037_ipaddress_assignment'),
|
||||
('virtualization', '0015_interface'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RunPython(
|
||||
code=replicate_interfaces
|
||||
),
|
||||
]
|
Reference in New Issue
Block a user