Skip to content

Commit 18f997c

Browse files
adamruzickaclaude
andcommitted
Refs #39464 - Add include_hosts param to avoid breaking public API
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 3b1f4cd commit 18f997c

3 files changed

Lines changed: 63 additions & 0 deletions

File tree

app/controllers/api/v2/job_invocations_controller.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,17 @@ def index
1919

2020
api :GET, '/job_invocations/:id', N_('Show job invocation')
2121
param :id, :identifier, :required => true
22+
param :include_hosts, :bool, :required => false, :default_value => true, :desc => N_('Include hosts and template invocations in the response. Defaults to true for backwards compatibility, pass false to skip serializing all hosts.')
23+
param :host_status, :bool, :required => false, :default_value => false, :desc => N_('Show job status for each host, only applicable when include_hosts is true')
2224
def show
2325
@pattern_template_invocations = @job_invocation.pattern_template_invocations.includes(:input_values)
2426
@job_organization = Taxonomy.find_by(id: @job_invocation.task.input[:current_organization_id])
2527
@job_location = Taxonomy.find_by(id: @job_invocation.task.input[:current_location_id])
28+
29+
if params[:include_hosts].nil? || Foreman::Cast.to_bool(params[:include_hosts])
30+
set_hosts_and_template_invocations
31+
set_statuses_and_smart_proxies if Foreman::Cast.to_bool(params[:host_status])
32+
end
2633
end
2734

2835
def_param_group :job_invocation do
@@ -80,12 +87,14 @@ def show
8087

8188
api :POST, '/job_invocations/', N_('Create a job invocation')
8289
param_group :job_invocation, :as => :create
90+
param :include_hosts, :bool, :required => false, :default_value => true, :desc => N_('Include hosts and template invocations in the response. Defaults to true for backwards compatibility, pass false to skip serializing all hosts.')
8391
def create
8492
composer = JobInvocationComposer.from_api_params(
8593
job_invocation_params
8694
)
8795
composer.trigger!
8896
@job_invocation = composer.job_invocation
97+
set_hosts_and_template_invocations if params[:include_hosts].nil? || Foreman::Cast.to_bool(params[:include_hosts])
8998
process_response @job_invocation
9099
rescue JobInvocationComposer::JobTemplateNotFound, JobInvocationComposer::FeatureNotFound => e
91100
not_found(error: { message: e.message })

app/views/api/v2/job_invocations/main.json.rabl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,32 @@ end
1818
child :targeting do
1919
attributes :bookmark_id, :bookmark_name, :search_query, :targeting_type, :user_id, :status, :status_label,
2020
:randomized_ordering
21+
22+
if @hosts
23+
child @hosts => :hosts do
24+
extends 'api/v2/hosts/base'
25+
26+
if @host_statuses
27+
node :job_status do |host|
28+
@host_statuses[host.id]
29+
end
30+
end
31+
end
32+
end
2133
end
2234

2335
child :task do
2436
attributes :id, :state, :started_at
2537
end
38+
39+
if @template_invocations
40+
child @template_invocations => :template_invocations do
41+
attributes :template_id, :template_name, :host_id
42+
child :input_values do
43+
attributes :template_input_name, :template_input_id
44+
node :value do |iv|
45+
iv.template_input.respond_to?(:hidden_value) && iv.template_input.hidden_value? ? '*' * 5 : iv.value
46+
end
47+
end
48+
end
49+
end

test/functional/api/v2/job_invocations_controller_test.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,22 @@ class JobInvocationsControllerTest < ActionController::TestCase
3636
get :show, params: taxonomy_params.merge(:id => @invocation.id)
3737
assert_response :success
3838
end
39+
40+
test 'should include hosts by default' do
41+
get :show, params: { :id => @invocation.id }
42+
assert_response :success
43+
result = ActiveSupport::JSON.decode(@response.body)
44+
assert_not_nil result['targeting']['hosts']
45+
assert_not_nil result['template_invocations']
46+
end
47+
48+
test 'should exclude hosts when include_hosts=false' do
49+
get :show, params: { :id => @invocation.id, :include_hosts => false }
50+
assert_response :success
51+
result = ActiveSupport::JSON.decode(@response.body)
52+
assert_nil result['targeting']['hosts']
53+
assert_nil result['template_invocations']
54+
end
3955
end
4056

4157
context 'creation' do
@@ -55,6 +71,20 @@ class JobInvocationsControllerTest < ActionController::TestCase
5571
assert_response :success
5672
end
5773

74+
test 'should include hosts in create response by default' do
75+
post :create, params: { job_invocation: @attrs }
76+
invocation = ActiveSupport::JSON.decode(@response.body)
77+
assert_not_nil invocation['targeting']['hosts']
78+
assert_response :success
79+
end
80+
81+
test 'should exclude hosts from create response when include_hosts=false' do
82+
post :create, params: { job_invocation: @attrs, include_hosts: false }
83+
invocation = ActiveSupport::JSON.decode(@response.body)
84+
assert_nil invocation['targeting']['hosts']
85+
assert_response :success
86+
end
87+
5888
test 'should create with description format overridden' do
5989
@attrs[:description_format] = 'format'
6090
post :create, params: { job_invocation: @attrs }

0 commit comments

Comments
 (0)