diff --git a/app/controllers/api/physical_racks_controller.rb b/app/controllers/api/physical_racks_controller.rb new file mode 100644 index 0000000000..18611841b2 --- /dev/null +++ b/app/controllers/api/physical_racks_controller.rb @@ -0,0 +1,33 @@ +module Api + class PhysicalRacksController < BaseController + def refresh_resource(type, id, _data = nil) + raise BadRequestError, "Must specify an id for refreshing a #{type} resource" unless id + + ensure_resource_exists(type, id) if single_resource? + + api_action(type, id) do |klass| + physical_rack = resource_search(id, type, klass) + api_log_info("Refreshing #{physical_rack_ident(physical_rack)}") + refresh_physical_rack(physical_rack) + end + end + + private + + def ensure_resource_exists(type, id) + raise NotFoundError, "#{type} with id:#{id} not found" unless collection_class(type).exists?(id) + end + + def refresh_physical_rack(physical_rack) + desc = "#{physical_rack_ident(physical_rack)} refreshing" + task_id = queue_object_action(physical_rack, desc, :method_name => "refresh_ems", :role => "ems_operations") + action_result(true, desc, :task_id => task_id) + rescue => err + action_result(false, err.to_s) + end + + def physical_rack_ident(physical_rack) + "Physical Rack id:#{physical_rack.id} name:'#{physical_rack.name}'" + end + end +end diff --git a/config/api.yml b/config/api.yml index 6c07c3dd8d..994d952974 100644 --- a/config/api.yml +++ b/config/api.yml @@ -1762,6 +1762,30 @@ :post: - :name: refresh :identifier: physical_chassis_refresh + :physical_racks: + :description: Physical Racks + :identifier: physical_rack + :options: + - :collection + :verbs: *gp + :klass: PhysicalRack + :subcollections: + :collection_actions: + :get: + - :name: read + :identifier: physical_rack_show_list + :post: + - :name: query + :identifier: physical_rack_show_list + - :name: refresh + :identifier: physical_rack_refresh + :resource_actions: + :get: + - :name: read + :identifier: physical_rack_show + :post: + - :name: refresh + :identifier: physical_rack_refresh :physical_servers: :description: Physical Servers :identifier: physical_server diff --git a/spec/requests/physical_racks_spec.rb b/spec/requests/physical_racks_spec.rb new file mode 100644 index 0000000000..8b25af9547 --- /dev/null +++ b/spec/requests/physical_racks_spec.rb @@ -0,0 +1,100 @@ +describe "Physical Racks API" do + context "GET /api/physical_racks" do + it "returns all Physical Racks" do + physical_rack = FactoryGirl.create(:physical_rack) + api_basic_authorize('physical_rack_show_list') + + get(api_physical_racks_url) + + expected = { + "name" => "physical_racks", + "resources" => [{"href" => api_physical_rack_url(nil, physical_rack)}] + } + expect(response).to have_http_status(:ok) + expect(response.parsed_body).to include(expected) + end + end + + context "GET /api/physical_racks/:id" do + it "returns a single Physical Rack" do + physical_rack = FactoryGirl.create(:physical_rack) + api_basic_authorize('physical_rack_show') + + get(api_physical_rack_url(nil, physical_rack)) + + expected = { + "name" => physical_rack.name, + "href" => api_physical_rack_url(nil, physical_rack) + } + expect(response).to have_http_status(:ok) + expect(response.parsed_body).to include(expected) + end + end + + describe "Physical Racks refresh action" do + context "with an invalid id" do + it "it responds with 404 Not Found" do + api_basic_authorize(action_identifier(:physical_racks, :refresh, :resource_actions, :post)) + + post(api_physical_rack_url(nil, 999_999), :params => gen_request(:refresh)) + + expect(response).to have_http_status(:not_found) + end + end + + context "without an appropriate role" do + it "it responds with 403 Forbidden" do + physical_rack = FactoryGirl.create(:physical_rack) + api_basic_authorize + + post(api_physical_rack_url(nil, physical_rack), :params => gen_request(:refresh)) + + expect(response).to have_http_status(:forbidden) + end + end + + context "with an appropriate role" do + it "rejects refresh for an unspecified Physical Rack" do + api_basic_authorize(action_identifier(:physical_racks, :refresh, :resource_actions, :post)) + + post(api_physical_racks_url, :params => gen_request(:refresh, [{"href" => api_physical_racks_url}, {"href" => api_physical_racks_url}])) + + expect_bad_request(/Must specify an id/i) + end + + it "refresh of a single Physical Rack" do + physical_rack = FactoryGirl.create(:physical_rack) + api_basic_authorize('physical_rack_refresh') + + post(api_physical_rack_url(nil, physical_rack), :params => gen_request(:refresh)) + + expect_single_action_result(:success => true, :message => /#{physical_rack.id}.* refreshing/i, :href => api_physical_rack_url(nil, physical_rack)) + end + + it "refresh of multiple Physical Racks" do + first_physical_rack = FactoryGirl.create(:physical_rack) + second_physical_rack = FactoryGirl.create(:physical_rack) + api_basic_authorize('physical_rack_refresh') + + post(api_physical_racks_url, :params => gen_request(:refresh, [{"href" => api_physical_rack_url(nil, first_physical_rack)}, {"href" => api_physical_rack_url(nil, second_physical_rack)}])) + + expected = { + "results" => a_collection_containing_exactly( + a_hash_including( + "message" => a_string_matching(/#{first_physical_rack.id}.* refreshing/i), + "success" => true, + "href" => api_physical_rack_url(nil, first_physical_rack) + ), + a_hash_including( + "message" => a_string_matching(/#{second_physical_rack.id}.* refreshing/i), + "success" => true, + "href" => api_physical_rack_url(nil, second_physical_rack) + ) + ) + } + expect(response.parsed_body).to include(expected) + expect(response).to have_http_status(:ok) + end + end + end +end