
    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c mc mc mc mZ  G d d	e      Zd
 Zedk(  r e        yy)    )absolute_importdivisionprint_functionaK  
---
module: zabbix_token

short_description: Create/Update/Generate/Delete Zabbix token.

description:
    - This module allows you to create, update, generate and delete Zabbix token.

author:
    - ONODERA Masaru(@masa-orca)

requirements:
    - "python >= 3.9"

version_added: 2.1.0

options:
    name:
        description:
            - Name of the token.
        required: true
        type: str
    description:
        description:
            - Description of the token.
        required: false
        type: str
    username:
        description:
            - Name of user who is the token assinged to.
        required: true
        type: str
    status:
        description:
            - Status of the token.
        required: false
        type: bool
    expires_at:
        description:
            - A timestamp of the token will be expired.
            - The token will never expire if C(0)
        required: false
        type: int
    generate_token:
        description:
            - New token string will be generated if C(true).
        required: false
        type: bool
        default: false
    state:
        description:
            - Create or delete token.
        type: str
        default: present
        choices:
            - present
            - absent

extends_documentation_fragment:
    - community.zabbix.zabbix
a  
# If you want to use Username and Password to be authenticated by Zabbix Server
- name: Set credentials to access Zabbix Server API
  ansible.builtin.set_fact:
    ansible_user: Admin
    ansible_httpapi_pass: zabbix

# If you want to use API token to be authenticated by Zabbix Server
# https://www.zabbix.com/documentation/current/en/manual/web_interface/frontend_sections/administration/general#api-tokens
- name: Set API token
  ansible.builtin.set_fact:
    ansible_zabbix_auth_key: 8ec0d52432c15c91fcafe9888500cf9a607f44091ab554dbee860f6b44fac895

- name: Create Zabbix token and generate token string
  # set task level variables as we change ansible_connection plugin here
  vars:
    ansible_network_os: community.zabbix.zabbix
    ansible_connection: httpapi
    ansible_httpapi_port: 443
    ansible_httpapi_use_ssl: true
    ansible_httpapi_validate_certs: false
    ansible_zabbix_url_path: "zabbixeu"  # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu
    ansible_host: zabbix-example-fqdn.org
  community.zabbix.zabbix_token:
    name: test token
    description: Admin test token
    username: Admin
    status: true
    expires_at: 1700000000
    generate_token: true
    state: present
a1  
msg:
    description: The result of the operation
    returned: success
    type: str
    sample: "Successfully created token"
token:
    description: Generated token string
    returned: I(generate_token=true)
    type: str
    sample: "8ec0d52432c15c91fcafe9888500cf9a607f44091ab554dbee860f6b44fac895"
)AnsibleModule)
ZabbixBaseNc                   *    e Zd Zd Zd Zd Zd Zd Zy)Tokenc                 .   	 | j                   j                  j                  dd|id      }|rt        |      dkD  r| j                  j                  d|z         |d   d   S # t        $ r)}| j                  j                  d|z         Y d }~y d }~ww xY w)	Nuseridusername)outputfilter   zUser '%s' cannot be foundr   zFailed to get userid: %smsg)_zapiusergetlen_module	fail_json	Exception)selfr   useridses       q/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/zabbix/plugins/modules/zabbix_token.pyget_userid_from_namezToken.get_userid_from_name   s    	Gjjoo))#
H/EFG c'lQ.&&'BX'MN1:h'' 	GLL""'AA'E"FF	Gs   AA" "	B+BBc                     	 | j                   j                  j                  d|gd|id      S # t        $ r)}| j                  j                  d|z         Y d }~y d }~ww xY w)Nextendname)r   r   r   zFailed to get token: %sr   )r   tokenr   r   r   r   )r   r    r   r   s       r   	get_tokenzToken.get_token   sg    	F::##''#VTNS   	FLL""'@1'D"EE	Fs   +. 	A AA c                    	 i }||d<   t        |t              r||d<   ||d<   t        |t              r|rd|d<   nd|d<   t        |t              rt        |      |d<   | j                  j                  r| j                  j                  d	       | j                  j                  j                  |      }|rM| j                  j                  j                  |d
         }	| j                  j                  dd|	d   d          y | j                  j                  dd       y # t        $ r)}
| j                  j                  d|
z         Y d }
~
y d }
~
ww xY w)Nr    descriptionr   1status0
expires_atTchangedtokenidszSuccessfully created token.r   r!   r*   r   r!   r*   r   zFailed to create token: %sr   )
isinstancestrboolr   
check_mode	exit_jsonr   r!   creategenerater   r   )r   r    r$   r   r&   r(   generate_tokenparamsresultgenerated_tokensr   s              r   create_tokenzToken.create_token   s@    	IF!F6N+s+(3}%%F8&$''*F8$'*F8$*c*'*:|$||&&&&t&4ZZ%%,,V4F#'::#3#3#<#<VJ=O#P && 5*1-g6 '  &&t9V&W 	ILL""'Ca'G"HH	Is   DD" D" "	E+EEc                    	 i }|d   |d<   ||d<   t        |t              r||d   k7  r||d<   t        |t              r|r|d   dk7  rd|d<   n|d   dk7  rd|d<   t        |t              rt        |      |d   k7  rt        |      |d<   t	        |j                               dk(  rR|s| j                  j                  d	
       n| j                  j                  rt| j                  j                  d
       nW| j                  j                  r| j                  j                  d
       | j                  j                  j                  |       |rN| j                  j                  j                  |d   g      }| j                  j                  dd|d   d          y | j                  j                  dd       y # t        $ r)}	| j                  j                  d|	z         Y d }	~	y d }	~	ww xY w)Ntokenidr    r$   r&   r'   r%   r(      Fr)   TzSuccessfully updated token.r   r!   r,   r-   zFailed to update token: %sr   )r.   r/   r0   intr   keysr   r2   r1   r   r!   updater4   r   r   )
r   r!   r    r$   r&   r(   r5   r6   r8   r   s
             r   update_tokenzToken.update_token   s   (	IF %i 0F9!F6N+s+u]?S0S(3}%&$'X#-+.x(X#-+.x(*c*s:%BU/U'*:|$ 6;;=!Q&%LL**5*9\\,,LL**4*8<<**LL**4*8

  ''/#'::#3#3#<#<eI>N=O#P && 5*1-g6 '  &&t9V&W 	ILL""'Ca'G"HH	Is   FF= F= =	G/G**G/c                 d   	 |d   }| j                   j                  r| j                   j                  d       | j                  j                  j                  |g       | j                   j                  dd       y # t        $ r)}| j                   j                  d|z         Y d }~y d }~ww xY w)Nr;   Tr)   zSuccessfully deleted token.r-   zFailed to delete token: %sr   )r   r1   r2   r   r!   deleter   r   )r   r!   r;   r   s       r   delete_tokenzToken.delete_token   s    	II&G||&&&&t&4JJ##WI.LL""45R"S 	ILL""'Ca'G"HH	Is   A:A= =	B/B**B/N)__name__
__module____qualname__r   r"   r9   r@   rC        r   r	   r	      s"    	GF#IJ+I\IrH   r	   c                  B   t        j                         } | j                  t        t        dd      t        d      t        dd      t        d      t        d      t        dd      t        dd	d
gd	                   t	        | d      }|j
                  d   }|j
                  d   }|j
                  d   }|j
                  d   }|j
                  d   }|j
                  d   }|j
                  d   }t        |      }	|	j                  |      }
|	j                  ||
      }|d
k(  r6t        |      dk(  r|	j                  |d          y |j                  d       y t        |      dk(  r|	j                  |d   |||||       y |	j                  |||
|||       y )Nr/   T)typerequired)rJ   r0   r=   F)rJ   defaultpresentabsent)rJ   choicesrL   )r    r$   r   r&   r(   r5   state)argument_specsupports_check_moder    r$   r   r&   r(   r5   rP   r   r   r)   )zabbix_utilszabbix_common_argument_specr?   dictr   r6   r	   r   r"   r   rC   r2   r@   r9   )rQ   moduler    r$   r   r&   r(   r5   rP   token_class_objr   tokenss               r   mainrY      s    <<>M540%(ut4V$'VU;EIx+@)T	

 # F
 == D--.K}}Z(H]]8$F|,J]]#34NMM'"EFmO11(;F&&tV4Fv;!((3U+v;!((q	4fj. ((k66:~rH   __main__)
__future__r   r   r   rJ   __metaclass__DOCUMENTATIONEXAMPLESRETURNansible.module_utils.basicr   >ansible_collections.community.zabbix.plugins.module_utils.baser   Aansible_collections.community.zabbix.plugins.module_utils.helpers	communityzabbixpluginsmodule_utilshelpersrS   r	   rY   rD   rG   rH   r   <module>rh      sf    A @=~B
 5 U X X XpIJ pIf*Z zF rH   