
    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_schema
short_description: Adds or removes Vertica database schema and roles
description:
  - Adds or removes Vertica database schema and, optionally, roles with schema access privileges.
  - A schema is not removed until all the objects have been dropped.
  - In such a situation, if the module tries to remove the schema it fails and only remove roles created for the schema if
    they have no dependencies.
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  schema:
    description:
      - Name of the schema to add or remove.
    required: true
    aliases: ['name']
    type: str
  usage_roles:
    description:
      - Comma separated list of roles to create and grant usage access to the schema.
    aliases: ['usage_role']
    type: str
  create_roles:
    description:
      - Comma separated list of roles to create and grant usage and create access to the schema.
    aliases: ['create_role']
    type: str
  owner:
    description:
      - Name of the user to set as owner of the schema.
    type: str
  state:
    description:
      - Whether to create V(present), or drop V(absent) a schema.
    default: present
    choices: ['present', 'absent']
    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 schema
  community.general.vertica_schema: name=schema_name db=db_name state=present

- name: Creating a new schema with specific schema owner
  community.general.vertica_schema: name=schema_name owner=dbowner db=db_name state=present

- name: Creating a new schema with roles
  community.general.vertica_schema: name=schema_name create_roles=schema_name_all usage_roles=schema_name_ro,schema_name_rw
    db=db_name state=present
NTF)AnsibleModulemissing_required_lib)	to_nativec                       e Zd Zy)NotSupportedErrorN__name__
__module____qualname__     t/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/vertica_schema.pyr
   r
   r       r   r
   c                       e Zd Zy)CannotDropErrorNr   r   r   r   r   r   v   r   r   r   c                 4   i }| j                  d||       	 | j                  d      }|snR|D ]L  }|j                  |j                  t	        |j
                        g g d||j                  j                         <   N f| j                  d||       	 | j                  d      }|s	 |S |D ]m  }|j                  j                         }d|j                  v r"||   d   j                  |j                         M||   d   j                  |j                         o )Nz
        select schema_name, schema_owner, create_time
        from schemata
        where not is_system_schema and schema_name not in ('public', 'TxtIndex')
        and (? = '' or schema_name ilike ?)
    d   )nameownercreate_timeusage_rolescreate_rolesa}  
        select g.object_name as schema_name, r.name as role_name,
        lower(g.privileges_description) privileges_description
        from roles r join grants g
        on g.grantee_id = r.role_id and g.object_type='SCHEMA'
        and g.privileges_description like '%USAGE%'
        and g.grantee not in ('public', 'dbadmin')
        and (? = '' or g.object_name ilike ?)
    creater   r   )
execute	fetchmanyschema_nameschema_ownerstrr   lowerprivileges_descriptionappend	role_name)cursorschemafactsrowsrow
schema_keys         r   get_schema_factsr,   |   s5   E
NN 
 
 $ 	$C))"3??3! ".$E#//'')*	$	  NN  
 $ L  	GC..0J3555j!.188Gj!-077F	G	 r   c                 B   t        ||z         t        ||z         z
  D ]"  }|j                  dj                  |             $ t        |      t        |      z
  D ]#  }|j                  dj                  ||             % t        ||z         t        ||z         z
  D ]C  }|j                  dj                  |             |j                  dj                  ||             E t        |      t        |      z
  D ]#  }|j                  dj                  ||             % y )Nzdrop role {0} cascadez$revoke create on schema {0} from {1}zcreate role {0}z grant usage on schema {0} to {1}z!grant create on schema {0} to {1})setr   format)schema_factsr&   r'   existingrequiredcreate_existingcreate_requiredroles           r   update_rolesr6      s    H./#h6P2QQ =.55d;<=O$s?';; T=DDVTRSTH./#h6P2QQ P(//569@@NOP O$s?';; Q:AA&$OPQr   c                     |j                         }|| vry|r(|j                         | |   d   j                         k(  ryt        |      t        | |   d         k7  ryt        |      t        | |   d         k7  ryy)NFr   r   r   T)r"   sorted)r0   r'   r   r   r   r+   s         r   checkr9      s    J%,z":7"C"I"I"KKkf\*%=m%LMMlvl:&>~&NOOr   c           	         |j                         }|| vrdj                  |      g}|r |j                  dj                  |             |j                  dj	                  |             t        | ||g |g |       | j                  t        ||             yd}|rG|j                         | |   d   j                         k7  r t        dj                  | |   d               t        |      t        | |   d         k7  st        |      t        | |   d	         k7  rt        | ||| |   d   || |   d	   |       d}|r| j                  t        ||             |S )
Nzcreate schema {0}zauthorization {0} TFr   z;Changing schema owner is not supported. Current owner: {0}.r   r   )
r"   r/   r$   r   joinr6   updater,   r
   r8   )	r0   r&   r'   r   r   r   r+   query_fragmentschangeds	            r   presentr@      sa   J%.55f=>""#6#=#=e#DEsxx01\662{BU,VV<=U[[]l:&>w&G&M&M&OO#&f\*-g679 9 +&j)A-)P"QQ,6,z*B>*R#SSvv%j1-@+%j1.A<Q G 0 @Ar   c           	          |j                         }|| v rHt        | ||| |   d   g | |   d   g        	 |j                  dj                  | |   d                | |= yy# t        j
                  $ r t        d      w xY w)Nr   r   zdrop schema {0} restrictr   z+Dropping schema failed due to dependencies.TF)r"   r6   r   r/   pyodbcErrorr   )r0   r&   r'   r   r   r+   s         r   absentrD      s    J\!\66!*-m<b,zBZ[iBjln	p	QNN5<<\*=UV\=]^_ $ || 	Q!"OPP	Qs   &A A=c                     t        t        t        ddg      t        dg      t        dg      t               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 |      }g }| j                  d   r*| j                  d   j                  d      }t        d |      }| j                  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   )r2   aliases
usage_role)rF   create_roler@   rD   )defaultchoices	localhost)rI   5433dbadmin)no_log)
r'   r   r   r   statedbclusterport
login_userlogin_password)argument_specsupports_check_moderB   )msg	exceptionr'   r   ,r   r   rO    rP   Fz_Driver=Vertica;Server={0};Port={1};Database={2};User={3};Password={4};ConnectionLoadBalance={5}rQ   rR   rS   rT   true)
autocommitz#Unable to connect to database: {0}.)rW   vertica_schemas)rW   ansible_facts)r?   r'   r^   )r   dictpyodbc_found	fail_jsonr   PYODBC_IMP_ERRparamssplitfilterr/   rB   connectr&   	Exceptionr   r,   
check_moder9   rD   rC   	traceback
format_excr@   r
   r   
SystemExit	exit_json)moduler'   r   r   r   rO   rP   r?   dsndb_connr&   er0   s                r   mainrq      s   x8l^4}o6&y8Y2GHv-f%I.t,
  $%F 1(;~V]]8$FK}}]#mmM288=T;/L}}^$}}^4::3?dL1MM'"EMM'"E	B}}T]]4 GY( &y)6==+@"|,fmm<L.MvW 	 ..6!M'/fk<QVWWGhU vv{LY iU!,\[`a WVDUWcCde7  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 \Yq\:K\9Z[[ \Yq\:K\9Z[[  MYq\Y5I5I5KLLMs   4A1I &-L- J #L- *K 	J*J		JK$.KL- KL- L*2.L% L- %L**L- -	O6MO&N

O.OO__main__)rZ   )
__future__r   r   r   type__metaclass__DOCUMENTATIONEXAMPLESri   rb   rB   r`   ImportErrorrj   ansible.module_utils.basicr   r   +ansible.module_utils.common.text.convertersr   rg   r
   r   r,   r6   r9   r@   rD   rq   r   r   r   r   <module>r{      s    A @JX
 
 L J A		 		i 	&RQ
:"Lf^ zF k  )Y))+NLs   A# #A>=A>