Skip to content

Commit

Permalink
Blazar request changes (#10)
Browse files Browse the repository at this point in the history
* blazar request adaptations

* switch to lazy_fixtures

* serialize multiple request formats

* temporarily mock the UUID on create requests

* invalid blazar request tests
  • Loading branch information
scrungus authored Jul 31, 2024
1 parent 1a44e8f commit a4fd054
Show file tree
Hide file tree
Showing 11 changed files with 331 additions and 225 deletions.
54 changes: 34 additions & 20 deletions coral_credits/api/business_objects.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from dataclasses import dataclass, field
from datetime import datetime
from typing import Any, Dict, List
from typing import Any, Dict, List, Optional
from uuid import UUID


Expand All @@ -12,16 +12,9 @@ class Context:
region_name: str


@dataclass
class Inventory:
data: Dict[str, Any]


@dataclass
class ResourceRequest:
inventories: Inventory
# TODO(tylerchristie)
# resource_provider_generation: int = None
resources: Dict[str, Any]


@dataclass
Expand All @@ -31,28 +24,49 @@ class Allocation:
extra: Dict[str, Any]


@dataclass
class Reservation:
@dataclass(kw_only=True)
class BaseReservation:
resource_type: str
allocations: List[Allocation] = field(default_factory=list)


@dataclass(kw_only=True)
class PhysicalReservation(BaseReservation):
min: int
max: int
resource_requests: ResourceRequest
hypervisor_properties: str = None
resource_properties: str = None
allocations: List[Allocation] = field(default_factory=list)
hypervisor_properties: Optional[str] = None
resource_properties: Optional[str] = None


@dataclass(kw_only=True)
class FlavorReservation(BaseReservation):
amount: int
flavor_id: str
affinity: str = "None"


@dataclass(kw_only=True)
class VirtualReservation(BaseReservation):
amount: int
vcpus: int
memory_mb: int
disk_gb: int
affinity: str = "None"
resource_properties: Optional[str] = None


@dataclass
class Lease:
lease_id: UUID
lease_name: str
id: UUID
name: str
start_date: datetime
end_time: datetime
reservations: List[Reservation]
end_date: datetime
reservations: List[BaseReservation]
resource_requests: ResourceRequest

@property
def duration(self):
return (self.end_time - self.start_date).total_seconds() / 3600
return (self.end_date - self.start_date).total_seconds() / 3600


@dataclass
Expand Down
71 changes: 35 additions & 36 deletions coral_credits/api/db_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

def get_current_lease(current_lease):
current_consumer = get_object_or_404(
models.Consumer, consumer_uuid=current_lease.lease_id
models.Consumer, consumer_uuid=current_lease.id
)
current_resource_requests = models.CreditAllocationResource.objects.filter(
consumer=current_consumer,
Expand Down Expand Up @@ -74,7 +74,7 @@ def get_resource_class(resource_class_name):
return resource_class


def get_valid_allocations(inventories):
def get_valid_allocations(resources):
"""Validates a dictionary of resource allocations.
Returns a list of dictionaries of the form:
Expand All @@ -87,7 +87,7 @@ def get_valid_allocations(inventories):
"""
try:
allocations = {}
for resource_class_name, resource_hours in inventories.data.items():
for resource_class_name, resource_hours in resources.items():
resource_class = get_resource_class(resource_class_name)
allocations[resource_class] = float(resource_hours)
except ValueError:
Expand All @@ -106,37 +106,36 @@ def get_resource_requests(lease, current_resource_requests=None):
"""
resource_requests = {}

for reservation in lease.reservations:
for (
resource_type,
amount,
) in reservation.resource_requests.inventories.data.items():
resource_class = get_object_or_404(models.ResourceClass, name=resource_type)
try:
# Keep it simple, ust take min for now
# TODO(tylerchristie): check we can allocate max
# CreditAllocationResource is a record of the number of resource_hours
# available for one unit of a ResourceClass, so we multiply
# lease_duration by units required.
requested_resource_hours = round(
float(amount["total"]) * reservation.min * lease.duration,
1,
)
if current_resource_requests:
delta_resource_hours = calculate_delta_resource_hours(
requested_resource_hours,
current_resource_requests,
resource_class,
)
else:
delta_resource_hours = requested_resource_hours

resource_requests[resource_class] = delta_resource_hours

except KeyError:
raise db_exceptions.ResourceRequestFormatError(
f"Unable to recognize {resource_type} format {amount}"
for (
resource_type,
amount,
) in lease.resource_requests.resources.items():
resource_class = get_object_or_404(models.ResourceClass, name=resource_type)
try:
# Keep it simple, ust take min for now
# TODO(tylerchristie): check we can allocate max
# CreditAllocationResource is a record of the number of resource_hours
# available for one unit of a ResourceClass, so we multiply
# lease_duration by units required.
requested_resource_hours = round(
float(amount) * lease.duration,
1,
)
if current_resource_requests:
delta_resource_hours = calculate_delta_resource_hours(
requested_resource_hours,
current_resource_requests,
resource_class,
)
else:
delta_resource_hours = requested_resource_hours

resource_requests[resource_class] = delta_resource_hours

except KeyError:
raise db_exceptions.ResourceRequestFormatError(
f"Unable to recognize {resource_type} format {amount}"
)

return resource_requests

Expand Down Expand Up @@ -201,12 +200,12 @@ def spend_credits(
):

consumer = models.Consumer.objects.create(
consumer_ref=lease.lease_name,
consumer_uuid=lease.lease_id,
consumer_ref=lease.name,
consumer_uuid=lease.id,
resource_provider_account=resource_provider_account,
user_ref=context.user_id,
start=lease.start_date,
end=lease.end_time,
end=lease.end_date,
)

for resource_class in resource_requests:
Expand Down
Loading

0 comments on commit a4fd054

Please sign in to comment.