Skip to content

Commit

Permalink
Merge pull request #115 from samuel-zuk/remove-ro-factory
Browse files Browse the repository at this point in the history
Removed ResourceObjectFactory
  • Loading branch information
tzumainn authored Jul 21, 2022
2 parents 6b17c32 + 8e5ed26 commit 4352f71
Show file tree
Hide file tree
Showing 16 changed files with 204 additions and 196 deletions.
9 changes: 4 additions & 5 deletions esi_leap/api/controllers/v1/lease.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from esi_leap.common import statuses
import esi_leap.conf
from esi_leap.objects import lease as lease_obj
from esi_leap.resource_objects import resource_object_factory as ro_factory
from esi_leap.resource_objects import get_resource_object

CONF = esi_leap.conf.CONF

Expand Down Expand Up @@ -102,8 +102,7 @@ def get_all(self, project_id=None, start_time=None, end_time=None,
if resource_type is None:
resource_type = CONF.api.default_resource_type

resource = ro_factory.ResourceObjectFactory.get_resource_object(
resource_type, resource_uuid)
resource = get_resource_object(resource_type, resource_uuid)
resource_uuid = resource.get_resource_uuid()

filters = LeasesController._lease_get_all_authorize_filters(
Expand Down Expand Up @@ -143,8 +142,8 @@ def post(self, new_lease):
lease_dict['uuid'] = uuidutils.generate_uuid()
if 'resource_type' not in lease_dict:
lease_dict['resource_type'] = CONF.api.default_resource_type
resource = ro_factory.ResourceObjectFactory.get_resource_object(
lease_dict['resource_type'], lease_dict['resource_uuid'])
resource = get_resource_object(lease_dict['resource_type'],
lease_dict['resource_uuid'])
lease_dict['resource_uuid'] = resource.get_resource_uuid()

if 'project_id' in lease_dict:
Expand Down
9 changes: 4 additions & 5 deletions esi_leap/api/controllers/v1/offer.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import esi_leap.conf
from esi_leap.objects import lease as lease_obj
from esi_leap.objects import offer as offer_obj
from esi_leap.resource_objects import resource_object_factory as ro_factory
from esi_leap.resource_objects import get_resource_object

CONF = esi_leap.conf.CONF

Expand Down Expand Up @@ -110,8 +110,7 @@ def get_all(self, project_id=None, resource_type=None,
if resource_uuid is not None:
if resource_type is None:
resource_type = CONF.api.default_resource_type
resource = ro_factory.ResourceObjectFactory.get_resource_object(
resource_type, resource_uuid)
resource = get_resource_object(resource_type, resource_uuid)
resource_uuid = resource.get_resource_uuid()

# either both are defined or both are None
Expand Down Expand Up @@ -193,8 +192,8 @@ def post(self, new_offer):
offer_dict['uuid'] = uuidutils.generate_uuid()
if 'resource_type' not in offer_dict:
offer_dict['resource_type'] = CONF.api.default_resource_type
resource = ro_factory.ResourceObjectFactory.get_resource_object(
offer_dict['resource_type'], offer_dict['resource_uuid'])
resource = get_resource_object(offer_dict['resource_type'],
offer_dict['resource_uuid'])
offer_dict['resource_uuid'] = resource.get_resource_uuid()

if 'lessee_id' in offer_dict:
Expand Down
9 changes: 4 additions & 5 deletions esi_leap/objects/lease.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from esi_leap.objects import base
from esi_leap.objects import fields
from esi_leap.objects import offer as offer_obj
from esi_leap.resource_objects import resource_object_factory as ro_factory
from esi_leap.resource_objects import get_resource_object

from oslo_config import cfg
from oslo_log import log as logging
Expand Down Expand Up @@ -98,8 +98,8 @@ def create(self, context=None):
parent_lease.verify_child_availability(updates['start_time'],
updates['end_time'])
else:
ro = ro_factory.ResourceObjectFactory.get_resource_object(
updates['resource_type'], updates['resource_uuid'])
ro = get_resource_object(updates['resource_type'],
updates['resource_uuid'])
ro.verify_availability(updates['start_time'],
updates['end_time'])

Expand Down Expand Up @@ -206,8 +206,7 @@ def expire(self, context=None):
self.save(context)

def resource_object(self):
return ro_factory.ResourceObjectFactory.get_resource_object(
self.resource_type, self.resource_uuid)
return get_resource_object(self.resource_type, self.resource_uuid)

def verify_child_availability(self, start_time, end_time):
return self.dbapi.lease_verify_child_availability(
Expand Down
9 changes: 4 additions & 5 deletions esi_leap/objects/offer.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from esi_leap.objects import base
from esi_leap.objects import fields
from esi_leap.objects import lease as lease_obj
from esi_leap.resource_objects import resource_object_factory as ro_factory
from esi_leap.resource_objects import get_resource_object

from oslo_config import cfg
from oslo_log import log as logging
Expand Down Expand Up @@ -142,8 +142,8 @@ def create(self, context=None):
parent_lease.verify_child_availability(updates['start_time'],
updates['end_time'])
else:
ro = ro_factory.ResourceObjectFactory.get_resource_object(
updates['resource_type'], updates['resource_uuid'])
ro = get_resource_object(updates['resource_type'],
updates['resource_uuid'])
ro.verify_availability(updates['start_time'],
updates['end_time'])

Expand Down Expand Up @@ -195,5 +195,4 @@ def save(self, context=None):
self._from_db_object(context, self, db_offer)

def resource_object(self):
return ro_factory.ResourceObjectFactory.get_resource_object(
self.resource_type, self.resource_uuid)
return get_resource_object(self.resource_type, self.resource_uuid)
34 changes: 34 additions & 0 deletions esi_leap/resource_objects/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# 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.
from esi_leap.common.exception import ResourceTypeUnknown
from esi_leap.resource_objects import base
# types derived from base won't show as subclasses unless imported somewhere
from esi_leap.resource_objects import dummy_node # noqa: F401
from esi_leap.resource_objects import ironic_node # noqa: F401
from esi_leap.resource_objects import test_node # noqa: F401


_RESOURCE_TYPE_MAP = {
typ.resource_type: typ for typ in
base.ResourceObjectInterface.__subclasses__()}
RESOURCE_TYPES = tuple(_RESOURCE_TYPE_MAP.keys())


def get_type(resource_type):
if resource_type in RESOURCE_TYPES:
return _RESOURCE_TYPE_MAP[resource_type]
else:
raise ResourceTypeUnknown(resource_type=resource_type)


def get_resource_object(resource_type, resource_ident):
return get_type(resource_type)(resource_ident)
14 changes: 7 additions & 7 deletions esi_leap/resource_objects/ironic_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
# License for the specific language governing permissions and limitations
# under the License.

from oslo_utils.uuidutils import is_uuid_like

from esi_leap.common import ironic
import esi_leap.conf
from esi_leap.resource_objects import base
Expand All @@ -30,13 +32,11 @@ class IronicNode(base.ResourceObjectInterface):

resource_type = 'ironic_node'

def __init__(self, uuid):
self._uuid = uuid

@classmethod
def get_by_name(cls, name):
node = get_ironic_client().node.get(name)
return IronicNode(node.uuid)
def __init__(self, ident):
if not is_uuid_like(ident):
node = get_ironic_client().node.get(ident)
ident = node.uuid
self._uuid = ident

def get_resource_uuid(self):
return self._uuid
Expand Down
36 changes: 0 additions & 36 deletions esi_leap/resource_objects/resource_object_factory.py

This file was deleted.

40 changes: 18 additions & 22 deletions esi_leap/tests/api/controllers/v1/test_lease.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ def test_one(self, mock_ga, mock_lgdwai, mock_gpl, mock_gnl):

@mock.patch('esi_leap.api.controllers.v1.utils.'
'lease_get_dict_with_added_info')
@mock.patch('esi_leap.resource_objects.resource_object_factory.'
'ResourceObjectFactory.get_resource_object')
@mock.patch('esi_leap.api.controllers.v1.lease.get_resource_object')
@mock.patch('esi_leap.common.keystone.get_project_uuid_from_ident')
@mock.patch('oslo_utils.uuidutils.generate_uuid')
@mock.patch('esi_leap.api.controllers.v1.utils.check_resource_admin')
Expand Down Expand Up @@ -133,16 +132,15 @@ def test_post(self, mock_create, mock_cra, mock_generate_uuid,

@mock.patch('esi_leap.api.controllers.v1.utils.'
'lease_get_dict_with_added_info')
@mock.patch('esi_leap.resource_objects.resource_object_factory.'
'ResourceObjectFactory.get_resource_object')
@mock.patch('esi_leap.api.controllers.v1.lease.get_resource_object')
@mock.patch('esi_leap.common.keystone.get_project_uuid_from_ident')
@mock.patch('oslo_utils.uuidutils.generate_uuid')
@mock.patch('esi_leap.api.controllers.v1.utils.check_resource_admin')
@mock.patch('esi_leap.objects.lease.Lease.create')
def test_post_default_resource_type(self, mock_create, mock_cra,
mock_generate_uuid, mock_gpufi,
mock_gro, mock_lgdwai):
resource = IronicNode('1234567890')
resource = IronicNode('13921c8d-ce11-4b6d-99ed-10e19d184e5f')
data = {
'project_id': 'lesseeid',
'resource_uuid': '1234567890',
Expand Down Expand Up @@ -181,16 +179,15 @@ def test_post_default_resource_type(self, mock_create, mock_cra,
'lease_get_dict_with_added_info')
@mock.patch('esi_leap.api.controllers.v1.utils.'
'check_resource_lease_admin')
@mock.patch('esi_leap.resource_objects.resource_object_factory.'
'ResourceObjectFactory.get_resource_object')
@mock.patch('esi_leap.api.controllers.v1.lease.get_resource_object')
@mock.patch('esi_leap.common.keystone.get_project_uuid_from_ident')
@mock.patch('oslo_utils.uuidutils.generate_uuid')
@mock.patch('esi_leap.api.controllers.v1.utils.check_resource_admin')
@mock.patch('esi_leap.objects.lease.Lease.create')
def test_post_non_admin_parent_lease(self, mock_create, mock_cra,
mock_generate_uuid, mock_gpufi,
mock_gro, mock_crla, mock_lgdwai):
resource = IronicNode('1234567890')
resource = IronicNode('13921c8d-ce11-4b6d-99ed-10e19d184e5f')
data = {
'project_id': 'lesseeid',
'resource_uuid': '1234567890',
Expand Down Expand Up @@ -238,32 +235,32 @@ def test_post_non_admin_parent_lease(self, mock_create, mock_cra,

@mock.patch('esi_leap.api.controllers.v1.utils.'
'check_resource_lease_admin')
@mock.patch('esi_leap.resource_objects.resource_object_factory.'
'ResourceObjectFactory.get_resource_object')
@mock.patch('esi_leap.api.controllers.v1.lease.get_resource_object')
@mock.patch('esi_leap.common.keystone.get_project_uuid_from_ident')
@mock.patch('oslo_utils.uuidutils.generate_uuid')
@mock.patch('esi_leap.api.controllers.v1.utils.check_resource_admin')
@mock.patch('esi_leap.objects.lease.Lease.create')
def test_post_non_admin_no_parent_lease(self, mock_create, mock_cra,
mock_generate_uuid, mock_gpufi,
mock_gro, mock_crla):
resource = IronicNode('1234567890')
fake_uuid = '13921c8d-ce11-4b6d-99ed-10e19d184e5f'
resource = IronicNode(fake_uuid)
mock_gro.return_value = resource
mock_gpufi.return_value = 'lesseeid'
mock_generate_uuid.return_value = self.test_lease.uuid
mock_cra.side_effect = exception.HTTPResourceForbidden(
resource_type='ironic_node', resource='1234567890')
resource_type='ironic_node', resource=fake_uuid)
mock_crla.return_value = None

data = {
'project_id': 'lesseeid',
'resource_uuid': '1234567890',
'resource_uuid': fake_uuid,
'start_time': '2016-07-17T19:20:30',
'end_time': '2016-08-14T19:20:30'
}
request = self.post_json('/leases', data, expect_errors=True)

mock_gro.assert_called_once_with('ironic_node', '1234567890')
mock_gro.assert_called_once_with('ironic_node', fake_uuid)
mock_generate_uuid.assert_called_once()
mock_cra.assert_called_once_with(
self.context.to_policy_values(),
Expand Down Expand Up @@ -380,8 +377,7 @@ def test_get_owner_filter(self, mock_get_all, mock_lgaaf,
@mock.patch('esi_leap.common.keystone.get_project_list')
@mock.patch('esi_leap.api.controllers.v1.utils.'
'lease_get_dict_with_added_info')
@mock.patch('esi_leap.resource_objects.resource_object_factory.'
'ResourceObjectFactory.get_resource_object')
@mock.patch('esi_leap.api.controllers.v1.lease.get_resource_object')
@mock.patch('esi_leap.api.controllers.v1.lease.LeasesController.'
'_lease_get_all_authorize_filters')
@mock.patch('esi_leap.objects.lease.Lease.get_all')
Expand Down Expand Up @@ -478,23 +474,23 @@ def _get_lease_response(l, use_datetime=False):
@mock.patch('esi_leap.common.keystone.get_project_list')
@mock.patch('esi_leap.api.controllers.v1.utils.'
'lease_get_dict_with_added_info')
@mock.patch('esi_leap.resource_objects.resource_object_factory.'
'ResourceObjectFactory.get_resource_object')
@mock.patch('esi_leap.api.controllers.v1.lease.get_resource_object')
@mock.patch('esi_leap.api.controllers.v1.lease.LeasesController.'
'_lease_get_all_authorize_filters')
@mock.patch('esi_leap.objects.lease.Lease.get_all')
def test_get_resource_filter_default_resource_type(self, mock_get_all,
mock_lgaaf, mock_gro,
mock_lgdwai, mock_gpl,
mock_gnl):
mock_gro.return_value = IronicNode('54321')
fake_uuid = uuidutils.generate_uuid()
mock_gro.return_value = IronicNode(fake_uuid)
mock_get_all.return_value = [self.test_lease, self.test_lease]
mock_gpl.return_value = []
mock_gnl.return_value = []

self.get_json('/leases?resource_uuid=54321')
self.get_json('/leases?resource_uuid=%s' % fake_uuid)

mock_gro.assert_called_once_with('ironic_node', '54321')
mock_gro.assert_called_once_with('ironic_node', fake_uuid)
mock_lgaaf.assert_called_once_with(self.context.to_policy_values(),
project_id=None,
start_time=None,
Expand All @@ -504,7 +500,7 @@ def test_get_resource_filter_default_resource_type(self, mock_get_all,
view=None,
owner_id=None,
resource_type='ironic_node',
resource_uuid='54321')
resource_uuid=fake_uuid)

mock_get_all.assert_called_once()
mock_gpl.assert_called_once()
Expand Down
Loading

0 comments on commit 4352f71

Please sign in to comment.