Skip to content

Commit

Permalink
Moved some utility functions into LeaseController and `OfferControl…
Browse files Browse the repository at this point in the history
…ler`.
  • Loading branch information
QuanMPhm committed Oct 15, 2024
1 parent c8bf5d5 commit 787749e
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 66 deletions.
21 changes: 17 additions & 4 deletions esi_leap/api/controllers/v1/lease.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def get_one(self, lease_id):
request, "esi_leap:lease:get", lease_id
)

return Lease(**utils.lease_get_dict_with_added_info(lease))
return Lease(**self._lease_get_dict_with_added_info(lease))

@wsme_pecan.wsexpose(
LeaseCollection,
Expand Down Expand Up @@ -163,7 +163,7 @@ def get_all(

leases_with_added_info = [
Lease(
**utils.lease_get_dict_with_added_info(
**self._lease_get_dict_with_added_info(
lease, project_list, node_list
)
)
Expand Down Expand Up @@ -233,7 +233,7 @@ def post(self, new_lease):

lease = lease_obj.Lease(**lease_dict)
lease.create(request)
return Lease(**utils.lease_get_dict_with_added_info(lease))
return Lease(**self._lease_get_dict_with_added_info(lease))

@wsme_pecan.wsexpose(Lease, wtypes.text, body={wtypes.text: wtypes.text})
def patch(self, lease_uuid, patch=None):
Expand All @@ -254,7 +254,7 @@ def patch(self, lease_uuid, patch=None):
updates = {"end_time": new_end_time}
lease.update(updates, request)

return Lease(**utils.lease_get_dict_with_added_info(lease))
return Lease(**self._lease_get_dict_with_added_info(lease))

@wsme_pecan.wsexpose(Lease, wtypes.text)
def delete(self, lease_id):
Expand Down Expand Up @@ -341,3 +341,16 @@ def _lease_get_all_authorize_filters(
del filters[k]

return filters

@staticmethod
def _lease_get_dict_with_added_info(lease, project_list=None, node_list=None):
resource = lease.resource_object()

idp = get_idp()
lease_dict = lease.to_dict()
lease_dict["project"] = idp.get_project_name(lease.project_id, project_list)
lease_dict["owner"] = idp.get_project_name(lease.owner_id, project_list)
lease_dict["resource"] = resource.get_name(node_list)
lease_dict["resource_class"] = resource.get_resource_class(node_list)
lease_dict["resource_properties"] = resource.get_properties(node_list)
return lease_dict
24 changes: 20 additions & 4 deletions esi_leap/api/controllers/v1/offer.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def get_one(self, offer_id):
)
utils.check_offer_lessee(cdict, offer)

o = utils.offer_get_dict_with_added_info(offer)
o = self._offer_get_dict_with_added_info(offer)

return Offer(**o)

Expand Down Expand Up @@ -200,7 +200,7 @@ def get_all(

offers_with_added_info = [
Offer(
**utils.offer_get_dict_with_added_info(o, project_list, node_list)
**self._offer_get_dict_with_added_info(o, project_list, node_list)
)
for o in offers
]
Expand Down Expand Up @@ -265,7 +265,7 @@ def post(self, new_offer):

o = offer_obj.Offer(**offer_dict)
o.create()
return Offer(**utils.offer_get_dict_with_added_info(o))
return Offer(**self._offer_get_dict_with_added_info(o))

@wsme_pecan.wsexpose(Offer, wtypes.text)
def delete(self, offer_id):
Expand Down Expand Up @@ -323,4 +323,20 @@ def claim(self, offer_uuid, new_lease):

new_lease = lease_obj.Lease(**lease_dict)
new_lease.create(request)
return lease.Lease(**utils.lease_get_dict_with_added_info(new_lease))
return lease.Lease(
**lease.LeasesController._lease_get_dict_with_added_info(new_lease)
)

@staticmethod
def _offer_get_dict_with_added_info(offer, project_list=None, node_list=None):
resource = offer.resource_object()

idp = get_idp()
o = offer.to_dict()
o["availabilities"] = offer.get_availabilities()
o["project"] = idp.get_project_name(offer.project_id, project_list)
o["lessee"] = idp.get_project_name(offer.lessee_id, project_list)
o["resource"] = resource.get_name(node_list)
o["resource_class"] = resource.get_resource_class(node_list)
o["resource_properties"] = resource.get_properties(node_list)
return o
27 changes: 0 additions & 27 deletions esi_leap/api/controllers/v1/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,33 +155,6 @@ def check_offer_lessee(cdict, offer):
)


def offer_get_dict_with_added_info(offer, project_list=None, node_list=None):
resource = offer.resource_object()
idp = get_idp()

o = offer.to_dict()
o["availabilities"] = offer.get_availabilities()
o["project"] = idp.get_project_name(offer.project_id, project_list)
o["lessee"] = idp.get_project_name(offer.lessee_id, project_list)
o["resource"] = resource.get_name(node_list)
o["resource_class"] = resource.get_resource_class(node_list)
o["resource_properties"] = resource.get_properties(node_list)
return o


def lease_get_dict_with_added_info(lease, project_list=None, node_list=None):
resource = lease.resource_object()
idp = get_idp()

lease_dict = lease.to_dict()
lease_dict["project"] = idp.get_project_name(lease.project_id, project_list)
lease_dict["owner"] = idp.get_project_name(lease.owner_id, project_list)
lease_dict["resource"] = resource.get_name(node_list)
lease_dict["resource_class"] = resource.get_resource_class(node_list)
lease_dict["resource_properties"] = resource.get_properties(node_list)
return lease_dict


def check_lease_length(cdict, start_time, end_time, max_time):
if (end_time - start_time) > datetime.timedelta(days=max_time):
# Check if the current project is admin
Expand Down
54 changes: 41 additions & 13 deletions esi_leap/tests/api/controllers/v1/test_lease.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from esi_leap.resource_objects.ironic_node import IronicNode
from esi_leap.resource_objects.fake_node import FakeNode
from esi_leap.tests.api import base as test_api_base
from esi_leap.conf import CONF


class TestLeasesController(test_api_base.APITestCase):
Expand Down Expand Up @@ -68,7 +69,9 @@ def test_empty(self):

@mock.patch("esi_leap.common.ironic.get_node_list")
@mock.patch("esi_leap.common.idp.dummyIDP.DummyIDP.get_project_list")
@mock.patch("esi_leap.api.controllers.v1.utils." "lease_get_dict_with_added_info")
@mock.patch(
"esi_leap.api.controllers.v1.lease.LeasesController._lease_get_dict_with_added_info"
)
@mock.patch("esi_leap.objects.lease.Lease.get_all")
def test_one(self, mock_ga, mock_lgdwai, mock_gpl, mock_gnl):
mock_ga.return_value = [self.test_lease]
Expand All @@ -83,7 +86,9 @@ def test_one(self, mock_ga, mock_lgdwai, mock_gpl, mock_gnl):
mock_gnl.assert_called_once()
mock_lgdwai.assert_called_once()

@mock.patch("esi_leap.api.controllers.v1.utils." "lease_get_dict_with_added_info")
@mock.patch(
"esi_leap.api.controllers.v1.lease.LeasesController._lease_get_dict_with_added_info"
)
@mock.patch("esi_leap.api.controllers.v1.lease.get_resource_object")
@mock.patch("esi_leap.common.idp.dummyIDP.DummyIDP.get_project_uuid_from_ident")
@mock.patch("oslo_utils.uuidutils.generate_uuid")
Expand Down Expand Up @@ -131,7 +136,9 @@ def test_post(
self.assertEqual(return_data, request.json)
self.assertEqual(http_client.CREATED, request.status_int)

@mock.patch("esi_leap.api.controllers.v1.utils." "lease_get_dict_with_added_info")
@mock.patch(
"esi_leap.api.controllers.v1.lease.LeasesController._lease_get_dict_with_added_info"
)
@mock.patch("esi_leap.api.controllers.v1.lease.get_resource_object")
@mock.patch("esi_leap.common.idp.dummyIDP.DummyIDP.get_project_uuid_from_ident")
@mock.patch("oslo_utils.uuidutils.generate_uuid")
Expand Down Expand Up @@ -178,7 +185,9 @@ def test_post_default_resource_type(
self.assertEqual(return_data, request.json)
self.assertEqual(http_client.CREATED, request.status_int)

@mock.patch("esi_leap.api.controllers.v1.utils." "lease_get_dict_with_added_info")
@mock.patch(
"esi_leap.api.controllers.v1.lease.LeasesController._lease_get_dict_with_added_info"
)
@mock.patch("esi_leap.api.controllers.v1.utils." "check_resource_lease_admin")
@mock.patch("esi_leap.api.controllers.v1.lease.get_resource_object")
@mock.patch("esi_leap.common.idp.dummyIDP.DummyIDP.get_project_uuid_from_ident")
Expand Down Expand Up @@ -281,7 +290,9 @@ def test_post_non_admin_no_parent_lease(
mock_create.assert_not_called()
self.assertEqual(http_client.FORBIDDEN, request.status_int)

@mock.patch("esi_leap.api.controllers.v1.utils." "lease_get_dict_with_added_info")
@mock.patch(
"esi_leap.api.controllers.v1.lease.LeasesController._lease_get_dict_with_added_info"
)
@mock.patch("esi_leap.objects.lease.Lease.update")
@mock.patch("esi_leap.api.controllers.v1.utils." "check_lease_policy_and_retrieve")
def test_patch(self, mock_clpar, mock_lease_update, mock_lgdwai):
Expand All @@ -297,7 +308,9 @@ def test_patch(self, mock_clpar, mock_lease_update, mock_lgdwai):
mock_lgdwai.assert_called_once()
self.assertEqual(http_client.OK, request.status_int)

@mock.patch("esi_leap.api.controllers.v1.utils." "lease_get_dict_with_added_info")
@mock.patch(
"esi_leap.api.controllers.v1.lease.LeasesController._lease_get_dict_with_added_info"
)
@mock.patch("esi_leap.objects.lease.Lease.update")
@mock.patch("esi_leap.api.controllers.v1.utils." "check_lease_policy_and_retrieve")
def test_patch_no_end_time(self, mock_clpar, mock_lease_update, mock_lgdwai):
Expand All @@ -315,7 +328,9 @@ def test_patch_no_end_time(self, mock_clpar, mock_lease_update, mock_lgdwai):
mock_lgdwai.assert_not_called()
self.assertEqual(http_client.INTERNAL_SERVER_ERROR, request.status_int)

@mock.patch("esi_leap.api.controllers.v1.utils." "lease_get_dict_with_added_info")
@mock.patch(
"esi_leap.api.controllers.v1.lease.LeasesController._lease_get_dict_with_added_info"
)
@mock.patch("esi_leap.objects.lease.Lease.update")
@mock.patch("esi_leap.api.controllers.v1.utils." "check_lease_policy_and_retrieve")
def test_patch_end_time_and_more(self, mock_clpar, mock_lease_update, mock_lgdwai):
Expand All @@ -335,7 +350,9 @@ def test_patch_end_time_and_more(self, mock_clpar, mock_lease_update, mock_lgdwa

@mock.patch("esi_leap.common.ironic.get_node_list")
@mock.patch("esi_leap.common.idp.dummyIDP.DummyIDP.get_project_list")
@mock.patch("esi_leap.api.controllers.v1.utils." "lease_get_dict_with_added_info")
@mock.patch(
"esi_leap.api.controllers.v1.lease.LeasesController._lease_get_dict_with_added_info"
)
@mock.patch(
"esi_leap.api.controllers.v1.lease.LeasesController."
"_lease_get_all_authorize_filters"
Expand Down Expand Up @@ -369,7 +386,9 @@ def test_get_nofilters(

@mock.patch("esi_leap.common.ironic.get_node_list")
@mock.patch("esi_leap.common.idp.dummyIDP.DummyIDP.get_project_list")
@mock.patch("esi_leap.api.controllers.v1.utils." "lease_get_dict_with_added_info")
@mock.patch(
"esi_leap.api.controllers.v1.lease.LeasesController._lease_get_dict_with_added_info"
)
@mock.patch("esi_leap.common.idp.dummyIDP.DummyIDP.get_project_uuid_from_ident")
@mock.patch(
"esi_leap.api.controllers.v1.lease.LeasesController."
Expand Down Expand Up @@ -405,7 +424,9 @@ def test_get_project_filter(

@mock.patch("esi_leap.common.ironic.get_node_list")
@mock.patch("esi_leap.common.idp.dummyIDP.DummyIDP.get_project_list")
@mock.patch("esi_leap.api.controllers.v1.utils." "lease_get_dict_with_added_info")
@mock.patch(
"esi_leap.api.controllers.v1.lease.LeasesController._lease_get_dict_with_added_info"
)
@mock.patch("esi_leap.common.idp.dummyIDP.DummyIDP.get_project_uuid_from_ident")
@mock.patch(
"esi_leap.api.controllers.v1.lease.LeasesController."
Expand Down Expand Up @@ -443,7 +464,9 @@ def test_get_owner_filter(

@mock.patch("esi_leap.common.ironic.get_node_list")
@mock.patch("esi_leap.common.idp.dummyIDP.DummyIDP.get_project_list")
@mock.patch("esi_leap.api.controllers.v1.utils." "lease_get_dict_with_added_info")
@mock.patch(
"esi_leap.api.controllers.v1.lease.LeasesController._lease_get_dict_with_added_info"
)
@mock.patch("esi_leap.api.controllers.v1.lease.get_resource_object")
@mock.patch(
"esi_leap.api.controllers.v1.lease.LeasesController."
Expand Down Expand Up @@ -481,7 +504,9 @@ def test_get_resource_filter(

@mock.patch("esi_leap.common.ironic.get_node_list")
@mock.patch("esi_leap.common.idp.dummyIDP.DummyIDP.get_project_list")
@mock.patch("esi_leap.api.controllers.v1.utils." "lease_get_dict_with_added_info")
@mock.patch(
"esi_leap.api.controllers.v1.lease.LeasesController._lease_get_dict_with_added_info"
)
@mock.patch(
"esi_leap.api.controllers.v1.lease.LeasesController."
"_lease_get_all_authorize_filters"
Expand Down Expand Up @@ -547,7 +572,9 @@ def _get_lease_response(lease, use_datetime=False):

@mock.patch("esi_leap.common.ironic.get_node_list")
@mock.patch("esi_leap.common.idp.dummyIDP.DummyIDP.get_project_list")
@mock.patch("esi_leap.api.controllers.v1.utils." "lease_get_dict_with_added_info")
@mock.patch(
"esi_leap.api.controllers.v1.lease.LeasesController._lease_get_dict_with_added_info"
)
@mock.patch("esi_leap.api.controllers.v1.lease.get_resource_object")
@mock.patch(
"esi_leap.api.controllers.v1.lease.LeasesController."
Expand Down Expand Up @@ -604,6 +631,7 @@ class TestLeaseControllersGetAllFilters(testtools.TestCase):
def setUp(self):
super(TestLeaseControllersGetAllFilters, self).setUp()

CONF.set_override("auth_enable", True, group="pecan")
self.admin_ctx = ctx.RequestContext(project_id="adminid", roles=["admin"])
self.owner_ctx = ctx.RequestContext(project_id="ownerid", roles=["owner"])
self.lessee_ctx = ctx.RequestContext(project_id="lesseeid", roles=["lessee"])
Expand Down
Loading

0 comments on commit 787749e

Please sign in to comment.