Merge "Add Zone resource"

This commit is contained in:
Jenkins 2017-09-06 23:30:22 +00:00 committed by Gerrit Code Review
commit 0203693ff7
6 changed files with 295 additions and 0 deletions

View File

@ -19,6 +19,7 @@ from sushy import exceptions
from sushy.resources import base
from rsd_lib.resources.fabric import endpoint
from rsd_lib.resources.fabric import zone
LOG = logging.getLogger(__name__)
@ -42,6 +43,8 @@ class Fabric(base.ResourceBase):
_endpoints = None # ref to EndpointCollection instance
_zones = None # ref to ZoneCollection instance
def __init__(self, connector, identity, redfish_version=None):
"""A class representing a Fabric
@ -75,9 +78,32 @@ class Fabric(base.ResourceBase):
return self._endpoints
def _get_zone_collection_path(self):
"""Helper function to find the ZoneCollection path"""
zone_col = self.json.get('Zones')
if not zone_col:
raise exceptions.MissingAttributeError(attribute='Zones',
resource=self._path)
return zone_col.get('@odata.id')
@property
def zones(self):
"""Property to provide reference to `ZoneCollection` instance
It is calculated once when it is queried for the first time. On
refresh, this property is reset.
"""
if self._zones is None:
self._zones = zone.ZoneCollection(
self._conn, self._get_zone_collection_path(),
redfish_version=self.redfish_version)
return self._zones
def refresh(self):
super(Fabric, self).refresh()
self._endpoints = None
self._zones = None
class FabricCollection(base.ResourceCollectionBase):

View File

@ -0,0 +1,82 @@
# Copyright 2017 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.
import logging
from sushy.resources import base
from sushy import utils
from rsd_lib.resources.fabric import endpoint
LOG = logging.getLogger(__name__)
class ZoneLinksField(base.CompositeField):
endpoint_identities = base.Field('Endpoints', default=[],
adapter=utils.get_members_identities)
class Zone(base.ResourceBase):
description = base.Field('Description')
"""The zone description"""
identity = base.Field('Id', required=True)
"""The zone identity string"""
name = base.Field('Name')
"""The zone name"""
links = ZoneLinksField('Links')
"""The zone links"""
_endpoints = None # ref to contained endpoints
def __init__(self, connector, identity, redfish_version=None):
"""A class representing a Zone
:param connector: A Connector instance
:param identity: The identity of the Zone resource
:param redfish_version: The version of RedFish. Used to construct
the object according to schema of the given version.
"""
super(Zone, self).__init__(connector, identity,
redfish_version)
def get_endpoints(self):
"""Return a list of Endpoints present in the Zone
:returns: A list of Endpoint objects
"""
return [endpoint.Endpoint(self._conn, id_, self.redfish_version) for
id_ in self.links.endpoint_identities]
class ZoneCollection(base.ResourceCollectionBase):
@property
def _resource_type(self):
return Zone
def __init__(self, connector, path, redfish_version=None):
"""A class representing a Zone Collection
:param connector: A Connector instance
:param path: The canonical path to the Zone collection resource
:param redfish_version: The version of RedFish. Used to construct
the object according to schema of the given version.
"""
super(ZoneCollection, self).__init__(connector, path,
redfish_version)

View File

@ -0,0 +1,22 @@
{
"@odata.context": "/redfish/v1/$metadata#Zone.Zone",
"@odata.id": "/redfish/v1/Fabrics/PCIe/Zones/1",
"@odata.type": "#Zone.v1_0_0.Zone",
"Id": "1",
"Name": "PCIe Zone 1",
"Description": "PCIe Zone 1",
"Status": {
"State": "Enabled",
"Health": "OK"
},
"Links": {
"Endpoints": [
{"@odata.id": "/redfish/v1/Fabrics/PCIe/Endpoints/HostRootComplex1"},
{"@odata.id": "/redfish/v1/Fabrics/PCIe/Endpoints/NVMeDrivePF2"}
],
"InvolvedSwitches": [
{"@odata.id": "/redfish/v1/Fabrics/PCIe/Switches/1"}
]
},
"Oem": {}
}

View File

@ -0,0 +1,16 @@
{
"@odata.context": "/redfish/v1/$metadata#ZoneCollection.ZoneCollection",
"@odata.id": "/redfish/v1/Fabrics/PCIe/Zones",
"@odata.type": "#ZoneCollection.ZoneCollection",
"Name": "PCIe Zone Collection",
"Description": "PCIe Zone Collection",
"Members@odata.count": 2,
"Members": [
{
"@odata.id": "/redfish/v1/Fabrics/PCIe/Zones/1"
},
{
"@odata.id": "/redfish/v1/Fabrics/PCIe/Zones/2"
}
]
}

View File

@ -21,6 +21,7 @@ import testtools
from rsd_lib.resources.fabric import endpoint
from rsd_lib.resources.fabric import fabric
from rsd_lib.resources.fabric import zone
class FabricTestCase(testtools.TestCase):
@ -45,6 +46,7 @@ class FabricTestCase(testtools.TestCase):
self.assertEqual('PCIe', self.fabric_inst.fabric_type)
self.assertEqual(5, self.fabric_inst.max_zones)
self.assertIsNone(self.fabric_inst._endpoints)
self.assertIsNone(self.fabric_inst._zones)
def test__get_endpoint_collection_path_missing_attr(self):
self.fabric_inst._json.pop('Endpoints')
@ -101,6 +103,61 @@ class FabricTestCase(testtools.TestCase):
self.assertIsInstance(self.fabric_inst.endpoints,
endpoint.EndpointCollection)
def test__get_zone_collection_path_missing_attr(self):
self.fabric_inst._json.pop('Zones')
self.assertRaisesRegex(
exceptions.MissingAttributeError, 'attribute Zones',
self.fabric_inst._get_zone_collection_path)
def test_zones(self):
# check for the underneath variable value
self.assertIsNone(self.fabric_inst._zones)
# | GIVEN |
self.conn.get.return_value.json.reset_mock()
with open('rsd_lib/tests/unit/json_samples/'
'zone_collection.json', 'r') as f:
self.conn.get.return_value.json.return_value = json.loads(f.read())
# | WHEN |
actual_zones = self.fabric_inst.zones
# | THEN |
self.assertIsInstance(actual_zones,
zone.ZoneCollection)
self.conn.get.return_value.json.assert_called_once_with()
# reset mock
self.conn.get.return_value.json.reset_mock()
# | WHEN & THEN |
# tests for same object on invoking subsequently
self.assertIs(actual_zones,
self.fabric_inst.zones)
self.conn.get.return_value.json.assert_not_called()
def test_zones_on_refresh(self):
# | GIVEN |
with open('rsd_lib/tests/unit/json_samples/'
'zone_collection.json', 'r') as f:
self.conn.get.return_value.json.return_value = json.loads(f.read())
# | WHEN & THEN |
self.assertIsInstance(self.fabric_inst.zones,
zone.ZoneCollection)
# On refreshing the fabric instance...
with open('rsd_lib/tests/unit/json_samples/'
'fabric.json', 'r') as f:
self.conn.get.return_value.json.return_value = json.loads(f.read())
self.fabric_inst.refresh()
# | WHEN & THEN |
self.assertIsNone(self.fabric_inst._zones)
# | GIVEN |
with open('rsd_lib/tests/unit/json_samples/'
'zone_collection.json', 'r') as f:
self.conn.get.return_value.json.return_value = json.loads(f.read())
# | WHEN & THEN |
self.assertIsInstance(self.fabric_inst.zones,
zone.ZoneCollection)
class FabricCollectionTestCase(testtools.TestCase):

View File

@ -0,0 +1,92 @@
# Copyright 2017 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.
import json
import mock
import testtools
from rsd_lib.resources.fabric import zone
class ZoneTestCase(testtools.TestCase):
def setUp(self):
super(ZoneTestCase, self).setUp()
self.conn = mock.Mock()
with open('rsd_lib/tests/unit/json_samples/zone.json',
'r') as f:
self.conn.get.return_value.json.return_value = json.loads(f.read())
self.zone_inst = zone.Zone(
self.conn, '/redfish/v1/Fabrics/PCIe/Zones/1',
redfish_version='1.0.2')
def test__parse_attributes(self):
self.zone_inst._parse_attributes()
self.assertEqual('1.0.2', self.zone_inst.redfish_version)
self.assertEqual('PCIe Zone 1',
self.zone_inst.description)
self.assertEqual('1', self.zone_inst.identity)
self.assertEqual('PCIe Zone 1', self.zone_inst.name)
self.assertEqual(('/redfish/v1/Fabrics/PCIe/'
'Endpoints/HostRootComplex1',
'/redfish/v1/Fabrics/PCIe/Endpoints/NVMeDrivePF2'),
self.zone_inst.links.endpoint_identities)
def test_get_endpoints(self):
self.conn.get.return_value.json.reset_mock()
with open('rsd_lib/tests/unit/json_samples/endpoint.json', 'r') as f:
self.conn.get.return_value.json.return_value = json.loads(f.read())
endpoints = self.zone_inst.get_endpoints()
self.assertEqual(endpoints[0].identity, 'NVMeDrivePF1')
class ZoneCollectionTestCase(testtools.TestCase):
def setUp(self):
super(ZoneCollectionTestCase, self).setUp()
self.conn = mock.Mock()
with open('rsd_lib/tests/unit/json_samples/'
'zone_collection.json', 'r') as f:
self.conn.get.return_value.json.return_value = json.loads(f.read())
self.zone_col = zone.ZoneCollection(
self.conn, '/redfish/v1/Fabrics/PCIe/Zones',
redfish_version='1.0.2')
def test__parse_attributes(self):
self.zone_col._parse_attributes()
self.assertEqual('1.0.2', self.zone_col.redfish_version)
self.assertEqual('PCIe Zone Collection',
self.zone_col.name)
self.assertEqual(('/redfish/v1/Fabrics/PCIe/Zones/1',
'/redfish/v1/Fabrics/PCIe/Zones/2'),
self.zone_col.members_identities)
@mock.patch.object(zone, 'Zone', autospec=True)
def test_get_member(self, mock_zone):
self.zone_col.get_member('/redfish/v1/Fabrics/PCIe/Zones/1')
mock_zone.assert_called_once_with(
self.zone_col._conn, '/redfish/v1/Fabrics/PCIe/Zones/1',
redfish_version=self.zone_col.redfish_version)
@mock.patch.object(zone, 'Zone', autospec=True)
def test_get_members(self, mock_zone):
members = self.zone_col.get_members()
mock_zone.assert_called_with(
self.zone_col._conn, '/redfish/v1/Fabrics/PCIe/Zones/2',
redfish_version=self.zone_col.redfish_version)
self.assertIsInstance(members, list)
self.assertEqual(2, len(members))