Add Bind/Unbind action in EthernetSwitchACL in RSD 2.1

Change-Id: Ief6ab579e5ca46ab0c18172ea8dc3d31fb4322f7
This commit is contained in:
Lin Yang 2019-03-26 15:48:29 -07:00
parent bb59b57537
commit 74247f2b2e
4 changed files with 189 additions and 12 deletions

View File

@ -13,13 +13,35 @@
# License for the specific language governing permissions and limitations
# under the License.
from sushy import exceptions
from sushy.resources import base
from sushy.resources import common
from sushy import utils
from rsd_lib import base as rsd_lib_base
from rsd_lib.resources.v2_1.ethernet_switch import ethernet_switch_acl_rule
class BindActionField(common.ActionField):
allowed_values = base.Field(
"Port@Redfish.AllowableValues", adapter=utils.get_members_identities
)
class UnbindActionField(common.ActionField):
allowed_values = base.Field(
"Port@Redfish.AllowableValues", adapter=utils.get_members_identities
)
class EthernetSwitchACLActionsField(base.CompositeField):
bind = BindActionField("#EthernetSwitchACL.Bind")
unbind = UnbindActionField("#EthernetSwitchACL.Unbind")
class LinksField(base.CompositeField):
bound_ports = base.Field(
@ -35,7 +57,77 @@ class EthernetSwitchACL(rsd_lib_base.ResourceBase):
links = LinksField("Links")
# TODO(linyang): Add Action Field
_actions = EthernetSwitchACLActionsField("Actions")
def _get_bind_action_element(self):
bind_action = self._actions.bind
if not bind_action:
raise exceptions.MissingActionError(
action="#EthernetSwitchACL.Bind", resource=self._path
)
return bind_action
def get_allowed_bind_ports(self):
"""Get the allowed ports for bind action.
:returns: A set with the allowed bind ports.
"""
bind_action = self._get_bind_action_element()
return bind_action.allowed_values
def bind_port(self, port):
"""Bind port from this switch ACL
:param port: Link to port to bind.
:raises: InvalidParameterValueError
"""
bind_action = self._get_bind_action_element()
valid_ports = bind_action.allowed_values
target_uri = bind_action.target_uri
if port and port not in valid_ports:
raise exceptions.InvalidParameterValueError(
parameter="port", value=port, valid_values=valid_ports
)
data = {"Port": {"@odata.id": port}}
self._conn.post(target_uri, data=data)
def _get_unbind_action_element(self):
unbind_action = self._actions.unbind
if not unbind_action:
raise exceptions.MissingActionError(
action="#EthernetSwitchACL.Unbind", resource=self._path
)
return unbind_action
def get_allowed_unbind_ports(self):
"""Get the allowed ports for unbind action.
:returns: A set with the allowed unbind ports.
"""
unbind_action = self._get_unbind_action_element()
return unbind_action.allowed_values
def unbind_port(self, port):
"""Unbind port from this switch ACL
:param port: Link to port to unbind.
:raises: InvalidParameterValueError
"""
unbind_action = self._get_unbind_action_element()
valid_ports = unbind_action.allowed_values
target_uri = unbind_action.target_uri
if port and port not in valid_ports:
raise exceptions.InvalidParameterValueError(
parameter="port", value=port, valid_values=valid_ports
)
data = {"Port": {"@odata.id": port}}
self._conn.post(target_uri, data=data)
@property
@utils.cache_it

View File

@ -19,7 +19,7 @@
},
"Actions": {
"#EthernetSwitchACL.Bind": {
"target": "/redfish/v1/EthernetSwitches/Switch1/ACLs/ACL1/Actions/EthernetSwitchACL.Bin d",
"target": "/redfish/v1/EthernetSwitches/Switch1/ACLs/ACL1/Actions/EthernetSwitchACL.Bind",
"Port@Redfish.AllowableValues": [
{
"@odata.id": "/redfish/v1/EthernetSwitches/Switch1/Ports/sw0p2"
@ -30,7 +30,7 @@
]
},
"#EthernetSwitchACL.Unbind": {
"target": "/redfish/v1/EthernetSwitches/Switch1/ACLs/ACL1/Actions/EthernetSwitchACL.Unb ind",
"target": "/redfish/v1/EthernetSwitches/Switch1/ACLs/ACL1/Actions/EthernetSwitchACL.Unbind",
"Port@Redfish.AllowableValues": [
{
"@odata.id": "/redfish/v1/EthernetSwitches/Switch1/Ports/sw0p1"

View File

@ -14,10 +14,11 @@
# under the License.
import json
import mock
import testtools
from sushy import exceptions
from rsd_lib.resources.v2_1.ethernet_switch import ethernet_switch_acl
from rsd_lib.resources.v2_1.ethernet_switch import ethernet_switch_acl_rule
@ -51,6 +52,92 @@ class EthernetSwitchACLTestCase(testtools.TestCase):
self.acl_inst.links.bound_ports,
)
def test__get_bind_action_element(self):
value = self.acl_inst._get_bind_action_element()
self.assertEqual(
"/redfish/v1/EthernetSwitches/Switch1/ACLs/ACL1/Actions/"
"EthernetSwitchACL.Bind",
value.target_uri,
)
self.assertEqual(
(
"/redfish/v1/EthernetSwitches/Switch1/Ports/sw0p2",
"/redfish/v1/EthernetSwitches/Switch1/Ports/sw0p3",
),
value.allowed_values,
)
def test_get_allowed_bind_ports(self):
expected = self.acl_inst.get_allowed_bind_ports()
result = (
"/redfish/v1/EthernetSwitches/Switch1/Ports/sw0p2",
"/redfish/v1/EthernetSwitches/Switch1/Ports/sw0p3",
)
self.assertEqual(expected, result)
def test_bind_port(self):
self.acl_inst.bind_port(
"/redfish/v1/EthernetSwitches/Switch1/Ports/sw0p2"
)
self.acl_inst._conn.post.assert_called_once_with(
"/redfish/v1/EthernetSwitches/Switch1/ACLs/ACL1/Actions/"
"EthernetSwitchACL.Bind",
data={
"Port": {
"@odata.id": "/redfish/v1/EthernetSwitches/Switch1/Ports/"
"sw0p2"
}
},
)
def test_bind_port_invalid_enabled(self):
with self.assertRaisesRegex(
exceptions.InvalidParameterValueError,
'The parameter "port" value "invalid-port" is invalid',
):
self.acl_inst.bind_port("invalid-port")
def test__get_unbind_action_element(self):
value = self.acl_inst._get_unbind_action_element()
self.assertEqual(
"/redfish/v1/EthernetSwitches/Switch1/ACLs/ACL1/Actions/"
"EthernetSwitchACL.Unbind",
value.target_uri,
)
self.assertEqual(
("/redfish/v1/EthernetSwitches/Switch1/Ports/sw0p1",),
value.allowed_values,
)
def test_get_allowed_unbind_ports(self):
expected = self.acl_inst.get_allowed_unbind_ports()
result = ("/redfish/v1/EthernetSwitches/Switch1/Ports/sw0p1",)
self.assertEqual(expected, result)
def test_unbind_port(self):
self.acl_inst.unbind_port(
"/redfish/v1/EthernetSwitches/Switch1/Ports/sw0p1"
)
self.acl_inst._conn.post.assert_called_once_with(
"/redfish/v1/EthernetSwitches/Switch1/ACLs/ACL1/Actions/"
"EthernetSwitchACL.Unbind",
data={
"Port": {
"@odata.id": "/redfish/v1/EthernetSwitches/Switch1/Ports/"
"sw0p1"
}
},
)
def test_unbind_port_invalid_enabled(self):
with self.assertRaisesRegex(
exceptions.InvalidParameterValueError,
'The parameter "port" value "invalid-port" is invalid',
):
self.acl_inst.unbind_port("invalid-port")
def test_acl_rule(self):
# | GIVEN |
self.conn.get.return_value.json.reset_mock()

View File

@ -133,21 +133,21 @@ class NodeTestCase(testtools.TestCase):
def test__get_assemble_action_element(self):
value = self.node_inst._get_assemble_action_element()
self.assertEqual(
"/redfish/v1/Nodes/Node1/Actions/" "ComposedNode.Assemble",
"/redfish/v1/Nodes/Node1/Actions/ComposedNode.Assemble",
value.target_uri,
)
def test__get_attach_endpoint_action_element(self):
value = self.node_inst._get_attach_endpoint_action_element()
self.assertEqual(
"/redfish/v1/Nodes/Node1/Actions/" "ComposedNode.AttachEndpoint",
"/redfish/v1/Nodes/Node1/Actions/ComposedNode.AttachEndpoint",
value.target_uri,
)
self.assertEqual(
(
"/redfish/v1/Chassis/PCIeSwitchChassis/" "Drives/Disk.Bay.1",
"/redfish/v1/Chassis/PCIeSwitchChassis/" "Drives/Disk.Bay.2",
"/redfish/v1/Chassis/PCIeSwitchChassis/Drives/Disk.Bay.1",
"/redfish/v1/Chassis/PCIeSwitchChassis/Drives/Disk.Bay.2",
),
value.allowed_values,
)
@ -155,14 +155,12 @@ class NodeTestCase(testtools.TestCase):
def test__get_detach_endpoint_action_element(self):
value = self.node_inst._get_detach_endpoint_action_element()
self.assertEqual(
"/redfish/v1/Nodes/Node1/Actions/" "ComposedNode.DetachEndpoint",
"/redfish/v1/Nodes/Node1/Actions/ComposedNode.DetachEndpoint",
value.target_uri,
)
self.assertEqual(
tuple(
["/redfish/v1/Chassis/" "PCIeSwitchChassis/Drives/Disk.Bay.3"]
),
tuple(["/redfish/v1/Chassis/PCIeSwitchChassis/Drives/Disk.Bay.3"]),
value.allowed_values,
)