ansible-collections / community.grafana

Grafana Collection for Ansible
http://galaxy.ansible.com/community/grafana
GNU General Public License v3.0
123 stars 78 forks source link

handle errors with negative return code better #359

Open rndmh3ro opened 3 months ago

rndmh3ro commented 3 months ago
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
  2. 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: 'user.name@whatever.com'
        password: 'hashed_passwd'
      - uid: username2
        description: 'User Name1'
        email: 'user.name2@whatever.com'
        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': 'user.name@whatever.com', 'password': 'hashed_passwd'}) => {"ansible_loop_var": "member", "changed": false, "member": {"description": "User Name", "email": "user.name@whatever.com", "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 \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 \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': 'user.name2@whatever.com', 'password': 'hashed_passwd'}) => {"ansible_loop_var": "member", "changed": false, "member": {"description": "User Name1", "email": "user.name2@whatever.com", "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 \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 \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
<!--- Pick one below and delete the rest -->
- Bugfix Pull Request
codecov[bot] commented 1 month ago

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 :warning:
plugins/modules/grafana_team.py 10.00% 9 Missing :warning:
plugins/modules/grafana_silence.py 11.11% 8 Missing :warning:
plugins/modules/grafana_datasource.py 14.28% 6 Missing :warning:
plugins/modules/grafana_folder.py 14.28% 6 Missing :warning:
plugins/modules/grafana_user.py 16.66% 5 Missing :warning:
plugins/modules/grafana_organization.py 25.00% 3 Missing :warning:
Additional details and impacted files ```diff @@ 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](https://app.codecov.io/gh/ansible-collections/community.grafana/pull/359/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=ansible-collections) | Coverage Δ | | |---|---|---| | [sanity](https://app.codecov.io/gh/ansible-collections/community.grafana/pull/359/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=ansible-collections) | `24.35% <13.84%> (+0.95%)` | :arrow_up: | | [units](https://app.codecov.io/gh/ansible-collections/community.grafana/pull/359/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=ansible-collections) | `?` | | Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=ansible-collections#carryforward-flags-in-the-pull-request-comment) to find out more.

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.