
    Vh                         d dl mZmZmZ eZddgd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Z d dlmZ d dlmZ dZ	 d dlZ e       Z G d de      Zy# e$ r dZY w xY w)    )absolute_importdivisionprint_functionz1.1preview	community)metadata_versionstatussupported_bya  
name: mongodb
author:
  - Matt Martz (@sivel)
version_added: "1.0.0"
short_description: Use MongoDB for caching
description:
    - This cache uses per host records saved in MongoDB.
requirements:
    - pymongo>=3
options:
    _uri:
        description:
        - MongoDB Connection String URI
        required: False
        env:
          - name: ANSIBLE_CACHE_PLUGIN_CONNECTION
        ini:
          - key: fact_caching_connection
            section: defaults
    _prefix:
        description: User defined prefix to use when creating the DB entries
        default: ansible_facts
        env:
          - name: ANSIBLE_CACHE_PLUGIN_PREFIX
        ini:
          - key: fact_caching_prefix
            section: defaults
    _timeout:
        default: 86400
        description: Expiration timeout in seconds for the cache plugin data. Set to 0 to never expire
        env:
          - name: ANSIBLE_CACHE_PLUGIN_TIMEOUT
        ini:
          - key: fact_caching_timeout
            section: defaults
        type: integer
N)contextmanager)	constants)AnsibleError)BaseCacheModule)Display)	to_nativeFTc                   z     e Zd ZdZ fdZd Zd Zed        Zd Z	d Z
d Zd	 Zd
 Zd Zd Zd Zd Zd Z xZS )CacheModulez-
    A caching module backed by mongodb.
    c                    	 t         rt        d      t        t        |   |i | | j                  d      | _        t        | j                  d            | _        | j                  d      | _	        i | _        d| _        y # t        $ rK t        j                  | _        t        t        j                        | _        t        j                  | _	        Y bw xY w)Nz^The 'pymongo' python module is required for the mongodb fact cache, 'pip install pymongo>=3.0'_uri_timeout_prefixF)pymongo_missingr   superr   __init__
get_option_connectionintr   r   KeyErrorCCACHE_PLUGIN_CONNECTIONCACHE_PLUGIN_TIMEOUTCACHE_PLUGIN_PREFIX_cache_managed_indexes)selfargskwargs	__class__s      k/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/mongodb/plugins/cache/mongodb.pyr   zCacheModule.__init__I   s    
	1"  $D  E  E+t-t>v>#v6D
 ;<DM??95DL  %  	1 88D 6 67DM00DL	1s   A/B ACCc                     d}	 |j                         }|D ]  }|d   dk(  sd} |S  	 |S # t        j                  j                  $ r}t	        dt        |      z        d}~ww xY w)z\
        Returns true if an index named ttl exists
        on the given collection.
        FnamettlTz Error checking MongoDB index: %sN)list_indexespymongoerrorsOperationFailurer   r   )r$   
collectionexistsindexesindexexceps         r(   _ttl_index_existszCacheModule._ttl_index_existsY   s    
 	V --/G  =E)!F   ~~.. 	VAIeDTTUU	Vs   + + + A$AA$c                 H   | j                   }|r|dkD  r	 |j                  dd|       y| j                  |      r|j                  d       yy# t        j                  j                  $ r6 | j                  |      r|j                  d       | j                  |      cY S w xY w)z
        This function manages indexes on the mongo collection.
        We only do this once, at run time based on _managed_indexes,
        rather than per connection instantiation as that would be overkill
        r   dater+   )r*   expireAfterSecondsN)r   create_indexr-   r.   r/   r5   
drop_index_manage_indexes)r$   r0   r   s      r(   r;   zCacheModule._manage_indexesi   s     ==1
8'''/ (  %%j1%%e, 2 >>22 8))*5))%0++J77	8s   A AB! B!c              #   6  K   t        j                  | j                        }	 |j                         }|d   }| j                  s| j                  |       d| _        | |j                          y# t         j                  j
                  $ r |d   }Y bw xY ww)a  
        This is a context manager for opening and closing mongo connections as needed. This exists as to not create a global
        connection, due to pymongo not being fork safe (https://www.mongodb.com/docs/languages/python/pymongo-driver/current/faq/#is-pymongo-fork-safe-)
        ansiblecacheTN)	r-   MongoClientr   get_default_databaser.   ConfigurationErrorr#   r;   close)r$   mongodbr0   s       r(   _collectionzCacheModule._collection   s      ##D$4$45	"++-B [
$$  ,$(D! ~~00 	" y!B	"s'    BA1 >B1"BBBBc                 "    | j                   |S N)r   )r$   keys     r(   	_make_keyzCacheModule._make_key   s    s++    c                 2   || j                   vrc| j                         5 }|j                  d| j                  |      i      }|rd|v r|d   | j                   |<   ni | j                   |<   d d d        | j                   j	                  |      S # 1 sw Y   $xY w)N_iddata)r"   rE   find_onerI   get)r$   rH   r0   values       r(   rO   zCacheModule.get   s    dkk!!!# *z"++UDNN34G,HIVu_',V}DKK$')DKK$* {{s##* *s   ABBc           	         || j                   |<   | j                         5 }|j                  d| j                  |      id| j                  |      |t        j                  j                         did       d d d        y # 1 sw Y   y xY w)NrL   z$set)rL   rM   r7   T)upsert)r"   rE   
update_onerI   datetimeutcnow)r$   rH   rP   r0   s       r(   setzCacheModule.set   s     C 	:!!s+,#~~c2 % ( 1 1 8 8 :  " 
	 	 	s   AB  B	c                     | j                         5 }|j                  i ddi      D cg c]  }|d   	 c}cd d d        S c c}w # 1 sw Y   y xY w)NrL   T)rE   find)r$   r0   docs      r(   keyszCacheModule.keys   sS     	N:*4//"udm*LM3CJM	N 	NM	N 	Ns   AA A AAc           	          | j                         5 }t        |j                  d| j                  |      i            cd d d        S # 1 sw Y   y xY wNrL   )rE   boolcount_documentsrI   r$   rH   r0   s      r(   containszCacheModule.contains   sJ     	R:
22E4>>#;N3OPQ	R 	R 	Rs   +AAc                     | j                   |= | j                         5 }|j                  d| j                  |      i       d d d        y # 1 sw Y   y xY wr\   )r"   rE   
delete_onerI   r_   s      r(   deletezCacheModule.delete   sQ    KK 	@:!!5$..*=">?	@ 	@ 	@s   #A

Ac                 p    | j                         5 }|j                  i        d d d        y # 1 sw Y   y xY wrG   )rE   delete_manyr$   r0   s     r(   flushzCacheModule.flush   s3     	':""2&	' 	' 	's   ,5c                     | j                         5 }t        d |j                  i       D              cd d d        S # 1 sw Y   y xY w)Nc              3   0   K   | ]  }|d    |d   f  yw)rL   rM   N ).0ds     r(   	<genexpr>z#CacheModule.copy.<locals>.<genexpr>   s     K!51V9-Ks   )rE   dictrX   rf   s     r(   copyzCacheModule.copy   s@     	L:Kzr7JKK	L 	L 	Ls	   !<Ac                     t               S rG   )rn   )r$   s    r(   __getstate__zCacheModule.__getstate__   s	    vrJ   c                 $    | j                          y rG   )r   )r$   rM   s     r(   __setstate__zCacheModule.__setstate__   s    rJ   )__name__
__module____qualname____doc__r   r5   r;   r   rE   rI   rO   rV   rZ   r`   rc   rg   ro   rq   rs   __classcell__)r'   s   @r(   r   r   E   sa    &  -.  0,	$NR@
'LrJ   r   )
__future__r   r   r   type__metaclass__ANSIBLE_METADATADOCUMENTATIONrT   
contextlibr   r=   r   r   ansible.errorsr   ansible.plugins.cacher   ansible.utils.displayr   ansible.module_utils._textr   r   r-   ImportErrordisplayr   rj   rJ   r(   <module>r      s    C B(-(k$/1 %N  % " ' 1 ) 0 )J/ J  Os   A A"!A"