Skip to content
This repository has been archived by the owner on Feb 24, 2022. It is now read-only.

Commit

Permalink
Sync bitbucket and Github
Browse files Browse the repository at this point in the history
  • Loading branch information
carchi8py committed Apr 13, 2020
1 parent 1b512f3 commit e8e0c32
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 12 deletions.
3 changes: 3 additions & 0 deletions ansible_collections/netapp/ontap/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
- Send an AutoSupport message from a node
options:
name:
description:
- The name of the node to send the message to.
Expand Down Expand Up @@ -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'),
Expand All @@ -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:
Expand All @@ -123,8 +151,8 @@ def apply(self):


def main():
alias = NetAppONTAPasupInvoke()
alias.apply()
message = NetAppONTAPasupInvoke()
message.apply()


if __name__ == '__main__':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 '''
Expand All @@ -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')
Expand Down Expand Up @@ -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."

0 comments on commit e8e0c32

Please sign in to comment.