
    Vh'.                         d dl mZmZmZ eZdZdZdZd dl	m
Z
 d dlmZmZmZmZmZmZmZmZmZ d Zd Zed	k(  r e        y
y
)    )absolute_importdivisionprint_functiona-  
---
module: mongodb_index

short_description: Creates or drops indexes on MongoDB collections.

description:
  - Creates or drops indexes on MongoDB collections.
  - Supports multiple index options, i.e. unique, sparse and partial.
  - Validates existence of indexes by name only.

author: Rhys Campbell (@rhysmeister)
version_added: "1.0.0"

extends_documentation_fragment:
  - community.mongodb.login_options
  - community.mongodb.ssl_options

options:
  indexes:
    description:
      - List of indexes to create or drop
    type: list
    elements: raw
    required: yes
  replica_set:
    description:
      - Replica set to connect to (automatically connects to primary for writes).
    type: str
notes:
    - Requires the pymongo Python package on the remote host, version 4+.

requirements:
  - pymongo
a  
- name: Create a single index on a collection
  community.mongodb.mongodb_index:
    login_user: admin
    login_password: secret
    indexes:
      - database: mydb
        collection: test
        keys:
          - username: 1
            last_login: -1
        options:
          name: myindex
        state: present

- name: Drop an index on a collection
  community.mongodb.mongodb_index:
    login_user: admin
    login_password: secret
    indexes:
      - database: mydb
        collection: test
        options:
          name: myindex
        state: absent

- name: Create multiple indexes
  community.mongodb.mongodb_index:
    login_user: admin
    login_password: secret
    indexes:
      - database: mydb
        collection: test
        keys:
          - username: 1
            last_login: -1
        options:
          name: myindex
        state: present
      - database: mydb
        collection: test
        keys:
          - email: 1
            last_login: -1
        options:
          name: myindex2
        state: present

- name: Add a unique index
  community.mongodb.mongodb_index:
    login_port: 27017
    login_user: admin
    login_password: secret
    login_database: "admin"
    indexes:
      - database: "test"
        collection: "rhys"
        keys:
          username: 1
        options:
          name: myuniqueindex
          unique: true
        state: present

- name: Add a ttl index
  community.mongodb.mongodb_index:
    login_port: 27017
    login_user: admin
    login_password: secret
    login_database: "admin"
    indexes:
      - database: "test"
        collection: "rhys"
        keys:
          created: 1
        options:
          name: myttlindex
          expireAfterSeconds: 3600
        state: present

- name: Add a sparse index
  community.mongodb.mongodb_index:
    login_port: 27017
    login_user: admin
    login_password: secret
    login_database: "admin"
    indexes:
      - database: "test"
        collection: "rhys"
        keys:
          last_login: -1
        options:
          name: mysparseindex
          sparse: true
        state: present

- name: Add a partial index
  community.mongodb.mongodb_index:
    login_port: 27017
    login_user: admin
    login_password: secret
    login_database: "admin"
    indexes:
      - database: "test"
        collection: "rhys"
        keys:
          last_login: -1
        options:
          name: mypartialindex
          partialFilterExpression:
            rating:
              $gt: 5
        state: present

- name: Add a index in the background (background option is deprecated from 4.2+)
  community.mongodb.mongodb_index:
    login_port: 27017
    login_user: admin
    login_password: secret
    login_database: "admin"
    indexes:
      - database: "test"
        collection: "rhys"
        options:
          name: idxbackground
        keys:
          username: -1
        backgroud: true
        state: present

- name: Check creating 5 index all with multiple options specified
  community.mongodb.mongodb_index:
    login_port: 27017
    login_user: admin
    login_password: secret
    login_database: "admin"
    indexes:
      - database: "test"
        collection: "indextest"
        options:
          name: "idx_unq_username"
          unique: true
        keys:
          username: -1
        state: present
      - database: "test"
        collection: "indextest"
        options:
          name: "idx_last_login"
          sparse: true
        keys:
          last_login: -1
        state: present
      - database: "test"
        collection: "indextest"
        options:
          name: "myindex"
        keys:
          first_name: 1
          last_name: -1
          city: 1
        state: present
      - database: "test"
        collection: partialtest
        options:
          name: "idx_partialtest"
          partialFilterExpression:
            rating:
              $gt: 5
        keys:
          rating: -1
          title: 1
        state: present
      - database: "test"
        collection: "wideindex"
        options:
          name: "mywideindex"
        keys:
          email: -1
          username: 1
          first_name: 1
          last_name: 1
          dob: -1
          city: 1
          last_login: -1
          review_count: 1
          rating_count: 1
          last_post: -1
        state: present
a  
indexes_created:
  description: List of indexes created.
  returned: always
  type: list
  sample: ["myindex", "myindex2"]
indexes_dropped:
  description: List of indexes dropped.
  returned: always
  type: list
  sample: ["myindex", "myindex2"]
changed:
  description: Indicates the module has changed something.
  returned: When the module has changed something.
  type: bool
failed:
  description: Indicates the module has failed.
  returned: When the module has encountered an error.
  type: bool
)AnsibleModule)	missing_required_libmongodb_common_argument_specPYMONGO_IMP_ERRpymongo_foundindex_existscreate_index
drop_index
mongo_authget_mongodb_clientc                 J   g d}| j                   d   }t        |      dk(  r| j                  d       t        d |D              s| j                  d       |D ]=  }|D ]6  }||j	                         vs| j                  dj                  |             8 ? |D ]  }t        |d	   t              s| j                  d
       *t        |d   t              s| j                  d       P|d   dk(  r%d|j	                         vr| j                  d       }|d   dk(  r&t        |d   t              s| j                  d       t        |d   t              s| j                  d       d|d   vr| j                  d       |d   dvs| j                  d        y)zA
    Runs validation rules specific the mongodb_index module
    )database
collectionoptionsstateindexesr   z%One or more indexes must be specifiedmsgc              3   <   K   | ]  }t        |t                y w)N)
isinstancedict).0is     s/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/mongodb/plugins/modules/mongodb_index.py	<genexpr>z"validate_module.<locals>.<genexpr>   s     4qz!T"4s   z(Indexes must be supplied as dictionarieszMissing required index key {0}r   zdatabase key should be strr   zcollection key should be strr   presentkeysz+keys must be supplied when state is presentzkeys key should be dictr   zoptions key should be dictnamez*The options dict must contain a name field)r   absentz&state must be one of present or absentN)	paramslen	fail_jsonallr    formatr   strr   )modulerequired_index_keysr   kr   s        r   validate_moduler,     s    mmI&G
7|qDE4G44GH ! Q 	QA   %E%L%LQ%O P	QQ  K!J--!=>AlOS1!?@wZ9$qvvx)?!NOwZ9$Z&	4-H!:;AiL$/!=>1Y<'!MNwZ44!IJK    c            	         t               } | j                  t        ddd      t        d             t        | ddd	gg
      }t        s |j                  t        d      t               t        |       |j                  d   }t        |      }t        ||      }g }g }d }|D ]|  }	 t        ||d   |d   |d   d         }|j                   rrE|d   dk(  rd}8|d   dk(  sA|j#                  dj                  |d   |d   |d   d                d}r|d   dk(  r1|j#                  dj                  |d   |d   |d   d                d}|d   dk(  sd}ra|d   dk(  rd}|d   dk(  s	 t%        ||d   |d   |d   d          |j#                  dj                  |d   |d   |d   d                d}|d   dk(  rO	 t'        ||d   |d   |d   |d          |j#                  dj                  |d   |d   |d   d                d}q|d   dk(  s{d} |j)                  |||       y # t        $ r5}	|j                  dj                  t        |	                   Y d }	~	d }	~	ww xY w# t        $ r5}	|j                  dj                  t        |	                   Y d }	~	d }	~	ww xY w# t        $ r5}	|j                  dj                  t        |	                   Y d }	~	Pd }	~	ww xY w)NlistrawT)typeelementsrequiredr(   )r1   )r   replica_set
login_userlogin_password)argument_specsupports_check_moderequired_togetherpymongo)r   	exceptionr   r   r   r   r!   z%Could not determine index status: {0}r   r   r   Fr"   z{0}.{1}.{2}zError dropping index: {0}r    )clientr   r   r    r   zError creating index: {0})changedindexes_createdindexes_dropped)r   updater   r   r
   r%   r   r	   r,   r#   r   r   r   	Exceptionr'   r(   
check_modeappendr   r   	exit_json)
r7   r)   r   r<   r>   r?   r=   r   idxexceps
             r   mainrG   >  s   02M&54@e$   # (*:;<F 1)<#2 	 	4 FmmI&G'F'F OOG 4$	]vq}aoq|TZG[\C W:*#GwZ8+#**=+?+?*@A,@A)V@T,V W #GW:*#**=+?+?*@A,@A)V@T,V W #GwZ8+#GW:*#GwZ8+]"61Z=!L/#$Y<#79'..}/C/CAjMDElODEiLQWDX0Z [ #'
 W:*]$F./
m01,*+F)-.y\	;
 (..}/C/CAjMDElODEiLQWDX0Z [ #' wZ8+#Gi4$l W%4%4  6g  	]!H!O!OPSTYPZ![\\	]> % ]((-H-O-OPSTYPZ-[(\\] % ]((-H-O-OPSTYPZ-[(\\]sJ   )H92A
I:AJ;9	I7*I22I7:	J8*J33J8;	K9*K44K9__main__N)
__future__r   r   r   r1   __metaclass__DOCUMENTATIONEXAMPLESRETURNansible.module_utils.basicr   Iansible_collections.community.mongodb.plugins.module_utils.mongodb_commonr   r   r	   r
   r   r   r   r   r   r,   rG   __name__ r-   r   <module>rR      sa    A @"H}~
* 5
 
 
&KXS6l zF r-   