
    VhL(                         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  G d d	      Zd
 Zedk(  r e        yy)    )absolute_importdivisionprint_functiona!  
module: icinga2_host
short_description: Manage a host in Icinga2
description:
  - Add or remove a host to Icinga2 through the API.
  - See U(https://www.icinga.com/docs/icinga2/latest/doc/12-icinga2-api/).
author: "Jurgen Brand (@t794104)"
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  url:
    type: str
    description:
      - HTTP, HTTPS, or FTP URL in the form V((http|https|ftp\)://[user[:pass]]@host.domain[:port]/path).
  use_proxy:
    description:
      - If V(false), it will not use a proxy, even if one is defined in an environment variable on the target hosts.
    type: bool
    default: true
  validate_certs:
    description:
      - If V(false), SSL certificates will not be validated. This should only be used on personally controlled sites using
        self-signed certificates.
    type: bool
    default: true
  url_username:
    type: str
    description:
      - The username for use in HTTP basic authentication.
      - This parameter can be used without O(url_password) for sites that allow empty passwords.
  url_password:
    type: str
    description:
      - The password for use in HTTP basic authentication.
      - If the O(url_username) parameter is not specified, the O(url_password) parameter will not be used.
  force_basic_auth:
    description:
      - C(httplib2), the library used by Ansible's HTTP request code only sends authentication information when a webservice responds to
        an initial request with a 401 status. Since some basic auth services do not properly send a 401, logins will fail.
        This option forces the sending of the Basic authentication header upon initial request.
    type: bool
    default: false
  client_cert:
    type: path
    description:
      - PEM formatted certificate chain file to be used for SSL client authentication. This file can also include the key
        as well, and if the key is included, O(client_key) is not required.
  client_key:
    type: path
    description:
      - PEM formatted file that contains your private key to be used for SSL client authentication. If O(client_cert) contains
        both the certificate and key, this option is not required.
  state:
    type: str
    description:
      - Apply feature state.
    choices: ["present", "absent"]
    default: present
  name:
    type: str
    description:
      - Name used to create / delete the host. This does not need to be the FQDN, but does needs to be unique.
    required: true
    aliases: [host]
  zone:
    type: str
    description:
      - The zone from where this host should be polled.
  template:
    type: str
    description:
      - The template used to define the host.
      - Template cannot be modified after object creation.
  check_command:
    type: str
    description:
      - The command used to check if the host is alive.
    default: "hostalive"
  display_name:
    type: str
    description:
      - The name used to display the host.
      - If not specified, it defaults to the value of the O(name) parameter.
  ip:
    type: str
    description:
      - The IP address of the host.
      - This is no longer required since community.general 8.0.0.
  variables:
    type: dict
    description:
      - Dictionary of variables.
extends_documentation_fragment:
  - ansible.builtin.url
  - community.general.attributes
a6  
- name: Add host to icinga
  community.general.icinga2_host:
    url: "https://icinga2.example.com"
    url_username: "ansible"
    url_password: "a_secret"
    state: present
    name: "{{ ansible_fqdn }}"
    ip: "{{ ansible_default_ipv4.address }}"
    variables:
      foo: "bar"
  delegate_to: 127.0.0.1
z
name:
  description: The name used to create, modify or delete the host.
  type: str
  returned: always
data:
  description: The data structure used for create, modify or delete of the host.
  type: dict
  returned: always
N)AnsibleModule)	fetch_urlurl_argument_specc                   B    e Zd ZdZd Zd
dZd Zd Zd Zd Z	d Z
d	 Zy)icinga2_apiNc                     || _         y )Nmodule)selfr   s     r/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/icinga2_host.py__init__zicinga2_api.__init__   s	        c           	      D   d|d}| j                   j                  j                  d      dz   |z   }t        | j                   ||||| j                   j                  d         \  }}d}|r#t	        j
                  |j                               }|d   d	k\  r|d
   }|d   |dS )Nzapplication/json)AcceptzX-HTTP-Method-Overrideurl/	use_proxy)r   r   dataheadersmethodr    statusi  body)coder   )r   paramsgetr   jsonloadsread)	r   pathr   r   r   r   rspinfor   s	            r   call_urlzicinga2_api.call_url   s    (&,
 kk  $$U+c1D8T[[cg^dptp{p{  qC  qC  DO  qP  Q	T::chhj)D>S <DX55r   c                 8    | j                  d      }|d   dk(  ryy)Nz	v1/statusr      TF)r&   )r   rets     r   check_connectionzicinga2_api.check_connection   s"    mmK(v;#r   c                     dd|z   dz   i}| j                  d| j                  j                  |            }|d   dk(  rt        |d   d	         d
k(  ryy)Nfilterzmatch("z", host.name)zv1/objects/hosts)r#   r   r   r(   r   results   TF)r&   r   jsonifylenr   hostnamer   r)   s       r   existszicinga2_api.exists   sm    j8+.>>
 mm#$$T*  
 v;#3v;y)*a/r   c                 f    | j                  d|z   | j                  j                  |      d      }|S )Nv1/objects/hosts/PUTr#   r   r   r&   r   r/   r1   s       r   createzicinga2_api.create   s9    mm$x/$$T*  

 
r   c                 n    ddi}| j                  d|z   | j                  j                  |      d      }|S )Ncascader.   r5   DELETEr7   r8   r1   s       r   deletezicinga2_api.delete   sB    1~mm$x/$$T*  

 
r   c                 f    | j                  d|z   | j                  j                  |      d      }|S )Nr5   POSTr7   r8   r1   s       r   modifyzicinga2_api.modify   s9    mm$x/$$T*  

 
r   c                     | j                  d|z   d      }d}|d   d   d   }|d   D ].  }||d   j                         vrd	}|d   |   |d   |   k7  s-d	}0 |S )
Nr5   GET)r#   r   Fr   r-   r   attrsT)r&   keys)r   r2   r   r)   changedic_datakeys          r   diffzicinga2_api.diff   s    mm$x/  
 f+i(+= 	C''*//11gs#ww'7'<<		
 r   )r   rB   )__name__
__module____qualname__r   r   r&   r*   r3   r9   r=   r@   rH    r   r   r
   r
      s/    F6r   r
   c                     t               } | j                  t        dddg      t        ddg      t               t        d       t        d      t        d       t               t        d	d 
             t        | d      }|j                  d   }|j                  d   }|j                  d   }g }|j                  d   r|j                  d   g}|j                  d   }|j                  d   }|j                  d   }|s|}|j                  d   }		 t        |      }
|
j                          |||||ddd}|d   j                  |	j                         D ci c]  \  }}d|z   | c}}       d}
j                  |      r|dk(  r_|j                  r|j                  d||       n 	 |
j                  |      }|d   d k(  rd}n|j                  d!|d   d"|d#   d$       n|
j                  ||      r|j                  r|j                  d||       |d&= |
j!                  ||      }|d   d k(  rd}nq|j                  d!|d   d'|d#   d$       nQ|dk(  rL|j                  rd}n=	 |
j#                  ||      }|d   d k(  rd}n|j                  d!|d   d(|d#   d$       |j                  |||       y # t        $ r }|j                  d|z         Y d }~d }~ww xY wc c}}w # t        $ r(}|j                  d%t        |      z          Y d }~sd }~ww xY w# t        $ r(}|j                  d)t        |      z          Y d }~d }~ww xY w)*Npresentabsent)defaultchoicesThost)requiredaliases)rP   	hostalivedict)typerP   )statenamezonetemplatecheck_commanddisplay_nameip	variables)argument_specsupports_check_moderX   rY   rZ   r[   r\   r^   r]   r_   r   z2unable to connect to Icinga. Exception message: %s)msgansible)addressr]   r\   rZ   zvars.made_by)	templatesrC   rC   zvars.F)rE   rY   r   r   r(   zbad return code (z) deleting host: 'r   'zexception deleting host: re   z) modifying host: 'z) creating host: 'zexception creating host: )r   updaterV   r   r   r
   r*   	Exception	fail_jsonitemsr3   
check_mode	exit_jsonr=   strrH   r@   r9   )r`   r   rX   rY   rZ   r[   r\   r^   r]   r_   icingaer   rG   valuerE   r)   s                    r   mainrq      s   %'M9x.CD4&2Vd#;/$'6FD1  	 # F
 MM'"E== D== DH}}Z MM*-.MM/2M	t	B==0Lk*IYF+!
 (*%
	D 	MARS:3'C-.STG}}TH    Dt DO ---C6{c)"&(([^_e[fhklrhs-t(u [[t$    T E [!--d+C6{c!  TWX^T_adekal%m n I  O --d3C6{c)"&(([^_e[fhklrhs-t(u W4d;u  YQUVWXXY T ! O$$)Ds1v)M$NNO8 ! O$$)Ds1v)M$NNOsN   K K-
-;K3 0<L' 	K*
K%%K*3	L$<LL$'	M0MM__main__)
__future__r   r   r   rW   __metaclass__DOCUMENTATIONEXAMPLESRETURNr    ansible.module_utils.basicr   ansible.module_utils.urlsr   r   r
   rq   rI   rL   r   r   <module>rz      s]    A @bH	
  4 BL Ld_<F zF r   