
    Vh8                        d dl mZmZmZ eZdZdZd dlZdZ		 d dl
Z
dZd dlmZmZ d dlmZ  G d	 d
e      Z G d de      ZddZd Zd Zd Zd Zd Zedk(  r e        yy# e$ r  ej                         Z	dZY _w xY w)    )absolute_importdivisionprint_functiona  
module: vertica_user
short_description: Adds or removes Vertica database users and assigns roles
description:
  - Adds or removes Vertica database user and, optionally, assigns roles.
  - A user is not removed until all the dependencies have been dropped.
  - In such a situation, if the module tries to remove the user it fails and only remove roles granted to the user.
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  user:
    description:
      - Name of the user to add or remove.
    required: true
    type: str
    aliases: ['name']
  profile:
    description:
      - Sets the user's profile.
    type: str
  resource_pool:
    description:
      - Sets the user's resource pool.
    type: str
  password:
    description:
      - The user's password encrypted by the MD5 algorithm.
      - The password must be generated with the format C("md5" + md5[password + username]), resulting in a total of 35 characters.
        An easy way to do this is by querying the Vertica database with select V('md5'|| md5('<user_password><user_name>'\)).
    type: str
  expired:
    description:
      - Sets the user's password expiration.
    type: bool
  ldap:
    description:
      - Set to V(true) if users are authenticated using LDAP.
      - The user is created with password expired and set to V($ldap$).
    type: bool
  roles:
    description:
      - Comma separated list of roles to assign to the user.
    aliases: ['role']
    type: str
  state:
    description:
      - Whether to create (V(present)), drop (V(absent)), or lock (V(locked)) a user.
    choices: ['present', 'absent', 'locked']
    default: present
    type: str
  db:
    description:
      - Name of the Vertica database.
    type: str
  cluster:
    description:
      - Name of the Vertica cluster.
    default: localhost
    type: str
  port:
    description:
      - Vertica cluster port to connect to.
    default: '5433'
    type: str
  login_user:
    description:
      - The username used to authenticate with.
    default: dbadmin
    type: str
  login_password:
    description:
      - The password used to authenticate with.
    type: str
notes:
  - The default authentication assumes that you are either logging in as or sudo'ing to the C(dbadmin) account on the host.
  - This module uses C(pyodbc), a Python ODBC database adapter. You must ensure that C(unixODBC) and C(pyodbc) is installed
    on the host and properly configured.
  - Configuring C(unixODBC) for Vertica requires C(Driver = /opt/vertica/lib64/libverticaodbc.so) to be added to the C(Vertica)
    section of either C(/etc/odbcinst.ini) or C($HOME/.odbcinst.ini) and both C(ErrorMessagesPath = /opt/vertica/lib64) and
    C(DriverManagerEncoding = UTF-16) to be added to the C(Driver) section of either C(/etc/vertica.ini) or C($HOME/.vertica.ini).
requirements: ['unixODBC', 'pyodbc']
author: "Dariusz Owczarek (@dareko)"
aW  
- name: Creating a new vertica user with password
  community.general.vertica_user: name=user_name password=md5<encrypted_password> db=db_name state=present

- name: Creating a new vertica user authenticated via ldap with roles assigned
  community.general.vertica_user: name=user_name ldap=true db=db_name roles=schema_name_ro state=present
NTF)AnsibleModulemissing_required_lib)	to_nativec                       e Zd Zy)NotSupportedErrorN__name__
__module____qualname__     r/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/vertica_user.pyr
   r
   z       r   r
   c                       e Zd Zy)CannotDropErrorNr   r   r   r   r   r   ~   r   r   r   c           
         i }| j                  d||       	 | j                  d      }|s	 |S |D ]  }|j                  j                         }|j                  t	        |j
                        |j                  t	        |j                        |j                  |j                  g g d||<   |j
                  rt	        |j                        ||   d<   |j                  r1|j                  j                  dd      j                  d      ||   d<   |j                  s|j                  j                  dd      j                  d      ||   d	<    9)
NaN  
        select u.user_name, u.is_locked, u.lock_time,
        p.password, p.acctexpired as is_expired,
        u.profile_name, u.resource_pool,
        u.all_roles, u.default_roles
        from users u join password_auditor p on p.user_id = u.user_id
        where not u.is_super_user
        and (? = '' or u.user_name ilike ?)
    d   )namelockedpasswordexpiredprofileresource_poolrolesdefault_roleslocked_time  ,r   r   )execute	fetchmany	user_namelowerstr	is_lockedr   
is_expiredprofile_namer   	lock_time	all_rolesreplacesplitr   )cursoruserfactsrowsrowuser_keys         r   get_user_factsr5      s=   E
NN  
t $$ L#  	aC}}**,Hcmm,LLs~~.++!$!2!2!#%E(O }}14S]]1Ch.}}+.==+@+@b+I+O+OPS+Th(  363D3D3L3LSRT3U3[3[\_3`h0!	a	 r   c                    t        t        |      t        |      z
        }|r0|j                  dj                  dj	                  |      |             t        t        |      t        |      z
        }|r0|j                  dj                  dj	                  |      |             |r1|j                  dj                  |dj	                  |                   y y )Nzrevoke {0} from {1}r"   zgrant {0} to {1}zalter user {0} default role {1})listsetr#   formatjoin)
user_factsr/   r0   existing_allexisting_defaultrequired	del_roles	new_roless           r   update_rolesrA      s    S&X67I,33CHHY4GNOS]S%667I)00)1DdKL8??chhxFXYZ r   c	                 \   |j                         }	|	| vry|r|| |	   d   k7  ry|r|| |	   d   k7  ry|| |	   d   dk(  k7  ry|r|| |	   d   k7  ry||| |	   d   dk(  k7  s||| |	   d   dk(  k7  ry|r;t        |      t        | |	   d         k7  st        |      t        | |	   d	         k7  ryy
)NFr   r   r   Truer   r   r   r   T)r&   sorted)
r;   r0   r   r   r   r   r   ldapr   r4   s
             r   checkrF      s    zz|Hz!7j29==*X*>*OO*X&x0F:;H
8 4Z @@Jx,@,Kv,U V*X*>y*IV*S!T&-6*X*>w*G#HH-6*X*>*O#PPr   c
                    |j                         }
|
| vr*dj                  |      g}|r|j                  d       |s|r4|r!|j                  dj                  |             n|j                  d       |s|r|j                  d       |r |j                  dj                  |             |r |j                  dj                  |             |j                  dj	                  |             |r&|d	k7  r!|j                  d
j                  ||             t        | ||g g |	       | j                  t        ||             yd}dj                  |      g}|7|| |
   d   dk(  k7  r)|rd}nd}|j                  dj                  |             d}|r-|| |
   d   k7  r"|j                  dj                  |             d}|r"|| |
   d   dk(  k7  rE|j                  d       d}n1|/|| |
   d   dk(  k7  r!|r|j                  d       d}nt        d      |r-|| |
   d   k7  r"|j                  dj                  |             d}|r|| |
   d   k7  rz|j                  dj                  |             | |
   d   d	k7  r'|j                  dj                  | |
   d   |             |d	k7  r!|j                  d
j                  ||             d}|r |j                  dj	                  |             |	rXt        |	      t        | |
   d         k7  st        |	      t        | |
   d         k7  rt        | ||| |
   d   | |
   d   |	       d}|r| j                  t        ||             |S )Nzcreate user {0}zaccount lockzidentified by '{0}'zidentified by '$ldap$'zpassword expirezprofile {0}zresource pool {0}r    generalz'grant usage on resource pool {0} to {1}TFzalter user {0}r   rC   lockunlockzaccount {0}r   r   z*Unexpiring user password is not supported.r   r   z*revoke usage on resource pool {0} from {1}r   r   )
r&   r9   appendr#   r:   rA   updater5   r
   rD   )r;   r/   r0   r   r   r   r   r   rE   r   r4   query_fragmentschangedstates                 r   presentrP      s   zz|Hz!,33D9:"">2t&&'<'C'CH'MN&&'?@d""#45""=#7#7#@A""#6#=#=m#LMsxx01]i7NNDKKt% &Zr2u=.67+22489&Z-A(-Kv-U"V ""=#7#7#>?GJx$8$DD""#8#?#?#IJG
8,Y76AB&&'89 WH1Ei1PTZ1Z%[&&'89'(TUUw*X"6y"AA""=#7#7#@AG]j.B?.SS""#6#=#=m#LM(#O4	AKRRx(94 A B	)HOO!4 ) *GNN388O45fUmvj.B7.K'LLUmvj.B?.S'TTVT#H-g6
88L_8]_dfGnVT:;r   c                     |j                         }|| v rGt        | ||| |   d   | |   d   g        	 |j                  dj                  | |   d                | |= yy# t        j
                  $ r t        d      w xY w)Nr   r   zdrop user {0}r   z)Dropping user failed due to dependencies.TF)r&   rA   r#   r9   pyodbcErrorr   )r;   r/   r0   r   r4   s        r   absentrT     s    zz|H:Z)'2Jx4H4Y[]	_	ONN?11*X2Fv2NOP x  || 	O!"MNN	Os   &A A<c                  J   t        t        t        ddg      t               t               t        d      t        d      t        d      t        dg      t        d	g d
      t               t        d      t        d      t        d      t        d            d      } t        s | j                  t	        d      t
               | j                  d   }| j                  d   }|r|j                         }| j                  d   }|r|j                         }| j                  d   }| j                  d   }| j                  d   }g }| j                  d   r*| j                  d   j                  d      }t        d |      }| j                  d   }|dk(  rd}	nd}	d}
| j                  d    r| j                  d    }
d}	 d!j                  | j                  d"   | j                  d#   |
| j                  d$   | j                  d%   d&      }t        j                  |d'      }|j                         }	 t              }| j                   rt#        |||||	||||	       }n.|d*k(  r	 t%        ||||      }n|d+v r	 t/        ||||||	||||
      }| j7                  ||d,i.       y # t        $ r+}| j                  d(j                  |      )       Y d }~d }~ww xY w# t        j&                  $ r8}| j                  t)        |      t+        j,                                Y d }~d }~ww xY w# t        j&                  $ r8}| j                  t)        |      t+        j,                                Y d }~d }~ww xY w# t0        $ r)}| j                  t)        |      d,i-       Y d }~d }~wt2        $ r)}| j                  t)        |      d,i-       Y d }~Gd }~wt4        $ r  t        $ r9}| j                  t)        |      t+        j,                                Y d }~d }~ww xY w)/NTr   )r>   aliases)no_logbool)typerole)rV   rP   )rT   rP   r   )defaultchoices	localhost)r[   5433dbadmin)r0   r   r   r   r   rE   r   rO   dbclusterport
login_userlogin_password)argument_specsupports_check_moderR   )msg	exceptionr0   r   r   r   r   rE   r   r"   rO   r   Fr!   r`   z_Driver=Vertica;Server={0};Port={1};Database={2};User={3};Password={4};ConnectionLoadBalance={5}ra   rb   rc   rd   true)
autocommitz#Unable to connect to database: {0}.)rg   rT   )rP   r   vertica_users)rg   ansible_facts)rN   r0   rl   )r   dictpyodbc_found	fail_jsonr   PYODBC_IMP_ERRparamsr&   r.   filterr9   rR   connectr/   	Exceptionr5   
check_moderF   rT   rS   r   	traceback
format_excrP   r
   r   
SystemExit	exit_json)moduler0   r   r   r   r   rE   r   rO   r   r`   rN   dsndb_connr/   er;   s                    r   mainr~   "  s   tfX6F&&f%6"x(y2QRv-f%I.t,
  $%F" 1(;~V== DmmI&G--/MM/2M%++-}}Z(HmmI&G== DE}}Wg&,,S1tU#MM'"E	B}}T]]4 GN( &y)6==+@"|,fmm<L.MvW 	 ..6!M#F+

D'= &'4H HGhU VT5A ++U!*fdG]"((GT5J W4Q[?\];  NBII!LMMN << U  Yq\Y=Q=Q=S TTU << U  Yq\Y=Q=Q=S TTU XYq\/:9VWW XYq\/:9VWW  MYq\Y5I5I5KLLMs   A1J$ 31M7 %K 3M7 9L) $	K-!KKL&..L!M7 !L&&M7 )M4<.M/*M7 /M44M7 7	P" N$$P"0OP").PP"__main__)r!   )
__future__r   r   r   rY   __metaclass__DOCUMENTATIONEXAMPLESrv   rp   rR   rn   ImportErrorrw   ansible.module_utils.basicr   r   +ansible.module_utils.common.text.convertersr   rt   r
   r   r5   rA   rF   rP   rT   r~   r   r   r   r   <module>r      s    A @Wr 
 L J A		 		i 	 F	[,EP"Y^x zF ]  )Y))+NLs   A# #A>=A>