From 07f7621185e892328910d236af397ded2084481e Mon Sep 17 00:00:00 2001 From: Jillian Tullo Date: Mon, 10 Apr 2017 12:14:49 -0400 Subject: [PATCH 1/3] refresh configuration script sources --- ...configuration_script_sources_controller.rb | 8 ++++ config/api.yml | 4 ++ .../api/configuration_script_sources_spec.rb | 45 +++++++++++++++++++ 3 files changed, 57 insertions(+) diff --git a/app/controllers/api/configuration_script_sources_controller.rb b/app/controllers/api/configuration_script_sources_controller.rb index d9e8c2caec8..878ccef8e67 100644 --- a/app/controllers/api/configuration_script_sources_controller.rb +++ b/app/controllers/api/configuration_script_sources_controller.rb @@ -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_id = EmsRefresh.queue_refresh_task(config_script_src).first + action_result(true, "Refreshing #{config_script_src_ident(config_script_src)}", :task_id => task_id) + rescue => err + action_result(false, err.to_s) + end + private def config_script_src_ident(config_script_src) diff --git a/config/api.yml b/config/api.yml index 141da2ae9f8..571166e9dad 100644 --- a/config/api.yml +++ b/config/api.yml @@ -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_edit :resource_actions: :get: - :name: read @@ -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_edit :delete: - :name: delete :identifier: embedded_configuration_script_source_delete diff --git a/spec/requests/api/configuration_script_sources_spec.rb b/spec/requests/api/configuration_script_sources_spec.rb index 6f1e2b53158..72321f06486 100644 --- a/spec/requests/api/configuration_script_sources_spec.rb +++ b/spec/requests/api/configuration_script_sources_spec.rb @@ -126,6 +126,29 @@ 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) + ), + a_hash_including( + 'success' => true, + 'message' => a_string_including("Refreshing ConfigurationScriptSource id:#{config_script_src_2.id}"), + 'task_id' => a_kind_of(Numeric) + ) + ] + } + expect(response).to have_http_status(:ok) + expect(response.parsed_body).to include(expected) + end end describe 'PUT /api/configuration_script_sources/:id' do @@ -219,6 +242,28 @@ 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) + } + 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) From 43b0c549859715bfbc205b952c74dfcb37914c57 Mon Sep 17 00:00:00 2001 From: Jillian Tullo Date: Mon, 10 Apr 2017 13:52:02 -0400 Subject: [PATCH 2/3] multiple task results update format of task ids being returned --- .../api/base_controller/results.rb | 14 +++++++++- ...configuration_script_sources_controller.rb | 4 +-- .../api/configuration_script_sources_spec.rb | 28 +++++++++++-------- 3 files changed, 32 insertions(+), 14 deletions(-) diff --git a/app/controllers/api/base_controller/results.rb b/app/controllers/api/base_controller/results.rb index 158e51a931f..ce4eaeafb4f 100644 --- a/app/controllers/api/base_controller/results.rb +++ b/app/controllers/api/base_controller/results.rb @@ -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 @@ -23,10 +24,17 @@ 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? @@ -34,6 +42,10 @@ def add_tag_to_result(hash, tag_spec) 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 diff --git a/app/controllers/api/configuration_script_sources_controller.rb b/app/controllers/api/configuration_script_sources_controller.rb index 878ccef8e67..ffa160ff90f 100644 --- a/app/controllers/api/configuration_script_sources_controller.rb +++ b/app/controllers/api/configuration_script_sources_controller.rb @@ -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 diff --git a/spec/requests/api/configuration_script_sources_spec.rb b/spec/requests/api/configuration_script_sources_spec.rb index 72321f06486..34be375c436 100644 --- a/spec/requests/api/configuration_script_sources_spec.rb +++ b/spec/requests/api/configuration_script_sources_spec.rb @@ -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/)] ) ] } @@ -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) From 3f8c1905bf677f7be422283ac3ca022144034bac Mon Sep 17 00:00:00 2001 From: Jillian Tullo Date: Mon, 10 Apr 2017 15:33:05 -0400 Subject: [PATCH 3/3] update product feature used --- config/api.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config/api.yml b/config/api.yml index 571166e9dad..5ff234c132d 100644 --- a/config/api.yml +++ b/config/api.yml @@ -622,7 +622,7 @@ - :name: create :identifier: embedded_configuration_script_source_add - :name: refresh - :identifier: embedded_configuration_script_source_edit + :identifier: embedded_configuration_script_source_refresh :resource_actions: :get: - :name: read @@ -633,7 +633,7 @@ - :name: delete :identifier: embedded_configuration_script_source_delete - :name: refresh - :identifier: embedded_configuration_script_source_edit + :identifier: embedded_configuration_script_source_refresh :delete: - :name: delete :identifier: embedded_configuration_script_source_delete