Add get_resource method for v2.2

Support getting a resource object by calling get_resource method for RSD v2.2.

Change-Id: Iaf8fa1fb5001fd318813423fd72ba252a15297e9
This commit is contained in:
leizhang
2019-06-18 12:05:13 +08:00
parent cc51729406
commit 184e4a9493
3 changed files with 108 additions and 0 deletions

View File

@@ -15,12 +15,14 @@
from sushy.resources import base
from rsd_lib import exceptions as rsd_lib_exceptions
from rsd_lib.resources import v2_1
from rsd_lib.resources.v2_2.ethernet_switch import ethernet_switch
from rsd_lib.resources.v2_2.fabric import fabric
from rsd_lib.resources.v2_2.node import node
from rsd_lib.resources.v2_2.system import system
from rsd_lib.resources.v2_2.telemetry_service import telemetry_service
from rsd_lib.resources.v2_2.types import RESOURCE_CLASS
from rsd_lib.resources.v2_2.update_service import update_service
@@ -149,3 +151,18 @@ class RSDLibV2_2(v2_1.RSDLibV2_1):
return fabric.Fabric(
self._conn, identity, redfish_version=self.redfish_version
)
def get_resource(self, path):
"""Return corresponding resource object from path
: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)
if not resource_class:
raise rsd_lib_exceptions.NoMatchingResourceError(uri=path)
return resource_class(
self._conn, path, redfish_version=self.redfish_version
)

View File

@@ -0,0 +1,61 @@
# Copyright 2019 Intel, Inc.
# All Rights Reserved.
#
# 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 copy import deepcopy
from rsd_lib.resources.v2_1.types import RESOURCE_CLASS as RESOURCE_CLASS_V21
from rsd_lib.resources.v2_2.ethernet_switch import ethernet_switch_metrics
from rsd_lib.resources.v2_2.ethernet_switch import ethernet_switch_port_metrics
from rsd_lib.resources.v2_2.system import computer_system_metrics
from rsd_lib.resources.v2_2.system import memory_metrics
from rsd_lib.resources.v2_2.system import processor_metrics
from rsd_lib.resources.v2_2.telemetry_service import metric
from rsd_lib.resources.v2_2.telemetry_service import metric_definition
from rsd_lib.resources.v2_2.telemetry_service import metric_report
from rsd_lib.resources.v2_2.telemetry_service import metric_report_definition
from rsd_lib.resources.v2_2.telemetry_service import telemetry_service
from rsd_lib.resources.v2_2.telemetry_service import triggers
from rsd_lib.resources.v2_2.update_service import action_info
from rsd_lib.resources.v2_2.update_service import update_service
RESOURCE_CLASS = deepcopy(RESOURCE_CLASS_V21)
RESOURCE_CLASS.update(
{
'ActionInfo': action_info.ActionInfo,
'ComputerSystemMetrics': computer_system_metrics.ComputerSystemMetrics,
'EthernetSwitchMetrics': ethernet_switch_metrics.EthernetSwitchMetrics,
'EthernetSwitchPortMetrics':
ethernet_switch_port_metrics.EthernetSwitchPortMetrics,
'MemoryMetrics': memory_metrics.MemoryMetrics,
'Metric': metric.Metric,
'MetricDefinition': metric_definition.MetricDefinition,
'MetricDefinitionCollection':
metric_definition.MetricDefinitionCollection,
'MetricReport': metric_report.MetricReport,
'MetricReportCollection': metric_report.MetricReportCollection,
'MetricReportDefinition':
metric_report_definition.MetricReportDefinition,
'MetricReportDefinitionCollection':
metric_report_definition.MetricReportDefinitionCollection,
# 'PortMetrics'
'ProcessorMetrics': processor_metrics.ProcessorMetrics,
'TelemetryService': telemetry_service.TelemetryService,
'Triggers': triggers.Triggers,
'TriggersCollection': triggers.TriggersCollection,
'UpdateService': update_service.UpdateService
}
)

View File

@@ -17,6 +17,7 @@ import json
import mock
import testtools
from rsd_lib.exceptions import NoMatchingResourceError
from rsd_lib.resources.v2_1.chassis import chassis as v2_1_chassis
from rsd_lib.resources.v2_1.event_service import event_service \
as v2_1_event_service
@@ -267,3 +268,32 @@ class RSDLibV2_2TestCase(testtools.TestCase):
"/redfish/v1/EventService",
redfish_version=self.rsd.redfish_version,
)
def test_get_resource(self):
with mock.patch.object(
self.rsd,
"_get_resource_class_from_path",
return_value=v2_2_telemetry_service.TelemetryService,
):
with open(
"rsd_lib/tests/unit/json_samples/v2_2/telemetry_service.json",
"r"
) as f:
self.conn.get.return_value.json.return_value = json.loads(
f.read()
)
self.assertIsInstance(
self.rsd.get_resource(
"/redfish/v1/TelemetryService"),
v2_2_telemetry_service.TelemetryService
)
def test_get_resource_with_no_class_match(self):
with mock.patch.object(
self.rsd, "_get_resource_class_from_path", return_value=None
):
self.assertRaises(
NoMatchingResourceError,
self.rsd.get_resource,
"/redfish/v1/chassis/1",
)