Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ResourceProviderAccount table #5

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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",
),
]
Original file line number Diff line number Diff line change
@@ -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,
),
]
35 changes: 32 additions & 3 deletions coral_credits/api/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.db import models
from django.db.models import Q

# TODO(tylerchristie): add allocation window in here, to simplify.

Expand Down Expand Up @@ -30,6 +31,24 @@ 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)
created = models.DateTimeField(auto_now_add=True)
Expand Down Expand Up @@ -73,16 +92,26 @@ 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)
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)
account = models.ForeignKey(CreditAccount, on_delete=models.DO_NOTHING)
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",
"resource_provider_account",
)

def __str__(self) -> str:
Expand Down
Loading