Skip to content

Commit

Permalink
multiple task results
Browse files Browse the repository at this point in the history
update format of task ids being returned
  • Loading branch information
Jillian Tullo committed Apr 10, 2017
1 parent 07f7621 commit 43b0c54
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 14 deletions.
14 changes: 13 additions & 1 deletion app/controllers/api/base_controller/results.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ def action_result(success, message = nil, options = {})
res[:message] = message if message.present?
res[:result] = options[:result] unless options[:result].nil?
add_task_to_result(res, options[:task_id]) if options[:task_id].present?
add_tasks_to_result(res, options[:task_ids]) if options[:task_ids].present?
res
end

Expand All @@ -23,17 +24,28 @@ def add_parent_href_to_result(hash)

def add_task_to_result(hash, task_id)
hash[:task_id] = task_id
hash[:task_href] = "#{@req.api_prefix}/tasks/#{task_id}"
hash[:task_href] = task_href(task_id)
hash
end

def add_tasks_to_result(hash, task_ids)
add_task_to_result(hash, task_ids.first)
hash[:tasks] = task_ids.collect do |task_id|
{ :id => task_id, :href => task_href(task_id) }
end
end

def add_tag_to_result(hash, tag_spec)
hash[:tag_category] = tag_spec[:category] if tag_spec[:category].present?
hash[:tag_name] = tag_spec[:name] if tag_spec[:name].present?
hash[:tag_href] = "#{@req.api_prefix}/tags/#{tag_spec[:id]}" if tag_spec[:id].present?
hash
end

def task_href(task_id)
"#{@req.api_prefix}/tasks/#{task_id}"
end

def add_subcollection_resource_to_result(hash, ctype, object)
return hash if object.blank?
ctype_pref = ctype.to_s.singularize
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ def create_resource(_type, _id, data)

def refresh_resource(type, id, _data)
config_script_src = resource_search(id, type, collection_class(type))
task_id = EmsRefresh.queue_refresh_task(config_script_src).first
action_result(true, "Refreshing #{config_script_src_ident(config_script_src)}", :task_id => task_id)
task_ids = EmsRefresh.queue_refresh_task(config_script_src)
action_result(true, "Refreshing #{config_script_src_ident(config_script_src)}", :task_ids => task_ids)
rescue => err
action_result(false, err.to_s)
end
Expand Down
28 changes: 17 additions & 11 deletions spec/requests/api/configuration_script_sources_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,19 +130,23 @@
it 'can refresh multiple configuration_script_source with an appropriate role' do
api_basic_authorize collection_action_identifier(:configuration_script_sources, :refresh, :post)

run_post(configuration_script_sources_url, :action => 'refresh', :resources => [{ :id => config_script_src.id}, {:id => config_script_src_2.id}])
run_post(configuration_script_sources_url, :action => :refresh, :resources => [{ :id => config_script_src.id}, {:id => config_script_src_2.id}])

expected = {
'results' => [
a_hash_including(
'success' => true,
'message' => a_string_including("Refreshing ConfigurationScriptSource id:#{config_script_src.id}"),
'task_id' => a_kind_of(Numeric)
'success' => true,
'message' => a_string_including("Refreshing ConfigurationScriptSource id:#{config_script_src.id}"),
'task_id' => a_kind_of(Numeric),
'task_href' => /task/,
'tasks' => [a_hash_including('id' => a_kind_of(Numeric), 'href' => /task/)]
),
a_hash_including(
'success' => true,
'message' => a_string_including("Refreshing ConfigurationScriptSource id:#{config_script_src_2.id}"),
'task_id' => a_kind_of(Numeric)
'success' => true,
'message' => a_string_including("Refreshing ConfigurationScriptSource id:#{config_script_src_2.id}"),
'task_id' => a_kind_of(Numeric),
'task_href' => /task/,
'tasks' => [a_hash_including('id' => a_kind_of(Numeric), 'href' => /task/)]
)
]
}
Expand Down Expand Up @@ -253,12 +257,14 @@
it 'can refresh a configuration_script_source with an appropriate role' do
api_basic_authorize action_identifier(:configuration_script_sources, :refresh)

run_post(configuration_script_sources_url(config_script_src.id), :action => 'refresh')
run_post(configuration_script_sources_url(config_script_src.id), :action => :refresh)

expected = {
'success' => true,
'message' => /Refreshing ConfigurationScriptSource/,
'task_id' => a_kind_of(Numeric)
'success' => true,
'message' => /Refreshing ConfigurationScriptSource/,
'task_id' => a_kind_of(Numeric),
'task_href' => /task/,
'tasks' => [a_hash_including('id' => a_kind_of(Numeric), 'href' => /tasks/)]
}
expect(response).to have_http_status(:ok)
expect(response.parsed_body).to include(expected)
Expand Down

0 comments on commit 43b0c54

Please sign in to comment.