From 10c5257f90cf587b50bc06502bfbc7edd045c8c8 Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Sat, 21 Mar 2020 11:59:04 +0100 Subject: [PATCH] Introduce "extra_data" jsonfield --- uncloud/uncloud/models.py | 22 ++++++++ uncloud/uncloud_pay/models.py | 6 +-- .../migrations/0005_auto_20200321_1058.py | 50 +++++++++++++++++++ uncloud/uncloud_vm/models.py | 9 ++-- .../0002_matrixserviceproduct_extra_data.py | 19 +++++++ 5 files changed, 100 insertions(+), 6 deletions(-) create mode 100644 uncloud/uncloud/models.py create mode 100644 uncloud/uncloud_vm/migrations/0005_auto_20200321_1058.py create mode 100644 uncloud/ungleich_service/migrations/0002_matrixserviceproduct_extra_data.py diff --git a/uncloud/uncloud/models.py b/uncloud/uncloud/models.py new file mode 100644 index 0000000..7ca5dfa --- /dev/null +++ b/uncloud/uncloud/models.py @@ -0,0 +1,22 @@ +from django.db import models +from django.contrib.postgres.fields import JSONField + +class UncloudModel(models.Model): + """ + This class extends the standard model with an + extra_data field that can be used to include public, + but internal information. + + For instance if you migrate from an existing virtualisation + framework to uncloud. + + The extra_data attribute should be considered a hack and whenever + data is necessary for running uncloud, it should **not** be stored + in there. + + """ + + extra_data = JSONField(editable=False, blank=True, null=True) + + class Meta: + abstract = True diff --git a/uncloud/uncloud_pay/models.py b/uncloud/uncloud_pay/models.py index a11c3c1..532e130 100644 --- a/uncloud/uncloud_pay/models.py +++ b/uncloud/uncloud_pay/models.py @@ -19,8 +19,7 @@ from decimal import Decimal import uncloud_pay.stripe from uncloud_pay.helpers import beginning_of_month, end_of_month from uncloud import AMOUNT_DECIMALS, AMOUNT_MAX_DIGITS - - +from uncloud.models import UncloudModel # Used to generate bill due dates. @@ -418,6 +417,7 @@ class OrderRecord(models.Model): description = models.TextField() + @property def recurring_period(self): return self.order.recurring_period @@ -436,7 +436,7 @@ class OrderRecord(models.Model): # Abstract (= no database representation) class used as parent for products # (e.g. uncloud_vm.models.VMProduct). -class Product(models.Model): +class Product(UncloudModel): uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) owner = models.ForeignKey(get_user_model(), on_delete=models.CASCADE, diff --git a/uncloud/uncloud_vm/migrations/0005_auto_20200321_1058.py b/uncloud/uncloud_vm/migrations/0005_auto_20200321_1058.py new file mode 100644 index 0000000..3799e6a --- /dev/null +++ b/uncloud/uncloud_vm/migrations/0005_auto_20200321_1058.py @@ -0,0 +1,50 @@ +# Generated by Django 3.0.3 on 2020-03-21 10:58 + +import django.contrib.postgres.fields.jsonb +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('uncloud_vm', '0004_remove_vmproduct_vmid'), + ] + + operations = [ + migrations.AddField( + model_name='vmdiskimageproduct', + name='extra_data', + field=django.contrib.postgres.fields.jsonb.JSONField(blank=True, editable=False, null=True), + ), + migrations.AddField( + model_name='vmdiskproduct', + name='extra_data', + field=django.contrib.postgres.fields.jsonb.JSONField(blank=True, editable=False, null=True), + ), + migrations.AddField( + model_name='vmhost', + name='extra_data', + field=django.contrib.postgres.fields.jsonb.JSONField(blank=True, editable=False, null=True), + ), + migrations.AddField( + model_name='vmproduct', + name='extra_data', + field=django.contrib.postgres.fields.jsonb.JSONField(blank=True, editable=False, null=True), + ), + migrations.AddField( + model_name='vmsnapshotproduct', + name='extra_data', + field=django.contrib.postgres.fields.jsonb.JSONField(blank=True, editable=False, null=True), + ), + migrations.AlterField( + model_name='vmdiskproduct', + name='vm', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='disks', to='uncloud_vm.VMProduct'), + ), + migrations.AlterField( + model_name='vmsnapshotproduct', + name='vm', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='snapshots', to='uncloud_vm.VMProduct'), + ), + ] diff --git a/uncloud/uncloud_vm/models.py b/uncloud/uncloud_vm/models.py index 7e38ded..bdd3a43 100644 --- a/uncloud/uncloud_vm/models.py +++ b/uncloud/uncloud_vm/models.py @@ -3,10 +3,13 @@ import uuid from django.db import models from django.contrib.auth import get_user_model + # Uncomment if you override model's clean method # from django.core.exceptions import ValidationError from uncloud_pay.models import Product, RecurringPeriod +from uncloud.models import UncloudModel + import uncloud_pay.models as pay_models import uncloud_storage.models @@ -22,7 +25,7 @@ STATUS_CHOICES = ( STATUS_DEFAULT = 'pending' -class VMHost(models.Model): +class VMHost(UncloudModel): uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) # 253 is the maximum DNS name length @@ -99,7 +102,7 @@ class VMWithOSProduct(VMProduct): pass -class VMDiskImageProduct(models.Model): +class VMDiskImageProduct(UncloudModel): """ Images are used for cloning/linking. @@ -138,7 +141,7 @@ class VMDiskImageProduct(models.Model): -class VMDiskProduct(models.Model): +class VMDiskProduct(UncloudModel): """ The VMDiskProduct is attached to a VM. diff --git a/uncloud/ungleich_service/migrations/0002_matrixserviceproduct_extra_data.py b/uncloud/ungleich_service/migrations/0002_matrixserviceproduct_extra_data.py new file mode 100644 index 0000000..f755ddb --- /dev/null +++ b/uncloud/ungleich_service/migrations/0002_matrixserviceproduct_extra_data.py @@ -0,0 +1,19 @@ +# Generated by Django 3.0.3 on 2020-03-21 10:58 + +import django.contrib.postgres.fields.jsonb +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('ungleich_service', '0001_initial'), + ] + + operations = [ + migrations.AddField( + model_name='matrixserviceproduct', + name='extra_data', + field=django.contrib.postgres.fields.jsonb.JSONField(blank=True, editable=False, null=True), + ), + ]