
    Vh)                     ~   d dl mZmZmZ dZdZdZd dlZd dlm	Z	 d dl
mZmZ d dlmZ d d	lmZ eZ G d
 de      Zd Zd Z ej.                         Zej3                   eddgd       edd       edd       edd       eddd       edd             ej7                  d       d Zedk(  r e        yy)    )absolute_importdivisionprint_functionaq  
---
module: grafana_user
author:
  - Antoine Tanzilli (@Tailzip)
  - Hong Viet LE (@pomverte)
  - Julien Alexandre (@jual)
  - Marc Cyprien (@LeFameux)
version_added: "1.0.0"
short_description: Manage Grafana User
description:
  - Create/update/delete Grafana User through the users and admin API.
  - Tested with Grafana v6.4.3
  - Password update is not supported at the time
options:
  name:
    description:
      - The name of the Grafana User.
    required: false
    type: str
  email:
    description:
      - The email of the Grafana User.
    required: false
    type: str
  login:
    description:
      - The login of the Grafana User.
    required: true
    type: str
  password:
    description:
      - The password of the Grafana User.
      - At the moment, this field is not updated yet.
    required: false
    type: str
  is_admin:
    description:
      - The Grafana User is an admin.
    required: false
    type: bool
    default: false
  state:
    description:
      - State if the user should be present in Grafana or not
    default: present
    type: str
    choices: ["present", "absent"]
notes:
- Unlike other modules from the collection, this module does not support C(grafana_api_key) authentication type. The Grafana API endpoint for users management
  requires basic auth and admin privileges.
extends_documentation_fragment:
- community.grafana.basic_auth
a  
---
- name: Create or update a Grafana user
  community.grafana.grafana_user:
    url: "https://grafana.example.com"
    url_username: admin
    url_password: changeme
    name: "Bruce Wayne"
    email: batman@gotham.city
    login: batman
    password: robin
    is_admin: true
    state: present

- name: Delete a Grafana user
  community.grafana.grafana_user:
    url: "https://grafana.example.com"
    url_username: admin
    url_password: changeme
    login: batman
    state: absent
a  
---
user:
    description: Information about the User
    returned: when state present
    type: complex
    contains:
        id:
            description: The User id
            returned: always
            type: int
            sample:
                - 42
        email:
            description: The User email address
            returned: always
            type: str
            sample:
                - "foo.bar@example.com"
        login:
            description: The User login
            returned: always
            type: str
            sample:
                - "batman"
        theme:
            description: The Grafana theme
            returned: always
            type: str
            sample:
                - "light"
        orgId:
            description: The organization id that the team is part of.
            returned: always
            type: int
            sample:
                - 1
        isGrafanaAdmin:
            description: The Grafana user permission for admin
            returned: always
            type: bool
            sample:
                - false
        isDisabled:
            description: The Grafana account status
            returned: always
            type: bool
            sample:
                - false
        isExternal:
            description: The Grafana account information on external user provider
            returned: always
            type: bool
            sample:
                - false
N)AnsibleModule)	fetch_urlbasic_auth_header)base)quotec                   8    e Zd Zd Zd	dZd Zd Zd Zd Zd Z	y)
GrafanaUserInterfacec                     || _         ddi| _        t        |j                  d   |j                  d         | j                  d<   t	        j
                  |j                  j                  d            | _        y )NzContent-Typezapplication/jsonurl_usernameurl_passwordAuthorizationurl)_moduleheadersr   paramsr	   	clean_urlgetgrafana_url)selfmodules     r/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/grafana/plugins/modules/grafana_user.py__init__zGrafanaUserInterface.__init__   sb    &(:;(9MM.)6==+H)
_%  >>&--*;*;E*BC    Nc           	      f   |t        j                  |d      }|sg }dj                  | j                  |      }t	        | j
                  ||||      \  }}|d   }|dk(  ry |dk(  r1| j
                  j                  dd	|d
|d| j                         nQ|dk(  r| j
                  j                  dd       n.|dk(  r)| j
                  j                  |j                               S | j
                  j                  dd|z  | j
                  j                  |j                                      y )NT)	sort_keysz{grafana_url}{path})r   pathdatar   methodstatusi  i  z Unauthorized to perform action 'z' on 'z
' header: failedmsgi  zPermission Denied   z'Grafana Users API answered with HTTP %d)r%   r&   body)
jsondumpsformatr   r   r   	fail_jsonr   	from_jsonread)	r   r   r!   r   r"   full_urlrespinfostatus_codes	            r   _send_requestz"GrafanaUserInterface._send_request   s    ::dd3DG(//D<L<LSV/WLL(wv

d 8n#CLL""8T\\3 # 
 CLL""$4G"HC<<))$))+669KG''		4 	 	
r   c                     |s| j                   j                  dd       d}t        ||||      }| j                  ||| j                  d       | j                  |      S )NTz$missing required arguments: passwordr$   z/api/admin/users)nameemailloginpasswordPOSTr    )r   r,   dictr3   r   get_user_from_login)r   r5   r6   r7   r8   r   users          r   create_userz GrafanaUserInterface.create_user   se    LL""!G #  !U%(K3T4<<O''..r   c                 t    dj                  t        |            }| j                  || j                  d      S )Nz&/api/users/lookup?loginOrEmail={login})r7   GETr   r"   )r+   r
   r3   r   )r   r7   r   s      r   r;   z(GrafanaUserInterface.get_user_from_login   s5    6==E%L=Q!!#t||E!JJr   c                     dj                  |      }t        |||      }| j                  ||| j                  d       | j	                  |      S )Nz/api/users/{user_id}user_id)r6   r5   r7   PUTr    )r+   r:   r3   r   r;   )r   rC   r6   r5   r7   r   r<   s          r   update_userz GrafanaUserInterface.update_user   sO    $++G+<%d%83T4<<N''..r   c                 |    dj                  |      }t        |      }| j                  ||| j                  d      S )Nz&/api/admin/users/{user_id}/permissionsrB   )isGrafanaAdminrD   r    )r+   r:   r3   r   )r   rC   is_adminr   permissionss        r   update_user_permissionsz,GrafanaUserInterface.update_user_permissions   sD    6==g=N(3!!k4<< " 
 	
r   c                 b    dj                  |      }| j                  || j                  d      S )Nz/api/admin/users/{user_id}rB   DELETEr@   )r+   r3   r   )r   rC   r   s      r   delete_userz GrafanaUserInterface.delete_user   s1    *11'1B!!#t||H!MMr   )NNr?   )
__name__
__module____qualname__r   r3   r=   r;   rE   rJ   rM    r   r   r   r      s(    D
:	/K
/
Nr   r   c                     t        | j                  d      | j                  d      | j                  d      | j                  d            }t        ||||      }||k7  S )Nr6   r5   r7   rG   )r6   r5   r7   rH   )r:   r   )target_userr6   r5   r7   rH   target_user_dict
param_dicts          r   is_user_update_requiredrV      s[    oog&__V$oog&!12	 EEHMJz))r   c                  \    t        t        dddddgggt        j                               } | S )NFstatepresentr5   r6   )argument_specsupports_check_moderequired_ifrequired_together)r   rZ   r	   grafana_required_together)r   s    r   setup_module_objectr_      s;    #!i&'!23
 88:F Mr   rY   absent)choicesdefaultstrF)typerequiredT)rd   re   no_logbool)rd   rb   )rX   r5   r6   r7   r8   rH   grafana_api_keyc                     t               } | j                  d   }| j                  d   }| j                  d   }| j                  d   }| j                  d   }| j                  d   }t        |       }|j                  |      }|dk(  rd}	||j	                  ||||      }d	}	t        |||||      rM|j                  d
      }
||j                  d      k7  r|j                  |
|       |j                  |
|||      }d	}	| j                  |	|       y |dk(  rW|| j                  d       |j                  |j                  d
            }| j                  d	|j                  d             y y )NrX   r5   r6   r7   r8   rH   rY   FTidrG   )changedr<   r`   zNo user found, nothing to do)messagerl   )rk   rl   )r_   r   r   r;   r=   rV   r   rJ   rE   	exit_jsonrM   )r   rX   r5   r6   r7   r8   rH   grafana_ifaceactual_grafana_userhas_changedactual_grafana_user_idresults               r   mainrs     s    "FMM'"E== DMM'"EMM'"E}}Z(H}}Z(H(0M (;;EB	&"/";";eUH# K"#6tUHU%8%<%<T%B".223CDD556LhW"/";";&tU# K3FG	(	&%CD**+>+B+B4+HIvzz)/DE	 
r   __main__)
__future__r   r   r   DOCUMENTATIONEXAMPLESRETURNr)   ansible.module_utils.basicr   ansible.module_utils.urlsr   r   :ansible_collections.community.grafana.plugins.module_utilsr	   +ansible.module_utils.six.moves.urllib.parser
   rd   __metaclass__objectr   rV   r_   grafana_argument_specrZ   updater:   poprs   rN   rQ   r   r   <module>r      s   ( A @5n.7
r  4 B K =JN6 JNZ	*	 +**,   
	8,i
@	55	)
EE
*
ED
)uuT:vu-     # $'FT zF r   