
    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 d dlmZmZmZmZmZmZ d dlZd	Z	 d d
lmZ dZd Zd Zd Zd Zd Ze dk(  r e        yy# e$ r&Z	 d d
lmZ dZn# e$ r
ZY dZ[ndZ[ww xY wY dZ[CdZ[ww xY w)    )absolute_importdivisionprint_functiona  
---
module: mongodb_schema

short_description: Manages MongoDB Document Schema Validators.

description:
- Manages MongoDB Document Schema Validators.
- Create, update and remove Validators on a collection.
- Supports the entire range of jsonSchema keywords.
- See [jsonSchema Available Keywords](https://docs.mongodb.com/manual/reference/operator/query/jsonSchema/#available-keywords) for details.

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

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

options:
  db:
    description:
      - The database to work with.
    required: yes
    type: str
  collection:
    description:
      - The collection to work with.
    required: yes
    type: str
  required:
    description:
      - List of fields that are required.
    type: list
    elements: str
  properties:
    description:
      - Individual property specification.
    type: dict
    default: {}
  action:
    description:
      - The validation action for MongoDB to perform when handling invalid documents.
    type: str
    choices:
      - "error"
      - "warn"
    default: "error"
  level:
    description:
      - The validation level MongoDB should apply when updating existing documents.
    type: str
    choices:
      - "strict"
      - "moderate"
    default: "strict"
  replica_set:
    description:
      - Replicaset name.
    type: str
    default: null
  state:
    description:
      - The state of the validator.
    type: str
    choices:
      - "present"
      - "absent"
    default: "present"
  debug:
    description:
      - Enable additional debugging output.
    type: bool
    default: false

notes:
    - Requires the pymongo Python package on the remote host, version 4+.

requirements:
  - pymongo
a  
---
- name: Require that an email address field is in every document
  community.mongodb.mongodb_schema:
    collection: contacts
    db: rhys
    required:
      - email

- name: Remove a schema rule
  community.mongodb.mongodb_schema:
    collection: contacts
    db: rhys
    state: absent


- name: More advanced example using properties
  community.mongodb.mongodb_schema:
    collection: contacts
    db: rhys
    properties:
      email:
        maxLength: 150
        minLength: 5
      options:
        bsonType: array
        maxItems: 10
        minItems: 5
        uniqueItems: true
      status:
        bsonType: string
        description: "can only be ACTIVE or DISABLED"
        enum:
          - ACTIVE
          - DISABLED
      year:
        bsonType: int
        description: "must be an integer from 2021 to 3020"
        exclusiveMaximum: false
        maximum: 3020
        minimum: 2021
    required:
      - email
      - first_name
      - last_name
a  
changed:
  description: If the module caused a change.
  returned: on success
  type: bool
msg:
  description: Status message.
  returned: always
  type: str
validator:
  description: The validator document as read from the instance.
  returned: when debug is true
  type: dict
module_config:
  description: The validator document as indicated by the module invocation.
  returned: when debug is true
  type: dict
)AnsibleModule)	to_native)missing_required_libmongodb_common_argument_spec
mongo_authPYMONGO_IMP_ERRpymongo_foundget_mongodb_clientNF)OrderedDictTc                    d }t        ddd|ifg      }d }| |   j                  |      d   d   }t        |      dkD  r|d   }|:d|v r6d|d   v r/|d   d   d	   }d
|d   v r|d   d
   |d
<   d|d   v r|d   d   |d<   |S )N)listCollections   filternamecursor
firstBatchr   options	validator$jsonSchemavalidationActionvalidationLevel)r   commandlen)clientdb
collectionr   cmd_docdocresultss          t/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/mongodb/plugins/modules/mongodb_schema.pyget_validatorr$      s    I	FJ'( G CRj  )(3LAG
7|aaj
9+s9~0M	N;/>	Y/,/	N;M,NI()I.+.y>:K+LI'(    c                 h   d}t        | ||      }|t        |      t        |j                  dg             k7  rd}||j                  dd      k7  rd}||j                  dd      k7  rd}t        j                  |d      }	t        j                  |j                  d	i       d      }
|	|
k7  rd}|S d}|S )
NFrequiredTr   errorr   strict)	sort_keys
properties)r$   sortedgetjsondumps)r   r   r   r'   r+   actionlevelis_differentr   dict1dict2s              r#   validator_is_differentr5      s    Lfb*5I(vimmJ&CDDLY]]#5w??LIMM"3X>>L

:6

9==r:dKE>L  r%   c           	          t        d|fddd||difd|fd|fg      }|| |   j                         vr| |   j                  |       | |   j                  |       y )NcollModr   r   object)bsonTyper'   r+   r   r   )r   list_collection_namescreate_collectionr   )r   r   r   r'   r+   r0   r1   r    s           r#   add_validatorr<      s    	J	}83;5?'A B 	C 
V$	E" G 99;;r
$$Z0
2Jwr%   c                 P    t        d|fdi fdg      }| |   j                  |       y )Nr7   r   )r   off)r   r   )r   r   r   r    s       r#   remove_validatorr?      s8    	J	b" G
 2Jwr%   c                     t               } | j                  t        dd      t        dd      t        dd      t        di       t        ddd	gd
      t        dddgd
      t        dddgd
      t        dd      t        dd       	       t        | dddggdg      }t        s|j                  d       t        s |j                  t        d      t               |j                  d   }|j                  d   }|j                  d   }|j                  d   }|j                  d   }|j                  d   }|j                  d    }|j                  d!   }		 t        |      }
t        ||
      }
t        d#      }t        
||      }|dk(  rp|Ft        |
||||||      }|r(|j                   st#        |
||||||       d|d$<   d%|d&<   nhd|d$<   d'|d&<   n]|j                   st#        |
||||||       d|d$<   d(|d&<   n5|dk(  r0|d|d$<   d)|d&<   n#|j                   st%        |
||       d|d$<   d*|d&<   |	r||d+<   ||||d,|d-<    |j&                  d.i | y # t        $ r)}|j                  d"t        |      z         Y d }~d }~ww xY w)/NstrT)typer'   list)rB   elementsdict)rB   defaultr(   warn)rB   choicesrF   r)   moderatepresentabsentboolF)	r   r   r'   r+   r0   r1   statedebugreplica_set
login_userlogin_password)rM   rJ   )r   r   )argument_specsupports_check_moderequired_togetherrequired_ifzWCannot import OrderedDict class. You can probably install with: pip install ordereddict)msgpymongo)rV   	exceptionr   r   r'   r+   r0   r1   rM   rN   z!Unable to connect to database: %s)changedrY   z1The validator was updated on the given collectionrV   z:The validator exists as configured on the given collectionz4The validator has been added to the given collectionz3A validator does not exist on the given collection.z8The validator has been removed from the given collectionr   )r'   r+   r   r   module_config )r	   updaterE   r   has_ordereddict	fail_jsonr   r   r   paramsr   r
   	Exceptionr   r$   r5   
check_moder<   r?   	exit_json)rR   moduler   r   r'   r+   r0   r1   rM   rN   r   eresultr   diffs                  r#   mainrg      s   02MUT*UT26E2VR0&(97K*'=xP	8'<iP.eT2  
 # (*:;<?@	F vw1)<#2 	 	4 
t	B|,J}}Z(H|,J]]8$FMM'"EMM'"EMM'"EQ#F+FF+ F fb*5I	 )&"j(*4feED((!&"$","*","("') %)y! Su$)y! \u$$f (&($#% !%F9RF5M	(	 %F9QF5M$$ Z8 $F9VF5M'{/71;7=6;#=
 Fvm  Q@9Q<OPPQs   I 	J"JJ__main__)!
__future__r   r   r   rB   __metaclass__DOCUMENTATIONEXAMPLESRETURNansible.module_utils.basicr   ansible.module_utils._textr   Iansible_collections.community.mongodb.plugins.module_utils.mongodb_commonr   r	   r
   r   r   r   r.   r]   collectionsr   ImportErrorexcepordereddictr$   r5   r<   r?   rg   __name__r[   r%   r#   <module>rv      s    A @Pd-^
& 5 0   'O&&  ]@ zF S  + 	s5   A B!A*)B*A9/B4A99BB