
    Vh$                         d dl mZmZmZ eZdZdZdZd dl	Z	d dl
mZ d dlmZ d dlmZ d dlmc mc mc mc mZ d	 Zd
 Zd Zd Zd Zd ZdZd Zedk(  r e        yy)    )absolute_importdivisionprint_functiona  
module: influxdb_user
short_description: Manage InfluxDB users
description:
  - Manage InfluxDB users.
author: "Vitaliy Zhhuta (@zhhuta)"
requirements:
  - "influxdb >= 0.9"
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  user_name:
    description:
      - Name of the user.
    required: true
    type: str
  user_password:
    description:
      - Password to be set for the user.
    required: false
    type: str
  admin:
    description:
      - Whether the user should be in the admin role or not.
      - Since version 2.8, the role will also be updated.
    default: false
    type: bool
  state:
    description:
      - State of the user.
    choices: [absent, present]
    default: present
    type: str
  grants:
    description:
      - Privileges to grant to this user.
      - Takes a list of dicts containing the "database" and "privilege" keys.
      - If this argument is not provided, the current grants will be left alone.
      - If an empty list is provided, all grants for the user will be removed.
    type: list
    elements: dict
extends_documentation_fragment:
  - community.general.influxdb
  - community.general.attributes
a  
- name: Create a user on localhost using default login credentials
  community.general.influxdb_user:
    user_name: john
    user_password: s3cr3t

- name: Create a user on localhost using custom login credentials
  community.general.influxdb_user:
    user_name: john
    user_password: s3cr3t
    login_username: "{{ influxdb_username }}"
    login_password: "{{ influxdb_password }}"

- name: Create an admin user on a remote host using custom login credentials
  community.general.influxdb_user:
    user_name: john
    user_password: s3cr3t
    admin: true
    hostname: "{{ influxdb_hostname }}"
    login_username: "{{ influxdb_username }}"
    login_password: "{{ influxdb_password }}"

- name: Create a user on localhost with privileges
  community.general.influxdb_user:
    user_name: john
    user_password: s3cr3t
    login_username: "{{ influxdb_username }}"
    login_password: "{{ influxdb_password }}"
    grants:
      - database: 'collectd'
        privilege: 'WRITE'
      - database: 'graphite'
        privilege: 'READ'

- name: Destroy a user using custom login credentials
  community.general.influxdb_user:
    user_name: john
    login_username: "{{ influxdb_username }}"
    login_password: "{{ influxdb_password }}"
    state: absent
#N)ConnectionError)AnsibleModule)	to_nativec                     d }	 |j                         }|D ]  }|d   |k(  s|} |S  	 |S # t        $ r&}| j                  t        |             Y d }~|S d }~ww xY w)Nusermsg)get_list_usersr   	fail_jsonr	   )moduleclient	user_nameuser_resultusersr   es          s/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/influxdb_user.py	find_userr   r   s}    K+%%' 	DF|y(" 	   +Yq\**+s   + + + 	AAAc                 0   	 |j                  ||       |j                          |j                  | j                  d   | j                  d          y# t        j                  j                  $ rJ}|j
                  dk(  r1Y d }~|j                  | j                  d   | j                  d          yY d }~d }~wt        $ r%}| j                  t        |             Y d }~d }~ww xY w# |j                  | j                  d   | j                  d          w xY w)Ni  usernamepasswordFr   T)
switch_userr   influx
exceptionsInfluxDBClientErrorcodeparamsr   r   r	   r   r   r   user_passwordr   s        r   check_user_passwordr#      s    
Q9m4 	6==4fmmJ6OP 00 66S=
 	6==4fmmJ6OP  +Yq\**+ 	6==4fmmJ6OPsA   "A C$.B3=C' .C' 3C$?CC' C$$C' '.Dc                     | j                   s	 |j                  ||       y y # t        $ r%}| j                  t	        |             Y d }~y d }~ww xY wNr   )
check_modeset_user_passwordr   r   r	   r!   s        r   r'   r'      sO    	/$$Y>   	/1..	/s   " 	AAAc                     | j                   s	 |j                  |||       y y # t        $ r%}| j                  t	        |             Y d }~y d }~ww xY wr%   )r&   create_userr   r   r	   )r   r   r   r"   adminr   s         r   r)   r)      sQ    	/y-?   	/1..	/s   # 	AAAc                     | j                   s	 |j                  |       | j                  d       y # t        j                  j                  $ r&}| j                  |j                         Y d }~Qd }~ww xY w)Nr   Tchanged)r&   	drop_userr   r   r   r   content	exit_json)r   r   r   r   s       r   r.   r.      sg    	,Y' T"   44 	,++	,s   2 A5A00A5c                    d}g }	 |j                  |      }	 g }t        |      D ],  \  }}	|	d   dk7  s|	d   dk(  rd|	d<   |j                  |	       . |D ].  }
|
|vs| j                  s|j                  |
d   |
d   |       d	}0 |D ].  }||vs| j                  s|j                  |d   |d   |       d	}0 	 |S # t        j                  j                  $ r@}| j                  rd|j
                  vr| j                  |j
                         Y d }~d }~ww xY w# t        j                  j                  $ r'}| j                  |j
                         Y d }~|S d }~ww xY w)
NFzuser not foundr   	privilegezNO PRIVILEGESzALL PRIVILEGESALLdatabaseT)get_list_privilegesr   r   r   r&   r/   r   	enumerateappendrevoke_privilegegrant_privilege)r   r   r   grantsr-   current_grantsr   parsed_grantsivcurrent_grantgrants               r   set_user_grantsrA      s   GN,33I>
(n- 	(DAq~0[>%55%*AkN$$Q'		( + 	MF*((++M+,F,9*,E,57 	  	EM)((**5+=+0+<+46 	 NE 00 ,  $4AII$E+,> 00 (QYY''N(s?   B= D )D 2D )D =D6DDE!:EE!zJerror authorizing query: create admin user first or disable authenticationc            
         t         j                  j                         } | j                  t	        ddddg      t	        dd      t	        ddd      t	        d	d
      t	        dd             t        | d      }|j                  d   }|j                  d   }|j                  d   }|j                  d   }|j                  d   }t        j                  |      }|j                         }d }		 t        |||      }	d}|dk(  r|	rot)        ||||      s|t+        ||||       d}	 |r%|	d   s |j,                  s|j/                  |       d}n&|s$|	d   r|j,                  s|j1                  |       d}n|xs d}t3        |||||       d}|t5        ||||      rd}|j7                  |       |dk(  r#|	rt9        |||       y |j7                  d       y y # t         j                  j                  $ r}
|
j                  dk(  r}d }	 t        j                  |
j                        }|d   }n0# t        t         f$ r |j#                  t%        |
             Y nw xY w|t&        k7  r7|j#                  t%        |
             n|j#                  t%        |
             Y d }
~
d }
~
ww xY w# t         j                  j                  $ r&}
|j#                  t%        |
             Y d }
~
]d }
~
ww xY w)Npresentstrabsent)defaulttypechoicesT)requiredrG   F)rI   rG   no_logFalsebool)rF   rG   listdict)rG   elements)stater   r"   r*   r:   )argument_specsupports_check_moderP   r   r"   r*   r:   i  errorr    r,   )r   InfluxDbinfluxdb_argument_specupdaterN   r   r    connect_to_influxdbr   r   r   r   jsonloadsr/   KeyError
ValueErrorr   r	   INFLUX_AUTH_FIRST_USER_REQUIREDr#   r'   r&   grant_admin_privilegesrevoke_admin_privilegesr)   rA   r0   r.   )rQ   r   rP   r   r"   r*   r:   influxdbr   r   r   reasonr   r-   s                 r   mainrb      s   OO::<M959h:OP51EdC70&1   # F
 MM'"Ek*IMM/2MMM'"E]]8$Fv&H))+FD/3 G	&vvy-PUbUn!&&)]K
3g!,,55i@"G4=!,,66yA"G
 */RM	=%HGvvy&A)ffi0U+	 [ 00 /66S=F3jj+Wj) 3  Yq\ 23 88  Yq\ 21./> $$88 3  Yq\ 223sV   0G 'AJ+ J(6J#$H-,J#-*IJ#IAJ##J(+K.K))K.__main__) 
__future__r   r   r   rG   __metaclass__DOCUMENTATIONEXAMPLESRETURNrY   ansible.module_utils.urlsr   ansible.module_utils.basicr   +ansible.module_utils.common.text.convertersr	   Cansible_collections.community.general.plugins.module_utils.influxdb	communitygeneralpluginsmodule_utilsr`   r   r   r#   r'   r)   r.   rA   r]   rb   __name__     r   <module>rt      s    A @/b(T 
  5 4 A T T T//#(V #o J,Z zF rs   