
    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mZ dZ	 d d	lm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[@dZ[ww xY w)    )absolute_importdivisionprint_functiona  
---
module: mongodb_oplog
short_description: Resizes the MongoDB oplog.
description:
  - Resizes the MongoDB oplog.
  - This module should only be used with MongoDB 3.6 and above.
  - Old MongoDB versions should use an alternative method.
  - Consult U(https://docs.mongodb.com/manual/tutorial/change-oplog-size) for further info.
author: Rhys Campbell (@rhysmeister)
version_added: "1.0.0"

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

options:
  oplog_size_mb:
    description:
      - New size of the oplog in MB.
    type: int
    required: true
  compact:
    description:
      - Runs compact against the oplog.rs collection in the local database to reclaim disk space.
      - Performs no actions against PRIMARY members.
      - The MongoDB user must have the compact role on the local database for this feature to work.
    type: bool
    default: false
    required: false
notes:
  - Requires the pymongo Python package on the remote host, version 4+.. This
    can be installed using pip or the OS package manager.
    @see U(http://api.mongodb.org/python/current/installation.html)
requirements:
  - pymongo
a  
- name: Resize oplog to 16 gigabytes, or 16000 megabytes
  community.mongodb.mongodb_oplog:
    oplog_size_mb: 16000

- name: Resize oplog to 8 gigabytes and compact secondaries to reclaim space
  community.mongodb.mongodb_oplog:
    oplog_size_mb: 8000
    compact: true
am  
changed:
  description: Whether the member oplog was modified.
  returned: success
  type: bool
compacted:
  description: Whether the member oplog was compacted.
  returned: success
  type: bool
msg:
  description: A short description of what happened.
  returned: success
  type: str
failed:
  description: If something went wrong
  returned: failed
  type: bool
)AnsibleModule)	to_native)missing_required_libmongodb_common_argument_specmember_state
mongo_authPYMONGO_IMP_ERRpymongo_foundget_mongodb_clientF)OrderedDictTNc                 P    t        | d   j                  dd      d         dz  dz  S )Nlocal	collStatsoplog.rsmaxSizei   )intcommandclients    s/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/mongodb/plugins/modules/mongodb_oplog.pyget_olplog_sizer   f   s.    vg&&{J?	JKdRUYYY    c                 J    t        dd|fg      }| d   j                  |       y )N)replSetResizeOplog   sizeadmin)r   r   )r   oplog_size_mbcmd_docs      r   set_oplog_sizer#   j   s0    !	 G 7OG$r   c                 .    | d   j                  dd       y )Nr   compactr   )r   r   s    r   compact_oplogr&   r   s    
7OIz2r   c                     t               } | j                  t        dd      t        dd             t        | ddd	gg
      }t        s|j                  d       t        s |j                  t        d      t               t        |j                  d         }|j                  d   }t        d      }	 t        |d      }t        ||d      }	 t              }|k(  r#dj!                  t#        |            |d<   d|d<   n	 t%              }|j&                  rDd|d<   dj!                  t#        |      t#        |            |d<   dk(  r|r||kD  rd|d<   nbd|d<   n\	 t)        |       d|d<   dj!                  t#        |      t#        |            |d<   dk(  r|r||kD  r	 t+               d|d<   nd|d<    |j,                  di | y # t        $ r)}|j                  dt        |      z         Y d }~,d }~ww xY w# t        $ r)}|j                  dt        |      z         Y d }~Ud }~ww xY w# t        $ r)}|j                  dt        |      z         Y d }~Vd }~ww xY w# t        $ r)}|j                  dt        |      z         Y d }~d }~ww xY w# t        $ r)}|j                  dt        |      z         Y d }~d }~ww xY w)NboolF)typedefaultr   T)r)   required)r%   r!   
login_userlogin_password)argument_specsupports_check_moderequired_togetherzWCannot import OrderedDict class. You can probably install with: pip install ordereddict)msgpymongo)r1   	exceptionr!   r%   )changed)directConnectionz Unable to connect to MongoDB: %sz$Unable to get current oplog size: %szoplog_size_mb is already {0} mbr1   	compactedzUnable to get member state: %sr4   z,oplog has been resized from {0} mb to {1} mb	SECONDARYzUnable to set oplog size: %sz!Error compacting member oplog: %s )r	   updatedictr   has_ordereddict	fail_jsonr   r   r   floatparamsr   r   	Exceptionr   r   formatr   r
   
check_moder#   r&   	exit_json)	r.   moduler!   r%   resultr   excepcurrent_oplog_sizestates	            r   mainrH   v   s   02M&%05   # (*:;<F vw1)<#2 	 	4 &--89MmmI&GFT#FTBFFTBX,V4 **9@@]ASTu#{	V (E  $F9JQQRUVhRiRUVcRdfF5M#4F4V&*{#&+{#Xv}5$(y! N U UVYZlVmVYZgVh!ju #4F4Va!&)*.F;' ',{#FvQ  T?)EBRRSST
  XCiPUFVVWWX  	V!AIeDT!TUU	V   X  %CiPUFV%V WWX ! a$$)LyY^O_)_$``asx   7G H H; #8I0 (J% 	HG>>H	H8H33H8;	I-I((I-0	J"9JJ"%	K.KK__main__) 
__future__r   r   r   r)   __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   ImportErrorrE   ordereddictr   r#   r&   rH   __name__r8   r   r   <module>rV      s    A @$L	
& 5 0   'OZ%3EP zF A  + 	s5   A BA%$A<%A4*A</A44A<<B