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

Ensure on_end makes input tz aware and fix on_end method #28

Merged
merged 8 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 deletions coral_credits/api/business_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class PhysicalReservation(BaseReservation):
class FlavorReservation(BaseReservation):
amount: int
flavor_id: str
affinity: str = "None"
affinity: Optional[str] = None


@dataclass(kw_only=True)
Expand All @@ -51,7 +51,7 @@ class VirtualReservation(BaseReservation):
vcpus: int
memory_mb: int
disk_gb: int
affinity: str = "None"
affinity: Optional[str] = None
resource_properties: Optional[str] = None


Expand Down
6 changes: 3 additions & 3 deletions coral_credits/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,16 +130,16 @@ class PhysicalReservationSerializer(BaseReservationSerializer):
class FlavorReservationSerializer(BaseReservationSerializer):
amount = serializers.IntegerField()
flavor_id = serializers.CharField()
affinity = serializers.CharField(required=False, default="None")
affinity = serializers.CharField(required=False, default=None, allow_null=True)


class VirtualReservationSerializer(BaseReservationSerializer):
amount = serializers.IntegerField()
vcpus = serializers.IntegerField()
memory_mb = serializers.IntegerField()
disk_gb = serializers.IntegerField()
affinity = serializers.CharField(required=False, default="None")
resource_properties = serializers.CharField(required=False, allow_blank=True)
affinity = serializers.CharField(required=False, default=None, allow_blank=True)
resource_properties = serializers.CharField(required=False, allow_null=True)


class ReservationFactory:
Expand Down
4 changes: 2 additions & 2 deletions coral_credits/api/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def flavor_request_data(base_request_data):
"amount": 2,
"flavor_id": "e26a4241-b83d-4516-8e0e-8ce2665d1966",
"resource_type": "flavor:instance",
"affinity": "None",
"affinity": None,
"allocations": [],
}
],
Expand Down Expand Up @@ -244,7 +244,7 @@ def virtual_request_data(base_request_data):
"vcpus": 1,
"memory_mb": 1,
"disk_gb": 0,
"affinity": "None",
"affinity": None,
"resource_properties": "",
"allocations": [],
}
Expand Down
28 changes: 19 additions & 9 deletions coral_credits/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ class ConsumerViewSet(viewsets.ModelViewSet):

@action(detail=False, methods=["post"], url_path="create")
def create_consumer(self, request):
LOG.info(f"About to process create commit:\n{request.data}")
return self._create_or_update(request)

@action(detail=False, methods=["post"], url_path="update")
Expand Down Expand Up @@ -176,21 +177,26 @@ def on_end(self, request):
# We can't just set everything to current time as we don't know
# the latency between blazars request and our reception.
# Unless we decide on how to round credit allocations.
if (
datetime.fromisoformat(request.data["lease"]["start_date"])
< time_now
):
request.data["lease"]["end_date"] = request.data["lease"][
"start_date"
]
req_start_date = datetime.fromisoformat(
request.data["lease"]["start_date"]
)
if req_start_date.tzinfo is None:
req_start_date = make_aware(req_start_date)
if req_start_date < time_now:
# TODO(johngarbutt) we need to check what we have in the db!
request.data["lease"]["end_date"] = req_start_date.isoformat()
else:
request.data["lease"]["end_date"] = time_now.isoformat()
LOG.info(f"About to process on-end request:\n{request.data}")
return self._create_or_update(
request, current_lease_required=True, dry_run=False
request,
is_delete=True,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should retest without this bit of the change, I think we do send the current lease.

Copy link
Contributor

@scrungus scrungus Sep 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On-end from blazar just sends the lease parameter I believe, not current_lease like update does. However we do want to do an update on coral_credits side, so we should handle this

)

@transaction.atomic
def _create_or_update(self, request, current_lease_required=False, dry_run=False):
def _create_or_update(
self, request, current_lease_required=False, dry_run=False, is_delete=False
):
"""Process a request for a reservation.

see consumer_tests.py for example requests.
Expand All @@ -203,6 +209,10 @@ def _create_or_update(self, request, current_lease_required=False, dry_run=False
context, lease, current_lease = self._validate_request(
request, current_lease_required
)
if is_delete:
# TODO(johng) hack to attemt to make on-end work
current_lease = lease
current_lease_required = True

LOG.info(
f"Incoming Request - Context: {context}, Lease: {lease}, "
Expand Down
Loading