From cc51729406ad9edb84d66faebdcb793b82c9a108 Mon Sep 17 00:00:00 2001 From: leizhang Date: Mon, 17 Jun 2019 19:10:40 +0800 Subject: [PATCH] Refactor get_resource method Add param resource_class for _get_resource_class_from_path method. Change-Id: I5870e1b616b25c62fd4cddd2a914186fa029b824 --- rsd_lib/resources/v2_1/__init__.py | 12 ++++++++---- .../tests/unit/resources/v2_1/test_rsdlib_v2_1.py | 7 ++++++- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/rsd_lib/resources/v2_1/__init__.py b/rsd_lib/resources/v2_1/__init__.py index f176115..52d40cc 100644 --- a/rsd_lib/resources/v2_1/__init__.py +++ b/rsd_lib/resources/v2_1/__init__.py @@ -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.system import system 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): @@ -283,10 +283,12 @@ class RSDLibV2_1(base.ResourceBase): 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 :param path: Path of any rsd resource + :param resource_class: Mapping for looking up resource class by + entity_type string :returns: Corresponding resource class """ 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 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): """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 :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: raise rsd_lib_exceptions.NoMatchingResourceError(uri=path) return resource_class( diff --git a/rsd_lib/tests/unit/resources/v2_1/test_rsdlib_v2_1.py b/rsd_lib/tests/unit/resources/v2_1/test_rsdlib_v2_1.py index da7a71a..7bd6d8f 100644 --- a/rsd_lib/tests/unit/resources/v2_1/test_rsdlib_v2_1.py +++ b/rsd_lib/tests/unit/resources/v2_1/test_rsdlib_v2_1.py @@ -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.system import system 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): @@ -243,6 +244,7 @@ class RSDLibV2_1TestCase(testtools.TestCase): exceptions.ConnectionError, self.rsd._get_resource_class_from_path, "/redfish/v1/Chassis/1", + RESOURCE_CLASS ) self.conn.reset() @@ -254,6 +256,7 @@ class RSDLibV2_1TestCase(testtools.TestCase): "/redfish/v1/Chassis/1", self.rsd._get_resource_class_from_path, "/redfish/v1/Chassis/1", + RESOURCE_CLASS ) self.conn.reset() @@ -263,7 +266,9 @@ class RSDLibV2_1TestCase(testtools.TestCase): } self.assertEqual( 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()