aboutsummaryrefslogtreecommitdiffstats
path: root/src/resource_inventory
diff options
context:
space:
mode:
authorParker Berberian <pberberian@iol.unh.edu>2020-02-27 16:26:47 -0500
committerParker Berberian <pberberian@iol.unh.edu>2020-02-27 16:26:47 -0500
commit4940cda1806aa13591485b996264ddc887202d35 (patch)
tree5e36f2fff3dc99fa26cbe11a8f8ca205e59947cc /src/resource_inventory
parentd73588dfe1066f85bb83df0e3c0881ff42c25e04 (diff)
Improve Data Migration
Improved migration files so that they preserve data Change-Id: I53283fd3fd207ed3f9773beea7b1ce2062b5bd9f Signed-off-by: Parker Berberian <pberberian@iol.unh.edu>
Diffstat (limited to 'src/resource_inventory')
-rw-r--r--src/resource_inventory/migrations/0012_auto_20200103_1850.py37
-rw-r--r--src/resource_inventory/migrations/0012_manual_20200218_1536.py25
-rw-r--r--src/resource_inventory/migrations/0013_auto_20200218_1536.py161
-rw-r--r--src/resource_inventory/models.py2
4 files changed, 94 insertions, 131 deletions
diff --git a/src/resource_inventory/migrations/0012_auto_20200103_1850.py b/src/resource_inventory/migrations/0012_auto_20200103_1850.py
index 2bb203e..569e433 100644
--- a/src/resource_inventory/migrations/0012_auto_20200103_1850.py
+++ b/src/resource_inventory/migrations/0012_auto_20200103_1850.py
@@ -4,30 +4,23 @@ from django.db import migrations, models
import django.db.models.deletion
-def genTempVlanNetwork(apps, editor):
+def pairVlanPhysicalNetworks(apps, editor):
+ PhysicalNetwork = apps.get_model("resource_inventory", "PhysicalNetwork")
Vlan = apps.get_model("resource_inventory", "Vlan")
- Network = apps.get_model("resource_inventory", "Network")
- tempVlanNetwork = apps.get_model("resource_inventory", "tempVlanNetwork")
for vlan in Vlan.objects.filter(network__isnull=False):
- tempVlanNetwork.objects.create(network=vlan.network, vlan=vlan)
+ if PhysicalNetwork.objects.filter(id=vlan.network.id).exists():
+ continue
+ PhysicalNetwork.objects.create(id=vlan.network.id, vlan_id=vlan.vlan_id, generic_network=vlan.network)
-def deleteTempVlanNetworks(apps, editor):
- tempVlanNetwork = apps.get_model("resource_inventory", "tempVlanNetwork")
- tempVlanNetwork.objects.all().delete()
-
-
-def pairVlanPhysicalNetworks(apps, editor):
- PhysicalNetwork = apps.get_model("resource_inventory", "PhysicalNetwork")
- tempVlanPair = apps.get_model("resource_inventory", "tempVlanNetwork")
- for pair in tempVlanPair.objects.all():
- physicalNetwork = PhysicalNetwork.objects.create(vlan_id=vlan.vlan_id,
- generic_network=pair.network)
- pair.vlan.network = physicalNetwork
def deletePhysicalNetworks(apps, editor):
+ Vlan = apps.get_model("resource_inventory", "Vlan")
+ for vlan in Vlan.objects.all():
+ vlan.network = None
PhysicalNetwork = apps.get_model("resource_inventory", "PhysicalNetwork")
PhysicalNetwork.objects.all().delete()
+
class Migration(migrations.Migration):
dependencies = [
@@ -56,21 +49,11 @@ class Migration(migrations.Migration):
name='id',
field=models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'),
),
- migrations.CreateModel(
- name='tempVlanNetwork',
- fields=[
- ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
- ('vlan', models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='resource_inventory.vlan')),
- ('network', models.ForeignKey(null=True, to='resource_inventory.network', on_delete=django.db.models.deletion.CASCADE)),
- ]
- ),
- migrations.RunPython(genTempVlanNetwork, deleteTempVlanNetworks),
+ migrations.RunPython(pairVlanPhysicalNetworks, deletePhysicalNetworks),
migrations.AlterField(
model_name='vlan',
name='network',
field=models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING,
to='resource_inventory.PhysicalNetwork', null=True),
),
- migrations.RunPython(pairVlanPhysicalNetworks, deletePhysicalNetworks),
- migrations.DeleteModel("tempVlanNetwork")
]
diff --git a/src/resource_inventory/migrations/0012_manual_20200218_1536.py b/src/resource_inventory/migrations/0012_manual_20200218_1536.py
new file mode 100644
index 0000000..378bdc3
--- /dev/null
+++ b/src/resource_inventory/migrations/0012_manual_20200218_1536.py
@@ -0,0 +1,25 @@
+# Generated by Django 2.2 on 2020-02-18 15:36
+
+from django.conf import settings
+from django.db import migrations
+
+
+def clear_networks(apps, schema_editor):
+ Network = apps.get_model('resource_inventory', 'Network')
+ Vlan = apps.get_model('resource_inventory', 'Vlan')
+ for vlan in Vlan.objects.all():
+ vlan.delete()
+ for net in Network.objects.all():
+ net.delete()
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ migrations.swappable_dependency(settings.AUTH_USER_MODEL),
+ ('resource_inventory', '0012_auto_20200103_1850'),
+ ]
+
+ operations = [
+ migrations.RunPython(clear_networks)
+ ]
diff --git a/src/resource_inventory/migrations/0013_auto_20200218_1536.py b/src/resource_inventory/migrations/0013_auto_20200218_1536.py
index 3049dc7..014cb2f 100644
--- a/src/resource_inventory/migrations/0013_auto_20200218_1536.py
+++ b/src/resource_inventory/migrations/0013_auto_20200218_1536.py
@@ -6,17 +6,33 @@ import django.db.models.deletion
import resource_inventory.models
+def clear_resource_bundles(apps, schema_editor):
+ ResourceBundle = apps.get_model('resource_inventory', 'ResourceBundle')
+ for rb in ResourceBundle.objects.all():
+ rb.template = None
+ rb.save()
+
+
+def create_default_template(apps, schema_editor):
+ ResourceTemplate = apps.get_model('resource_inventory', 'ResourceTemplate')
+ ResourceTemplate.objects.create(id=1, name="Default Template")
+
+
def populate_servers(apps, schema_editor):
"""Convert old Host models to Server Resources."""
Host = apps.get_model('resource_inventory', 'Host')
Server = apps.get_model('resource_inventory', 'Server')
+ ResourceProfile = apps.get_model('resource_inventory', 'ResourceProfile')
for h in Host.objects.all():
+ rp = ResourceProfile.objects.get(id=h.profile.id)
Server.objects.create(
working=h.working,
vendor=h.vendor,
labid=h.labid,
booked=h.booked,
- name=h.labid
+ name=h.labid,
+ lab=h.lab,
+ profile=rp
)
@@ -42,14 +58,16 @@ def populate_resource_profiles(apps, schema_editor):
HostProfile = apps.get_model('resource_inventory', 'HostProfile')
ResourceProfile = apps.get_model('resource_inventory', 'ResourceProfile')
for hp in HostProfile.objects.all():
- rp = ResourceProfile.objects.create(name=hp.name, description=hp.description)
+ rp = ResourceProfile.objects.create(id=hp.id, name=hp.name, description=hp.description)
rp.labs.add(*list(hp.labs.all()))
+ """
+ TODO: link these models together
rp.interfaceprofile = hp.interfaceprofile
rp.storageprofile = hp.storageprofile
rp.cpuprofile = hp.cpuprofile
rp.ramprofile = hp.ramprofile
rp.save()
- rp.interfaceprofile.host = rp
+ hp.interfaceprofile.host = rp
rp.storageprofile.host = rp
rp.cpuprofile.host = rp
rp.ramprofile.host = rp
@@ -57,30 +75,17 @@ def populate_resource_profiles(apps, schema_editor):
rp.storageprofile.save()
rp.cpuprofile.save()
rp.ramprofile.save()
+ """
class Migration(migrations.Migration):
- """TODO: Creation of the following models.
-
- - Server
- - ResourceTemplate
- - ResourceProfile
- - ResourceConfiguration (?)
- - InterfaceConfiguration (?)
-
- And set the correct defaults for the following fields:
- - Interface.profile
- - OpnfvConfig.template (?)
- - physicalnetwork.lab
- - physicalnetwork.profile
- """
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('booking', '0007_remove_booking_config_bundle'),
('account', '0004_downtime'),
- ('api', '0011_auto_20200218_1536'),
- ('resource_inventory', '0012_auto_20200103_1850'),
+ ('api', '0013_manual_20200218_1536'),
+ ('resource_inventory', '0012_manual_20200218_1536'),
]
operations = [
@@ -143,6 +148,36 @@ class Migration(migrations.Migration):
'abstract': False,
},
),
+ migrations.AddField(
+ model_name='server',
+ name='bundle',
+ field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='resource_inventory.ResourceBundle'),
+ ),
+ migrations.AddField(
+ model_name='server',
+ name='config',
+ field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='resource_inventory.ResourceConfiguration'),
+ ),
+ migrations.AddField(
+ model_name='server',
+ name='interfaces',
+ field=models.ManyToManyField(to='resource_inventory.Interface'),
+ ),
+ migrations.AddField(
+ model_name='server',
+ name='lab',
+ field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='account.Lab'),
+ ),
+ migrations.AddField(
+ model_name='server',
+ name='profile',
+ field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='resource_inventory.ResourceProfile'),
+ ),
+ migrations.AddField(
+ model_name='server',
+ name='remote_management',
+ field=models.ForeignKey(default=resource_inventory.models.get_default_remote_info, on_delete=models.SET(resource_inventory.models.get_default_remote_info), to='resource_inventory.RemoteInfo'),
+ ),
migrations.RunPython(populate_servers),
migrations.RemoveField(
model_name='generichost',
@@ -251,47 +286,6 @@ class Migration(migrations.Migration):
name='order',
field=models.IntegerField(default=-1),
),
- migrations.AddField(
- model_name='physicalnetwork',
- name='bundle',
- field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='resource_inventory.ResourceBundle'),
- ),
- migrations.AddField(
- model_name='physicalnetwork',
- name='interfaces',
- field=models.ManyToManyField(to='resource_inventory.Interface'),
- ),
- migrations.AddField(
- model_name='physicalnetwork',
- name='lab',
- field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, to='account.Lab'),
- preserve_default=False,
- ),
- migrations.AddField(
- model_name='physicalnetwork',
- name='labid',
- field=models.CharField(default='default_id', max_length=200, unique=True),
- ),
- migrations.AddField(
- model_name='physicalnetwork',
- name='model',
- field=models.CharField(default='unknown', max_length=150),
- ),
- migrations.AddField(
- model_name='physicalnetwork',
- name='remote_management',
- field=models.ForeignKey(default=resource_inventory.models.get_default_remote_info, on_delete=models.SET(resource_inventory.models.get_default_remote_info), to='resource_inventory.RemoteInfo'),
- ),
- migrations.AddField(
- model_name='physicalnetwork',
- name='vendor',
- field=models.CharField(default='unknown', max_length=100),
- ),
- migrations.AddField(
- model_name='physicalnetwork',
- name='working',
- field=models.BooleanField(default=True),
- ),
migrations.AlterField(
model_name='cpuprofile',
name='host',
@@ -322,6 +316,7 @@ class Migration(migrations.Migration):
name='host',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='ramprofile', to='resource_inventory.ResourceProfile'),
),
+ migrations.RunPython(clear_resource_bundles),
migrations.AlterField(
model_name='resourcebundle',
name='template',
@@ -343,9 +338,6 @@ class Migration(migrations.Migration):
name='GenericResourceBundle',
),
migrations.DeleteModel(
- name='Host',
- ),
- migrations.DeleteModel(
name='HostConfiguration',
),
migrations.DeleteModel(
@@ -354,35 +346,8 @@ class Migration(migrations.Migration):
migrations.DeleteModel(
name='HostProfile',
),
- migrations.AddField(
- model_name='server',
- name='bundle',
- field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='resource_inventory.ResourceBundle'),
- ),
- migrations.AddField(
- model_name='server',
- name='config',
- field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='resource_inventory.ResourceConfiguration'),
- ),
- migrations.AddField(
- model_name='server',
- name='interfaces',
- field=models.ManyToManyField(to='resource_inventory.Interface'),
- ),
- migrations.AddField(
- model_name='server',
- name='lab',
- field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='account.Lab'),
- ),
- migrations.AddField(
- model_name='server',
- name='profile',
- field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='resource_inventory.ResourceProfile'),
- ),
- migrations.AddField(
- model_name='server',
- name='remote_management',
- field=models.ForeignKey(default=resource_inventory.models.get_default_remote_info, on_delete=models.SET(resource_inventory.models.get_default_remote_info), to='resource_inventory.RemoteInfo'),
+ migrations.DeleteModel(
+ name='Host',
),
migrations.AddField(
model_name='resourceopnfvconfig',
@@ -429,21 +394,11 @@ class Migration(migrations.Migration):
name='acts_as',
field=models.OneToOneField(null=True, on_delete=django.db.models.deletion.SET_NULL, to='resource_inventory.InterfaceConfiguration'),
),
+ migrations.RunPython(create_default_template),
migrations.AddField(
model_name='opnfvconfig',
name='template',
field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, related_name='opnfv_config', to='resource_inventory.ResourceTemplate'),
preserve_default=False,
),
- migrations.AddField(
- model_name='physicalnetwork',
- name='config',
- field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='resource_inventory.ResourceConfiguration'),
- ),
- migrations.AddField(
- model_name='physicalnetwork',
- name='profile',
- field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, to='resource_inventory.ResourceProfile'),
- preserve_default=False,
- ),
]
diff --git a/src/resource_inventory/models.py b/src/resource_inventory/models.py
index eec009a..d11f71b 100644
--- a/src/resource_inventory/models.py
+++ b/src/resource_inventory/models.py
@@ -382,7 +382,7 @@ class Network(models.Model):
return self.name
-class PhysicalNetwork(Resource):
+class PhysicalNetwork(models.Model):
vlan_id = models.IntegerField()
generic_network = models.ForeignKey(Network, on_delete=models.CASCADE)