
    Vh{                         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_hostgroup
author: Thomas Krahn (@Nosmoht)
short_description: Manage FreeIPA host-group
description:
  - Add, modify and delete an IPA host-group using IPA API.
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  append:
    description:
      - If V(true), add the listed O(host) to the O(hostgroup).
      - If V(false), only the listed O(host) will be in O(hostgroup), removing any other hosts.
    default: false
    type: bool
    version_added: 6.6.0
  cn:
    description:
      - Name of host-group.
      - Can not be changed as it is the unique identifier.
    required: true
    aliases: ["name"]
    type: str
  description:
    description:
      - Description.
    type: str
  host:
    description:
      - List of hosts that belong to the host-group.
      - If an empty list is passed all hosts will be removed from the group.
      - 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 group.
    type: list
    elements: str
  hostgroup:
    description:
      - List of host-groups than belong to that host-group.
      - If an empty list is passed all host-groups will be removed from the group.
      - 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 group.
    type: list
    elements: str
  state:
    description:
      - State to ensure.
      - V("absent") and V("disabled") give the same results.
      - V("present") and V("enabled") give the same results.
    default: "present"
    choices: ["absent", "disabled", "enabled", "present"]
    type: str
extends_documentation_fragment:
  - community.general.ipa.documentation
  - community.general.attributes
a  
- name: Ensure host-group databases is present
  community.general.ipa_hostgroup:
    name: databases
    state: present
    host:
      - db.example.com
    hostgroup:
      - mysql-server
      - oracle-server
    ipa_host: ipa.example.com
    ipa_user: admin
    ipa_pass: topsecret

- name: Ensure host-group databases is absent
  community.general.ipa_hostgroup:
    name: databases
    state: absent
    ipa_host: ipa.example.com
    ipa_user: admin
    ipa_pass: topsecret
z]
hostgroup:
  description: Hostgroup as returned by IPA API.
  returned: always
  type: dict
N)AnsibleModule)	IPAClientipa_argument_spec)	to_nativec                   Z     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 xZS )HostGroupIPAClientc                 2    t         t        |   ||||       y N)superr   __init__)selfmodulehostportprotocol	__class__s        s/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/ipa_hostgroup.pyr   zHostGroupIPAClient.__init__k   s     $0tXN    c                 0    | j                  dd d|d      S )Nhostgroup_findT)allcnmethodnameitem
_post_jsonr   r   s     r   r   z!HostGroupIPAClient.hostgroup_findn   s    &6TPT\`Habbr   c                 *    | j                  d||      S )Nhostgroup_addr   r    r   r   r   s      r   r$   z HostGroupIPAClient.hostgroup_addq       oDtLLr   c                 *    | j                  d||      S )Nhostgroup_modr   r    r%   s      r   r(   z HostGroupIPAClient.hostgroup_modt   r&   r   c                 (    | j                  d|      S )Nhostgroup_del)r   r   r    r"   s     r   r*   z HostGroupIPAClient.hostgroup_delw   s    oDAAr   c                 *    | j                  d||      S )Nhostgroup_add_memberr   r    r%   s      r   r,   z'HostGroupIPAClient.hostgroup_add_memberz   s    &<4dSSr   c                 ,    | j                  |d|i      S Nr   r   r   r,   r%   s      r   hostgroup_add_hostz%HostGroupIPAClient.hostgroup_add_host}   s    ((d&$(HHr   c                 ,    | j                  |d|i      S N	hostgroupr/   r0   r%   s      r   hostgroup_add_hostgroupz*HostGroupIPAClient.hostgroup_add_hostgroup   s    ((d+t9L(MMr   c                 *    | j                  d||      S )Nhostgroup_remove_memberr   r    r%   s      r   r7   z*HostGroupIPAClient.hostgroup_remove_member   s    &?dQUVVr   c                 ,    | j                  |d|i      S r.   r7   r%   s      r   hostgroup_remove_hostz(HostGroupIPAClient.hostgroup_remove_host   s    ++VTN+KKr   c                 ,    | j                  |d|i      S r3   r9   r%   s      r   hostgroup_remove_hostgroupz-HostGroupIPAClient.hostgroup_remove_hostgroup   s    ++[$<O+PPr   )__name__
__module____qualname__r   r   r$   r(   r*   r,   r1   r5   r7   r:   r<   __classcell__)r   s   @r   r   r   j   sE    OcMMBTINWLQr   r   c                     i }| | |d<   |S )Ndescription )rB   datas     r   get_hostgroup_dictrE      s    D)]Kr   c                 (    | j                  ||      S )N)ipa_datamodule_data)get_diff)clientipa_hostgroupmodule_hostgroups      r   get_hostgroup_diffrM      s    ??M?O?PPr   c           	         | j                   d   }| j                   d   }| j                   d   }| j                   d   }| j                   d   }|j                  |      }t        | j                   d         }d	}	|d
v r8|s"d}	| j                  sm|j	                  ||      }nYt        |||      }
t        |
      dkD  r>d}	| j                  s0i }|
D ]  }|j                  |      ||<    |j                  ||       |[|j                  ||j                  dg       |D cg c]  }|j                          c}|j                  |j                  |      xs |	}	|~|j                  ||j                  dg       |D cg c]  }|j                          c}|j                  |j                  |      xs |	}	n"|r d}	| j                  s|j                  |       |	|j                  |      fS c c}w c c}w )Nr   stater   r4   append)r   rB   )rB   F)presentenabledTr/   r   member_host)rP   member_hostgroup)paramsr   rE   
check_moder$   rM   lengetr(   modify_if_difflowerr1   r:   r5   r<   r*   )r   rJ   r   rO   r   r4   rP   rK   rL   changeddiffrD   keyr   s                 r   ensurer^      s   ==DMM'"E== Dk*I]]8$F))t)4M)fmmM6RSG&&G$$ & 4 4$EU 4 V%fm=MND4y1}((D# >$4$8$8$=S	>((d(>++D-2C2CMSU2VFJ,KdTZZ\,K,2,E,E,2,H,H39	 , ; F ?F	   ++D-2C2CDVXZ2[FO,PdTZZ\,P,2,J,J,2,M,M39	 , ; F ?F	  G$$$$$$/F))t)444% -L -Qs   G,;G1c                     t               } | j                  t        dddg      t        d      t        dd      t        dd      t        ddg d	
      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)ra   list)ra   elementsrQ   )rQ   absentrR   disabled)ra   defaultchoicesboolF)ra   rh   )r   rB   r   r4   rO   rP   )argument_specsupports_check_modeipa_hostipa_portipa_prot)r   r   r   r   ipa_useripa_pass)usernamepassword)r[   r4   )msg	exception)r   updatedictr   r   rU   loginr^   	exit_json	Exception	fail_jsonr	   	traceback
format_exc)rk   r   rJ   r[   r4   es         r   mainr      s)   %'MDedVHM%)u%5"?#'Ve#D#	Kwx $&% @  B /35F  v%+]]:%>%+]]:%>)/z)BDF
MfmmJ7$mmJ7 	 	9#FF3I> MYq\Y5I5I5KLLMs   ,AC< <	D=.D88D=__main__r   )
__future__r   r   r   ra   __metaclass__DOCUMENTATIONEXAMPLESRETURNr|   ansible.module_utils.basicr   >ansible_collections.community.general.plugins.module_utils.ipar   r   +ansible.module_utils.common.text.convertersr	   r   rE   rM   r^   r   r=   rC   r   r   <module>r      sq    A @9v.
  4 g A Q  QFQ.5bM4 zF r   