Skip to content

Commit

Permalink
on_end (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
scrungus authored Aug 22, 2024
1 parent 51b20ae commit bfc1e5d
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 25 deletions.
23 changes: 9 additions & 14 deletions coral_credits/api/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,27 +178,22 @@ def flavor_request_data(base_request_data):

@pytest.fixture
def flavor_extend_current_request_data(flavor_request_data, request):
delete_request_data = {"current_lease": copy.deepcopy(flavor_request_data["lease"])}
delete_request_data["lease"] = {
extend_request_data = {"current_lease": copy.deepcopy(flavor_request_data["lease"])}
extend_request_data["lease"] = {
"end_date": request.config.END_LATE_DATE.isoformat()
}
return deep_merge(flavor_request_data, delete_request_data)
return deep_merge(flavor_request_data, extend_request_data)


@pytest.fixture
def flavor_delete_current_request_data(flavor_request_data, request):
delete_request_data = {"current_lease": copy.deepcopy(flavor_request_data["lease"])}
delete_request_data["lease"] = {
def flavor_shorten_current_request_data(flavor_request_data, request):
shorten_request_data = {
"current_lease": copy.deepcopy(flavor_request_data["lease"])
}
shorten_request_data["lease"] = {
"end_date": request.config.END_EARLY_DATE.isoformat()
}
return deep_merge(flavor_request_data, delete_request_data)


@pytest.fixture
def flavor_delete_upcoming_request_data(flavor_request_data, request):
delete_request_data = {"current_lease": copy.deepcopy(flavor_request_data["lease"])}
delete_request_data["lease"] = {"end_date": request.config.START_DATE.isoformat()}
return deep_merge(flavor_request_data, delete_request_data)
return deep_merge(flavor_request_data, shorten_request_data)


@pytest.fixture
Expand Down
32 changes: 21 additions & 11 deletions coral_credits/api/tests/consumer_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ def consumer_update_request(api_client, request_data, expected_response):
)


def consumer_delete_request(api_client, request_data, expected_response):
return consumer_request(
reverse("resource-request-on-end"),
api_client,
request_data,
expected_response,
)


@pytest.mark.parametrize(
"allocation_hours,request_data",
[
Expand Down Expand Up @@ -99,12 +108,11 @@ def test_flavor_create_request(


@pytest.mark.parametrize(
"allocation_hours,request_data,delete_request_data",
"allocation_hours,request_data",
[
(
{"VCPU": 96.0, "MEMORY_MB": 24000.0, "DISK_GB": 840.0},
lazy_fixture("flavor_request_data"),
lazy_fixture("flavor_delete_upcoming_request_data"),
),
],
)
Expand All @@ -116,7 +124,6 @@ def test_flavor_delete_upcoming_request(
resource_provider_account,
api_client,
request_data,
delete_request_data,
allocation_hours,
request, # contains pytest global vars
):
Expand All @@ -129,14 +136,15 @@ def test_flavor_delete_upcoming_request(
consumer_create_request(api_client, request_data, status.HTTP_204_NO_CONTENT)

# Delete
consumer_update_request(api_client, delete_request_data, status.HTTP_204_NO_CONTENT)
consumer_delete_request(api_client, request_data, status.HTTP_204_NO_CONTENT)

# Find consumer and check duration is 0.
new_consumer = models.Consumer.objects.filter(
consumer_ref=request.config.LEASE_NAME
).first()
assert new_consumer is not None
assert new_consumer.end == request.config.START_DATE
# Don't assert this for now as we are unsure of the behaviour of on_end from blazar.
# assert new_consumer.end == request.config.START_DATE

# Check our original allocations are intact.
for resource_class in resource_classes:
Expand All @@ -150,24 +158,24 @@ def test_flavor_delete_upcoming_request(


@pytest.mark.parametrize(
"allocation_hours,request_data,delete_request_data",
"allocation_hours,request_data,shorten_request_data",
[
(
{"VCPU": 96.0, "MEMORY_MB": 24000.0, "DISK_GB": 840.0},
lazy_fixture("flavor_request_data"),
lazy_fixture("flavor_delete_current_request_data"),
lazy_fixture("flavor_shorten_current_request_data"),
),
],
)
@pytest.mark.django_db
def test_flavor_delete_currently_active_request(
def test_flavor_shorten_currently_active_request(
resource_classes,
credit_allocation,
create_credit_allocation_resources,
resource_provider_account,
api_client,
request_data,
delete_request_data,
shorten_request_data,
allocation_hours,
request, # contains pytest global vars
):
Expand All @@ -179,8 +187,10 @@ def test_flavor_delete_currently_active_request(
# Create
consumer_create_request(api_client, request_data, status.HTTP_204_NO_CONTENT)

# Delete
consumer_update_request(api_client, delete_request_data, status.HTTP_204_NO_CONTENT)
# Update
consumer_update_request(
api_client, shorten_request_data, status.HTTP_204_NO_CONTENT
)

# Find consumer and check end date is changed.
new_consumer = models.Consumer.objects.filter(
Expand Down
29 changes: 29 additions & 0 deletions coral_credits/api/views.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import copy
from datetime import datetime
import logging
import uuid

from django.db import transaction
from django.db.utils import IntegrityError
from django.shortcuts import get_object_or_404
from django.utils.timezone import make_aware
from rest_framework import permissions, status, viewsets
from rest_framework.decorators import action
from rest_framework.response import Response
Expand Down Expand Up @@ -160,6 +163,32 @@ def check_update(self, request):
request, current_lease_required=True, dry_run=True
)

@action(detail=False, methods=["post"], url_path="on-end")
def on_end(self, request):
# For a deletion, we convert this to an update request
# With the new lease's end date set to now.
# TODO(tylerchristie) this is not very nice. we can probably do better.
if request.data["lease"]:
request.data["current_lease"] = copy.deepcopy(request.data["lease"])
if ("start_date" and "end_date") in request.data["lease"]:
time_now = make_aware(datetime.now())
# Current vs upcoming deletion
# 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"
]
else:
request.data["lease"]["end_date"] = time_now.isoformat()
return self._create_or_update(
request, current_lease_required=True, dry_run=False
)

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

0 comments on commit bfc1e5d

Please sign in to comment.