
    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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_functionaR  
---
module: zabbix_regexp

short_description: Create/update/delete Zabbix regular expression


description:
    - This module allows you to create, update and delete Zabbix regular expression.

author:
    - ONODERA Masaru(@masa-orca)

requirements:
    - "python >= 3.9"

version_added: 2.1.0

options:
    name:
        description:
            - Name of this regular expression
        type: str
        required: true
    test_string:
        description:
            - A test string for this regular expression
        type: str
    expressions:
        description:
            - List of expressions.
            - The regular expression returns true when all expressions return true.
            - Required when C(state=present).
        type: list
        elements: dict
        suboptions:
            expression:
                description:
                    - A expression string
                type: str
                required: true
            expression_type:
                description:
                    - A expression string
                type: str
                required: true
                choices:
                    - "character_string_included"
                    - "any_character_string_included"
                    - "character_string_not_included"
                    - "result_is_true"
                    - "result_is_false"
            exp_delimiter:
                description:
                    - Delimiter for expression.
                    - Used if expression_type is C(any_character_string_included).
                    - Default values is C(,)
                type: str
                choices: [",", ".", "/"]
            case_sensitive:
                description:
                    - If true, the expression will be case sensitive.
                type: bool
                default: false
    state:
        description:
            - State of the regular expression.
        type: str
        choices: ['present', 'absent']
        default: 'present'


notes:
    - Only Zabbix >= 6.0 is supported.

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: Update regexp of 'File systems for discovery'
  # 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_regexp:
    name: File systems for discovery
    test_string: ext2
    expressions:
      - expression: "^(btrfs|ext2|ext3|ext4|reiser|xfs|ffs|ufs|jfs|jfs2|vxfs|hfs|apfs|refs|ntfs|fat32|zfs)$"
        expression_type: result_is_true
z
msg:
    description: The result of the operation
    returned: success
    type: str
    sample: 'Successfully updated regular expression setting'
)AnsibleModule)
ZabbixBase)LooseVersionNc                   >     e Zd Zd fd	Zd Zd Zd Zd Zd Z xZ	S )RegularExpressionc                     t         t        |   |||       t        | j                        t        d      k  r|j                  d       y y )Nz6.0z:This module doesn't support Zabbix versions lower than 6.0msg)superr
   __init__r   _zbx_api_version	fail_json)selfmodulezbxzapi_wrapper	__class__s       r/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/zabbix/plugins/modules/zabbix_regexp.pyr   zRegularExpression.__init__   sK    /\J--.e1DDP   E    c                    	 | j                   j                  j                  dg dd|id      }t        |      dk\  r| j                  j                  d       |S # t        $ r)}| j                  j                  d|z         Y d }~y d }~ww xY w)	Nextend
expressionexpression_typeexp_delimitercase_sensitivename)outputselectExpressionsfilter   zToo many regexps are matched.z,Failed to get regular expression setting: %sr   )_zapiregexpgetlen_moduler   	Exception)r   regexp_nameregexpses       r   get_regexpszRegularExpression.get_regexps   s    	jj''++&*  &{3	G 7|q &&'FGN 	LL""BQF #  	s   AA 	B#BBc                 d   	 |d   g}| j                   j                  r| j                   j                  d       | j                  j                  j                  |       | j                   j                  dd       y # t        $ r)}| j                   j                  d|z         Y d }~y d }~ww xY w)NregexpidTchangedz0Successfully deleted regular expression setting.r2   r   z/Failed to delete regular expression setting: %sr   )r)   
check_mode	exit_jsonr%   r&   deleter*   r   )r   r&   	parameterr-   s       r   delete_regexpzRegularExpression.delete_regexp   s    	
+,I||&&&&t&4JJ$$Y/LL"""T #   	LL""EI #  	s   A:A= =	B/B**B/c                 8   g d}g }|D ]  }i }|d   |d<   t        j                  ||d         }t        |      |d<   |d   dk(  r|d   r	|d   |d<   n&d|d<   n |d   r| j                  j	                  d       d}|d	   rd
}||d	<   |j                  |        |S )Ncharacter_string_includedany_character_string_includedcharacter_string_not_includedresult_is_trueresult_is_falser   r   r<   r   ,zhA value of exp_delimiter will be ignored because expression_type is not 'any_character_string_included'.0r   1)zabbix_utilshelper_to_numeric_valuestrr)   warnappend)r   expressionsexpression_type_valuesexpression_jsonsr   expression_jsonr   r   s           r   _convert_expressions_to_jsonz.RegularExpression._convert_expressions_to_json   s    "
 % 	5J O,6|,DOL)*BB&
3D(EO 25_1EO-.+,0OOo.7A/7ROO47:OO4O,!!~ !N*+!$0>O,-##O4-	5.  r   c                 ~   	 | j                   j                  r| j                   j                  d       | j                  j                  j                  ||| j                  |      d       | j                   j                  dd       y # t        $ r)}| j                   j                  d|z         Y d }~y d }~ww xY w)NTr1   )r    test_stringrH   z0Successfully created regular expression setting.r3   z/Failed to create regular expression setting: %sr   )	r)   r4   r5   r%   r&   createrL   r*   r   )r   r    rN   rH   r-   s        r   create_regexpzRegularExpression.create_regexp   s    	||&&&&t&4JJ$$ #.#'#D#D[#Q LL"""T #   	LL""EI #  	s   BB
 
	B<B77B<c                    	 g }|d   D ]6  }|d   dk7  rt        j                  |dg      d   }|j                  |       8 | j                  |      }g }t        j                  |||       |d   |k(  r3|d   |k(  r+t        |      dk(  r| j                  j                  d	
       y | j                  j                  r| j                  j                  d
       | j                  j                  j                  |d   |||d       | j                  j                  dd       y # t        $ r)}	| j                  j                  d|	z         Y d }	~	y d }	~	ww xY w)NrH   r   rB   r   )del_keysr   r    rN   Fr1   Tr0   )r0   r    rN   rH   z0Successfully updated regular expression setting.r3   z/Failed to update regular expression setting: %sr   )rC   helper_normalize_datarG   rL   helper_compare_listsr(   r)   r5   r4   r%   r&   updater*   r   )
r   current_regexpr    rN   rH   current_expressionsr   future_expressionsdiff_expressionsr-   s
             r   update_regexpzRegularExpression.update_regexp   ss   $	"$,]; 7
/0C7!-!C!C"o->""J $**:67 "&!B!B;!O!--#%79I v&$."=1[@()Q.&&u&5<<**LL**4*8

!!(($2:$> $'2'9	 && &X '   	LL""EI #  	s   B$D$ 'A<D$ $	E-EE)NN)
__name__
__module____qualname__r   r.   r8   rL   rP   rZ   __classcell__)r   s   @r   r
   r
      s"    ,! F&%r   r
   c                     t        j                         } | j                  t        t        dd      t        d      t        ddt        t        dd      t        ddg d      t        dg d	
      t        dd                  t        dddddg                   t	        | dddgggd      }|j
                  d   }|j
                  d   }|j
                  d   }|j
                  d   }t        |      }|j                  |      }|dk(  r6t        |      dk(  r|j                  |d          y|j                  d       yt        |      dk(  r|j                  |d   |||       y|j                  |||       y)zMain ansible module functionrE   T)typerequired)r`   listdictr:   )r`   ra   choices)r@   ./)r`   rd   boolF)r`   defaultr   )r`   elementsoptionspresentabsent)r`   ra   rh   rd   )r    rN   rH   staterm   rH   )argument_specrequired_ifsupports_check_moder    rN      r   r1   N)rC   zabbix_common_argument_specrU   rc   r   paramsr
   r.   r(   r8   r5   rZ   rP   )rn   r   r    rN   rH   rm   regexp_class_objr,   s           r   mainru     s    !<<>M540 #>$("!%!
% #'E?"K#'VU#C( !"H-	3	
!F #y=/:; F == D--.K--.KMM'"E(0**40Gw<1**71:6U+w<1**71:t[+V**4kJr   __main__)
__future__r   r   r   r`   __metaclass__DOCUMENTATIONEXAMPLESRETURNansible.module_utils.basicr   >ansible_collections.community.zabbix.plugins.module_utils.baser   #ansible.module_utils.compat.versionr   Aansible_collections.community.zabbix.plugins.module_utils.helpers	communityzabbixpluginsmodule_utilshelpersrC   r
   ru   r[    r   r   <module>r      sh    A @N`>
 5 U < X X XG
 GT>KB zF r   