
    Vh:                     B   d dl mZmZmZ eZdZdZdZd dl	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	Z	 d d
lmZ dZd Zd Zd Zd Zd Zd Z d 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[[dZ[ww xY w)    )absolute_importdivisionprint_functionaQ  
---
module: mongodb_balancer
short_description: Manages the MongoDB Sharded Cluster Balancer.
description:
  - Manages the MongoDB Sharded Cluster Balancer.
  - Start or stop the balancer.
  - Adjust the cluster chunksize.
  - Enable or disable autosplit.
  - Add or remove a balancer window.
author: Rhys Campbell (@rhysmeister)
version_added: "1.0.0"

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

options:
  autosplit:
    description:
      - Disable or enable the autosplit flag in the config.settings collection.
      - From MongoDB 6.1 automatic chunk splitting is not performed so this parameter is not valid in this and later versions. See more see [enableAutoSplit](https://www.mongodb.com/docs/manual/reference/method/sh.enableAutoSplit/).  # noqa: E501
      - This parameter is deprecated and will be removed in a future release.
    required: false
    type: bool
  chunksize:
    description:
      - Control the size of chunks in the sharded cluster.
      - Value should be given in MB.
    required: false
    type: int
  state:
    description:
      - Manage the Balancer for the Cluster
    required: false
    type: str
    choices:
      - "started"
      - "stopped"
    default: "started"
  mongos_process:
    description:
      - Provide a custom name for the mongos process.
      - Most users can ignore this setting.
    required: false
    type: str
    default: "mongos"
  window:
    description:
      - Schedule the balancer window.
      - Provide the following dictionary keys start, stop, state
      - The state key should be "present" or "absent".
      - The start and stop keys are ignored when state is "absent".
      - start and stop should be strings in "HH:MM" format indicating the time bounds of the window.
    type: raw
    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(https://www.mongodb.com/docs/languages/python/pymongo-driver/current/get-started/download-and-install/)
requirements:
  - pymongo
a  
- name: Start the balancer
  community.mongodb.mongodb_balancer:
    state: started

- name: Stop the balancer and disable autosplit
  community.mongodb.mongodb_balancer:
    state: stopped
    autosplit: false

- name: Enable autosplit
  community.mongodb.mongodb_balancer:
    autosplit: true

- name: Change the default chunksize to 128MB
  community.mongodb.mongodb_balancer:
    chunksize: 128

- name: Add or update a balancing window
  community.mongodb.mongodb_balancer:
    window:
      start: "23:00"
      stop: "06:00"
      state: "present"

- name: Remove a balancing window
  community.mongodb.mongodb_balancer:
    window:
      state: "absent"
a  
changed:
  description: Whether the balancer state or autosplit changed.
  returned: success
  type: bool
old_balancer_state:
  description: The previous state of the balancer
  returned: When balancer state is changed
  type: str
new_balancer_state:
  description: The new state of the balancer.
  returned: When balancer state is changed
  type: str
old_autosplit:
  description: The previous state of autosplit.
  returned: When autosplit is changed.
  type: str
new_autosplit:
  description: The new state of autosplit.
  returned: When autosplit is changed.
  type: str
old_chunksize:
  description: The previous value for chunksize.
  returned: When chunksize is changed.
  type: int
new_chunksize:
  description: The new value for chunksize.
  returned: When chunksize is changed.
  type: int
msg:
  description: A short description of what happened.
  returned: failure
  type: str
failed:
  description: If something went wrong
  returned: failed
  type: bool
N)AnsibleModule)	to_native)missing_required_libmongodb_common_argument_spec
mongo_authPYMONGO_IMP_ERRpymongo_foundget_mongodb_clientF)OrderedDictTc                 p    d}| d   j                   j                  ddi      }|sd}|S |d   du rd}|S d}|S )a  
    Gets the state of the MongoDB balancer. The config.settings collection does
    not exist until the balancer has been started for the first time
    { "_id" : "balancer", "mode" : "full", "stopped" : false }
    { "_id" : "autosplit", "enabled" : true }
    Nconfig_idbalancerstoppedFstartedsettingsfind_one)clientbalancer_stateresults      v/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/mongodb/plugins/modules/mongodb_balancer.pyget_balancer_stater      sb     NH&&//
0CDF" 	 )%&N  'N    c                 p    t        ddg      }| d   j                  |       t        j                  d       y)z 
    Stops MongoDB balancer
    )balancerStop   	maxTimeMSi`  adminr    Nr   commandtimesleepr   cmd_docs     r   stop_balancerr*      s8      G 7OG$JJqMr   c                 p    t        ddg      }| d   j                  |       t        j                  d       y)z!
    Starts MongoDB balancer
    )balancerStartr    r!   r#   r    Nr$   r(   s     r   start_balancerr-      s8      G 7OG$JJqMr   c                 R    | d   j                   j                  ddidddiid       y )Nr   r   	autosplit$setenabledTupsertr   
update_oner   s    r   enable_autosplitr7      s7    
8((%)=*09d2C)D04 ) 6r   c                 R    | d   j                   j                  ddidddiid       y )	Nr   r   r/   r0   r1   FTr2   r4   r6   s    r   disable_autosplitr9      s7    
8((%)=*09e2D)E04 ) 6r   c                 X    d}| d   j                   j                  ddi      }||d   }|S )NFr   r   r/   r1   r   )r   r/   r   s      r   get_autosplitr;      s=    IH&&//0DEF9%	r   c                 `    d}| d   j                   j                  ddi      }|sd}|S |d   }|S )z#
    Default chunksize is 64MB
    Nr   r   	chunksize@   valuer   )r   r=   r   s      r   get_chunksizer@      sJ     IH&&//0DEF	  7O	r   c                 R    | d   j                   j                  ddidd|iid       y )Nr   r   r=   r0   r?   Tr2   r4   )r   r=   s     r   set_chunksizerB      s7    
8((%)=*07I2F)G04 ) 6r   c                     d}| d   j                   j                  ddidd||diid	      }|j                  d
k(  s|j                  d}|S )NFr   r   r   r0   activeWindow)startstopTr2   r    )r   r5   modified_countupserted_idr   rE   rF   sr   s        r   set_balancing_windowrK      sw    AH&&115*2E396DCHBFGH<I 3J :> 2 ?F !V%7%7%CHr   c                 v    d}| d   j                   j                  ddidddii      }|j                  dk(  rd}|S )	NFr   r   r   z$unsetrD   Tr    )r   r5   rG   )r   rJ   r   s      r   remove_balancing_windowrM     sO    AH&&115*2E3;nd=S2TVF!Hr   c                     d}|%|#| d   j                   j                  d||d      }n#| d   j                   j                  dddid      }|rd}|S )NFr   r   )r   zactiveWindow.startzactiveWindow.stopz$existsT)r   rD   r   rI   s        r   balancing_windowrO     sr    AT-!**33JKPJN5P Q !**33JYbdhXi4jkHr   c                    | }d| j                         vr|j                  d       y
| d   dvr|j                  d       y
| d   dk(  r6d| j                         vsd| j                         vr|j                  d	       y
)Nstatez(Balancing window state must be specifiedmsg)presentabsentz0Balancing window state must be present or absentrT   rE   rF   z8Balancing window start and stop values must be specifiedT)keys	fail_json)windowmodules     r   validate_windowrZ     s    &++-'!KL  G_$99!ST
 	 G_	)FKKM1fkkm3![\r   c                  l   t               } | j                  t        dd       t        dd       t        ddd      t        dddd	g
      t        dd              t        | dddg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   }t        ||       t        d      }		 t        |      }
t        ||
      }
d}d }d }d }d }d }d }d }d }d }	 
d   j                  d      d    |k7  r#|j                  d!j!                  |||             t#        |
      }|t%        |
      }|t'        |
      }|j(                  rk||k7  r|}|}d}|||k7  r|}|}d}|||k7  r|}|}d}|,t+        |
|j-                  d"      |j-                  d#            r|d   d$k(  rnd}n|d   d$k(  rd}nn|?||k7  r:|dk(  rt/        |
       |}t#        |
      }d}nt1        |
       |}t#        |
      }d}|*||k7  r%|rt3        |
       |}|}d}nt5        |
       |}|}d}|||k7  rt7        |
|       |}|}d}|at+        |
|j-                  d"      |j-                  d#            r|d   d$k(  rn-t9        |
       d}n|d   d$k(  rt;        |
|d"   |d#          d}n	 ||	d'<   |
||	d(<   ||	d)<   |
||	d*<   ||	d+<   |
||	d,<   ||	d-<    |j<                  d.i |	 y # t        $ r)}|j                  dt        |      z         Y d }~@d }~ww xY w# t        $ r}d%j!                  |      |	d&<   Y d }~d }~ww xY w)/Nbool)typedefaultintstrFmongos)r]   requiredr^   r   r   )r]   r^   choicesraw)r/   r=   mongos_processrQ   rX   T
login_userlogin_password)argument_specsupports_check_moderequired_togetherzWCannot import OrderedDict class. You can probably install with: pip install ordereddictrR   pymongo)rS   	exception
login_host
login_portrQ   r/   r=   re   rX   )changedz Unable to connect to MongoDB: %sr#   serverStatusprocessz'Process running on {0}:{1} is not a {2}rE   rF   rT   zAn error occurred: {0}rS   ro   old_balancer_statenew_balancer_stateold_autosplitnew_autosplitold_chunksizenew_chunksize )r	   updatedictr   has_ordereddictrW   r   r   r   paramsrZ   r   r
   	Exceptionr   r%   formatr   r;   r@   
check_moderO   getr-   r*   r7   r9   rB   rM   rK   	exit_json)rh   rY   rm   rn   r   r/   r=   re   rX   r   r   excepro   cluster_balancer_statecluster_autosplitcluster_chunksizerr   rs   rt   ru   rv   rw   s                         r   mainr   +  s   02MFD1E40Iy9i:PQ-   # (*:;<F vw1)<#2 	 	4 |,J|,J]]7+Nk*Ik*I]]#34N]]8$F FF#FT#F+FF+ G!MMMMT?'?"">29=O!J!Q!QR\^hjx!yz!3F!;  -f 5  -f 5!77%;"%3"%!%66 1 )%!%66 1 )!#FFJJw,?FASTg)3"&g)3"&)&*@@!Y."6*)?&);F)C&"G!&))?&);F)C&"G$!%66$V,$5M$-M"G%f-$5M$-M"G%!%66fi0 1 )!#FFJJw,?FASTg)3/7"&g)3,V-3G_-3F^= #'  F9%'9#$'9#$ "/"/ "/"/Fv_  T?)EBRRSSTB  ?077>u?s1   -M GN 	N	 NN		N3N..N3__main__))
__future__r   r   r   r]   __metaclass__DOCUMENTATIONEXAMPLESRETURNr&   ansible.module_utils.basicr   ansible.module_utils._textr   Iansible_collections.community.mongodb.plugins.module_utils.mongodb_commonr   r	   r
   r   r   r   r{   collectionsr   ImportErrorr   ordereddictr   r*   r-   r7   r9   r;   r@   rB   rK   rM   rO   rZ   r   __name__rx   r   r   <module>r      s    A @=~>%
N  4 0  'O&		66
6


Wt zF I	  + 	s5   A3 3B9BBBBBBB