
    Vh3                         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mZ d dlmZmZ d d	lmZ d d
lmZmZmZ  G d de      Zd Zd Z G d de      Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functiona$  
module: docker_plugin
short_description: Manage Docker plugins
version_added: 1.3.0
description:
  - This module allows to install, delete, enable and disable Docker plugins.
  - Performs largely the same function as the C(docker plugin) CLI subcommand.
notes:
  - The C(--grant-all-permissions) CLI flag is true by default in this module.
extends_documentation_fragment:
  - community.docker.docker.api_documentation
  - community.docker.attributes
  - community.docker.attributes.actiongroup_docker

attributes:
  check_mode:
    support: full
  diff_mode:
    support: full
  idempotent:
    support: full

options:
  plugin_name:
    description:
      - Name of the plugin to operate on.
    required: true
    type: str

  state:
    description:
      - V(absent) remove the plugin.
      - V(present) install the plugin, if it does not already exist.
      - V(enable) enable the plugin.
      - V(disable) disable the plugin.
    default: present
    choices:
      - absent
      - present
      - enable
      - disable
    type: str

  alias:
    description:
      - Local name for plugin.
    type: str
    version_added: 1.8.0

  plugin_options:
    description:
      - Dictionary of plugin settings.
    type: dict
    default: {}

  force_remove:
    description:
      - Remove even if the plugin is enabled.
    default: false
    type: bool

  enable_timeout:
    description:
      - Timeout in seconds.
    type: int
    default: 0

author:
  - Sakar Mehra (@sakar97)
  - Vladimir Porshkevich (@porshkevich)

requirements:
  - "Docker API >= 1.25"
a  
---
- name: Install a plugin
  community.docker.docker_plugin:
    plugin_name: plugin_one
    state: present

- name: Remove a plugin
  community.docker.docker_plugin:
    plugin_name: plugin_one
    state: absent

- name: Enable the plugin
  community.docker.docker_plugin:
    plugin_name: plugin_one
    state: enable

- name: Disable the plugin
  community.docker.docker_plugin:
    plugin_name: plugin_one
    state: disable

- name: Install a plugin with options
  community.docker.docker_plugin:
    plugin_name: weaveworks/net-plugin:latest_release
    plugin_options:
      IPALLOC_RANGE: "10.32.0.0/12"
      WEAVE_PASSWORD: "PASSWORD"
a  
plugin:
  description:
    - Plugin inspection results for the affected plugin.
  returned: success
  type: dict
  sample: {}
actions:
  description:
    - List of actions performed during task execution.
  returned: when O(state) is not V(absent)
  type: list
N)	to_native)AnsibleDockerClientRequestException)DockerBaseClassDifferenceTracker)auth)APIErrorDockerExceptionNotFoundc                        e Zd Z fdZ xZS )TaskParametersc                     t         t        |           || _        d | _        d | _        d | _        d | _        d | _        d | _	        |j                  j                  j                         D ]  \  }}t        | ||        y N)superr   __init__clientplugin_namealiasplugin_optionsdebugforce_removeenable_timeoutmoduleparamsitemssetattr)selfr   keyvalue	__class__s       r/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/docker/plugins/modules/docker_plugin.pyr   zTaskParameters.__init__   sw    nd,.
"
 " --..446 	&JCD#u%	&    )__name__
__module____qualname__r   __classcell__)r#   s   @r$   r   r      s    & &r%   r   c                 n    | r,| j                         D cg c]  \  }}|d||nd c}}S g S c c}}w )N= )r   )optionskvs      r$   prepare_optionsr0      s6    V]'--/R$!Qqq}!"45ReceeRs   1c                 .    | rt        d | D              S i S )Nc              3   @   K   | ]  }|j                  d d        yw)r+      N)split).0xs     r$   	<genexpr>z parse_options.<locals>.<genexpr>   s     6AQ6s   )dict)options_lists    r$   parse_optionsr:      s    :F4666NBNr%   c                   X    e 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        Zy)DockerPluginManagerc                 z   || _         t        |      | _        | j                  j                  xs | j                  j                  | _        | j                   j                  | _        | j                   j                  j                  | _	        t               | _        t               | _        g | _        d| _        | j!                         | _        | j                  j$                  }|dk(  r| j'                          nA|dk(  r| j)                          n+|dk(  r| j+                          n|dk(  r| j-                          | j                  s"| j                  s| j                  j.                  rU| j                  r7| j                  j1                         \  | j                  d<   | j                  d<   | j                  | _	        y y )NFpresentabsentenabledisablebeforeafter)r   r   
parametersr   r   preferred_name
check_moder   _diffdiffr
   diff_trackerr8   diff_resultactionschangedget_existing_pluginexisting_pluginstater>   r?   r@   rA   r   get_before_after)r    r   rO   s      r$   r   zDockerPluginManager.__init__   sG   (0"oo33Rt7R7R++00KK&&,,	-/6#779%%ILLNhKKMhKKMiLLN994??+@+@yyHLHYHYHjHjHlE  *D,<,<W,E((DI ,Ar%   c                     	 | j                   j                  d| j                        S # t        $ r Y y t        $ r.}| j                   j                  t        |             Y d }~y d }~ww xY w)N/plugins/{0}/json)r   get_jsonrE   r   r   failr   r    es     r$   rM   z'DockerPluginManager.get_existing_plugin   sY    	+;;''(;T=P=PQQ 	 	+KKYq\**	+s   %( 	A)A)$A$$A)c                    t               }| j                  j                  r| j                  j	                  d      }|s*|j                  d| j                  j                  |       |S t        |j	                  d            }| j                  j                  j                         D ]I  \  }}|j	                  |      s|s|r	|||   k7  s$|j                  d|z  ||j	                  |             K |S )z
        Return the list of differences between the current parameters and the existing plugin.

        :return: list of options that differ
        Settingsr   )rD   activeEnvzplugin_options.%s	parameterrY   )r
   rD   r   rN   getaddr:   r   )r    differencessettingsexisting_optionsr!   r"   s         r$   has_different_configz(DockerPluginManager.has_different_config   s     ()??))++//
;H 0T__=[=[dlm  $1e1D#E "&//"@"@"F"F"H JJC-11#65 %!%5c%::#(;c(A27/?/C/CC/H ( J	J r%   c                    | j                   s| j                  s	 i }t        j                  | j                  j
                        \  }}t        j                  | j                  |      }|r||d<   | j                  j                  dd| j                  j
                  i|      }d| j                  j
                  i}| j                  j                  r| j                  j                  |d<   | j                  j                  | j                  j                  d      |||d      }| j                  j                  |       | j                  j                  |d	      D ]  } | j                  j                  d
| j                        | _         | j                  j                  rGt!        | j                  j                        }| j                  j#                  d| j                  |       | j*                  j-                  d| j                  z         d| _        y y # t$        $ r.}	| j                  j'                  t)        |	             Y d }	~	cd }	~	ww xY w)NzX-Registry-Authz/plugins/privilegesremote)r   headersnamez/plugins/pullT)r   re   datastream)decoderR   /plugins/{0}/setrg   zInstalled plugin %s)rN   rF   r   resolve_repository_namerD   r   get_config_headerr   rS   r   
_post_json_url_raise_for_status_stream_helperrE   r   r0   	post_jsonr   rT   r   rK   appendrL   )
r    re   registry	repo_nameheader
privilegesr   responserg   rV   s
             r$   install_pluginz"DockerPluginManager.install_plugin   s   ##??3 G*.*F*FtGbGb*c'Hi!33DKKJF5; 12!%!5!56KU]_c_n_n_z_zT{  FM!5  "NJ !$//"="=F ,,)-)>)>v#{{55dkk6F6F6W`fpw  I  RV5   WHKK11(; $ : :8D : Q  ,0;;+?+?@SUYUhUh+iD(55.t/M/MN--.@$BUBU\`-a LL 58K8K KLDL; $2   3KK$$Yq\223s   GH 	I$$IIc                    | j                   j                  }| j                  rg| j                  s+	 | j                  j                  d| j                  d|i       | j                  j                  d| j                  z         d| _        y y # t        $ r.}| j                  j                  t        |             Y d }~cd }~ww xY w)Nz/plugins/{0}forcer   zRemoved plugin %sT)rD   r   rN   rF   r   delete_callrE   r   rT   r   rK   rs   rL   )r    r{   rV   s      r$   remove_pluginz!DockerPluginManager.remove_plugin  s    ,,??3KK++ND<O<OY`bgXh+i LL 3d6I6I IJDL     3KK$$Yq\223s   *B 	C$B==Cc                    | j                   r| j                         }|j                  s| j                  sH	 t	        | j
                  j                        }| j                  j                  d| j                  |       | j                  j                  d| j                  z         d| _        y y | j                  j                  d       y # t        $ r.}| j                  j                  t        |             Y d }~d }~ww xY w)Nrj   rk   zUpdated plugin %s settingsTz/Cannot update the plugin: Plugin does not exist)rN   rb   emptyrF   r0   rD   r   r   rr   rE   r   rT   r   rK   rs   rL   )r    r_   rg   rV   s       r$   update_pluginz!DockerPluginManager.update_plugin  s    335K$$7.t/M/MN--.@$BUBU\`-a ##$@4CVCV$VW# % KKNO $ 7((1667s   AC
 
	D$C<<Dc                    t               }| j                  r| j                         }| j                  j	                  dd| j                  d u       | j                  r| j                          n| j                          | j                  s"| j                  s| j                  j                  r| j                  j                  |       | j                  s| j                  j                  sd | _        y y y )NexistsTr[   )r
   rN   rb   rI   r^   r   ry   rH   rF   rD   r   mergerK   )r    r_   s     r$   r>   zDockerPluginManager.present(  s    ')335Kh$t?S?S[_?_` !994??+@+@##K0t'<'<DL (=r%   c                 $    | j                          y r   )r~   )r    s    r$   r?   zDockerPluginManager.absent:  s    r%   c                 "   | j                   j                  }| j                  r| j                  j                  d      sg| j                  s+	 | j
                  j                  d| j                  d|i       | j                  j                  d| j                  z         d| _        y y | j                          | j                  s+	 | j
                  j                  d| j                  d|i       | j                  j                  d| j                  z         d| _        y # t        $ r.}| j
                  j                  t        |             Y d }~d }~ww xY w# t        $ r.}| j
                  j                  t        |             Y d }~d }~ww xY w)NEnabledz/plugins/{0}/enabletimeoutr|   zEnabled plugin %sT)rD   r   rN   r]   rF   r   rr   rE   r   rT   r   rK   rs   rL   ry   )r    r   rV   s      r$   r@   zDockerPluginManager.enable=  sJ   //00''++I67--.CTEXEXbkmtau-v ##$7$:M:M$MN# 7 !??3KK))*?ATAT^gip]q)r LL 3d6I6I IJDL $ 7((1667   3KK$$Yq\223s0   *D *E 	E&$EE	F $F		Fc                    | j                   r| j                   j                  d      rc| j                  s'	 | j                  j	                  d| j
                         | j                  j                  d| j
                  z         d| _        y y | j                  j                  d       y # t        $ r.}| j                  j                  t        |             Y d }~d }~ww xY w)Nr   z/plugins/{0}/disablezDisable plugin %sTz(Plugin not found: Plugin does not exist.)rN   r]   rF   r   rr   rE   r   rT   r   rK   rs   rL   rU   s     r$   rA   zDockerPluginManager.disableR  s    ##''	27--.DdFYFYZ ##$7$:M:M$MN# 3 KKGH $ 7((1667s   &B( (	C1$CCc                 .   i }| j                   j                  dk7  r'	 | j                  j                  d| j                        }| j                  | j                  | j                  |d}t        d |j                         D              S # t
        $ r Y Pw xY w)Nr?   rR   )rK   rL   rH   pluginc              3   0   K   | ]  \  }}|	||f  y wr    )r5   r.   r/   s      r$   r7   z-DockerPluginManager.result.<locals>.<genexpr>n  s     Gtq!QFGs   
	)rD   rO   r   rS   rE   r   rK   rL   rH   r8   r   )r    plugin_dataresults      r$   r   zDockerPluginManager.result_  s    ??  H,"kk223FH[H[\
 ||||II!	
 Gv||~GGG  s   &B 	BBN)r&   r'   r(   r   rM   rb   ry   r~   r   r>   r?   r@   rA   propertyr   r   r%   r$   r<   r<      sM    )<+0 @
 P $ *I H Hr%   r<   c                     t        t        d      t        dd      t        ddg d      t        di 	      t        d
d	      t        d
d	      t        dd	            } t        | d      }	 t        |      } |j                  j                  di |j
                   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)Nstr)typeT)r   requiredr>   )r>   r?   r@   rA   )r   defaultchoicesr8   )r   r   boolFintr   )r   r   rO   r   r   r   r   )argument_specsupports_check_modez(An unexpected docker error occurred: {0})	exceptionzSAn unexpected requests error occurred when trying to talk to the Docker daemon: {0}r   )r8   r   r<   r   	exit_jsonr   r   rT   formatr   	traceback
format_excr   )r   r   cmrV   s       r$   mainr   q  s   ed3y:de4.vu52M !# F
. (,")), w>EEiPQlS_h_s_s_uvv .ahhirstiuv**, 	 	. 	..s$   01B" "	E +=C--E 9=D;;E __main__)
__future__r   r   r   r   __metaclass__DOCUMENTATIONEXAMPLESRETURNr   +ansible.module_utils.common.text.convertersr   Dansible_collections.community.docker.plugins.module_utils.common_apir   r   >ansible_collections.community.docker.plugins.module_utils.utilr	   r
   >ansible_collections.community.docker.plugins.module_utils._apir   Eansible_collections.community.docker.plugins.module_utils._api.errorsr   r   r   r   r0   r:   objectr<   r   r&   r   r%   r$   <module>r      s    A @IV<
  A

 P u u&_ &fOAH& AHH.4 zF r%   