Skip to content

Commit

Permalink
Added dummy identity provider to remove Keystone dependancy
Browse files Browse the repository at this point in the history
during testing

The dummy identity provider (idp) can be enabled by setting
the environment variable ESI_DEBUG to True. For now, the
dummy idp returns information about a dummy project

Some functions from `api/controllers/v1/utils.py` have been
moved into the only controllers that use them and turned into
static class methods.

Allow idp injection through config file
  • Loading branch information
QuanMPhm committed Jul 19, 2024
1 parent 822016c commit 3288e63
Show file tree
Hide file tree
Showing 15 changed files with 317 additions and 115 deletions.
4 changes: 2 additions & 2 deletions esi_leap/api/controllers/v1/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from esi_leap.api.controllers import types
from esi_leap.api.controllers.v1 import utils
from esi_leap.common import exception
from esi_leap.common import keystone
from esi_leap.common.idp import get_idp
import esi_leap.conf
from esi_leap.objects import event as event_obj
from esi_leap.resource_objects import get_resource_object
Expand Down Expand Up @@ -72,7 +72,7 @@ def get_all(self, last_event_id=None, lessee_or_owner_id=None,
lessee_or_owner_id = cdict['project_id']

if lessee_or_owner_id is not None:
lessee_or_owner_id = keystone.get_project_uuid_from_ident(
lessee_or_owner_id = idp.get_project_uuid_from_ident(
lessee_or_owner_id)

if resource_uuid is not None:
Expand Down
33 changes: 24 additions & 9 deletions esi_leap/api/controllers/v1/lease.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
from esi_leap.api.controllers.v1 import utils
from esi_leap.common import constants
from esi_leap.common import exception
from esi_leap.common.idp import get_idp
from esi_leap.common import ironic
from esi_leap.common import keystone
from esi_leap.common import statuses
import esi_leap.conf
from esi_leap.objects import lease as lease_obj
Expand Down Expand Up @@ -84,7 +84,7 @@ def get_one(self, lease_id):
lease = utils.check_lease_policy_and_retrieve(
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, wtypes.text,
datetime.datetime, datetime.datetime, wtypes.text,
Expand All @@ -97,10 +97,10 @@ def get_all(self, project_id=None, start_time=None, end_time=None,
cdict = request.to_policy_values()

if project_id is not None:
project_id = keystone.get_project_uuid_from_ident(project_id)
project_id = idp.get_project_uuid_from_ident(project_id)

if owner_id is not None:
owner_id = keystone.get_project_uuid_from_ident(owner_id)
owner_id = idp.get_project_uuid_from_ident(owner_id)

if resource_uuid is not None:
if resource_type is None:
Expand All @@ -126,12 +126,12 @@ def get_all(self, project_id=None, start_time=None, end_time=None,

with concurrent.futures.ThreadPoolExecutor() as executor:
f1 = executor.submit(ironic.get_node_list)
f2 = executor.submit(keystone.get_project_list)
f2 = executor.submit(idp.get_project_list)
node_list = f1.result()
project_list = f2.result()

leases_with_added_info = [
Lease(**utils.lease_get_dict_with_added_info(l, project_list,
Lease(**self._lease_get_dict_with_added_info(l, project_list,
node_list))
for l in leases]
if resource_class:
Expand Down Expand Up @@ -159,7 +159,7 @@ def post(self, new_lease):
lease_dict['resource_uuid'] = resource.get_uuid()

if 'project_id' in lease_dict:
lease_dict['project_id'] = keystone.get_project_uuid_from_ident(
lease_dict['project_id'] = idp.get_project_uuid_from_ident(
lease_dict['project_id'])

if 'start_time' not in lease_dict:
Expand Down Expand Up @@ -189,7 +189,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 @@ -208,7 +208,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 @@ -285,3 +285,18 @@ def _lease_get_all_authorize_filters(cdict,
del filters[k]

return filters

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

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
14 changes: 7 additions & 7 deletions esi_leap/api/controllers/v1/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@

from esi_leap.api.controllers import base
from esi_leap.api.controllers import types
from esi_leap.common.idp import get_idp
from esi_leap.common import ironic
from esi_leap.common import keystone
from esi_leap.common import statuses
import esi_leap.conf
from esi_leap.objects import lease as lease_obj
Expand Down Expand Up @@ -71,9 +71,9 @@ def get_all(self, resource_class=None, owner=None,
context = pecan.request.context

if owner is not None:
owner = keystone.get_project_uuid_from_ident(owner)
owner = idp.get_project_uuid_from_ident(owner)
if lessee is not None:
lessee = keystone.get_project_uuid_from_ident(lessee)
lessee = idp.get_project_uuid_from_ident(lessee)

filter_args = {
'resource_class': resource_class,
Expand All @@ -89,7 +89,7 @@ def get_all(self, resource_class=None, owner=None,
k: v for k, v in filter_args.items() if v is not None
}
f1 = executor.submit(ironic.get_node_list, context, **filter_args)
f2 = executor.submit(keystone.get_project_list)
f2 = executor.submit(idp.get_project_list)
nodes = f1.result()
project_list = f2.result()

Expand Down Expand Up @@ -126,9 +126,9 @@ def get_all(self, resource_class=None, owner=None,
properties=ironic.get_condensed_properties(
node.properties),
maintenance=str(node.maintenance),
owner=keystone.get_project_name(node.owner, project_list),
lessee=keystone.get_project_name(node.lessee,
project_list),
owner=idp.get_project_name(node.owner, project_list),
lessee=idp.get_project_name(node.lessee,
project_list),
future_offers=future_offers,
future_leases=f_lease_uuids)

Expand Down
31 changes: 24 additions & 7 deletions esi_leap/api/controllers/v1/offer.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
from esi_leap.api.controllers.v1 import lease
from esi_leap.api.controllers.v1 import utils
from esi_leap.common import exception
from esi_leap.common.idp import get_idp
from esi_leap.common import ironic
from esi_leap.common import keystone
from esi_leap.common import statuses
import esi_leap.conf
from esi_leap.objects import lease as lease_obj
Expand Down Expand Up @@ -90,7 +90,7 @@ def get_one(self, offer_id):
request, 'esi_leap:offer:get', 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 All @@ -107,8 +107,9 @@ def get_all(self, project_id=None, resource_type=None,
cdict = request.to_policy_values()
utils.policy_authorize('esi_leap:offer:get_all', cdict, cdict)

idp = get_idp()
if project_id is not None:
project_id = keystone.get_project_uuid_from_ident(project_id)
project_id = idp.get_project_uuid_from_ident(project_id)

if resource_uuid is not None:
if resource_type is None:
Expand Down Expand Up @@ -176,12 +177,12 @@ def get_all(self, project_id=None, resource_type=None,
node_list = None
with concurrent.futures.ThreadPoolExecutor() as executor:
f1 = executor.submit(ironic.get_node_list)
f2 = executor.submit(keystone.get_project_list)
f2 = executor.submit(idp.get_project_list)
node_list = f1.result()
project_list = f2.result()

offers_with_added_info = [
Offer(**utils.offer_get_dict_with_added_info(o, project_list,
Offer(**self._offer_get_dict_with_added_info(o, project_list,
node_list))
for o in offers]
if resource_class:
Expand All @@ -208,8 +209,9 @@ def post(self, new_offer):
offer_dict['resource_uuid'])
offer_dict['resource_uuid'] = resource.get_uuid()

idp = get_idp()
if 'lessee_id' in offer_dict:
offer_dict['lessee_id'] = keystone.get_project_uuid_from_ident(
offer_dict['lessee_id'] = idp.get_project_uuid_from_ident(
offer_dict['lessee_id'])

if 'start_time' not in offer_dict:
Expand Down Expand Up @@ -238,7 +240,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 @@ -284,3 +286,18 @@ 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))

@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
32 changes: 3 additions & 29 deletions esi_leap/api/controllers/v1/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import datetime

from esi_leap.common import exception
from esi_leap.common import keystone
from esi_leap.common.idp import get_idp
from esi_leap.common import policy
from esi_leap.objects import lease as lease_obj
from esi_leap.objects import offer as offer_obj
Expand Down Expand Up @@ -140,39 +140,13 @@ def check_offer_lessee(cdict, offer):
if offer.lessee_id is None or offer.project_id == project_id:
return

if offer.lessee_id not in keystone.get_parent_project_id_tree(project_id):
idp = get_idp()
if offer.lessee_id not in idp.get_parent_project_id_tree(project_id):
resource_policy_authorize(
'esi_leap:offer:offer_admin',
cdict, cdict, 'offer', offer.uuid)


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

o = offer.to_dict()
o['availabilities'] = offer.get_availabilities()
o['project'] = keystone.get_project_name(offer.project_id, project_list)
o['lessee'] = keystone.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()

lease_dict = lease.to_dict()
lease_dict['project'] = keystone.get_project_name(lease.project_id,
project_list)
lease_dict['owner'] = keystone.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
11 changes: 11 additions & 0 deletions esi_leap/common/idp/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import os

from esi_leap.common.idp import keystoneIDP, dummyIDP

CONF = esi_leap.conf.CONF


def get_idp():
module_path, class_name = CONF.esi.idp_plugin_class.rsplit('.', 1)
module = import_module(module_path)
return getattr(module, class_name)()
32 changes: 32 additions & 0 deletions esi_leap/common/idp/baseIDP.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import abc


class BaseIDP(abc.ABC):

@abc.abstractmethod
def get_project_list():
pass

@abc.abstractmethod
def get_project_name(self, id, project_list=None):
pass

@abc.abstractmethod
def get_parent_project_id_tree(project_id):
pass

@abc.abstractmethod
def get_project_uuid_from_ident(project_ident):
pass
Loading

0 comments on commit 3288e63

Please sign in to comment.