Refactor get_resource method

Add param resource_class for _get_resource_class_from_path method.

Change-Id: I5870e1b616b25c62fd4cddd2a914186fa029b824
This commit is contained in:
leizhang 2019-06-17 19:10:40 +08:00
parent d34ece37cd
commit cc51729406
2 changed files with 14 additions and 5 deletions
rsd_lib
resources/v2_1
tests/unit/resources/v2_1

@ -27,7 +27,7 @@ from rsd_lib.resources.v2_1.registries import message_registry_file
from rsd_lib.resources.v2_1.storage_service import storage_service from rsd_lib.resources.v2_1.storage_service import storage_service
from rsd_lib.resources.v2_1.system import system from rsd_lib.resources.v2_1.system import system
from rsd_lib.resources.v2_1.task import task_service from rsd_lib.resources.v2_1.task import task_service
from rsd_lib.resources.v2_1 import types from rsd_lib.resources.v2_1.types import RESOURCE_CLASS
class RSDLibV2_1(base.ResourceBase): class RSDLibV2_1(base.ResourceBase):
@ -283,10 +283,12 @@ class RSDLibV2_1(base.ResourceBase):
redfish_version=self.redfish_version, redfish_version=self.redfish_version,
) )
def _get_resource_class_from_path(self, path): def _get_resource_class_from_path(self, path, resource_class):
"""Get resource class from a given path """Get resource class from a given path
:param path: Path of any rsd resource :param path: Path of any rsd resource
:param resource_class: Mapping for looking up resource class by
entity_type string
:returns: Corresponding resource class :returns: Corresponding resource class
""" """
body = self._conn.get(path=path).json() body = self._conn.get(path=path).json()
@ -299,7 +301,7 @@ class RSDLibV2_1(base.ResourceBase):
# Here we use entity_type to find the corresponding resource class # Here we use entity_type to find the corresponding resource class
entity_type = body["@odata.type"].split(".")[-1] entity_type = body["@odata.type"].split(".")[-1]
return types.RESOURCE_CLASS.get(entity_type) return resource_class.get(entity_type)
def get_resource(self, path): def get_resource(self, path):
"""Return corresponding resource object from path """Return corresponding resource object from path
@ -307,7 +309,9 @@ class RSDLibV2_1(base.ResourceBase):
:param path: The path of a resource or resource collection :param path: The path of a resource or resource collection
:returns: corresponding resource or resource collection object :returns: corresponding resource or resource collection object
""" """
resource_class = self._get_resource_class_from_path(path) resource_class = self._get_resource_class_from_path(
path,
RESOURCE_CLASS)
if not resource_class: if not resource_class:
raise rsd_lib_exceptions.NoMatchingResourceError(uri=path) raise rsd_lib_exceptions.NoMatchingResourceError(uri=path)
return resource_class( return resource_class(

@ -30,6 +30,7 @@ from rsd_lib.resources.v2_1.registries import message_registry_file
from rsd_lib.resources.v2_1.storage_service import storage_service from rsd_lib.resources.v2_1.storage_service import storage_service
from rsd_lib.resources.v2_1.system import system from rsd_lib.resources.v2_1.system import system
from rsd_lib.resources.v2_1.task import task_service from rsd_lib.resources.v2_1.task import task_service
from rsd_lib.resources.v2_1.types import RESOURCE_CLASS
class RSDLibV2_1TestCase(testtools.TestCase): class RSDLibV2_1TestCase(testtools.TestCase):
@ -243,6 +244,7 @@ class RSDLibV2_1TestCase(testtools.TestCase):
exceptions.ConnectionError, exceptions.ConnectionError,
self.rsd._get_resource_class_from_path, self.rsd._get_resource_class_from_path,
"/redfish/v1/Chassis/1", "/redfish/v1/Chassis/1",
RESOURCE_CLASS
) )
self.conn.reset() self.conn.reset()
@ -254,6 +256,7 @@ class RSDLibV2_1TestCase(testtools.TestCase):
"/redfish/v1/Chassis/1", "/redfish/v1/Chassis/1",
self.rsd._get_resource_class_from_path, self.rsd._get_resource_class_from_path,
"/redfish/v1/Chassis/1", "/redfish/v1/Chassis/1",
RESOURCE_CLASS
) )
self.conn.reset() self.conn.reset()
@ -263,7 +266,9 @@ class RSDLibV2_1TestCase(testtools.TestCase):
} }
self.assertEqual( self.assertEqual(
chassis.Chassis, chassis.Chassis,
self.rsd._get_resource_class_from_path("/redfish/v1/Chassis/1"), self.rsd._get_resource_class_from_path(
"/redfish/v1/Chassis/1",
RESOURCE_CLASS),
) )
self.conn.reset() self.conn.reset()