# Buggy code
def call_api(self, call, query=None):
    api_call = netapp_utils.zapi.NaElement(call)
    result = None

    if query:
        for k, v in query.items():
            # Can v be nested?
            api_call.add_new_child(k, v)
    try:
        result = self.server.invoke_successfully(api_call, enable_tunneling=False)
        return result
    except netapp_utils.zapi.NaApiError as e:
        self.module.fail_json(
            msg="Error calling API %s: %s" % (call, to_native(e)),
            exception=traceback.format_exc(),
        )


# Traceback (TypeError)
TASK [Test_gather] **************************************************************************************************************************************************************************************************************************
task path: /git/devel/gather_system.yml:21
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: root
<127.0.0.1> EXEC /bin/sh -c 'echo ~root &amp;&amp; sleep 0'
<127.0.0.1> EXEC /bin/sh -c '( umask 77 &amp;&amp; mkdir -p "` echo /root/.ansible/tmp/ansible-tmp-1536039256.01-6248969617936 `" &amp;&amp; echo ansible-tmp-1536039256.01-6248969617936="` echo /root/.ansible/tmp/ansible-tmp-1536039256.01-6248969617936 `" ) &amp;&amp; sleep 0'
Using module file /home/centos/ansible2.6/lib/python2.7/site-packages/ansible/modules/storage/netapp/na_ontap_gather_facts.py
<127.0.0.1> PUT /root/.ansible/tmp/ansible-local-26695H5a1l7/tmpw3CAow TO /root/.ansible/tmp/ansible-tmp-1536039256.01-6248969617936/na_ontap_gather_facts.py
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /root/.ansible/tmp/ansible-tmp-1536039256.01-6248969617936/ /root/.ansible/tmp/ansible-tmp-1536039256.01-6248969617936/na_ontap_gather_facts.py &amp;&amp; sleep 0'
<127.0.0.1> EXEC /bin/sh -c '/home/centos/ansible2.6/bin/python /root/.ansible/tmp/ansible-tmp-1536039256.01-6248969617936/na_ontap_gather_facts.py &amp;&amp; sleep 0'
<127.0.0.1> EXEC /bin/sh -c 'rm -f -r /root/.ansible/tmp/ansible-tmp-1536039256.01-6248969617936/ > /dev/null 2>&amp;1 &amp;&amp; sleep 0'
The full traceback is:
Traceback (most recent call last):
File "/tmp/ansible_eHzOnQ/ansible_module_na_ontap_gather_facts.py", line 295, in <module>
main()
File "/tmp/ansible_eHzOnQ/ansible_module_na_ontap_gather_facts.py", line 289, in main
g = v.get_all()
File "/tmp/ansible_eHzOnQ/ansible_module_na_ontap_gather_facts.py", line 199, in get_all
query={'max-records': '1024'}
File "/tmp/ansible_eHzOnQ/ansible_module_na_ontap_gather_facts.py", line 156, in get_generic_get_iter
unique_key = ':'.join([_finditem(d, el) for el in field])
TypeError: sequence item 1: expected string or Unicode, NoneType found

fatal: [localhost]: FAILED! => {
"changed": false,
"module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_eHzOnQ/ansible_module_na_ontap_gather_facts.py\", line 295, in <module>\n    main()\n  File \"/tmp/ansible_eHzOnQ/ansible_module_na_ontap_gather_facts.py\", line 289, in main\n    g = v.get_all()\n  File \"/tmp/ansible_eHzOnQ/ansible_module_na_ontap_gather_facts.py\", line 199, in get_all\n    query={'max-records': '1024'}\n  File \"/tmp/ansible_eHzOnQ/ansible_module_na_ontap_gather_facts.py\", line 156, in get_generic_get_iter\n    unique_key = ':'.join([_finditem(d, el) for el in field])\nTypeError: sequence item 1: expected string or Unicode, NoneType found\n",
"module_stdout": "",
"msg": "MODULE FAILURE",
"rc": 1
}

# Fixed code
def call_api(self, call, query=None):
    api_call = netapp_utils.zapi.NaElement(call)
    result = None

    if query:
        for k, v in query.items():
            # Can v be nested?
            api_call.add_new_child(k, v)
    try:
        result = self.server.invoke_successfully(api_call, enable_tunneling=False)
        return result
    except netapp_utils.zapi.NaApiError as e:
        if call in ["security-key-manager-key-get-iter"]:
            return result
        else:
            self.module.fail_json(
                msg="Error calling API %s: %s" % (call, to_native(e)),
                exception=traceback.format_exc(),
            )