From ee8adbbb1d09240e15bda0c6e50c7109cb08411b Mon Sep 17 00:00:00 2001 From: Lin Yang Date: Tue, 26 Mar 2019 20:18:47 -0700 Subject: [PATCH] Add update method in EthernetSwitchStaticMAC in RSD 2.1 Change-Id: I04790902566a6456b6d95089eca1d8a2925d73b6 --- .../ethernet_switch_static_mac.py | 27 ++++++++++++++ .../test_ethernet_switch_static_mac.py | 36 ++++++++++++++++++- 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/rsd_lib/resources/v2_1/ethernet_switch/ethernet_switch_static_mac.py b/rsd_lib/resources/v2_1/ethernet_switch/ethernet_switch_static_mac.py index 70ba9ea..b44ff6b 100644 --- a/rsd_lib/resources/v2_1/ethernet_switch/ethernet_switch_static_mac.py +++ b/rsd_lib/resources/v2_1/ethernet_switch/ethernet_switch_static_mac.py @@ -16,6 +16,7 @@ from jsonschema import validate import logging +from sushy import exceptions from sushy.resources import base from rsd_lib import base as rsd_lib_base @@ -36,6 +37,32 @@ class EthernetSwitchStaticMAC(rsd_lib_base.ResourceBase): mac_address = base.Field("MACAddress") """The static mac address""" + def update(self, mac_address, vlan_id=None): + """Update attributes of static MAC + + :param mac_address: MAC address that should be forwarded to this port + :param vlan_id: If specified, defines which packets tagged with + specific VLANId should be forwarded to this port + """ + if not isinstance(mac_address, type("")): + raise exceptions.InvalidParameterValueError( + parameter="mac_address", + value=mac_address, + valid_values="string", + ) + data = {"MACAddress": mac_address} + + if vlan_id is not None: + if not isinstance(vlan_id, int): + raise exceptions.InvalidParameterValueError( + parameter="vlan_id", + value=vlan_id, + valid_values="int", + ) + data["VLANId"] = vlan_id + + self._conn.patch(self.path, data=data) + def delete(self): """Delete this static mac address""" self._conn.delete(self._path) diff --git a/rsd_lib/tests/unit/resources/v2_1/ethernet_switch/test_ethernet_switch_static_mac.py b/rsd_lib/tests/unit/resources/v2_1/ethernet_switch/test_ethernet_switch_static_mac.py index f02bd1b..ee46f05 100644 --- a/rsd_lib/tests/unit/resources/v2_1/ethernet_switch/test_ethernet_switch_static_mac.py +++ b/rsd_lib/tests/unit/resources/v2_1/ethernet_switch/test_ethernet_switch_static_mac.py @@ -15,10 +15,11 @@ import json import jsonschema - import mock import testtools +from sushy import exceptions + from rsd_lib.resources.v2_1.ethernet_switch import ethernet_switch_static_mac from rsd_lib.tests.unit.fakes import request_fakes @@ -52,6 +53,39 @@ class EthernetSwitchStaticMACTestCase(testtools.TestCase): self.assertEqual("00:11:22:33:44:55", self.static_mac_inst.mac_address) self.assertEqual(112, self.static_mac_inst.vlan_id) + def test_update(self): + reqs = {"MACAddress": "00:11:22:33:44:55"} + self.static_mac_inst.update("00:11:22:33:44:55") + self.static_mac_inst._conn.patch.assert_called_once_with( + "/redfish/v1/EthernetSwitches/Switch1/Ports/StaticMACs/1", + data=reqs, + ) + + self.static_mac_inst._conn.patch.reset_mock() + reqs = {"MACAddress": "00:11:22:33:44:55", "VLANId": 69} + self.static_mac_inst.update( + "00:11:22:33:44:55", vlan_id=69 + ) + self.static_mac_inst._conn.patch.assert_called_once_with( + "/redfish/v1/EthernetSwitches/Switch1/Ports/StaticMACs/1", + data=reqs, + ) + + def test_update_invalid_reqs(self): + with self.assertRaisesRegex( + exceptions.InvalidParameterValueError, + ('The parameter "mac_address" value "True" is invalid'), + ): + self.static_mac_inst.update(True) + + with self.assertRaisesRegex( + exceptions.InvalidParameterValueError, + ('The parameter "vlan_id" value "invalid-value" is invalid'), + ): + self.static_mac_inst.update( + "00:11:22:33:44:55", vlan_id="invalid-value" + ) + def test_delete(self): self.static_mac_inst.delete() self.static_mac_inst._conn.delete.assert_called_once_with(