Skip to content

Commit

Permalink
Merge pull request #14714 from jntullo/enhancement/project_refresh
Browse files Browse the repository at this point in the history
Refresh Configuration Script Sources action
  • Loading branch information
abellotti authored Apr 11, 2017
2 parents c5956c8 + 3f8c190 commit e284b35
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 1 deletion.
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 @@ -31,6 +31,14 @@ def create_resource(_type, _id, data)
action_result(false, err.to_s)
end

def refresh_resource(type, id, _data)
config_script_src = resource_search(id, type, collection_class(type))
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

private

def config_script_src_ident(config_script_src)
Expand Down
4 changes: 4 additions & 0 deletions config/api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,8 @@
:identifier: embedded_configuration_script_source_delete
- :name: create
:identifier: embedded_configuration_script_source_add
- :name: refresh
:identifier: embedded_configuration_script_source_refresh
:resource_actions:
:get:
- :name: read
Expand All @@ -630,6 +632,8 @@
:identifier: embedded_configuration_script_source_edit
- :name: delete
:identifier: embedded_configuration_script_source_delete
- :name: refresh
:identifier: embedded_configuration_script_source_refresh
:delete:
- :name: delete
:identifier: embedded_configuration_script_source_delete
Expand Down
51 changes: 51 additions & 0 deletions spec/requests/api/configuration_script_sources_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,33 @@

expect(response).to have_http_status(:forbidden)
end

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}])

expected = {
'results' => [
a_hash_including(
'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),
'task_href' => /task/,
'tasks' => [a_hash_including('id' => a_kind_of(Numeric), 'href' => /task/)]
)
]
}
expect(response).to have_http_status(:ok)
expect(response.parsed_body).to include(expected)
end
end

describe 'PUT /api/configuration_script_sources/:id' do
Expand Down Expand Up @@ -219,6 +246,30 @@
expect(response).to have_http_status(:forbidden)
end

it 'forbids refresh without an appropriate role' do
api_basic_authorize

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

expect(response).to have_http_status(:forbidden)
end

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)

expected = {
'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)
end

it 'can delete a configuration_script_source with an appropriate role' do
api_basic_authorize action_identifier(:configuration_script_sources, :delete)

Expand Down

0 comments on commit e284b35

Please sign in to comment.