
    VhF:                         d dl mZmZmZ eZdZdZdZd dl	Z	d dl
Z
d dlZ	 d dlmZmZ d dlmZmZ d dlmZmZmZ d d	lmZmZ  G d
 de      Zd Zedk(  r e        yy# e$ r Y =w xY w)    )absolute_importdivisionprint_functiona  
module: docker_config

short_description: Manage docker configs

description:
  - Create and remove Docker configs in a Swarm environment. Similar to C(docker config create) and C(docker config rm).
  - Adds to the metadata of new configs C(ansible_key), an encrypted hash representation of the data, which is then used in
    future runs to test if a config has changed. If C(ansible_key) is not present, then a config will not be updated unless
    the O(force) option is set.
  - Updates to configs are performed by removing the config and creating it again.
extends_documentation_fragment:
  - community.docker.docker
  - community.docker.docker.docker_py_2_documentation
  - community.docker.attributes
  - community.docker.attributes.actiongroup_docker

attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
  idempotent:
    support: partial
    details:
      - If O(force=true) the module is not idempotent.

options:
  data:
    description:
      - The value of the config.
      - Mutually exclusive with O(data_src). One of O(data) and O(data_src) is required if O(state=present).
    type: str
  data_is_b64:
    description:
      - If set to V(true), the data is assumed to be Base64 encoded and will be decoded before being used.
      - To use binary O(data), it is better to keep it Base64 encoded and let it be decoded by this option.
    type: bool
    default: false
  data_src:
    description:
      - The file on the target from which to read the config.
      - Mutually exclusive with O(data). One of O(data) and O(data_src) is required if O(state=present).
    type: path
    version_added: 1.10.0
  labels:
    description:
      - A map of key:value meta data, where both the C(key) and C(value) are expected to be a string.
      - If new meta data is provided, or existing meta data is modified, the config will be updated by removing it and creating
        it again.
    type: dict
  force:
    description:
      - Use with O(state=present) to always remove and recreate an existing config.
      - If V(true), an existing config will be replaced, even if it has not been changed.
    type: bool
    default: false
  rolling_versions:
    description:
      - If set to V(true), configs are created with an increasing version number appended to their name.
      - Adds a label containing the version number to the managed configs with the name C(ansible_version).
    type: bool
    default: false
    version_added: 2.2.0
  versions_to_keep:
    description:
      - When using O(rolling_versions), the number of old versions of the config to keep.
      - Extraneous old configs are deleted after the new one is created.
      - Set to V(-1) to keep everything or V(0) or V(1) to keep only the current one.
    type: int
    default: 5
    version_added: 2.2.0
  name:
    description:
      - The name of the config.
    type: str
    required: true
  state:
    description:
      - Set to V(present), if the config should exist, and V(absent), if it should not.
    type: str
    default: present
    choices:
      - absent
      - present
  template_driver:
    description:
      - Set to V(golang) to use a Go template in O(data) or a Go template file in O(data_src).
    type: str
    choices:
      - golang
    version_added: 2.5.0

requirements:
  - "L(Docker SDK for Python,https://docker-py.readthedocs.io/en/stable/) >= 2.6.0"
  - "Docker API >= 1.30"

author:
  - Chris Houseknecht (@chouseknecht)
  - John Hu (@ushuz)
a  
---
- name: Create config foo (from a file on the control machine)
  community.docker.docker_config:
    name: foo
    # If the file is JSON or binary, Ansible might modify it (because
    # it is first decoded and later re-encoded). Base64-encoding the
    # file directly after reading it prevents this to happen.
    data: "{{ lookup('file', '/path/to/config/file') | b64encode }}"
    data_is_b64: true
    state: present

- name: Create config foo (from a file on the target machine)
  community.docker.docker_config:
    name: foo
    data_src: /path/to/config/file
    state: present

- name: Change the config data
  community.docker.docker_config:
    name: foo
    data: Goodnight everyone!
    labels:
      bar: baz
      one: '1'
    state: present

- name: Add a new label
  community.docker.docker_config:
    name: foo
    data: Goodnight everyone!
    labels:
      bar: baz
      one: '1'
      # Adding a new label will cause a remove/create of the config
      two: '2'
    state: present

- name: No change
  community.docker.docker_config:
    name: foo
    data: Goodnight everyone!
    labels:
      bar: baz
      one: '1'
      # Even though 'two' is missing, there is no change to the existing config
    state: present

- name: Update an existing label
  community.docker.docker_config:
    name: foo
    data: Goodnight everyone!
    labels:
      bar: monkey # Changing a label will cause a remove/create of the config
      one: '1'
    state: present

- name: Force the (re-)creation of the config
  community.docker.docker_config:
    name: foo
    data: Goodnight everyone!
    force: true
    state: present

- name: Remove config foo
  community.docker.docker_config:
    name: foo
    state: absent
a\  
config_id:
  description:
    - The ID assigned by Docker to the config object.
  returned: success and O(state=present)
  type: str
  sample: 'hzehrmyjigmcp2gb6nlhmjqcv'
config_name:
  description:
    - The name of the created config object.
  returned: success and O(state=present)
  type: str
  sample: 'awesome_config'
  version_added: 2.2.0
N)DockerExceptionAPIError)AnsibleDockerClientRequestException)DockerBaseClasscompare_genericsanitize_labels)	to_nativeto_bytesc                   N     e Zd Z fdZd Zd Zd Zd Zd Zd Z	d Z
d	 Z xZS )
ConfigManagerc           	      @   t         t        |           || _        || _        | j                  j
                  | _        | j                  j                  j                  }|j                  d      | _	        |j                  d      | _
        |j                  d      | _        | j                  P|j                  d      r%t        j                  | j                        | _        nt        | j                        | _        |j                  d      }|+	 t        |d      5 }|j!                         | _        d d d        |j                  d	      | _        |j                  d
      | _        |j                  d      | _        |j                  d      | _        |j                  d      | _        | j.                  rd| _        d | _        g | _        y # 1 sw Y   xY w# t"        $ r?}| j                  j%                  dj'                  |t)        |                   Y d }~d }~ww xY w)Nnamestatedatadata_is_b64data_srcrbz"Error while reading {src}: {error})srcerrorlabelsforcerolling_versionsversions_to_keeptemplate_driverr   )superr   __init__clientresults
check_modemoduleparamsgetr   r   r   base64	b64decoder   openread	Exceptionfailformatr   r   r   r   r   r   versiondata_keyconfigs)selfr!   r"   
parametersr   fexc	__class__s          r/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/docker/plugins/modules/docker_config.pyr    zConfigManager.__init__   s   mT+-++00[[''..
NN6*	^^G,
NN6*	99 ~~m,",,TYY7	$TYY/	>>*-r(D) )Q !DI) !nnX.^^G,
 */A B */A B)~~.?@  DL) ) r  !E!L!LQYajknao!L!pqqrs0   G G	1G 	GG 	H5HHc                 (   | j                          | j                  dk(  rSt        j                  | j                        j                         | _        | j                          | j                          y | j                  dk(  r| j                          y y )Npresentabsent)

get_configr   hashlibsha224r   	hexdigestr/   r8   remove_old_versionsr9   r1   s    r6   __call__zConfigManager.__call__  sd    ::"#NN4995??ADMLLN$$&ZZ8#KKM $    c                     	 t        |j                  di       j                  di       j                  dd            S # t        $ r Y yw xY w)NSpecLabelsansible_versionr   )intr&   
ValueErrorr1   configs     r6   get_versionzConfigManager.get_version  sK    	vzz&"-11(B?CCDUWXYZZ 		s   := 	A	A	c                 ^   | j                   r| j                  dk  ry | j                  st        | j                        t        | j                  d      kD  rX| j                  | j                  j                  d             t        | j                        t        | j                  d      kD  rWy y y )Nr      )r   r   r#   lenr0   maxremove_configpopr?   s    r6   r>   z!ConfigManager.remove_old_versions  s    $$(=(=(Adll#c$*?*?&CC""4<<#3#3A#67 dll#c$*?*?&CC rA   c           	      F   	 | j                   j                  d| j                  i      }| j                  rmD cg c]5  }|d   d   j                  dj                  | j                  	            r|7 c}| _        | j                  j                  | j                  
       yD cg c]  }|d   d   | j                  k(  s| c}| _        y# t        $ r>}| j                   j	                  d| j                  dt        |             Y d}~d}~ww xY wc c}w c c}w )z Find an existing config. r   filterszError accessing config : NrC   Namez{name}_v)r   )key)r!   r0   r   r   r,   r   r   
startswithr-   sortrJ   )r1   r0   r4   rI   s       r6   r:   zConfigManager.get_config  s   	\kk))64992E)FG    &&>&)44Z5F5FDII5F5VW DL
 LL$"2"23 &-!vv0F$))0SDL  	\KK		9UX>Z[[	\s(   (C :D'DD	D4DDc                    d}d| j                   i}| j                  rY| xj                  dz  c_        t        | j                        |d<   dj	                  | j
                  | j                        | _        | j                  r|j                  | j                         	 | j                  si }| j                  rd| j                  i|d<    | j                  j                  | j
                  | j                  fd	|i|}| xj                  | j                  j                  d
|i      z  c_        t#        |t$              r|d   }|S # t        $ r1}| j                  j                  dt!        |      z         Y d}~Ld}~ww xY w)z Create a new config Nansible_keyrL   rE   z{name}_v{version})r   r.   r   
templatingr   idrR   zError creating config: %sID)r/   r   r.   strr-   r   r   updater#   r   r!   create_configr   r0   r   r,   r   
isinstancedict)r1   	config_idr   kwargsr4   s        r6   r`   zConfigManager.create_config+  sO   	 4==
   LLAL(+DLL(9F$%+22		4<<2XDI;;MM$++&	K??'' 4 4,F<( 6DKK55diidSYd]cd	 3 3T9<M 3 NN i&!$I  	KKK89S>IJJ	Ks   BE 	E?'E::E?c           	          	 | j                   s| j                  j                  |d          y y # t        $ r:}| j                  j	                  d|d   d   dt        |             Y d }~y d }~ww xY w)Nr]   zError removing config rC   rU   rT   )r#   r!   rO   r   r,   r   )r1   rI   r4   s      r6   rO   zConfigManager.remove_configK  sf    	h??))&,7 # 	hKKvf~f?UW`adWefgg	hs   *. 	A10A,,A1c                 $   | j                   r| j                   d   }|d   | j                  d<   |d   d   | j                  d<   d}d}|j                  di       }|j                  di       j                  d	      r|d   d	   | j                  k7  r4d
}n1| j                  s%| j
                  j                  j                  d       |j                  di       j                  d      r|d   d   | j                  k7  rd
}n| j                  rd
}t        | j                  |j                  d      dd       }| j                  r| j                  |      | _        |s|s|s| j                  rd| j                  s| j                          | j                         }d
| j                  d<   || j                  d<   | j                   | j                  d<   yyd
| j                  d<   | j                         | j                  d<   | j                   | j                  d<   y)z= Handles state == 'present', creating or updating the config r]   rc   rC   rU   config_nameFrD   rZ   Tze'ansible_key' label not found. Config will not be changed unless the force parameter is set to 'true'
Templatingallow_more_presentrb   changedN)r0   r"   r&   r/   r   r!   r$   warnr   r   r   r   rJ   r.   r9   r`   r   )r1   rI   data_changedtemplate_driver_changedattrslabels_changedrc   s          r6   r8   zConfigManager.presentR  s   <<\\"%F(.tDLL%*0.*@DLL' L&+#JJvr*Eyy2&**=9?=1T]]B#'LzzKK&&++  -T  U yyr*..v6&v.$2F2FF.2+%%*.'!0eii>QSgio!ppN$$#//76.DJJ,,KKM ..0	*.Y',5[).2ii]+ MW '+DLL#(,(:(:(<DLL%*.))DLL'rA   c                     | j                   r2| j                   D ]  }| j                  |        d| j                  d<   yy)z0 Handles state == 'absent', removing the config Trk   N)r0   rO   r"   rH   s     r6   r9   zConfigManager.absentx  s=    <<,, +""6*+&*DLL# rA   )__name__
__module____qualname__r    r@   rJ   r>   r:   r`   rO   r8   r9   __classcell__)r5   s   @r6   r   r      s4    !F8&@h$4L+rA   r   c                  t   t        t        dd      t        ddddg      t        d      t        dd	
      t        d      t        d      t        dd	
      t        dd	
      t        dd
      t        ddg      
      } ddddgdfg}dg}t        t        dd            }t        | d||dd|      }t        |j                  j                  d   d|       	 t        d	      } t        ||               |j                  j                  d"i | y # t        $ rG}|j                  dj                  t        |            t        j                                 Y d }~y d }~wt        $ rG}|j                  d!j                  t        |            t        j                                 Y d }~y d }~ww xY w)#Nr^   T)typerequiredr8   r9   )rw   defaultchoices)rw   boolF)rw   ry   pathrb   rF      golang)rw   rz   )
r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   z5.0.3z1.37)docker_py_versiondocker_api_version)r   z2.6.0z1.30)argument_specsupports_check_moderequired_ifmutually_exclusivemin_docker_versionmin_docker_api_versionoption_minimal_versionsr   )rk   z(An unexpected docker error occurred: {0})	exceptionzhAn unexpected requests error occurred when Docker SDK for Python tried to talk to the docker daemon: {0} )rb   r   r   r$   r%   r   	exit_jsonr   r,   r-   r   	traceback
format_excr	   )r   r   r   r   r!   r"   es          r6   mainr     s   ut,y8Y:OPufe46" .6595!4%(<M 
)fj148K
 	 #w6R !# -"% 7F FMM((2HfE.
 	'fg&(*'* w>EEiPQlS_h_s_s_uvv .v}}  H  IJ  K  L**, 	 	. 	..s$   9D 	F7"=E$$F70=F22F7__main__)
__future__r   r   r   rw   __metaclass__DOCUMENTATIONEXAMPLESRETURNr'   r;   r   docker.errorsr   r   ImportError@ansible_collections.community.docker.plugins.module_utils.commonr   r	   >ansible_collections.community.docker.plugins.module_utils.utilr
   r   r   +ansible.module_utils.common.text.convertersr   r   r   r   rr   r   rA   r6   <module>r      s    A @dLDL
    	7
 
 L`+O `+F1.h zF O  		s   A A&%A&