
    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_functionu  
module: ipa_service
author: Cédric Parent (@cprh)
short_description: Manage FreeIPA service
description:
  - Add and delete an IPA service using IPA API.
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  krbcanonicalname:
    description:
      - Principal of the service.
      - Can not be changed as it is the unique identifier.
    required: true
    aliases: ["name"]
    type: str
  hosts:
    description:
      - Defines the list of C(ManagedBy) hosts.
    required: false
    type: list
    elements: str
  force:
    description:
      - Force principal name even if host is not in DNS.
    required: false
    type: bool
  skip_host_check:
    description:
      - Force service to be created even when host object does not exist to manage it.
      - This is only used on creation, not for updating existing services.
    required: false
    type: bool
    default: false
    version_added: 4.7.0
  state:
    description: State to ensure.
    required: false
    default: present
    choices: ["absent", "present"]
    type: str
extends_documentation_fragment:
  - community.general.ipa.documentation
  - community.general.attributes
ax  
- name: Ensure service is present
  community.general.ipa_service:
    name: http/host01.example.com
    state: present
    ipa_host: ipa.example.com
    ipa_user: admin
    ipa_pass: topsecret

- name: Ensure service is absent
  community.general.ipa_service:
    name: http/host01.example.com
    state: absent
    ipa_host: ipa.example.com
    ipa_user: admin
    ipa_pass: topsecret

- name: Changing Managing hosts list
  community.general.ipa_service:
    name: http/host01.example.com
    hosts:
      - host01.example.com
      - host02.example.com
    ipa_host: ipa.example.com
    ipa_user: admin
    ipa_pass: topsecret
zY
service:
  description: Service as returned by IPA API.
  returned: always
  type: dict
N)AnsibleModule)	IPAClientipa_argument_spec)	to_nativec                   H     e Zd Z fdZd Zd Zd Zd Zd Zd Z	d Z
 xZS )	ServiceIPAClientc                 2    t         t        |   ||||       y )N)superr   __init__)selfmodulehostportprotocol	__class__s        q/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/ipa_service.pyr   zServiceIPAClient.__init__f   s    .vtT8L    c                 0    | j                  dd d|d      S )Nservice_findT)allkrbcanonicalnamemethodnameitem
_post_jsonr   r   s     r   r   zServiceIPAClient.service_findi   s    n4dhlFmnnr   c                 *    | j                  d||      S )Nservice_addr   r   r   r   services      r   r#   zServiceIPAClient.service_addl       m$WMMr   c                 *    | j                  d||      S )Nservice_modr   r   r$   s      r   r(   zServiceIPAClient.service_modo   r&   r   c                 (    | j                  d|      S )Nservice_delr   r   r   r!   s     r   r*   zServiceIPAClient.service_delr   s    m$??r   c                 (    | j                  d|      S )Nservice_disabler+   r   r!   s     r   r-   z ServiceIPAClient.service_disableu   s    &7dCCr   c                 .    | j                  d|d|i      S )Nservice_add_hostr   r   r   r   r   r   s      r   r/   z!ServiceIPAClient.service_add_hostx   s    &8t6SW.YYr   c                 .    | j                  d|d|i      S )Nservice_remove_hostr   r   r   r0   s      r   r2   z$ServiceIPAClient.service_remove_host{   s    &;$fVZ^\\r   )__name__
__module____qualname__r   r   r#   r(   r*   r-   r/   r2   __classcell__)r   s   @r   r   r   e   s3    MoNN@DZ]r   r   c                 4    i }| | |d<   |||d<   |||d<   |S )Nforcer   skip_host_check )r8   r   r9   datas       r   get_service_dictr<      s=    DW##3 ""1Kr   c                 N    g d}|D ]
  }||v s||=  | j                  ||      S )N)r8   r   r9   )ipa_datamodule_data)get_diff)clientipa_hostmodule_servicenon_updateable_keyskeys        r   get_service_diffrF      s<    J" $. s#$ ??H.?IIr   c                 b   | j                   d   }| j                   d   }| j                   d   }|j                  |      }t        | j                   d   | j                   d         }d}|d	v r|s"d
}| j                  sm|j	                  ||       nYt        |||      }t        |      dkD  r>d
}| j                  s0i }	|D ]  }
|j                  |
      |	|
<    |j                  ||	       |d|v ra|d   D ](  }||vs| j                  s|j                  ||       d
}* |D ]+  }||d   vs| j                  s|j                  ||       d
}- nK|D ]#  }| j                  s|j                  ||       d
}% n"|r d
}| j                  s|j                  |       ||j                  |      fS )Nr   statehosts)r   r8   r9   )r8   r9   F)presentenableddisabledT)r   r%   r   managedby_host)r   r   )paramsr   r<   
check_moder#   rF   lengetr(   r2   r/   r*   )r   rA   r   rH   rI   ipa_servicerC   changeddiffr;   rE   r   s               r   ensurerU      s   ==+,DMM'"EMM'"E%%4%0K%FMM',BTZTaTabsTtuNG22G$$""n"E#FKHD4y1}((D# <$2$6$6s$;S	<&&D$&?;.'(89 'D5(%00"66Dt6L"&	'
 " 'D;/?#@@%00"33D3I"&	' " #D!,,//T/E"G# G$$"""-F''T'222r   c                  p   t               } | j                  t        dddg      t        dd      t        ddd      t        d	dd
      t        dddddg             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boolF)rX   rY   )rX   defaultrY   list)rX   rY   elementsrJ   absent)rX   rY   r\   choices)r   r8   r9   rI   rH   )argument_specsupports_check_moderB   ipa_portipa_prot)r   r   r   r   ipa_useripa_pass)usernamepassword)rS   r   )msg	exception)r   updatedictr   r   rN   loginrU   	exit_json	Exception	fail_jsonr	   	traceback
format_exc)ra   r   rA   rS   r   es         r   mainrt      s-   %'M54&J/&%%H?y%x02  3 /35F V#)==#<#)==#<'-}}Z'@BF
MfmmJ7$mmJ7 	 	9vv.t4 MYq\Y5I5I5KLLMs   $AC4 4	D5=.D00D5__main__)NNN)
__future__r   r   r   rX   __metaclass__DOCUMENTATIONEXAMPLESRETURNrq   ansible.module_utils.basicr   >ansible_collections.community.general.plugins.module_utils.ipar   r   +ansible.module_utils.common.text.convertersr	   r   r<   rF   rU   rt   r3   r:   r   r   <module>r~      sp    A @/b8
  4 g A]y ]4J.3bM6 zF r   