
    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
Z
d dlmc mZ dZ	 d dlZdZd dlmZmZ d d	lmZ  G d
 de      Zd Zd Zd Zedk(  r e        yy# e$ r  e
j&                         ZdZY Jw xY w)    )absolute_importdivisionprint_functiona  
module: jenkins_job
short_description: Manage jenkins jobs
description:
  - Manage Jenkins jobs by using Jenkins REST API.
requirements:
  - "python-jenkins >= 0.4.12"
author: "Sergio Millan Rodriguez (@sermilrod)"
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: full
  diff_mode:
    support: full
options:
  config:
    type: str
    description:
      - Config in XML format.
      - Required if job does not yet exist.
      - Mutually exclusive with O(enabled).
      - Considered if O(state=present).
    required: false
  enabled:
    description:
      - Whether the job should be enabled or disabled.
      - Mutually exclusive with O(config).
      - Considered if O(state=present).
    type: bool
    required: false
  name:
    type: str
    description:
      - Name of the Jenkins job.
    required: true
  password:
    type: str
    description:
      - Password to authenticate with the Jenkins server.
    required: false
  state:
    type: str
    description:
      - Attribute that specifies if the job has to be created or deleted.
    required: false
    default: present
    choices: ['present', 'absent']
  token:
    type: str
    description:
      - API token used to authenticate alternatively to password.
    required: false
  url:
    type: str
    description:
      - URL where the Jenkins server is accessible.
    required: false
    default: http://localhost:8080
  user:
    type: str
    description:
      - User to authenticate with the Jenkins server.
    required: false
  validate_certs:
    type: bool
    default: true
    description:
      - If set to V(false), the SSL certificates will not be validated. This should only set to V(false) used on personally
        controlled sites using self-signed certificates as it avoids verifying the source site.
      - The C(python-jenkins) library only handles this by using the environment variable E(PYTHONHTTPSVERIFY).
    version_added: 2.3.0
a  
- name: Create a jenkins job using basic authentication
  community.general.jenkins_job:
    config: "{{ lookup('file', 'templates/test.xml') }}"
    name: test
    password: admin
    url: http://localhost:8080
    user: admin

- name: Create a jenkins job using the token
  community.general.jenkins_job:
    config: "{{ lookup('template', 'templates/test.xml.j2') }}"
    name: test
    token: asdfasfasfasdfasdfadfasfasdfasdfc
    url: http://localhost:8080
    user: admin

- name: Delete a jenkins job using basic authentication
  community.general.jenkins_job:
    name: test
    password: admin
    state: absent
    url: http://localhost:8080
    user: admin

- name: Delete a jenkins job using the token
  community.general.jenkins_job:
    name: test
    token: asdfasfasfasdfasdfadfasfasdfasdfc
    state: absent
    url: http://localhost:8080
    user: admin

- name: Disable a jenkins job using basic authentication
  community.general.jenkins_job:
    name: test
    password: admin
    enabled: false
    url: http://localhost:8080
    user: admin

- name: Disable a jenkins job using the token
  community.general.jenkins_job:
    name: test
    token: asdfasfasfasdfasdfadfasfasdfasdfc
    enabled: false
    url: http://localhost:8080
    user: admin
a  
name:
  description: Name of the jenkins job.
  returned: success
  type: str
  sample: test-job
state:
  description: State of the jenkins job.
  returned: success
  type: str
  sample: present
enabled:
  description: Whether the jenkins job is enabled or not.
  returned: success
  type: bool
  sample: true
user:
  description: User used for authentication.
  returned: success
  type: str
  sample: admin
url:
  description: URL to connect to the Jenkins server.
  returned: success
  type: str
  sample: https://jenkins.mydomain.com
NTF)AnsibleModulemissing_required_lib)	to_nativec                   `    e 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y)
JenkinsJobc                    || _         |j                  j                  d      | _        |j                  j                  d      | _        |j                  j                  d      | _        |j                  j                  d      | _        |j                  j                  d      | _        |j                  j                  d      | _        |j                  j                  d      | _	        |j                  j                  d      | _
        | j                         | _        d	| j                  | j                  | j                  | j                  d
d
dd| _        d| _        |j                  d   sdt        j                   d<   y y )NconfignamepasswordstateenabledtokenuserurlF )beforeafter)changedr   r   r   r   diffzexcluded statevalidate_certs0PYTHONHTTPSVERIFY)moduleparamsgetr   r   r   r   r   r   r   jenkins_urlget_jenkins_connectionserverresult
EXCL_STATEosenviron)selfr   s     q/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/jenkins_job.py__init__zJenkinsJob.__init__   s+   mm''1MM%%f-	))*5]]&&w/
}}((3]]&&w/
MM%%f-	!==,,U3113 ##IIIIZZ

 +}}-..1BJJ*+ /    c                    	 | j                   rA| j                  r5t        j                  | j                  | j                   | j                        S | j                   rA| j
                  r5t        j                  | j                  | j                   | j
                        S | j                   rB| j                  s6| j
                  s*t        j                  | j                  | j                         S t        j                  | j                        S # t        $ rE}| j                  j                  dt        |      z  t        j                                Y d }~y d }~ww xY w)Nz'Unable to connect to Jenkins server, %smsg	exception)r   r   jenkinsJenkinsr   r   	Exceptionr   	fail_jsonr   	traceback
format_excr&   es     r'   r    z!JenkinsJob.get_jenkins_connection   s    
	ByyT]]t'7'7DMMRRtzzt'7'7DJJOODMMTZZt'7'7CCt'7'788 	BKK!!&OR[\]R^&^jsj~j~  kA!  B  B	Bs,   AD	 AD	 AD	 *D	 		E;EEc                 ,   	 | j                   j                  | j                        }d|vr| j                  S t	        |d         S # t
        $ rE}| j                  j                  dt	        |      z  t        j                                Y d }~y d }~ww xY w)Ncolorz#Unable to fetch job information, %sr+   )
r!   get_job_infor   r#   r   r0   r   r1   r2   r3   )r&   responser5   s      r'   get_job_statuszJenkinsJob.get_job_status   s    	~{{//		:Hh&& '!233 	~KK!!&KiXYl&Zfofzfzf|!}}	~s   4A A 	B;BBc                    	 t        | j                  j                  | j                              S # t        $ rR}| j
                  j                  dt        |      d| j                  t        j                                Y d }~y d }~ww xY w)Nz"Unable to validate if job exists,  for r+   )boolr!   
job_existsr   r0   r   r1   r   r   r2   r3   r4   s     r'   r>   zJenkinsJob.job_exists   sw    	D..tyy9:: 	DKK!!W`abWceieueu&v,5,@,@,B " D D	Ds   -0 	BABBc                 ,    t        | j                        S )N)job_config_to_stringr   r&   s    r'   
get_configzJenkinsJob.get_config   s    #DKK00r)   c                 |    t        | j                  j                  | j                        j	                  d            S )Nzutf-8)r@   r!   get_job_configr   encoderA   s    r'   get_current_configzJenkinsJob.get_current_config   s,    #DKK$>$>tyy$I$P$PQX$YZZr)   c                     | j                   y| j                         }| j                         }|| j                  d   d<   || j                  d   d<   ||k7  ryy)NFr   r   r   T)r   rB   rF   r"   )r&   config_filemachine_files      r'   has_config_changedzJenkinsJob.has_config_changed   s]    ;;oo'..0'2FG$(4FH%;&r)   c                     | j                   (| j                  | j                  j                  d       | j	                         s| j                          y | j                          y )NzHone of the following params is required on state=present: config,enabledr,   )r   r   r   r1   r>   
create_job
update_jobrA   s    r'   present_jobzJenkinsJob.present_job  sI    ;;4<<#7KK!!&p!q OOOOr)   c                 t    | j                   y| j                   du xr |dk7  xs | j                   du xr |dk(  S )NFdisabledT)r   )r&   statuss     r'   has_state_changedzJenkinsJob.has_state_changed  sC    <<%>&J*>rDLLTXDXDq]cgq]qrr)   c                     | j                   du r&| j                  j                  | j                         y | j                  j	                  | j                         y )NF)r   r!   disable_jobr   
enable_jobrA   s    r'   switch_statezJenkinsJob.switch_state  s:    <<5 KK##DII.KK""499-r)   c                 h   	 | j                         }| j                         rZd| j                  d<   | j                  j                  s| j
                  j                  | j                  | j                                y || j                  k7  rI| j                  |      r7d| j                  d<   | j                  j                  s| j                          y y y y y # t        $ rR}| j                  j                  dt        |      d| j                  t!        j"                                Y d }~y d }~ww xY w)NTr   zUnable to reconfigure job, r<   r+   )r:   rJ   r"   r   
check_moder!   reconfig_jobr   rB   r#   rS   rW   r0   r1   r   r   r2   r3   )r&   rR   r5   s      r'   rN   zJenkinsJob.update_job  s   	D((*F &&()-I&{{--KK,,TYY8IJ 4??*t/E/Ef/M)-I&{{--%%' . 0N*	 .  	DKK!!PYZ[P\^b^n^n&o,5,@,@,B " D D	Ds    A9C <AC 	D1AD,,D1c                    | j                   | j                  j                  d       d| j                  d<   	 | j	                         }|| j                  d   d<   | j                  j
                  s'| j                  j                  | j                  |       y y # t        $ rR}| j                  j                  dt        |      d| j                  t        j                         	       Y d }~y d }~ww xY w)
Nzmissing required param: configrL   Tr   r   r   zUnable to create job, r<   r+   )r   r   r1   r"   rB   rY   r!   rM   r   r0   r   r   r2   r3   )r&   rH   r5   s      r'   rM   zJenkinsJob.create_job0  s    ;;KK!!&F!G!%I	D//+K+6DKK(;;))&&tyy+> * 	DKK!!9UV<Y]YiYi&j,5,@,@,B " D D	Ds   AB 	C4"AC//C4c                    | j                         rmd| j                  d<   | j                         | j                  d   d<   | j                  j                  s'	 | j
                  j                  | j                         y y y # t        $ rR}| j                  j                  dt        |      d| j                  t        j                                Y d }~y d }~ww xY w)NTr   r   r   zUnable to delete job, r<   r+   )r>   r"   rF   r   rY   r!   
delete_jobr   r0   r1   r   r   r2   r3   r4   s     r'   
absent_jobzJenkinsJob.absent_job>  s    ??%)DKK	",0,C,C,EDKK);;))LKK**4995 *  ! LKK))S\]^S_aeaqaq.r4=4H4H4J * L LLs   %A? ?	CACCc                 x    | j                   }| j                         r| j                         dk7  |d<   |S d |d<   |S )NrQ   r   )r"   r>   r:   )r&   r"   s     r'   
get_resultzJenkinsJob.get_resultI  sE    ?? $ 3 3 5 CF9  !%F9r)   N)__name__
__module____qualname__r(   r    r:   r>   rB   rF   rJ   rO   rS   rW   rN   rM   r^   r`    r)   r'   r
   r
      sP    2:B	~D1[s.D(D	Lr)   r
   c                 V    t         s#| j                  t        dd      t               y y )Nzpython-jenkinsz<https://python-jenkins.readthedocs.io/en/latest/install.html)r   r+   )python_jenkins_installedr1   r   JENKINS_IMP_ERR)r   s    r'   test_dependenciesrh   R  s/    #$%5)gi% 	 	' $r)   c                 p    t        j                  t        j                  |             j                  d      S )Nascii)ETtostring
fromstringdecode)xml_strs    r'   r@   r@   Z  s%    ;;r}}W-.55g>>r)   c                     t        t        t        dd      t        dd      t        ddd      t        ddddgd      t        dd	
      t        ddd      t        ddd      t        dd      t        d	d      	      ddgddggd      } t        |        t        |       }| j                  j                  d      dk(  r|j                          n|j                          |j                         } | j                  di | y )NstrF)typerequiredT)rr   rs   no_logpresentabsent)rr   rs   choicesdefaultr=   )rs   rr   zhttp://localhost:8080)rr   rs   rx   )rr   rx   )	r   r   r   r   r   r   r   r   r   r   r   r   r   )argument_specmutually_exclusivesupports_check_moder   rd   )
r   dictrh   r
   r   r   rO   r^   r`   	exit_json)r   jenkins_jobr"   s      r'   mainr   ^  s    UU3540uuTBEEIx;PZcd%f5EE$?%%9PQ551VT:

 !y!
 !!F& fV$K}}!Y.! ##%FFvr)   __main__)
__future__r   r   r   rr   __metaclass__DOCUMENTATIONEXAMPLESRETURNr$   r2   xml.etree.ElementTreeetreeElementTreerk   rg   r.   rf   ImportErrorr3   ansible.module_utils.basicr   r   +ansible.module_utils.common.text.convertersr   objectr
   rh   r@   r   ra   rd   r)   r'   <module>r      s    A @HT0d
8 
  " "%#
 K A\ \~'?@ zF g  %*i**,O$%s   A A87A8