mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-14 01:41:22 -06:00
Initial work on ObjectChange data migrations
This commit is contained in:
parent
cc099e86e1
commit
1a5cb5a9f9
@ -1,4 +1,5 @@
|
||||
import django.db.models.deletion
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
@ -49,3 +50,28 @@ class Migration(migrations.Migration):
|
||||
# Copy over existing site assignments
|
||||
migrations.RunPython(code=copy_site_assignments, reverse_code=migrations.RunPython.noop),
|
||||
]
|
||||
|
||||
|
||||
def oc_circuittermination_termination(objectchange, revert):
|
||||
site_ct = ContentType.objects.get_by_natural_key('dcim', 'site').pk
|
||||
provider_network_ct = ContentType.objects.get_by_natural_key('circuits', 'providernetwork').pk
|
||||
for data in (objectchange.prechange_data, objectchange.postchange_data):
|
||||
if data is None:
|
||||
continue
|
||||
if site_id := data.get('site'):
|
||||
data.update({
|
||||
'termination_type': site_ct,
|
||||
'termination_id': site_id,
|
||||
})
|
||||
elif provider_network_id := data.get('provider_network'):
|
||||
data.update({
|
||||
'termination_type': provider_network_ct,
|
||||
'termination_id': provider_network_id,
|
||||
})
|
||||
data.pop('site')
|
||||
data.pop('provider_network')
|
||||
|
||||
|
||||
objectchange_migrators = {
|
||||
'circuits.circuittermination': oc_circuittermination_termination,
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
import django.db.models.deletion
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
@ -82,3 +83,21 @@ class Migration(migrations.Migration):
|
||||
),
|
||||
),
|
||||
]
|
||||
|
||||
|
||||
def oc_circuitgroupassignment_member(objectchange, revert):
|
||||
circuit_ct = ContentType.objects.get_by_natural_key('circuits', 'circuit').pk
|
||||
for data in (objectchange.prechange_data, objectchange.postchange_data):
|
||||
if data is None:
|
||||
continue
|
||||
if circuit_id := data.get('circuit'):
|
||||
data.update({
|
||||
'member_type': circuit_ct,
|
||||
'member_id': circuit_id,
|
||||
})
|
||||
data.pop('circuit')
|
||||
|
||||
|
||||
objectchange_migrators = {
|
||||
'circuits.circuitgroupassignment': oc_circuitgroupassignment_member,
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
import django.db.models.deletion
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
@ -44,3 +45,21 @@ class Migration(migrations.Migration):
|
||||
# Copy over existing site assignments
|
||||
migrations.RunPython(code=copy_site_assignments, reverse_code=migrations.RunPython.noop),
|
||||
]
|
||||
|
||||
|
||||
def oc_prefix_scope(objectchange, revert):
|
||||
site_ct = ContentType.objects.get_by_natural_key('dcim', 'site').pk
|
||||
for data in (objectchange.prechange_data, objectchange.postchange_data):
|
||||
if data is None:
|
||||
continue
|
||||
if site_id := data.get('site'):
|
||||
data.update({
|
||||
'scope_type': site_ct,
|
||||
'scope_id': site_id,
|
||||
})
|
||||
data.pop('site')
|
||||
|
||||
|
||||
objectchange_migrators = {
|
||||
'ipam.prefix': oc_prefix_scope,
|
||||
}
|
||||
|
@ -1,3 +1,4 @@
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.db import migrations
|
||||
from django.db.models import F
|
||||
|
||||
@ -54,3 +55,28 @@ class Migration(migrations.Migration):
|
||||
reverse_code=repopulate_device_and_virtualmachine_relations,
|
||||
)
|
||||
]
|
||||
|
||||
|
||||
def oc_service_parent(objectchange, revert):
|
||||
device_ct = ContentType.objects.get_by_natural_key('dcim', 'device').pk
|
||||
virtual_machine_ct = ContentType.objects.get_by_natural_key('virtualization', 'virtualmachine').pk
|
||||
for data in (objectchange.prechange_data, objectchange.postchange_data):
|
||||
if data is None:
|
||||
continue
|
||||
if device_id := data.get('device'):
|
||||
data.update({
|
||||
'parent_object_type': device_ct,
|
||||
'parent_object_id': device_id,
|
||||
})
|
||||
elif virtual_machine_id := data.get('virtual_machine'):
|
||||
data.update({
|
||||
'parent_object_type': virtual_machine_ct,
|
||||
'parent_object_id': virtual_machine_id,
|
||||
})
|
||||
data.pop('device')
|
||||
data.pop('virtual_machine')
|
||||
|
||||
|
||||
objectchange_migrators = {
|
||||
'ipam.service': oc_service_parent,
|
||||
}
|
||||
|
@ -66,3 +66,17 @@ class Migration(migrations.Migration):
|
||||
name='group',
|
||||
),
|
||||
]
|
||||
|
||||
|
||||
def oc_contact_groups(objectchange, revert):
|
||||
for data in (objectchange.prechange_data, objectchange.postchange_data):
|
||||
if data is None:
|
||||
continue
|
||||
if 'group' in data:
|
||||
data['groups'] = [data['group']] if data['group'] else []
|
||||
data.pop('group')
|
||||
|
||||
|
||||
objectchange_migrators = {
|
||||
'tenancy.contact': oc_contact_groups,
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
import django.db.models.deletion
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
@ -43,3 +44,21 @@ class Migration(migrations.Migration):
|
||||
# Copy over existing site assignments
|
||||
migrations.RunPython(code=copy_site_assignments, reverse_code=migrations.RunPython.noop),
|
||||
]
|
||||
|
||||
|
||||
def oc_cluster_scope(objectchange, revert):
|
||||
site_ct = ContentType.objects.get_by_natural_key('dcim', 'site').pk
|
||||
for data in (objectchange.prechange_data, objectchange.postchange_data):
|
||||
if data is None:
|
||||
continue
|
||||
if site_id := data.get('site'):
|
||||
data.update({
|
||||
'scope_type': site_ct,
|
||||
'scope_id': site_id,
|
||||
})
|
||||
data.pop('site')
|
||||
|
||||
|
||||
objectchange_migrators = {
|
||||
'virtualization.cluster': oc_cluster_scope,
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user