Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle errors with negative return code better #359

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

rndmh3ro
Copy link
Collaborator

SUMMARY

In some cases the error returned by Grafana could have a status code -1, e.g.:

{'url': 'https://127.0.0.1:3000/api/users/lookup?loginOrEmail=username2', 'status': -1, 'msg': 'Request failed: <urlopen error [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1007)>'}

or

{'url': 'https://127.0.0.1/api/users/lookup?loginOrEmail=username2', 'status': -1, 'msg': 'Request failed: <urlopen error [Errno 111] Connection refused>'}

This initial commit fixes the problem for the grafana_user module. However this could happen in all modules. Therefore we should refactor out the send_request function in all modules and fix it there..

test-case:

  1. run grafana
> docker run -d -p 3000:3000 grafana/grafana:10.2.2
  1. run the following playbook:
---
- hosts: localhost
  gather_facts: no
  vars:
    grafana_admin_user: grafana
    grafana_admin_password: grafana
    members:
      - uid: username
        description: 'User Name'
        email: '[email protected]'
        password: 'hashed_passwd'
      - uid: username2
        description: 'User Name1'
        email: '[email protected]'
        password: 'hashed_passwd'
  tasks:
    - name: Create Grafana users
      community.grafana.grafana_user:
        url: "https://127.0.0.1"        # ## notice this is wrong
        url_username: "{{ grafana_admin_user }}"
        url_password: "{{ grafana_admin_password }}"
        login: "{{ member.uid }}"
        email: "{{ member.email }}"
        name: "{{ member.description }}"
        password: "{{ member.password }}"
        state: "present"
      loop: "{{ members }}"
      loop_control:
        loop_var: member

result:

> ansible-playbook test2.yml
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
 __________________
< PLAY [localhost] >
 ------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

 _____________________________
< TASK [Create Grafana users] >
 -----------------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

An exception occurred during task execution. To see the full traceback, use -vvv. The error was: AttributeError: 'NoneType' object has no attribute 'read'
failed: [localhost] (item={'uid': 'username', 'description': 'User Name', 'email': '[email protected]', 'password': 'hashed_passwd'}) => {"ansible_loop_var": "member", "changed": false, "member": {"description": "User Name", "email": "[email protected]", "password": "hashed_passwd", "uid": "username"}, "module_stderr": "Traceback (most recent call last):\n  File \"/home/segu/.ansible/tmp/ansible-tmp-1713969893.8799918-1346142-270361372579429/AnsiballZ_grafana_user.py\", line 107, in <module>\n    _ansiballz_main()\n  File \"/home/segu/.ansible/tmp/ansible-tmp-1713969893.8799918-1346142-270361372579429/AnsiballZ_grafana_user.py\", line 99, in _ansiballz_main\n    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\n  File \"/home/segu/.ansible/tmp/ansible-tmp-1713969893.8799918-1346142-270361372579429/AnsiballZ_grafana_user.py\", line 47, in invoke_module\n    runpy.run_module(mod_name='ansible_collections.community.grafana.plugins.modules.grafana_user', init_globals=dict(_module_fqn='ansible_collections.community.grafana.plugins.modules.grafana_user', _modlib_path=modlib_path),\n  File \"/usr/lib/python3.10/runpy.py\", line 224, in run_module\n    return _run_module_code(code, init_globals, run_name, mod_spec)\n  File \"/usr/lib/python3.10/runpy.py\", line 96, in _run_module_code\n    _run_code(code, mod_globals, init_globals,\n  File \"/usr/lib/python3.10/runpy.py\", line 86, in _run_code\n    exec(code, run_globals)\n  File \"/tmp/ansible_community.grafana.grafana_user_payload_ctp3n329/ansible_community.grafana.grafana_user_payload.zip/ansible_collections/community/grafana/plugins/modules/grafana_user.py\", line 323, in <module>\n  File \"/tmp/ansible_community.grafana.grafana_user_payload_ctp3n329/ansible_community.grafana.grafana_user_payload.zip/ansible_collections/community/grafana/plugins/modules/grafana_user.py\", line 292, in main\n  File \"/tmp/ansible_community.grafana.grafana_user_payload_ctp3n329/ansible_community.grafana.grafana_user_payload.zip/ansible_collections/community/grafana/plugins/modules/grafana_user.py\", line 221, in get_user_from_login\n  File \"/tmp/ansible_community.grafana.grafana_user_payload_ctp3n329/ansible_community.grafana.grafana_user_payload.zip/ansible_collections/community/grafana/plugins/modules/grafana_user.py\", line 204, in _send_request\nAttributeError: 'NoneType' object has no attribute 'read'\n", "module_stdout": "", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 1}
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: AttributeError: 'NoneType' object has no attribute 'read'
failed: [localhost] (item={'uid': 'username2', 'description': 'User Name1', 'email': '[email protected]', 'password': 'hashed_passwd'}) => {"ansible_loop_var": "member", "changed": false, "member": {"description": "User Name1", "email": "[email protected]", "password": "hashed_passwd", "uid": "username2"}, "module_stderr": "Traceback (most recent call last):\n  File \"/home/segu/.ansible/tmp/ansible-tmp-1713969894.262415-1346142-167829676315139/AnsiballZ_grafana_user.py\", line 107, in <module>\n    _ansiballz_main()\n  File \"/home/segu/.ansible/tmp/ansible-tmp-1713969894.262415-1346142-167829676315139/AnsiballZ_grafana_user.py\", line 99, in _ansiballz_main\n    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\n  File \"/home/segu/.ansible/tmp/ansible-tmp-1713969894.262415-1346142-167829676315139/AnsiballZ_grafana_user.py\", line 47, in invoke_module\n    runpy.run_module(mod_name='ansible_collections.community.grafana.plugins.modules.grafana_user', init_globals=dict(_module_fqn='ansible_collections.community.grafana.plugins.modules.grafana_user', _modlib_path=modlib_path),\n  File \"/usr/lib/python3.10/runpy.py\", line 224, in run_module\n    return _run_module_code(code, init_globals, run_name, mod_spec)\n  File \"/usr/lib/python3.10/runpy.py\", line 96, in _run_module_code\n    _run_code(code, mod_globals, init_globals,\n  File \"/usr/lib/python3.10/runpy.py\", line 86, in _run_code\n    exec(code, run_globals)\n  File \"/tmp/ansible_community.grafana.grafana_user_payload_vtib7xty/ansible_community.grafana.grafana_user_payload.zip/ansible_collections/community/grafana/plugins/modules/grafana_user.py\", line 323, in <module>\n  File \"/tmp/ansible_community.grafana.grafana_user_payload_vtib7xty/ansible_community.grafana.grafana_user_payload.zip/ansible_collections/community/grafana/plugins/modules/grafana_user.py\", line 292, in main\n  File \"/tmp/ansible_community.grafana.grafana_user_payload_vtib7xty/ansible_community.grafana.grafana_user_payload.zip/ansible_collections/community/grafana/plugins/modules/grafana_user.py\", line 221, in get_user_from_login\n  File \"/tmp/ansible_community.grafana.grafana_user_payload_vtib7xty/ansible_community.grafana.grafana_user_payload.zip/ansible_collections/community/grafana/plugins/modules/grafana_user.py\", line 204, in _send_request\nAttributeError: 'NoneType' object has no attribute 'read'\n", "module_stdout": "", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 1}
 ____________
< PLAY RECAP >
 ------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

localhost                  : ok=0    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

Fixes #333

ISSUE TYPE
  • Bugfix Pull Request

@rndmh3ro rndmh3ro force-pushed the better_error_handling branch 2 times, most recently from 4ae9640 to 6e3198a Compare April 26, 2024 11:11
@Nemental Nemental force-pushed the better_error_handling branch from 6832cd8 to 809df70 Compare May 31, 2024 10:59
Copy link

codecov bot commented May 31, 2024

Codecov Report

Attention: Patch coverage is 13.84615% with 56 lines in your changes are missing coverage. Please review.

Project coverage is 24.35%. Comparing base (fff9558) to head (3d0dea7).
Report is 6 commits behind head on main.

Files Patch % Lines
plugins/module_utils/base.py 13.63% 19 Missing ⚠️
plugins/modules/grafana_team.py 10.00% 9 Missing ⚠️
plugins/modules/grafana_silence.py 11.11% 8 Missing ⚠️
plugins/modules/grafana_datasource.py 14.28% 6 Missing ⚠️
plugins/modules/grafana_folder.py 14.28% 6 Missing ⚠️
plugins/modules/grafana_user.py 16.66% 5 Missing ⚠️
plugins/modules/grafana_organization.py 25.00% 3 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##             main     #359       +/-   ##
===========================================
- Coverage   45.63%   24.35%   -21.28%     
===========================================
  Files          21       16        -5     
  Lines        2220     1630      -590     
  Branches      433      336       -97     
===========================================
- Hits         1013      397      -616     
- Misses       1167     1226       +59     
+ Partials       40        7       -33     
Flag Coverage Δ
sanity 24.35% <13.84%> (+0.95%) ⬆️
units ?

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

AttributeError 'NoneType' object has no attribute get - Module failure
2 participants