
    Vhy                         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 G d de      Z G d de      Z G d de      Z G d de      Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functiona"  
---
module: zabbix_usergroup
short_description: Create/delete/update Zabbix user groups
description:
   - Create user groups if they do not exist.
   - Delete existing user groups if they exist and are empty.
   - Update existing user groups.
author:
    - "Tobias Birkefeld (@tcraxs)"
requirements:
    - "python >= 3.9"
options:
    name:
        description:
            - Name of the user group to create, update or delete.
        required: true
        type: str
        aliases: [ "user_group" ]
    gui_access:
        description:
            - Frontend authentication method of the users in the group.
            - "Possible values:"
            - default -  use the system default authentication method;
            - internal - use internal authentication;
            - LDAP - use LDAP authentication;
            - disable - disable access to the frontend.
        required: false
        type: str
        default: "default"
        choices: [ "default", "internal", "LDAP", "disable"]
    debug_mode:
        description:
            - Whether debug mode is enabled or disabled.
        required: false
        type: str
        default: "disabled"
        choices: [ "disabled", "enabled" ]
    status:
        description:
            - Whether the user group is enabled or disabled.
        required: false
        type: str
        default: "enabled"
        choices: [ "enabled", "disabled" ]
    rights:
        description:
            - Permissions to assign to the group
            - For <= Zabbix 6.0
        required: false
        type: list
        elements: dict
        suboptions:
            host_group:
                description:
                    - Name of the host group to add permission to.
                required: true
                type: str
            permission:
                description:
                    - Access level to the host group.
                required: true
                type: str
                choices: [ "denied", "read-only", "read-write" ]
    hostgroup_rights:
        description:
            - Host group permissions to assign to the user group
            - For => Zabbix 6.2
        required: false
        type: list
        elements: dict
        suboptions:
            host_group:
                description:
                    - Name of the host group to add permission to.
                required: true
                type: str
            permission:
                description:
                    - Access level to the host group.
                required: true
                type: str
                choices: [ "denied", "read-only", "read-write" ]
    templategroup_rights:
        description:
            - Template group permissions to assign to the user group
            - For => Zabbix 6.2
        required: false
        type: list
        elements: dict
        suboptions:
            template_group:
                description:
                    - Name of the template group to add permission to.
                required: true
                type: str
            permission:
                description:
                    - Access level to the templategroup.
                required: true
                type: str
                choices: [ "denied", "read-only", "read-write" ]
    tag_filters:
        description:
            - Tag based permissions to assign to the group
        required: false
        type: list
        elements: dict
        suboptions:
            host_group:
                description:
                    - Name of the host group to add permission to.
                required: true
                type: str
            tag:
                description:
                    - Tag name.
                required: false
                type: str
                default: ""
            value:
                description:
                    - Tag value.
                required: false
                type: str
                default: ""
    userdirectory:
        description:
            - Authentication user directory when gui_access set to LDAP or System default.
            - For => Zabbix 6.2
        required: false
        type: str
    state:
        description:
            - State of the user group.
            - On C(present), it will create if user group does not exist or update the user group if the associated data is different.
            - On C(absent) will remove a user group if it exists.
        required: false
        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

# Base create user group example
- name: Create user group
    # 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_usergroup:
    name: ACME
    userdirectory: LDAP infra 1
    state: present

# Base create user group with selected user directory for LDAP authentication
- name: Create user group
    # 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_usergroup:
    name: ACME
    userdirectory: LDAP infra 1
    state: present

# Base create user group with disabled gui access
- name: Create user group with disabled gui access
    # 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_usergroup:
    name: ACME
    gui_access: disable

# Base create user group with permissions for Zabbix <= 6.0
- name: Create user group with permissions
    # 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_usergroup:
    name: ACME
    rights:
        - host_group: Webserver
          permission: read-write
        - host_group: Databaseserver
          permission: read-only
    state: present

# Base create user group with permissions for Zabbix => 6.2
- name: Create user group with permissions
    # 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_usergroup:
    name: ACME
    hostgroup_rights:
        - host_group: Webserver
          permission: read-write
        - host_group: Databaseserver
          permission: read-only
    templategroup_rights:
        - template_group: Linux Templates
          permission: read-write
        - template_group: Templates
          permission: read-only
    state: present

# Base create user group with tag permissions
- name: Create user group with tag permissions
    # 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_usergroup:
    name: ACME
    tag_filters:
        - host_group: Webserver
          tag: Application
          value: Java
        - host_group: Discovered hosts
          tag: Service
          value: JIRA
    state: present

# Base delete user groups example
- name: Delete user groups
    # 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_usergroup:
    name: ACME
    state: absent
a  
state:
  description: User group state at the end of execution.
  returned: on success
  type: str
  sample: "present"
usergroup:
  description: User group name.
  returned: on success
  type: str
  sample: "ACME"
usrgrpid:
    description: User group id, if created, changed or deleted.
    returned: on success
    type: str
    sample: "42"
msg:
    description: The result of the operation
    returned: always
    type: str
    sample: "User group created: ACME, ID: 42"
)AnsibleModule)
ZabbixBase)LooseVersionNc                       e Zd ZdZd Zd Zy)RightszP
    Restructure the user defined rights to fit the Zabbix API requirements
    c                 0   	 | j                   j                  j                  dd|gid      }t        |      dk  r | j                  j                  d|z         y
|d   S # t        $ r,}| j                  j                  d|d	|       Y d
}~y
d
}~ww xY wzGet host group by host group name.

        Parameters:
            name: Name of the host group.

        Returns:
            host group matching host group name.
        extendnameoutputfilter   zHost group not found: %smsgr   zFailed to get host group '': N_zapi	hostgroupgetlen_module	fail_json	Exceptionselfr   
_hostgroupes       u/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/zabbix/plugins/modules/zabbix_usergroup.pyget_hostgroup_by_hostgroup_namez&Rights.get_hostgroup_by_hostgroup_nameO  s    		X--11#/?@J :"&&+E+L&M!!}$ 	XLL""dTU'V"WW	X   AA  A   	B)"BBc                     |g S g }|D ]^  }| j                  |j                  d            d   t        j                  g d|j                  d            d}|j	                  |       ` t        j
                  |      S )zConstruct the user defined rights to fit the Zabbix API requirements

        Parameters:
            _rights: rights to construct

        Returns:
            dict: user defined rights
        
host_groupgroupiddeniedN	read-only
read-write
permissionidr,   r#   r   zabbix_utilshelper_to_numeric_valueappendhelper_cleanup_datar   _rightsconstructed_datarightconstructed_rights        r"   construct_the_datazRights.construct_the_datac  s     ?I 		7E::599\;RS +BB?<AX	! ##$56		7 //0@AA    N__name__
__module____qualname____doc__r#   r9    r:   r"   r
   r
   J  s    X(Br:   r
   c                       e Zd ZdZd Zd Zy)HostgroupRightsz[
    Restructure the user defined host group rights to fit the Zabbix API requirements
    c                 0   	 | j                   j                  j                  dd|gid      }t        |      dk  r | j                  j                  d|z         y
|d   S # t        $ r,}| j                  j                  d|d	|       Y d
}~y
d
}~ww xY wr   r   r   s       r"   r#   z/HostgroupRights.get_hostgroup_by_hostgroup_name  s    
	X--11"!D6*3 J :"&&+E+L&M!!}$ 	XLL""dTU'V"WW	Xr$   c                     |g S g }|D ]^  }| j                  |j                  d            d   t        j                  g d|j                  d            d}|j	                  |       ` t        j
                  |      S )zConstruct the user defined host group rights to fit the Zabbix API requirements

        Parameters:
            _rights: rights to construct

        Returns:
            dict: user defined rights
        r&   r'   r(   r,   r-   r/   r4   s        r"   r9   z"HostgroupRights.construct_the_data  s     ?I 
	7E::599\;RST]^*BB D" $)99\#:	! ##$56
	7 //0@AAr:   Nr;   r@   r:   r"   rB   rB   |  s    X*Br:   rB   c                       e Zd ZdZd Zd Zy)TemplategroupRightsz_
    Restructure the user defined template group rights to fit the Zabbix API requirements
    c                 0   	 | j                   j                  j                  dd|gid      }t        |      dk  r | j                  j                  d|z         y
|d   S # t        $ r,}| j                  j                  d|d	|       Y d
}~y
d
}~ww xY w)zGet template group by template group name.

        Parameters:
            name: Name of the template group.

        Returns:
            template group matching template group name.
        r   r   r   r   zTemplate group not found: %sr   r   zFailed to get template group 'r   N)r   templategroupr   r   r   r   r   )r   r   _templategroupr!   s       r"   'get_templategroup_by_templategroup_namez;TemplategroupRights.get_templategroup_by_templategroup_name  s    
	\!ZZ5599"!D6*; N >"Q&&&+ID+P&Q%a(( 	\LL""RVXY'Z"[[	\r$   c                     |g S g }|D ]^  }| j                  |j                  d            d   t        j                  g d|j                  d            d}|j	                  |       ` t        j
                  |      S )zConstruct the user defined template rights to fit the Zabbix API requirements

        Parameters:
            _rights: rights to construct

        Returns:
            dict: user defined rights
        template_groupr'   r(   r,   r-   )rJ   r   r0   r1   r2   r3   r4   s        r"   r9   z&TemplategroupRights.construct_the_data  s     ?I 
	7EBB599M]C^_`ij*BB D" $)99\#:	! ##$56
	7 //0@AAr:   N)r<   r=   r>   r?   rJ   r9   r@   r:   r"   rF   rF     s    \*Br:   rF   c                       e Zd ZdZd Zy)
TagFilterszU
    Restructure the user defined tag_filters to fit the Zabbix API requirements
    c                     |g S g }|D ]X  }| j                  |j                  d            d   |j                  d      |j                  d      d}|j                  |       Z t        j                  |      S )zConstruct the user defined tag filters to fit the Zabbix API requirements

        Parameters:
            _tag_filters: tag filters to construct

        Returns:
            dict: user defined tag filters
        r&   r'   tagvalue)r'   rP   rQ   )r#   r   r2   r0   r3   )r   _tag_filtersr6   
tag_filterconstructed_tag_filters        r"   r9   zTagFilters.construct_the_data  s     I& 	<J??NN<0 "~~e,#0&" ##$:;	< //0@AAr:   N)r<   r=   r>   r?   r9   r@   r:   r"   rN   rN     s    Br:   rN   c                   6    e Zd Zd Zd Zd Zd Zd Zd Zd Z	y)		UserGroupc                 "   |d   t        j                  g d|d         t        j                  ddg|d         t        j                  ddg|d         |d   d	}t        | j                        t        d
      k  r
|d   |d<   |S |d   |d<   |d   |d<   |d   r	 t        | j                        t        d
      k  r/| j                  j
                  j                  dd|d   gid      }n.| j                  j
                  j                  dd|d   gid      }t              dk(  r"| j                  j                  d|d   z         |d   d   |d<   |S # t        $ r/}| j                  j                  d|d   d|       Y d}~pd}~ww xY w)zConstruct parameters of UserGroup object

        Parameters:
            **kwargs: Arbitrary keyword parameters.

        Returns:
            dict: dictionary of specified parameters
        r   defaultinternalLDAPdisable
gui_accessdisabledenabled
debug_modestatustag_filters)r   r]   r`   users_statusrb   6.2rightshostgroup_rightstemplategroup_rightsuserdirectoryr   r   )r   searchzFailed to get user directory 'r   r   Nr   zUser directory '%s' not founduserdirectoryid)r0   r1   r   _zbx_api_versionr   rh   r   r   r   r   r   )r   kwargs_params_userdirr!   s        r"   _construct_parameterszUserGroup._construct_parameters  s    6N&>>:F<<P '>>Y')= )@@J')9 "-0
 --.e1DD &x 0GH@ = +11C*DG&'.45K.LG*+o&#D$9$9:l5>QQ#'::#;#;#?#?*2+1F?4K3L*M$ $(::#;#;#?#?*2+1F?4K3L*M$ x=A%LL**;f_>UU +  .6a[9J-K)* ! LL**!/2A7 +  s   A>E 	F%F		Fc                     	 | j                   j                  j                  dd|gid      }t        |      dkD  r|S y# t        $ r,}| j
                  j                  d|d|       Y d}~yd}~ww xY w)	zCheck if user group exists.

        Parameters:
            name: Name of the user group.

        Returns:
            The return value. True for success, False otherwise.
        r   r   r   r   zFailed to check if user group 'z
' exists: r   N)r   	usergroupr   r   r   r   r   r   r   
_usergroupr!   s       r"   check_if_usergroup_existsz#UserGroup.check_if_usergroup_exists;  s}    		--11#/?@J :"!! # 	LL""GKQO #  	s   :> 	A3"A..A3c                    	 t        | j                        t        d      k  r.| j                  j                  j	                  dddd|gid      }n.| j                  j                  j	                  ddddd|gid      }t        |      dk  r | j                  j                  d|z         y|d	   S # t        $ r,}| j                  j                  d
|d|       Y d}~yd}~ww xY w)zGet user group by user group name.

        Parameters:
            name: Name of the user group.

        Returns:
            User group matching user group name.
        rd   r   r   )r   selectTagFiltersselectRightsr   )r   rv   selectHostGroupRightsselectTemplateGroupRightsr   r   zUser group not found: %sr   r   zFailed to get user group 'r   N)	r   rk   r   rq   r   r   r   r   r   rr   s       r"   get_usergroup_by_usergroup_namez)UserGroup.get_usergroup_by_usergroup_nameO  s    	XD112\%5HH!ZZ1155"*,4(0#)D6"2	
 "ZZ1155"*,4195=#)D6"2
 :"&&+E+L&M!!}$ 	XLL""dTU'V"WW	Xs   B*B2 -B2 2	C';"C""C'c                     t        j                  | j                  |d               }t        j                   | j                  di |      }i }t        j                  |||      }|S )zCheck difference between user group and user specified parameters.

        Parameters:
            **kwargs: Arbitrary keyword parameters.

        Returns:
            dict: dictionary of differences
        r   r@   )r0   helper_convert_unicode_to_strrz   ro   helper_compare_dictionaries)r   rl   existing_usergroup
parameterschange_parameters_diffs         r"   check_differencezUserGroup.check_differencet  st     *GG00@
 "??&D&&00

 88*,=
 r:   c                 (   	 | j                   j                  r| j                   j                  d       | j                  j                  j                  |      S # t        $ r/}| j                   j                  d|d   d|       Y d}~yd}~ww xY w)zUpdate user group.

        Parameters:
            **kwargs: Arbitrary keyword parameters.

        Returns:
            usergroup: updated user group
        TchangedzFailed to update user group 'usrgrpidr   r   N)r   
check_mode	exit_jsonr   rq   updater   r   )r   rl   r!   s      r"   r   zUserGroup.update  s~    	||&&&&t&4::''..v66 	LL"">DZ>PRST #  	s   AA 	B"%BBc                 \   	 | j                   j                  r| j                   j                  d        | j                  d
i |}| j                  j
                  j                  |      }|d   d   S # t        $ r/}| j                   j                  d|d   d|       Y d	}~y	d	}~ww xY w)zAdd user group.

        Parameters:
            **kwargs: Arbitrary keyword parameters.

        Returns:
            usergroup: added user group
        Tr   	usrgrpidsr   zFailed to create user group 'r   r   r   Nr@   )	r   r   r   ro   r   rq   creater   r   )r   rl   r   rq   r!   s        r"   addzUserGroup.add  s    		||&&&&t&4333=f=J

,,33J?I[)!,, 	LL"">DVnaP #  	s   A0A3 3	B+<%B&&B+c                 &   	 | j                   j                  r| j                   j                  d       y| j                  j                  j                  |g      S # t        $ r,}| j                   j                  d|d|       Y d}~yd}~ww xY w)zDelete user group.

        Parameters:
            usrgrpid: User group id.

        Returns:
            usergroup: deleted user group
        Tr   zFailed to delete user group 'r   r   N)r   r   r   r   rq   deleter   r   )r   r   r!   s      r"   r   zUserGroup.delete  sz    	||&&&&t&4zz++22H:>> 	LL"">FJ #  	s   2A %A 	B$"BBN)
r<   r=   r>   ro   rt   rz   r   r   r   r   r@   r:   r"   rV   rV     s(    7r(#XJ*$(r:   rV   c                     t        j                         } | j                  t        dddg      t        dddg d      t        ddd	d	d
g      t        ddd
d
d	g      t        dddt        t        dd      t        ddg d                  t        dddt        t        dd      t        ddg d                  t        dddt        t        dd      t        ddg d                  t        dddt        t        dd      t        dd      t        dd                  t        dd      t        ddddg      
       t	        | d      }|j
                  d   }|j
                  d   }|j
                  d   }|j
                  d   }|j
                  d   }|j
                  d    }|j
                  d!   }|j
                  d"   }	|j
                  d#   }
|j
                  d$   }t        |      }|j                  }t        |j                        t        d%      k  rt        ||      }nt        ||      }t        ||      }t        ||      }|j                  |      }|r6|j                  |      d&   }|dk(  r.|j!                  |       |j#                  d|||d'|d(|)       y t        |j                        t        d%      k  r7|j%                  |||||j'                  |      |j'                  |	      *      }nG|j%                  |||||j'                  |      j'                  |      |j'                  |	      |
+	      }|i k(  r|j#                  d|||d,|z  )       y  |j                  d3d&|i| |j#                  d|||d-|d(|)       y |dk(  r|j#                  d||d.|z  /       y t        |j                        t        d%      k  r6|j)                  ||||j'                  |      |j'                  |	      0      }nF|j)                  ||||j'                  |      j'                  |      |j'                  |	      |
1      }|j#                  d|||d2|d(|)       y )4NstrT
user_group)typerequiredaliasesFrY   rX   )r   r   rY   choicesr^   r_   listdict)r   r   )r)   r*   r+   )r   r   r   )r&   r,   )r   elementsr   options)rL   r,    )r   rY   )r&   rP   rQ   presentabsent)r   rY   r   )
r   r]   r`   ra   re   rf   rg   rb   rh   state)argument_specsupports_check_moder   r]   r`   ra   re   rf   rg   rb   rh   r   rd   r   zUser group deleted: z, ID: )r   r   rq   r   r   )r   r   r]   r`   ra   re   rb   )	r   r   r]   r`   ra   rf   rg   rb   rh   zUser group is up to date: %szUser group updated: z0User group %s does not exists, nothing to delete)r   r   rq   r   )r   r]   r`   ra   re   rb   )r   r]   r`   ra   rf   rg   rb   rh   zUser group created: r@   )r0   zabbix_common_argument_specr   r   r   paramsrV   r   r   rk   r
   rB   rF   rN   rt   rz   r   r   r   r9   r   )r   moduler   r]   r`   ra   re   rf   rg   rb   rh   r   	userGroupzbxrgtshostgroup_rgtstemplategroup_rgtstgfltsusergroup_existsr   
differences                        r"   mainr     s    <<>Mutl^D>	
 +	
 
+	
 UT:!A	
 UT:!A	
 "#>!A	
 UT:eR0r2		
 6y9h:OPM  GR DQF== D|,J|,J]]8$F]]8$F}}%78!==)?@--.KMM/2MMM'"E&!I
//CI../,u2EEfc"(50=$F ::4@<<TB:NHX&!8<hG   I667,u:MM&77%))!226: & 9 9+ F 8 
 '77%))!%3%F%F(& *<)N)N,* !' 9 9+ F"/ 8 
 R  !"%6= !  !	  A(AjA   "%<@(K !  HFM	   I667,u:MM$==))!226: & 9 9+ F )  %==))!%3%F%F(& *<)N)N,* !' 9 9+ F"/ )  !8<hG  r:   __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helpersr0   r
   rB   rF   rN   rV   r   r<   r@   r:   r"   <module>r      s    A @PdM^
. 5 U < X X X/BZ /Bd1Bj 1Bh1B* 1BhB B:
 DAH zF r:   