Make 'method' explicit for VendorInterface.validate()

Method should be more explicit than only having it via kwargs.

Closes-Bug: #1394470
Change-Id: I6a55e1be013629e22c384b968699980e83ae0cd3
This commit is contained in:
Tan Lin 2014-12-29 22:45:29 +00:00 committed by Lin Tan
parent 892e382c21
commit 9650c88d97
10 changed files with 33 additions and 31 deletions

View File

@ -468,12 +468,13 @@ class VendorInterface(object):
"""
@abc.abstractmethod
def validate(self, task, **kwargs):
def validate(self, task, method=None, **kwargs):
"""Validate vendor-specific actions.
If invalid, raises an exception; otherwise returns None.
:param task: a task from TaskManager.
:param method: method to be validated
:param kwargs: info for action.
:raises: UnsupportedDriverExtension if 'method' can not be mapped to
the supported interfaces.

View File

@ -296,12 +296,13 @@ class AgentVendorInterface(base.VendorInterface):
# not by the operator.
return {}
def validate(self, task, **kwargs):
def validate(self, task, method, **kwargs):
"""Validate the driver-specific Node deployment info.
No validation necessary.
:param task: a TaskManager instance
:param method: method to be validated
"""
pass

View File

@ -89,8 +89,7 @@ class FakeVendorA(base.VendorInterface):
return {'A1': 'A1 description. Required.',
'A2': 'A2 description. Optional.'}
def validate(self, task, **kwargs):
method = kwargs.get('method')
def validate(self, task, method, **kwargs):
if method == 'first_method':
bar = kwargs.get('bar')
if not bar:
@ -110,8 +109,7 @@ class FakeVendorB(base.VendorInterface):
return {'B1': 'B1 description. Required.',
'B2': 'B2 description. Required.'}
def validate(self, task, **kwargs):
method = kwargs.get('method')
def validate(self, task, method, **kwargs):
if method in ('second_method', 'third_method_sync'):
bar = kwargs.get('bar')
if not bar:

View File

@ -486,14 +486,15 @@ class VendorPassthru(base.VendorInterface):
def get_properties(self):
return COMMON_PROPERTIES
def validate(self, task, **kwargs):
def validate(self, task, method, **kwargs):
"""Validate vendor-specific actions.
Checks if a valid vendor passthru method was passed and validates
the parameters for the vendor passthru method.
:param task: a TaskManager instance containing the node to act on.
:param kwargs: kwargs containing the vendor passthru method and its
:param method: method to be validated.
:param kwargs: kwargs containing the vendor passthru method's
parameters.
:raises: MissingParameterValue, if some required parameters were not
passed.

View File

@ -884,7 +884,7 @@ class VendorPassthru(base.VendorInterface):
def get_properties(self):
return COMMON_PROPERTIES
def validate(self, task, **kwargs):
def validate(self, task, method, **kwargs):
"""Validate vendor-specific actions.
If invalid, raises an exception; otherwise returns None.
@ -894,13 +894,13 @@ class VendorPassthru(base.VendorInterface):
* bmc_reset
:param task: a task from TaskManager.
:param method: method to be validated
:param kwargs: info for action.
:raises: InvalidParameterValue when an invalid parameter value is
specified.
:raises: MissingParameterValue if a required parameter is missing.
"""
method = kwargs['method']
if method == 'send_raw':
if not kwargs.get('raw_bytes'):
raise exception.MissingParameterValue(_(

View File

@ -432,7 +432,7 @@ class VendorPassthru(base.VendorInterface):
def get_properties(self):
return COMMON_PROPERTIES
def validate(self, task, **kwargs):
def validate(self, task, method, **kwargs):
"""Validates the inputs for a vendor passthru.
This method checks whether the vendor passthru method is a valid one,
@ -440,9 +440,9 @@ class VendorPassthru(base.VendorInterface):
vendor passthru has been provided or not.
:param task: a TaskManager instance containing the node to act on.
:param kwargs: kwargs containins the method name and its parameters.
:raises: InvalidParameterValue if method is invalid or any parameters
to the method is invalid.
:param method: method to be validated.
:param kwargs: kwargs containins the method's parameters.
:raises: InvalidParameterValue if any parameters is invalid.
"""
iscsi_deploy.get_deploy_info(task.node, **kwargs)

View File

@ -451,7 +451,7 @@ class VendorPassthru(base.VendorInterface):
def get_properties(self):
return COMMON_PROPERTIES
def validate(self, task, **kwargs):
def validate(self, task, method, **kwargs):
_parse_driver_info(task.node)
@base.passthru(['POST'])

View File

@ -68,12 +68,11 @@ class MixinVendorInterface(base.VendorInterface):
pass
return d
def _get_route(self, **kwargs):
def _get_route(self, method):
"""Return the driver interface which contains the given method.
:param method: The name of the vendor method.
"""
method = kwargs.get('method')
if not method:
raise exception.MissingParameterValue(
_("Method not specified when calling vendor extension."))
@ -98,17 +97,18 @@ class MixinVendorInterface(base.VendorInterface):
properties.update(interface.get_properties())
return properties
def validate(self, *args, **kwargs):
def validate(self, task, method, **kwargs):
"""Call validate on the appropriate interface only.
:raises: UnsupportedDriverExtension if 'method' can not be mapped to
the supported interfaces.
:raises: InvalidParameterValue if kwargs does not contain 'method'.
:raisee: MissingParameterValue if missing parameters in kwargs.
:raises: InvalidParameterValue if 'method' is invalid.
:raisee: MissingParameterValue if missing 'method' or parameters
in kwargs.
"""
route = self._get_route(**kwargs)
route.validate(*args, **kwargs)
route = self._get_route(method)
route.validate(task, method=method, **kwargs)
def get_node_mac_addresses(task):

View File

@ -145,7 +145,8 @@ class TestAgentVendor(db_base.DbTestCase):
def test_validate(self):
with task_manager.acquire(self.context, self.node.uuid) as task:
self.passthru.validate(task)
method = 'heartbeat'
self.passthru.validate(task, method)
def test_driver_validate(self):
kwargs = {'version': '2'}

View File

@ -44,16 +44,16 @@ class UtilsTestCase(db_base.DbTestCase):
@mock.patch.object(fake.FakeVendorA, 'validate')
def test_vendor_interface_validate_valid_methods(self,
mock_fakea_validate):
self.driver.vendor.validate(method='first_method')
mock_fakea_validate.assert_called_once_with(method='first_method')
with task_manager.acquire(self.context, self.node.uuid) as task:
self.driver.vendor.validate(task, method='first_method')
mock_fakea_validate.assert_called_once_with(task,
method='first_method')
def test_vendor_interface_validate_bad_method(self):
self.assertRaises(exception.InvalidParameterValue,
self.driver.vendor.validate, method='fake_method')
def test_vendor_interface_validate_none_method(self):
self.assertRaises(exception.MissingParameterValue,
self.driver.vendor.validate)
with task_manager.acquire(self.context, self.node.uuid) as task:
self.assertRaises(exception.InvalidParameterValue,
self.driver.vendor.validate,
task, method='fake_method')
def test_get_node_mac_addresses(self):
ports = []