Add method to create new ethernet switch ACL

Change-Id: I5cd2d254816f8873478880c74727257b3b609ebb
This commit is contained in:
Lin Yang 2019-03-26 21:04:30 -07:00
parent 66fa276d41
commit ee1a29e504
4 changed files with 53 additions and 16 deletions

View File

@ -13,6 +13,8 @@
# License for the specific language governing permissions and limitations
# under the License.
import logging
from sushy import exceptions
from sushy.resources import base
from sushy.resources import common
@ -21,6 +23,8 @@ 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
LOG = logging.getLogger(__name__)
class BindActionField(common.ActionField):
@ -149,6 +153,20 @@ class EthernetSwitchACL(rsd_lib_base.ResourceBase):
class EthernetSwitchACLCollection(rsd_lib_base.ResourceCollectionBase):
@property
def _resource_type(self):
return EthernetSwitchACL
def create_acl(self):
"""Create a new ACL
:returns: The location of the acl rule
"""
target_uri = self._path
resp = self._conn.post(target_uri, data={})
acl_url = resp.headers["Location"]
LOG.info("Create ACL at %s", acl_url)
return acl_url[acl_url.find(self._path):]

View File

@ -136,8 +136,8 @@ class EthernetSwitchACLRuleCollection(rsd_lib_base.ResourceCollectionBase):
def _resource_type(self):
return EthernetSwitchACLRule
def add_acl_rule(self, acl_rule_req):
"""Add a acl rule
def create_acl_rule(self, acl_rule_req):
"""Create a new ACL rule
:param acl_rule: JSON for acl_rule
:returns: The location of the acl rule
@ -146,5 +146,5 @@ class EthernetSwitchACLRuleCollection(rsd_lib_base.ResourceCollectionBase):
validate(acl_rule_req, acl_rule_schema.acl_rule_req_schema)
resp = self._conn.post(target_uri, data=acl_rule_req)
acl_rule_url = resp.headers["Location"]
LOG.info("ACL Rule add at %s", acl_rule_url)
LOG.info("Create ACL Rule at %s", acl_rule_url)
return acl_rule_url[acl_rule_url.find(self._path):]

View File

@ -21,6 +21,7 @@ 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
from rsd_lib.tests.unit.fakes import request_fakes
class EthernetSwitchACLTestCase(testtools.TestCase):
@ -28,7 +29,7 @@ class EthernetSwitchACLTestCase(testtools.TestCase):
super(EthernetSwitchACLTestCase, self).setUp()
self.conn = mock.Mock()
with open(
"rsd_lib/tests/unit/json_samples/v2_1/ethernet_switch_acl." "json",
"rsd_lib/tests/unit/json_samples/v2_1/ethernet_switch_acl.json",
"r",
) as f:
self.conn.get.return_value.json.return_value = json.loads(f.read())
@ -214,11 +215,19 @@ class EthernetSwitchACLCollectionTestCase(testtools.TestCase):
"r",
) as f:
self.conn.get.return_value.json.return_value = json.loads(f.read())
self.acl_col = ethernet_switch_acl.EthernetSwitchACLCollection(
self.conn,
"/redfish/v1/EthernetSwitches/ACLs",
redfish_version="1.0.2",
)
self.conn.post.return_value = request_fakes.fake_request_post(
None,
headers={
"Location": "https://localhost:8443/redfish/v1/"
"EthernetSwitches/Switch1/ACLs/ACL1"
},
)
self.acl_col = ethernet_switch_acl.EthernetSwitchACLCollection(
self.conn,
"/redfish/v1/EthernetSwitches/Switch1/ACLs",
redfish_version="1.0.2",
)
def test__parse_attributes(self):
self.acl_col._parse_attributes()
@ -252,3 +261,13 @@ class EthernetSwitchACLCollectionTestCase(testtools.TestCase):
)
self.assertIsInstance(members, list)
self.assertEqual(1, len(members))
def test_create_acl(self):
result = self.acl_col.create_acl()
self.acl_col._conn.post.assert_called_once_with(
"/redfish/v1/EthernetSwitches/Switch1/ACLs", data={},
)
self.assertEqual(
result,
"/redfish/v1/EthernetSwitches/Switch1/ACLs/ACL1",
)

View File

@ -268,7 +268,7 @@ class EthernetSwitchACLRuleCollectionTestCase(testtools.TestCase):
self.assertIsInstance(members, list)
self.assertEqual(1, len(members))
def test_add_acl_rule_reqs(self):
def test_create_acl_rule_reqs(self):
reqs = {
"RuleId": 1,
"Action": "Mirror",
@ -296,7 +296,7 @@ class EthernetSwitchACLRuleCollectionTestCase(testtools.TestCase):
"L4Protocol": 1,
},
}
result = self.acl_rule_col.add_acl_rule(reqs)
result = self.acl_rule_col.create_acl_rule(reqs)
self.acl_rule_col._conn.post.assert_called_once_with(
"/redfish/v1/EthernetSwitches/Switch1/ACLs/ACL1/Rules", data=reqs
)
@ -339,7 +339,7 @@ class EthernetSwitchACLRuleCollectionTestCase(testtools.TestCase):
acl_rule_req.pop("Action")
self.assertRaises(
jsonschema.exceptions.ValidationError,
self.acl_rule_col.add_acl_rule,
self.acl_rule_col.create_acl_rule,
acl_rule_req,
)
@ -348,7 +348,7 @@ class EthernetSwitchACLRuleCollectionTestCase(testtools.TestCase):
acl_rule_req.update({"RuleId": "WrongFormat"})
self.assertRaises(
jsonschema.exceptions.ValidationError,
self.acl_rule_col.add_acl_rule,
self.acl_rule_col.create_acl_rule,
acl_rule_req,
)
@ -357,7 +357,7 @@ class EthernetSwitchACLRuleCollectionTestCase(testtools.TestCase):
acl_rule_req["Additional"] = "AdditionalField"
self.assertRaises(
jsonschema.exceptions.ValidationError,
self.acl_rule_col.add_acl_rule,
self.acl_rule_col.create_acl_rule,
acl_rule_req,
)
@ -366,7 +366,7 @@ class EthernetSwitchACLRuleCollectionTestCase(testtools.TestCase):
acl_rule_req["MirrorType"] = "WrongEnum"
self.assertRaises(
jsonschema.exceptions.ValidationError,
self.acl_rule_col.add_acl_rule,
self.acl_rule_col.create_acl_rule,
acl_rule_req,
)
@ -375,6 +375,6 @@ class EthernetSwitchACLRuleCollectionTestCase(testtools.TestCase):
acl_rule_req.pop("ForwardMirrorInterface")
self.assertRaises(
jsonschema.exceptions.ValidationError,
self.acl_rule_col.add_acl_rule,
self.acl_rule_col.create_acl_rule,
acl_rule_req,
)