
    Vhs"                        d dl mZmZmZ eZdZdZdZd dl	m
Z
mZ d dlmZmZ d dlmZ d dlmZmZmZ  ej*                  d	 ed
            5  d dlZd dlZddd        G d de      Zd Zedk(  r e        yy# 1 sw Y   %xY w)    )absolute_importdivisionprint_functiona  
module: ldap_inc
short_description: Use the Modify-Increment LDAP V3 feature to increment an attribute value
version_added: 10.2.0
description:
  - Atomically increments the value of an attribute and return its new value.
notes:
  - When implemented by the directory server, the module uses the ModifyIncrement extension defined in L(RFC4525, https://www.rfc-editor.org/rfc/rfc4525.html)
    and the control PostRead. This extension and the control are implemented in OpenLdap but not all directory servers implement
    them. In this case, the module automatically uses a more classic method based on two phases, first the current value is
    read then the modify operation remove the old value and add the new one in a single request. If the value has changed
    by a concurrent call then the remove action will fail. Then the sequence is retried 3 times before raising an error to
    the playbook. In an heavy modification environment, the module does not guarante to be systematically successful.
  - This only deals with integer attribute of an existing entry. To modify attributes of an entry, see M(community.general.ldap_attrs)
    or to add or remove whole entries, see M(community.general.ldap_entry).
author:
  - Philippe Duveau (@pduveau)
requirements:
  - python-ldap
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  dn:
    required: true
    type: str
    description:
      - The DN entry containing the attribute to increment.
  attribute:
    required: true
    type: str
    description:
      - The attribute to increment.
  increment:
    required: false
    type: int
    default: 1
    description:
      - The value of the increment to apply.
  method:
    required: false
    type: str
    default: auto
    choices: [auto, rfc4525, legacy]
    description:
      - If V(auto), the module determines automatically the method to use.
      - If V(rfc4525) or V(legacy) force to use the corresponding method.
extends_documentation_fragment:
  - community.general.ldap.documentation
  - community.general.attributes
a)  
- name: Increments uidNumber 1 Number for example.com
  community.general.ldap_inc:
    dn: "cn=uidNext,ou=unix-management,dc=example,dc=com"
    attribute: "uidNumber"
    increment: "1"
  register: ldap_uidNumber_sequence

- name: Modifies the user to define its identification number (uidNumber) when incrementation is successful
  community.general.ldap_attrs:
    dn: "cn=john,ou=posix-users,dc=example,dc=com"
    state: present
    attributes:
      - uidNumber: "{{ ldap_uidNumber_sequence.value }}"
  when: ldap_uidNumber_sequence.incremented
a!  
incremented:
  description:
    - It is set to V(true) if the attribute value has changed.
  returned: success
  type: bool
  sample: true

attribute:
  description:
    - The name of the attribute that was incremented.
  returned: success
  type: str
  sample: uidNumber

value:
  description:
    - The new value after incrementing.
  returned: success
  type: str
  sample: "2"

rfc4525:
  description:
    - Is V(true) if the method used to increment is based on RFC4525, V(false) if legacy.
  returned: success
  type: bool
  sample: true
)AnsibleModulemissing_required_lib)	to_nativeto_bytes)deps)LdapGeneric	gen_specsldap_required_togetherldapzpython-ldap)reasonNc                   4    e Zd Zd Zd Zd Zd Z ed      Zy)LdapIncc                     t        j                  | |       | j                  j                  d   | _        | j                  j                  d   | _        | j                  j                  d   | _        y )N	attribute	incrementmethod)r   __init__moduleparamsattrr   r   )selfr   s     n/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/ldap_inc.pyr   zLdapInc.__init__   sT    T6*KK&&{3	++K8kk((2    c                 x    t         j                  | j                  t        t	        | j
                              gfgS N)r   MOD_INCREMENTr   r	   strr   r   s    r   inc_rfc4525zLdapInc.inc_rfc4525   s-    ##TYY#dnn:M1N0OPQQr   c                     t         j                  | j                  t        |      gft         j                  | j                  t        |      gfgS r   )r   
MOD_DELETEr   r	   MOD_ADD)r   curr_valnew_vals      r   
inc_legacyzLdapInc.inc_legacy   sB    $))hx.@-ABtyy8G+<*=>@ 	@r   c                 n    t         j                  j                  j                  | j                  g      gS )N)attrList)r   controls	readentryPostReadControlr   r!   s    r   serverControlszLdapInc.serverControls   s)    ''77$))7MNNr   z1.3.6.1.1.14N)	__name__
__module____qualname__r   r"   r(   r.   r	   LDAP_MOD_INCREMENT r   r   r   r      s&    3R@O ".1r   r   c                      t        t        t        dd      t        ddd      t        ddg d	
            dt                     } t	        j
                  |        t        |       }d}d}d}	 |j                  dk7  r?| j                  s2d}|j                  dk7  r|j                  dk(  }n|j                  j                  dt        j                  ddg      }t        |      dk(  rTt        t        j                         |d   d   d   v r0|j"                  |d   d   d   v s|j"                  |d   d   d   v rd}|rz|j                  j%                  |j&                  |j)                         |j+                         d       \  }}}}t        |      dk(  r|d   j,                  |j.                     d   }nd}d}	||	k  r|dz   }|j                  j                  |j&                  t        j                  d|j.                  z  |j.                  g      }
t        |
      dk7  r| j1                  d       y 	 t3        t5        |
d   d   |j.                     d         |j                  z         }|j                  j7                  |j&                  |j9                  |
d   d   |j.                     d   |             n|j                  j                  |j&                  t        j                  d|j.                  z  |j.                  g      }
t        |
      dk(  rGt3        t5        |
d   d   |j.                     d         |j                  z         }|j                  dk7  }n| j1                  d       | jA                  |||j.                  ||!       y # t        j:                  $ r+ ||	k(  r#| j1                  dt3        |	      z   dz          Y y Y nw xY w||	k  rj# t<        $ r&}| j1                  dt?        |              Y d }~d }~ww xY w)"Nr    T)typerequiredint   F)r5   defaultr6   auto)r:   rfc4525legacy)r5   r9   choices)r   r   r   )argument_specsupports_check_moderequired_together r   rfc425*+)basescopeattrlistsupportedControlsupportedFeaturessupportedExtension)dnmodlistserverctrlsclientctrls   z(%s=*))rE   rF   	filterstrrG   zEThe entry does not exist or does not contain the specified attribute.)msg)rK   rL   z)The increment could not be applied after z tries.zAttribute action failed.)rQ   details)changedincrementedr   valuer;   )!r   r   dictr   r
   validater   r   
check_moder   
connectionsearch_ext_sr   
SCOPE_BASElenr	   CONTROL_POST_READr2   modify_ext_srK   r"   r.   entryr   	fail_jsonr    r7   modify_sr(   NO_SUCH_ATTRIBUTE	Exceptionr   	exit_json)r   modrS   retr;   rootDSEdummy
resp_ctrlstries	max_triesresultes               r   mainrn      s   5q5AUF<YZ

 !02F 	MM& &/CG
CGAO==Af&7&7GzzV#**0..55//!3Z 6 ) w<1$ 6 6771:a=I[;\\..'!*Q-@S2TT..'!*Q-@T2UU"&25..2M2MvvOO- # 2 2 4 $	 3N 3&/ueZ
 z?a'$Q---chh7:C 	i'!AIE ^^88 VV"oo"*SXX"5"%((	 9 -F
 6{a'((-t(u
#!#fQil388&<Q&?"@3=="PQ//"vv$'NN6!9Q<3I!3Lc$R 0 T  ^^00VVoo"SXX-((	 1 %F
 6{a#fQil3884Q783==HI--1,  %l m
 W'SXXUXbij)  11 # I-",,1\_bcl_m1mpy1y,z" .## i'D  O71NNOsK   /GO 9BN =B7O 9OO O OO O 	P'PP__main__)
__future__r   r   r   r5   __metaclass__DOCUMENTATIONEXAMPLESRETURNansible.module_utils.basicr   r   +ansible.module_utils.common.text.convertersr   r	   :ansible_collections.community.general.plugins.module_utilsr
   ?ansible_collections.community.general.plugins.module_utils.ldapr   r   r   declarer   ldap.controls.readentryr   rn   r/   r3   r   r   <module>r{      s    A @4n$
< K K K z zT\\&!5m!DE #"#
2k 2*Wkt zF i# #s   	A::B