From c7eda83926a2f4423c8a6880e4b367ee331a5117 Mon Sep 17 00:00:00 2001 From: scrungus Date: Thu, 4 Jul 2024 17:00:36 +0100 Subject: [PATCH 1/3] ResourceProviderAccount --- .../0004_resourceprovideraccount_and_more.py | 74 +++++++++++++++++++ coral_credits/api/models.py | 24 +++++- 2 files changed, 95 insertions(+), 3 deletions(-) create mode 100644 coral_credits/api/migrations/0004_resourceprovideraccount_and_more.py diff --git a/coral_credits/api/migrations/0004_resourceprovideraccount_and_more.py b/coral_credits/api/migrations/0004_resourceprovideraccount_and_more.py new file mode 100644 index 0000000..7fc2529 --- /dev/null +++ b/coral_credits/api/migrations/0004_resourceprovideraccount_and_more.py @@ -0,0 +1,74 @@ +# Generated by Django 5.0.6 on 2024-07-04 15:54 + +import django.db.models.deletion +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("api", "0003_rename_consume_ref_consumer_consumer_ref_and_more"), + ] + + operations = [ + migrations.CreateModel( + name="ResourceProviderAccount", + fields=[ + ( + "id", + models.BigAutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ("project_id", models.UUIDField()), + ( + "account", + models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, + to="api.creditaccount", + ), + ), + ( + "provider", + models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, + to="api.resourceprovider", + ), + ), + ], + options={ + "unique_together": { + ("account", "provider"), + ("provider", "project_id"), + }, + }, + ), + migrations.AlterUniqueTogether( + name="consumer", + unique_together=set(), + ), + migrations.AddField( + model_name="consumer", + name="resource_provider_account", + field=models.ForeignKey( + default=1, + on_delete=django.db.models.deletion.DO_NOTHING, + to="api.resourceprovideraccount", + ), + preserve_default=False, + ), + migrations.AlterUniqueTogether( + name="consumer", + unique_together={("consumer_ref", "resource_provider_account")}, + ), + migrations.RemoveField( + model_name="consumer", + name="account", + ), + migrations.RemoveField( + model_name="consumer", + name="resource_provider", + ), + ] diff --git a/coral_credits/api/models.py b/coral_credits/api/models.py index 01cd4a5..80d9c69 100644 --- a/coral_credits/api/models.py +++ b/coral_credits/api/models.py @@ -29,6 +29,25 @@ class CreditAccount(models.Model): def __str__(self) -> str: return f"{self.name}" +class ResourceProviderAccount(models.Model): + account = models.ForeignKey(CreditAccount, on_delete=models.CASCADE) + provider = models.ForeignKey(ResourceProvider, on_delete=models.CASCADE) + project_id = models.UUIDField() + + class Meta: + unique_together = ( + ( + "account", + "provider", + ), + ( + "provider", + "project_id" + ), + ) + def __str__(self) -> str: + return f"{self.project_id} for {self.account} in {self.provider}" + class CreditAllocation(models.Model): name = models.CharField(max_length=200) @@ -73,16 +92,15 @@ def __str__(self) -> str: class Consumer(models.Model): consumer_ref = models.CharField(max_length=200) - resource_provider = models.ForeignKey(ResourceProvider, on_delete=models.DO_NOTHING) + resource_provider_account = models.ForeignKey(ResourceProviderAccount, on_delete=models.DO_NOTHING) created = models.DateTimeField(auto_now_add=True) - account = models.ForeignKey(CreditAccount, on_delete=models.DO_NOTHING) start = models.DateTimeField() end = models.DateTimeField() class Meta: unique_together = ( "consumer_ref", - "resource_provider", + "resource_provider_account", ) def __str__(self) -> str: From 16b10adf1cf4b60d1b0265f40e4b784c93be0fb2 Mon Sep 17 00:00:00 2001 From: scrungus Date: Thu, 4 Jul 2024 17:20:41 +0100 Subject: [PATCH 2/3] add consumer_uuid + user_ref --- ...onsumer_consumer_uuid_consumer_user_ref.py | 24 +++++++++++++++++++ coral_credits/api/models.py | 10 ++++++++ 2 files changed, 34 insertions(+) create mode 100644 coral_credits/api/migrations/0005_consumer_consumer_uuid_consumer_user_ref.py diff --git a/coral_credits/api/migrations/0005_consumer_consumer_uuid_consumer_user_ref.py b/coral_credits/api/migrations/0005_consumer_consumer_uuid_consumer_user_ref.py new file mode 100644 index 0000000..3aa5839 --- /dev/null +++ b/coral_credits/api/migrations/0005_consumer_consumer_uuid_consumer_user_ref.py @@ -0,0 +1,24 @@ +# Generated by Django 5.0.6 on 2024-07-04 16:20 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("api", "0004_resourceprovideraccount_and_more"), + ] + + operations = [ + migrations.AddField( + model_name="consumer", + name="consumer_uuid", + field=models.UUIDField(default=""), + preserve_default=False, + ), + migrations.AddField( + model_name="consumer", + name="user_ref", + field=models.UUIDField(default=""), + preserve_default=False, + ), + ] diff --git a/coral_credits/api/models.py b/coral_credits/api/models.py index 80d9c69..ff82326 100644 --- a/coral_credits/api/models.py +++ b/coral_credits/api/models.py @@ -1,4 +1,5 @@ from django.db import models +from django.db.models import Q # TODO(tylerchristie): add allocation window in here, to simplify. @@ -92,12 +93,21 @@ def __str__(self) -> str: class Consumer(models.Model): consumer_ref = models.CharField(max_length=200) + consumer_uuid = models.UUIDField() resource_provider_account = models.ForeignKey(ResourceProviderAccount, on_delete=models.DO_NOTHING) + user_ref = models.UUIDField() created = models.DateTimeField(auto_now_add=True) start = models.DateTimeField() end = models.DateTimeField() class Meta: + # TODO(tylerchristie): allow either/or nullable? + # constraints = [ + # models.CheckConstraint( + # check=Q(consumer_ref=False) | Q(consumer_uuid=False), + # name='not_both_null' + # ) + # ] unique_together = ( "consumer_ref", "resource_provider_account", From b2773b4c7f560cc086be3783b05d2aa76a6a3f1f Mon Sep 17 00:00:00 2001 From: scrungus Date: Thu, 4 Jul 2024 17:52:09 +0100 Subject: [PATCH 3/3] black --- coral_credits/api/models.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/coral_credits/api/models.py b/coral_credits/api/models.py index ff82326..3eddfb2 100644 --- a/coral_credits/api/models.py +++ b/coral_credits/api/models.py @@ -30,22 +30,21 @@ class CreditAccount(models.Model): def __str__(self) -> str: return f"{self.name}" + class ResourceProviderAccount(models.Model): account = models.ForeignKey(CreditAccount, on_delete=models.CASCADE) provider = models.ForeignKey(ResourceProvider, on_delete=models.CASCADE) project_id = models.UUIDField() - + class Meta: unique_together = ( ( "account", "provider", ), - ( - "provider", - "project_id" - ), + ("provider", "project_id"), ) + def __str__(self) -> str: return f"{self.project_id} for {self.account} in {self.provider}" @@ -94,14 +93,16 @@ def __str__(self) -> str: class Consumer(models.Model): consumer_ref = models.CharField(max_length=200) consumer_uuid = models.UUIDField() - resource_provider_account = models.ForeignKey(ResourceProviderAccount, on_delete=models.DO_NOTHING) + resource_provider_account = models.ForeignKey( + ResourceProviderAccount, on_delete=models.DO_NOTHING + ) user_ref = models.UUIDField() created = models.DateTimeField(auto_now_add=True) start = models.DateTimeField() end = models.DateTimeField() class Meta: - # TODO(tylerchristie): allow either/or nullable? + # TODO(tylerchristie): allow either/or nullable? # constraints = [ # models.CheckConstraint( # check=Q(consumer_ref=False) | Q(consumer_uuid=False),