jenkins_job: implement diff support

pull/18777/head
Rene Moser 8 years ago committed by Matt Clay
parent 171b71cfa0
commit 3c04ed977d

@ -159,6 +159,10 @@ class Jenkins:
self.user = user self.user = user
self.jenkins_url = url self.jenkins_url = url
self.server = self.get_jenkins_connection() self.server = self.get_jenkins_connection()
self.diff = {
'before': "",
'after': "",
}
def get_jenkins_connection(self): def get_jenkins_connection(self):
try: try:
@ -204,7 +208,9 @@ class Jenkins:
changed = False changed = False
config_file = self.get_config() config_file = self.get_config()
self.diff['after'] = config_file
machine_file = job_config_to_string(self.server.get_job_config(self.name).encode('utf-8')) machine_file = job_config_to_string(self.server.get_job_config(self.name).encode('utf-8'))
self.diff['before'] = machine_file
if machine_file != config_file: if machine_file != config_file:
changed = True changed = True
return changed return changed
@ -256,29 +262,31 @@ class Jenkins:
e = get_exception() e = get_exception()
module.fail_json(msg='Unable to reconfigure job, %s for %s' % (str(e), self.jenkins_url)) module.fail_json(msg='Unable to reconfigure job, %s for %s' % (str(e), self.jenkins_url))
module.exit_json(changed=changed, name=self.name, state=self.state, url=self.jenkins_url) module.exit_json(changed=changed, name=self.name, state=self.state, url=self.jenkins_url, diff=self.diff)
def create_job(self, module): def create_job(self, module):
if self.config is None: if self.config is None:
module.fail_json(msg='missing required param: config') module.fail_json(msg='missing required param: config')
changed = False
try:
changed = True changed = True
try:
config_file = self.get_config()
self.diff['after'] = config_file
if not module.check_mode: if not module.check_mode:
self.server.create_job(self.name, self.get_config()) self.server.create_job(self.name, config_file)
self.change_state() self.change_state()
except Exception: except Exception:
e = get_exception() e = get_exception()
module.fail_json(msg='Unable to create job, %s for %s' % (str(e), self.jenkins_url)) module.fail_json(msg='Unable to create job, %s for %s' % (str(e), self.jenkins_url))
module.exit_json(changed=changed, name=self.name, state=self.state, url=self.jenkins_url) module.exit_json(changed=changed, name=self.name, state=self.state, url=self.jenkins_url, diff=self.diff)
def delete_job(self, module): def delete_job(self, module):
changed = False changed = False
if self.job_exists(module): if self.job_exists(module):
changed = True changed = True
self.diff['before'] = job_config_to_string(self.server.get_job_config(self.name).encode('utf-8'))
if not module.check_mode: if not module.check_mode:
try: try:
self.server.delete_job(self.name) self.server.delete_job(self.name)
@ -286,7 +294,7 @@ class Jenkins:
e = get_exception() e = get_exception()
module.fail_json(msg='Unable to delete job, %s for %s' % (str(e), self.jenkins_url)) module.fail_json(msg='Unable to delete job, %s for %s' % (str(e), self.jenkins_url))
module.exit_json(changed=changed, name=self.name, state=self.state, url=self.jenkins_url) module.exit_json(changed=changed, name=self.name, state=self.state, url=self.jenkins_url, diff=self.diff)
def test_dependencies(module): def test_dependencies(module):
if not python_jenkins_installed: if not python_jenkins_installed:

Loading…
Cancel
Save