# Buggy code
def main():
    argument_spec = openstack_full_argument_spec(
        zone=dict(required=True),
        name=dict(required=True),
        recordset_type=dict(required=False),
        records=dict(required=False, type="list"),
        description=dict(required=False, default=None),
        ttl=dict(required=False, default=None, type="int"),
        state=dict(default="present", choices=["absent", "present"]),
    )

    module_kwargs = openstack_module_kwargs()
    module = AnsibleModule(
        argument_spec,
        required_if=[("state", "present", ["recordset_type", "records"])],
        supports_check_mode=True,
        **module_kwargs,
    )

    zone = module.params.get("zone")
    name = module.params.get("name")
    state = module.params.get("state")

    sdk, cloud = openstack_cloud_from_module(module)
    try:
        recordset_type = module.params.get("recordset_type")
        recordset_filter = {"type": recordset_type}

        recordsets = cloud.search_recordsets(
            zone, name_or_id=name + "." + zone, filters=recordset_filter
        )

        if len(recordsets) == 1:
            recordset = recordsets[0]
            try:
                recordset_id = recordset["id"]
            except KeyError as e:
                module.fail_json(msg=str(e))
        else:
            # recordsets is filtered by type and should never be more than 1 return
            recordset = None

        if state == "present":
            records = module.params.get("records")
            description = module.params.get("description")
            ttl = module.params.get("ttl")

            if module.check_mode:
                module.exit_json(
                    changed=_system_state_change(
                        state, records, description, ttl, zone, recordset
                    )
                )

            if recordset is None:
                recordset = cloud.create_recordset(
                    zone=zone,
                    name=name,
                    recordset_type=recordset_type,
                    records=records,
                    description=description,
                    ttl=ttl,
                )
                changed = True
            else:
                if records is None:
                    records = []

                pre_update_recordset = recordset
                changed = _system_state_change(
                    state, records, description, ttl, zone, pre_update_recordset
                )
                if changed:
                    zone = cloud.update_recordset(
                        zone,
                        recordset_id,
                        records=records,
                        description=description,
                        ttl=ttl,
                    )

            module.exit_json(changed=changed, recordset=recordset)

        elif state == "absent":
            if module.check_mode:
                module.exit_json(
                    changed=_system_state_change(
                        state, None, None, None, None, recordset
                    )
                )

            if recordset is None:
                changed = False
            else:
                cloud.delete_recordset(zone, recordset_id)
                changed = True
            module.exit_json(changed=changed)

    except sdk.exceptions.OpenStackCloudException as e:
        module.fail_json(msg=str(e))


# Traceback (AttributeError)
The full traceback is:
Traceback (most recent call last):
File "/var/folders/9b/d7gxcd593hj6k54zk8gh19b55dmxzn/T/ansible_vr2cn8s0/ansible_module_os_recordset.py", line 236, in <module>
main()
File "/var/folders/9b/d7gxcd593hj6k54zk8gh19b55dmxzn/T/ansible_vr2cn8s0/ansible_module_os_recordset.py", line 172, in main
recordsets = cloud.search_recordsets(zone, name_or_id=name + '.' + zone, filters=recordset_filter)
File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/openstack/cloud/openstackcloud.py", line 8551, in search_recordsets
return _utils._filter_list(recordsets, name_or_id, filters)
File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/openstack/cloud/_utils.py", line 113, in _filter_list
e_id = _make_unicode(e.get('id', None))
AttributeError: 'str' object has no attribute 'get'

fatal: [localhost]: FAILED! => {
"changed": false,
"module_stderr": "Traceback (most recent call last):\n  File \"/var/folders/9b/d7gxcd593hj6k54zk8gh19b55dmxzn/T/ansible_vr2cn8s0/ansible_module_os_recordset.py\", line 236, in <module>\n    main()\n  File \"/var/folders/9b/d7gxcd593hj6k54zk8gh19b55dmxzn/T/ansible_vr2cn8s0/ansible_module_os_recordset.py\", line 172, in main\n    recordsets = cloud.search_recordsets(zone, name_or_id=name + '.' + zone, filters=recordset_filter)\n  File \"/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/openstack/cloud/openstackcloud.py\", line 8551, in search_recordsets\n    return _utils._filter_list(recordsets, name_or_id, filters)\n  File \"/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/openstack/cloud/_utils.py\", line 113, in _filter_list\n    e_id = _make_unicode(e.get('id', None))\nAttributeError: 'str' object has no attribute 'get'\n",
"module_stdout": "",
"msg": "MODULE FAILURE",
"rc": 1
}

# Fixed code
def main():
    argument_spec = openstack_full_argument_spec(
        zone=dict(required=True),
        name=dict(required=True),
        recordset_type=dict(required=False),
        records=dict(required=False, type="list"),
        description=dict(required=False, default=None),
        ttl=dict(required=False, default=None, type="int"),
        state=dict(default="present", choices=["absent", "present"]),
    )

    module_kwargs = openstack_module_kwargs()
    module = AnsibleModule(
        argument_spec,
        required_if=[("state", "present", ["recordset_type", "records"])],
        supports_check_mode=True,
        **module_kwargs,
    )

    zone = module.params.get("zone")
    name = module.params.get("name")
    state = module.params.get("state")

    sdk, cloud = openstack_cloud_from_module(module)
    try:
        recordset_type = module.params.get("recordset_type")
        recordset_filter = {"type": recordset_type}

        recordsets = cloud.search_recordsets(
            zone, name_or_id=name, filters=recordset_filter
        )

        if len(recordsets) == 1:
            recordset = recordsets[0]
            try:
                recordset_id = recordset["id"]
            except KeyError as e:
                module.fail_json(msg=str(e))
        else:
            # recordsets is filtered by type and should never be more than 1 return
            recordset = None

        if state == "present":
            records = module.params.get("records")
            description = module.params.get("description")
            ttl = module.params.get("ttl")

            if module.check_mode:
                module.exit_json(
                    changed=_system_state_change(
                        state, records, description, ttl, zone, recordset
                    )
                )

            if recordset is None:
                recordset = cloud.create_recordset(
                    zone=zone,
                    name=name,
                    recordset_type=recordset_type,
                    records=records,
                    description=description,
                    ttl=ttl,
                )
                changed = True
            else:
                if records is None:
                    records = []

                pre_update_recordset = recordset
                changed = _system_state_change(
                    state, records, description, ttl, zone, pre_update_recordset
                )
                if changed:
                    zone = cloud.update_recordset(
                        zone,
                        recordset_id,
                        records=records,
                        description=description,
                        ttl=ttl,
                    )

            module.exit_json(changed=changed, recordset=recordset)

        elif state == "absent":
            if module.check_mode:
                module.exit_json(
                    changed=_system_state_change(
                        state, None, None, None, None, recordset
                    )
                )

            if recordset is None:
                changed = False
            else:
                cloud.delete_recordset(zone, recordset_id)
                changed = True
            module.exit_json(changed=changed)

    except sdk.exceptions.OpenStackCloudException as e:
        module.fail_json(msg=str(e))