From e8e0c3220649a0a91c2f38d36014a777e13a5cc1 Mon Sep 17 00:00:00 2001 From: Chris Archibald Date: Mon, 13 Apr 2020 14:17:50 -0400 Subject: [PATCH] Sync bitbucket and Github --- ansible_collections/netapp/ontap/README.md | 3 ++ .../modules/na_ontap_autosupport_invoke.py | 48 +++++++++++++++---- .../test_na_ontap_autosupport_invoke.py | 23 ++++++++- 3 files changed, 62 insertions(+), 12 deletions(-) diff --git a/ansible_collections/netapp/ontap/README.md b/ansible_collections/netapp/ontap/README.md index 52997e90..7e2fbb05 100644 --- a/ansible_collections/netapp/ontap/README.md +++ b/ansible_collections/netapp/ontap/README.md @@ -26,6 +26,9 @@ Join our Slack Channel at [Netapp.io](http://netapp.io/slack) ### New Options - na_ontap_firmware_upgrade: `force_disruptive_update` and `package_url` options allows to make choices for download and upgrading packages. +### Added REST support to existing modules +- na_ontap_autosupport_invoke: added REST support for sending autosupport message. + ### Bug Fixes - na_ontap_volume: `volume_security_style` option now allows modify. - na_ontap_info: `metrocluster_check_info` has been removed as it was breaking the info module for everyone who didn't have a metrocluster set up. We are working on adding this back in a future update diff --git a/ansible_collections/netapp/ontap/plugins/modules/na_ontap_autosupport_invoke.py b/ansible_collections/netapp/ontap/plugins/modules/na_ontap_autosupport_invoke.py index 42c19c2b..ff3ff7e0 100644 --- a/ansible_collections/netapp/ontap/plugins/modules/na_ontap_autosupport_invoke.py +++ b/ansible_collections/netapp/ontap/plugins/modules/na_ontap_autosupport_invoke.py @@ -25,6 +25,7 @@ - Send an AutoSupport message from a node options: + name: description: - The name of the node to send the message to. @@ -65,17 +66,21 @@ RETURN = ''' ''' +import traceback from ansible.module_utils.basic import AnsibleModule +from ansible.module_utils._text import to_native from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule from ansible_collections.netapp.ontap.plugins.module_utils.netapp import OntapRestAPI import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils +HAS_NETAPP_LIB = netapp_utils.has_netapp_lib() + class NetAppONTAPasupInvoke(object): def __init__(self): - + self.use_rest = False self.argument_spec = netapp_utils.na_ontap_host_argument_spec() self.argument_spec.update(dict( name=dict(required=False, type='str'), @@ -97,24 +102,47 @@ def __init__(self): if self.restApi.is_rest(): self.use_rest = True else: - self.module.fail_json(msg="This module only supports REST API.") + if not HAS_NETAPP_LIB: + self.module.fail_json(msg="the python NetApp-Lib module is required") + else: + self.server = netapp_utils.setup_na_ontap_zapi(module=self.module) def send_message(self): params = dict() - if self.parameters.get('name'): - params['node.name'] = self.parameters['name'] if self.parameters.get('message'): params['message'] = self.parameters['message'] if self.parameters.get('type'): params['type'] = self.parameters['type'] if self.parameters.get('uri'): params['uri'] = self.parameters['uri'] - api = 'support/autosupport/messages' - message, error = self.restApi.post(api, params) - if error is not None: - self.module.fail_json(msg="Error on sending autosupport message: %s." % error) + + if self.use_rest: + if self.parameters.get('name'): + params['node.name'] = self.parameters['name'] + api = 'support/autosupport/messages' + message, error = self.restApi.post(api, params) + if error is not None: + self.module.fail_json(msg="Error on sending autosupport message: %s." % error) + else: + if self.parameters.get('name'): + params['node-name'] = self.parameters['name'] + send_message = netapp_utils.zapi.NaElement.create_node_with_children( + 'autosupport-invoke', **params) + try: + self.server.invoke_successfully(send_message, enable_tunneling=False) + except netapp_utils.zapi.NaApiError as error: + self.module.fail_json(msg="Error on sending autosupport message: %s." + % (to_native(error)), + exception=traceback.format_exc()) + + def ems_log_event(self): + results = netapp_utils.get_cserver(self.server) + cserver = netapp_utils.setup_na_ontap_zapi(module=self.module, vserver=results) + return netapp_utils.ems_log_event("na_ontap_autosupport_invoke", cserver) def apply(self): + if not self.use_rest: + self.ems_log_event() if self.module.check_mode: pass else: @@ -123,8 +151,8 @@ def apply(self): def main(): - alias = NetAppONTAPasupInvoke() - alias.apply() + message = NetAppONTAPasupInvoke() + message.apply() if __name__ == '__main__': diff --git a/ansible_collections/netapp/ontap/tests/unit/plugins/modules/test_na_ontap_autosupport_invoke.py b/ansible_collections/netapp/ontap/tests/unit/plugins/modules/test_na_ontap_autosupport_invoke.py index b341c7aa..9127cf3f 100644 --- a/ansible_collections/netapp/ontap/tests/unit/plugins/modules/test_na_ontap_autosupport_invoke.py +++ b/ansible_collections/netapp/ontap/tests/unit/plugins/modules/test_na_ontap_autosupport_invoke.py @@ -12,6 +12,7 @@ from ansible_collections.netapp.ontap.tests.unit.compat.mock import patch, Mock from ansible.module_utils import basic from ansible.module_utils._text import to_bytes +import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils from ansible_collections.netapp.ontap.plugins.modules.na_ontap_autosupport_invoke \ import NetAppONTAPasupInvoke as invoke_module # module under test @@ -54,6 +55,12 @@ def fail_json(*args, **kwargs): # pylint: disable=unused-argument kwargs['failed'] = True raise AnsibleFailJson(kwargs) +class MockONTAPConnection(object): + ''' mock server connection to ONTAP host ''' + + def invoke_successfully(self, xml, enable_tunneling): + raise netapp_utils.zapi.NaApiError('test', 'Expected error') + class TestMyModule(unittest.TestCase): ''' Unit tests for na_ontap_wwpn_alias ''' @@ -80,8 +87,11 @@ def mock_args(self): 'password': 'test_pass!' } - def get_invoke_mock_object(self): + def get_invoke_mock_object(self, use_rest=True): invoke_obj = invoke_module() + if not use_rest: + invoke_obj.ems_log_event = Mock() + invoke_obj.server = MockONTAPConnection() return invoke_obj @patch('ansible_collections.netapp.ontap.plugins.module_utils.netapp.OntapRestAPI.send_request') @@ -111,4 +121,13 @@ def test_rest_send_error(self, mock_request): ] with pytest.raises(AnsibleFailJson) as exc: self.get_invoke_mock_object().apply() - assert exc.value.args[0]['msg'] == "Error on sending autosupport message: Expected error." + assert exc.value.args[0]['msg'] == "Error on sending autosupport message: Expected error." + + def test_zapi_send_error(self): + '''Test rest send error''' + data = self.mock_args() + data['use_rest'] = 'Never' + set_module_args(data) + with pytest.raises(AnsibleFailJson) as exc: + self.get_invoke_mock_object(use_rest=False).apply() + assert exc.value.args[0]['msg'] == "Error on sending autosupport message: NetApp API failed. Reason - test:Expected error."