
    Vh'                         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_role
short_description: Adds or removes Vertica database roles and assigns roles to them
description:
  - Adds or removes Vertica database role and, optionally, assign other roles.
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  role:
    description:
      - Name of the role to add or remove.
    required: true
    type: str
    aliases: ['name']
  assigned_roles:
    description:
      - Comma separated list of roles to assign to the role.
    aliases: ['assigned_role']
    type: str
  state:
    description:
      - Whether to create V(present), drop V(absent) or lock V(locked) a role.
    choices: ['present', 'absent']
    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)"
a
  
- name: Creating a new vertica role
  community.general.vertica_role: name=role_name db=db_name state=present

- name: Creating a new vertica role with other role assigned
  community.general.vertica_role: name=role_name assigned_role=other_role_name 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_role.pyr
   r
   b       r   r
   c                       e Zd Zy)CannotDropErrorNr   r   r   r   r   r   f   r   r   r   c                 >   i }| j                  d||       	 | j                  d      }|s	 |S |D ]l  }|j                  j                         }|j                  g d||<   |j                  s<|j                  j                  dd      j                  d      ||   d<   n )Nzk
        select r.name, r.assigned_roles
        from roles r
        where (? = '' or r.name ilike ?)
    d   )nameassigned_roles  ,r   )execute	fetchmanyr   lowerr   replacesplit)cursorrolefactsrowsrowrole_keys         r   get_role_factsr'   l   s    E
NN  
t	
 $ L  	cCxx~~'H"$&E(O !!474F4F4N4NsTV4W4]4]^a4bh 01	c	 r   c                     t        |      t        |      z
  D ]#  }|j                  dj                  ||             % t        |      t        |      z
  D ]#  }|j                  dj                  ||             % y )Nzrevoke {0} from {1}zgrant {0} to {1})setr   format)
role_factsr!   r"   existingrequiredassigned_roles         r   update_rolesr/      sr    XX6 J,33M4HIJXX6 G)00EFGr   c                 n    |j                         }|| vry|rt        |      t        | |   d         k7  ryy)NFr   T)r   sorted)r+   r"   r   r&   s       r   checkr2      s=    zz|Hz!&0F:h;OP`;a4bbr   c                 n   |j                         }|| vrK|j                  dj                  |             t        | ||g |       | j	                  t        ||             yd}|r4t        |      t        | |   d         k7  rt        | ||| |   d   |       d}|r| j	                  t        ||             |S )Nzcreate role {0}TFr   )r   r   r*   r/   updater'   r1   )r+   r!   r"   r   r&   changeds         r   presentr6      s    zz|Hz!(//56Zr>B.67vn5
8@TUe@f9ggVT#H-.>?QGnVT:;r   c                     |j                         }|| v r?t        | ||| |   d   g        |j                  dj                  | |   d                | |= yy)Nr   zdrop role {0} cascader   TF)r   r/   r   r*   )r+   r!   r"   r   r&   s        r   absentr8      sd    zz|H:Z)*:;R	A.55j6J66RSTx r   c                     t        t        t        ddg      t        d dg      t        dddg      t               t        d	
      t        d
      t        d
      t        d            d      } t        s | j                  t	        d      t
               | j                  d   }g }| j                  d   r*| j                  d   j                  d      }t        d |      }| j                  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k(  r	 t-        |
|||      }| j5                  ||d"
i$       y # t        $ r4}	| j                  d j                  t        |	            !       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# t.        $ r)}	| j                  t        |	      d"
i#       Y d }	~	 d }	~	wt0        $ r)}	| j                  t        |	      d"
i#       Y d }	~	Pd }	~	wt2        $ r  t        $ r9}	| j                  t        |	      t)        j*                                Y d }	~	d }	~	ww xY w)%NTr   )r-   aliasesr.   )defaultr:   r6   r8   )r;   choices	localhost)r;   5433dbadmin)no_log)r"   r   statedbclusterport
login_userlogin_password)argument_specsupports_check_modepyodbc)msg	exceptionr"   r   r   rA   r   rB   Fz_Driver=Vertica;Server={0};Port={1};Database={2};User={3};Password={4};ConnectionLoadBalance={5}rC   rD   rE   rF   true)
autocommitz#Unable to connect to database: {0}.)rJ   vertica_roles)rJ   ansible_facts)r5   r"   rO   )r   dictpyodbc_found	fail_jsonr   PYODBC_IMP_ERRparamsr    filterr*   rI   connectr!   	Exceptionr   r'   
check_moder2   r8   Error	traceback
format_excr6   r
   r   
SystemExit	exit_json)moduler"   r   rA   rB   r5   dsndb_connr!   er+   s              r   mainrb      s,   tfX66GHy8Y2GHv-f%I.t,	
  $
%F 1(;~V== DN}}%&'78>>sCn5MM'"E	B}}T]]4 GY( &y)6==+@"|,fmm<L.MvW 	 ..6!M#F+

D.AAGhU VT>J iU!*fdNK W4Q[?\]7  YBII)TU,WXXY << 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   A1G. +K
 4H. K
 	I< .	H+7*H&&H+.I9.I4/K
 4I99K
 <K.K=K
 KK
 
	M5K77M5L''M5<.M00M5__main__)r   )
__future__r   r   r   type__metaclass__DOCUMENTATIONEXAMPLESrZ   rS   rI   rQ   ImportErrorr[   ansible.module_utils.basicr   r   +ansible.module_utils.common.text.convertersr   rW   r
   r   r'   r/   r2   r6   r8   rb   r   r   r   r   <module>rl      s    A @>@ 
 L J A		 		i 	*G$	E^P zF E  )Y))+NLs   A# #A>=A>