
    Vh8+                         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  G d	 d
e      ZddZd Zd Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functiona
  
module: ipa_role
author: Thomas Krahn (@Nosmoht)
short_description: Manage FreeIPA role
description:
  - Add, modify and delete a role within FreeIPA server using FreeIPA API.
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  cn:
    description:
      - Role name.
      - Can not be changed as it is the unique identifier.
    required: true
    aliases: ['name']
    type: str
  description:
    description:
      - A description of this role-group.
    type: str
  group:
    description:
      - List of group names assign to this role.
      - If an empty list is passed all assigned groups will be unassigned from the role.
      - If option is omitted groups will not be checked or changed.
      - If option is passed all assigned groups that are not passed will be unassigned from the role.
    type: list
    elements: str
  host:
    description:
      - List of host names to assign.
      - If an empty list is passed all assigned hosts will be unassigned from the role.
      - If option is omitted hosts will not be checked or changed.
      - If option is passed all assigned hosts that are not passed will be unassigned from the role.
    type: list
    elements: str
  hostgroup:
    description:
      - List of host group names to assign.
      - If an empty list is passed all assigned host groups will be removed from the role.
      - If option is omitted host groups will not be checked or changed.
      - If option is passed all assigned hostgroups that are not passed will be unassigned from the role.
    type: list
    elements: str
  privilege:
    description:
      - List of privileges granted to the role.
      - If an empty list is passed all assigned privileges will be removed.
      - If option is omitted privileges will not be checked or changed.
      - If option is passed all assigned privileges that are not passed will be removed.
    type: list
    elements: str
  service:
    description:
      - List of service names to assign.
      - If an empty list is passed all assigned services will be removed from the role.
      - If option is omitted services will not be checked or changed.
      - If option is passed all assigned services that are not passed will be removed from the role.
    type: list
    elements: str
  state:
    description: State to ensure.
    default: "present"
    choices: ["absent", "present"]
    type: str
  user:
    description:
      - List of user names to assign.
      - If an empty list is passed all assigned users will be removed from the role.
      - If option is omitted users will not be checked or changed.
    type: list
    elements: str
extends_documentation_fragment:
  - community.general.ipa.documentation
  - community.general.attributes
a  
- name: Ensure role is present
  community.general.ipa_role:
    name: dba
    description: Database Administrators
    state: present
    user:
      - pinky
      - brain
    ipa_host: ipa.example.com
    ipa_user: admin
    ipa_pass: topsecret

- name: Ensure role with certain details
  community.general.ipa_role:
    name: another-role
    description: Just another role
    group:
      - editors
    host:
      - host01.example.com
    hostgroup:
      - hostgroup01
    privilege:
      - Group Administrators
      - User Administrators
    service:
      - service01

- name: Ensure role is absent
  community.general.ipa_role:
    name: dba
    state: absent
    ipa_host: ipa.example.com
    ipa_user: admin
    ipa_pass: topsecret
zS
role:
  description: Role as returned by IPA API.
  returned: always
  type: dict
N)AnsibleModule)	IPAClientipa_argument_spec)	to_nativec                        e Zd Z fdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Z xZS )RoleIPAClientc                 2    t         t        |   ||||       y N)superr   __init__)selfmodulehostportprotocol	__class__s        n/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/ipa_role.pyr   zRoleIPAClient.__init__   s    mT+FD$I    c                 0    | j                  dd d|d      S )N	role_findT)allcnmethodnameitem
_post_jsonr   r   s     r   r   zRoleIPAClient.role_find   s    k4W[C\]]r   c                 *    | j                  d||      S )Nrole_addr   r    r   r   r   s      r   r$   zRoleIPAClient.role_add       jt$GGr   c                 *    | j                  d||      S )Nrole_modr   r    r%   s      r   r(   zRoleIPAClient.role_mod   r&   r   c                 (    | j                  d|      S )Nrole_del)r   r   r    r"   s     r   r*   zRoleIPAClient.role_del   s    jt<<r   c                 *    | j                  d||      S )Nrole_add_memberr   r    r%   s      r   r,   zRoleIPAClient.role_add_member   s    &7dNNr   c                 ,    | j                  |d|i      S Ngroupr   r   r,   r%   s      r   role_add_groupzRoleIPAClient.role_add_group   s    ##WdO#DDr   c                 ,    | j                  |d|i      S Nr   r0   r1   r%   s      r   role_add_hostzRoleIPAClient.role_add_host       ##VTN#CCr   c                 ,    | j                  |d|i      S N	hostgroupr0   r1   r%   s      r   role_add_hostgroupz RoleIPAClient.role_add_hostgroup   s    ##[$4G#HHr   c                 ,    | j                  |d|i      S Nservicer0   r1   r%   s      r   role_add_servicezRoleIPAClient.role_add_service   s    ##Y4E#FFr   c                 ,    | j                  |d|i      S Nuserr0   r1   r%   s      r   role_add_userzRoleIPAClient.role_add_user   r6   r   c                 *    | j                  d||      S )Nrole_remove_memberr   r    r%   s      r   rD   z RoleIPAClient.role_remove_member   s    &:DQQr   c                 ,    | j                  |d|i      S r.   rD   r%   s      r   role_remove_groupzRoleIPAClient.role_remove_group   s    &&D&GGr   c                 ,    | j                  |d|i      S r4   rF   r%   s      r   role_remove_hostzRoleIPAClient.role_remove_host       &&D~&FFr   c                 ,    | j                  |d|i      S r8   rF   r%   s      r   role_remove_hostgroupz#RoleIPAClient.role_remove_hostgroup   s    &&DT7J&KKr   c                 ,    | j                  |d|i      S r<   rF   r%   s      r   role_remove_servicez!RoleIPAClient.role_remove_service   s    &&D	47H&IIr   c                 ,    | j                  |d|i      S r@   rF   r%   s      r   role_remove_userzRoleIPAClient.role_remove_user   rJ   r   c                 .    | j                  d|d|i      S )Nrole_add_privilege	privileger   r    r%   s      r   rR   z RoleIPAClient.role_add_privilege   s    &:[Z^L_``r   c                 .    | j                  d|d|i      S )Nrole_remove_privilegerS   r   r    r%   s      r   rU   z#RoleIPAClient.role_remove_privilege   s    &=DP[]aObccr   )__name__
__module____qualname__r   r   r$   r(   r*   r,   r2   r5   r:   r>   rB   rD   rG   rI   rL   rN   rP   rR   rU   __classcell__)r   s   @r   r   r      st    J^HH=OEDIGDRHGLJGadr   r   c                     i }| | |d<   |S )Ndescription )r[   datas     r   get_role_dictr^      s    D)]Kr   c                 (    | j                  ||      S )N)ipa_datamodule_data)get_diff)clientipa_rolemodule_roles      r   get_role_diffrf      s    ??H+?FFr   c                    | j                   d   }| j                   d   }| j                   d   }| j                   d   }| j                   d   }| j                   d   }| j                   d   }| j                   d   }	t        | j                   d	   
      }
|j                  |      }d}|dk(  r|s"d}| j                  sm|j	                  ||
      }nYt        |||
      }t        |      dkD  r>d}| j                  s0i }|D ]  }|
j                  |      ||<    |j                  ||       |=|j                  ||j                  dg       ||j                  |j                        xs |}|=|j                  ||j                  dg       ||j                  |j                        xs |}|=|j                  ||j                  dg       ||j                  |j                        xs |}|=|j                  ||j                  dg       ||j                   |j"                        xs |}|=|j                  ||j                  dg       ||j$                  |j&                        xs |}|	_|j                  ||j                  dg       |	|j(                  |j*                        xs |}n!|rd}| j                  s|j-                  |       ||j                  |      fS )Nstater   r/   r   r9   rS   r=   rA   r[   )r[   )r   FpresentTr0   r   member_groupmember_hostmember_hostgroupmemberof_privilegemember_servicemember_user)paramsr^   r   
check_moder$   rf   lengetr(   modify_if_diffr2   rG   r5   rI   r:   rL   rR   rU   r>   rN   rB   rP   r*   )r   rc   rh   r   r/   r   r9   rS   r=   rA   re   rd   changeddiffr]   keys                   r   ensurerx      s   MM'"E==DMM'"E== Dk*Ik*ImmI&G== DFMM-,HIKT*HG	G$$!??;?G ;?D4y1}((D# 9$/OOC$8S	9OODO9++D(,,~r2RTY,2,A,A,2,D,DF QIP  ++D(,,}b2QSW,2,@,@,2,C,CE PHO   ++D(,,?QSU2VXa,2,E,E,2,H,HJ UMT   ++D(,,?SUW2XZc,2,E,E,2,H,HJ UMT  ++D(,,?OQS2TV],2,C,C,2,F,FH SKR  ++D(,,}b2QSW,2,@,@,2,C,CE PHO 
 G$$%F$$$$///r   c                     t               } | j                  t        dddg      t        d      t        dd      t        dd      t        dd      t        dd      t        dd      t        dddd	g
      t        dd      	       t        | d      }t	        ||j
                  d   |j
                  d   |j
                  d         }	 |j                  |j
                  d   |j
                  d          t        ||      \  }}|j                  ||       y # t        $ r8}|j                  t        |      t        j                                Y d }~y d }~ww xY w)NstrTr   )typerequiredaliases)r{   list)r{   elementsri   absent)r{   defaultchoices)	r   r[   r/   r   r9   rS   r=   rh   rA   )argument_specsupports_check_modeipa_hostipa_portipa_prot)r   r   r   r   ipa_useripa_pass)usernamepassword)ru   role)msg	exception)r   updatedictr   r   rp   loginrx   	exit_json	Exception	fail_jsonr	   	traceback
format_exc)r   r   rc   ru   r   es         r   mainr     sJ   %'MDedVHM%)u%5#%@"?#'Ve#D#'Ve#D!%6E!B#	IW_K`a"?  A /35F & &j 9 &j 9$*MM*$=?F
MfmmJ7$mmJ7 	 	9vv.t4 MYq\Y5I5I5KLLMs   AD   	E!).EE!__main__r   )
__future__r   r   r   r{   __metaclass__DOCUMENTATIONEXAMPLESRETURNr   ansible.module_utils.basicr   >ansible_collections.community.general.plugins.module_utils.ipar   r   +ansible.module_utils.common.text.convertersr	   r   r^   rf   rx   r   rV   r\   r   r   <module>r      ss    A @N`$L
  4 g A8dI 8dvG>0BM: zF r   