
    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ddZddZddZddZddZd Zedk(  r e        yy# e$ r  ej                         Z	dZY Xw xY w)    )absolute_importdivisionprint_functiona  
module: vertica_info
short_description: Gathers Vertica database facts
description:
  - Gathers Vertica database information.
extends_documentation_fragment:
  - community.general.attributes
  - community.general.attributes.info_module
options:
  cluster:
    description:
      - Name of the cluster running the schema.
    default: localhost
    type: str
  port:
    description: Database port to connect to.
    default: '5433'
    type: str
  db:
    description:
      - Name of the database running the schema.
    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) are 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)"
z
- name: Gathering vertica facts
  community.general.vertica_info: db=db_name
  register: result

- name: Print schemas
  ansible.builtin.debug:
    msg: "{{ result.vertica_schemas }}"
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_info.pyr
   r
   N   s    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')
        and (? = '' or schema_name ilike ?)
    d   )nameownercreate_timeusage_rolescreate_rolesaw  
        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 = r.name 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(   T   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           
         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)
NaO  
        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 ?)
     r   )r   lockedpasswordexpiredprofileresource_poolrolesdefault_roleslocked_time  ,r/   r0   )r   r   	user_namer   r   	is_lockedr+   
is_expiredprofile_namer.   	lock_time	all_rolesreplacesplitr0   )r"   userr$   r%   r&   user_keys         r   get_user_factsr?   }   s=   E
NN 	  $$ 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                 >   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 ?)
    r   )r   assigned_rolesr2   r3   r4   rA   )r   r   r   r   rA   r;   r<   )r"   roler$   r%   r&   role_keys         r   get_role_factsrD      s    E
NN  
t	
 $ L  	cCxx~~'H"$&E(O !!474F4F4N4NsTV4W4]4]^a4bh 01	c	 r   c                     i }| j                  d||       	 | j                  d      }|s	 |S |D ]A  }|j                  |j                  |j                  d||j                  j                         <   C ])Nz
        select c.parameter_name, c.current_value, c.default_value
        from configuration_parameters c
        where c.node_name = 'ALL'
        and (? = '' or c.parameter_name ilike ?)
    r   )parameter_namecurrent_valuedefault_value)r   r   rF   rG   rH   r   )r"   	parameterr$   r%   r&   s        r   get_configuration_factsrJ      s    E
NN 
 
I $ L  	4C"%"4"4!$!2!2!$!2!214E#$$**,-	4	 r   c                     i }| j                  d       	 | j                  d      }|s	 |S |D ]I  }|j                  |j                  |j                  |j
                  |j                  d||j                  <   K e)Nz
        select node_name, node_address, export_address, node_state, node_type,
            catalog_path
        from nodes
    r   )	node_nameexport_address
node_state	node_typecatalog_path)r   r   rL   rM   rN   rO   rP   node_address)r"   r#   r$   r%   r&   s        r   get_node_factsrR      s    E
NN  	
 $ L  	2C ]]"%"4"4!nn ]] # 0 0'2E#""#	2	 r   c                  N   t        t        t        d      t        d      t        d       t        d      t        d d            d      } t        s | j                  t	        d	      t
        
       d}| j                  d   r| j                  d   }d| j                  d   d| j                  d   d|d| j                  d   d| j                  d   dd}t        j                  |d      }|j                         }	 t              }t        |      }t!        |      }t#        |      }	t%        |      }
| j'                  d||||	|
       y # t        $ r;}| j                  dt        |      z  t        j                         
       Y d }~d }~ww xY w# t(        $ r8}| j                  t        |      t        j                         
       Y d }~y d }~wt*        $ r  t        $ r8}| j                  t        |      t        j                         
       Y d }~y d }~ww xY w)N	localhost)default5433dbadminT)rU   no_log)clusterportdb
login_userlogin_password)argument_specsupports_check_modepyodbc)msg	exceptionr3   r[   zDriver=Vertica;Server=rY   z;Port=rZ   z
;Database=z;User=r\   z
;Password=r]   z;ConnectionLoadBalance=true)
autocommitz"Unable to connect to database: %s.F)changedvertica_schemasvertica_usersvertica_rolesvertica_configurationvertica_nodes)r   dictpyodbc_found	fail_jsonr   PYODBC_IMP_ERRparamsr`   connectr"   	Exceptionr   	traceback
format_excr(   r?   rD   rJ   rR   	exit_jsonr
   
SystemExit)moduler[   dsndb_connr"   eschema_facts
user_facts
role_factsconfiguration_facts
node_factss              r   mainr      s   -f%D!I.T:
  $%F 1(;~V	B}}T]]4 t ]]9%v}}V'<b]]<(&--8H*I6	S ..6!M'/#F+
#F+
5f=#F+
)5'1'1/B'1 	 	3  tAIaLP\e\p\p\rsst   MYq\Y5I5I5KLL  MYq\Y5I5I5KLLMs>   A/E AF 	F1FF	H$$.GH$,.HH$__main__)r3   )
__future__r   r   r   type__metaclass__DOCUMENTATIONEXAMPLESrr   rn   r`   rl   ImportErrorrs   ansible.module_utils.basicr   r   +ansible.module_utils.common.text.convertersr   rq   r
   r(   r?   rD   rJ   rR   r   r   r   r   r   <module>r      s    A @(T 
 L J A		 	&R F*(.5Mp zF i  )Y))+NLs   A A76A7