From 582842f54f74d71be215ae49a312029475740ea7 Mon Sep 17 00:00:00 2001 From: Matthew Jones Date: Tue, 19 May 2015 15:48:45 -0400 Subject: [PATCH] Add the ability to pass extra_vars when launching callback jobs. Update the docs to reflect passing json for extra_vars and the host config key --- .../templates/api/job_template_callback.md | 8 +++++-- awx/api/views.py | 9 ++++++-- awx/main/tests/jobs/jobs_monolithic.py | 23 ++++++++++++------- 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/awx/api/templates/api/job_template_callback.md b/awx/api/templates/api/job_template_callback.md index 2a62cf9d36..99ae79b42a 100644 --- a/awx/api/templates/api/job_template_callback.md +++ b/awx/api/templates/api/job_template_callback.md @@ -8,11 +8,15 @@ job template. For example, using curl: - curl --data-urlencode host_config_key=HOST_CONFIG_KEY http://server/api/v1/job_templates/N/callback/ + curl -H "Content-Type: application/json" -d '{"host_config_key": "HOST_CONFIG_KEY"}' http://server/api/v1/job_templates/N/callback/ Or using wget: - wget -O /dev/null --post-data="host_config_key=HOST_CONFIG_KEY" http://server/api/v1/job_templates/N/callback/ + wget -O /dev/null --post-data='{"host_config_key": "HOST_CONFIG_KEY"}' --header=Content-Type:application/json http://server/api/v1/job_templates/N/callback/ + +You may also pass `extra_vars` to the callback: + + curl -H "Content-Type: application/json" -d '{"host_config_key": "HOST_CONFIG_KEY", "extra_vars": {"key": "value"}}' http://server/api/v1/job_templates/N/callback/ The response will return status 202 if the request is valid, 403 for an invalid host config key, or 400 if the host cannot be determined from the diff --git a/awx/api/views.py b/awx/api/views.py index 3da808b3e8..3e56b8a0ae 100644 --- a/awx/api/views.py +++ b/awx/api/views.py @@ -1768,6 +1768,9 @@ class JobTemplateCallback(GenericAPIView): return Response(data) def post(self, request, *args, **kwargs): + extra_vars = None + if request.content_type == "application/json": + extra_vars = request.DATA.get("extra_vars", None) # Permission class should have already validated host_config_key. job_template = self.get_object() # Attempt to find matching hosts based on remote address. @@ -1822,8 +1825,10 @@ class JobTemplateCallback(GenericAPIView): job = job_template.create_job(limit=limit, launch_type='callback') # Send a signal to celery that the job should be started. - isau = inventory_sources_already_updated - result = job.signal_start(inventory_sources_already_updated=isau) + kv = {"inventory_sources_already_updated": inventory_sources_already_updated} + if extra_vars is not None: + kv['extra_vars'] = extra_vars + result = job.signal_start(**kv) if not result: data = dict(msg='Error starting job!') return Response(data, status=status.HTTP_400_BAD_REQUEST) diff --git a/awx/main/tests/jobs/jobs_monolithic.py b/awx/main/tests/jobs/jobs_monolithic.py index 589ae3bb8a..6002e1ddd4 100644 --- a/awx/main/tests/jobs/jobs_monolithic.py +++ b/awx/main/tests/jobs/jobs_monolithic.py @@ -754,6 +754,13 @@ class JobTemplateCallbackTest(BaseJobTestMixin, django.test.LiveServerTestCase): self.assertEqual(job.hosts.count(), 1) self.assertEqual(job.hosts.all()[0], host) + # Run the callback job again with extra vars and verify their presence + data.update(dict(extra_vars=dict(key="value"))) + result = self.post(url, data, expect=202, remote_addr=host_ip) + jobs_qs = job_template.jobs.filter(launch_type='callback').order_by('-pk') + job = jobs_qs[0] + self.assertTrue("key" in job.extra_vars) + # GET as unauthenticated user will prompt for authentication. self.get(url, expect=401, remote_addr=host_ip) @@ -797,9 +804,9 @@ class JobTemplateCallbackTest(BaseJobTestMixin, django.test.LiveServerTestCase): if host_ip: break self.assertTrue(host) - self.assertEqual(jobs_qs.count(), 1) - self.post(url, data, expect=202, remote_addr=host_ip) self.assertEqual(jobs_qs.count(), 2) + self.post(url, data, expect=202, remote_addr=host_ip) + self.assertEqual(jobs_qs.count(), 3) job = jobs_qs[0] self.assertEqual(job.launch_type, 'callback') self.assertEqual(job.limit, host.name) @@ -822,9 +829,9 @@ class JobTemplateCallbackTest(BaseJobTestMixin, django.test.LiveServerTestCase): if host_ip: break self.assertTrue(host) - self.assertEqual(jobs_qs.count(), 2) - self.post(url, data, expect=202, remote_addr=host_ip) self.assertEqual(jobs_qs.count(), 3) + self.post(url, data, expect=202, remote_addr=host_ip) + self.assertEqual(jobs_qs.count(), 4) job = jobs_qs[0] self.assertEqual(job.launch_type, 'callback') self.assertEqual(job.limit, host.name) @@ -836,9 +843,9 @@ class JobTemplateCallbackTest(BaseJobTestMixin, django.test.LiveServerTestCase): host_qs = host_qs.filter(variables__icontains='ansible_ssh_host') host = host_qs[0] host_ip = host.variables_dict['ansible_ssh_host'] - self.assertEqual(jobs_qs.count(), 3) - self.post(url, data, expect=202, remote_addr=host_ip) self.assertEqual(jobs_qs.count(), 4) + self.post(url, data, expect=202, remote_addr=host_ip) + self.assertEqual(jobs_qs.count(), 5) job = jobs_qs[0] self.assertEqual(job.launch_type, 'callback') self.assertEqual(job.limit, host.name) @@ -868,9 +875,9 @@ class JobTemplateCallbackTest(BaseJobTestMixin, django.test.LiveServerTestCase): host_ip = list(ips)[0] break self.assertTrue(host) - self.assertEqual(jobs_qs.count(), 4) - self.post(url, data, expect=202, remote_addr=host_ip) self.assertEqual(jobs_qs.count(), 5) + self.post(url, data, expect=202, remote_addr=host_ip) + self.assertEqual(jobs_qs.count(), 6) job = jobs_qs[0] self.assertEqual(job.launch_type, 'callback') self.assertEqual(job.limit, ':&'.join([job_template.limit, host.name]))