Skip to content

Commit

Permalink
Merge pull request ManageIQ#410 from douglasgabriel/ph_chassis_actions
Browse files Browse the repository at this point in the history
Adding Physical Chassis Location LED actions
  • Loading branch information
gtanzillo authored Aug 2, 2018
2 parents 4eea3ed + 88a46c4 commit b1c8463
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 32 deletions.
38 changes: 9 additions & 29 deletions app/controllers/api/physical_chassis_controller.rb
Original file line number Diff line number Diff line change
@@ -1,42 +1,22 @@
module Api
class PhysicalChassisController < BaseController
include Subcollections::EventStreams
include Api::Mixins::Operations

def refresh_resource(type, id, _data = nil)
raise BadRequestError, "Must specify an id for refreshing a #{type} resource" if id.blank?

ensure_resource_exists(type, id) if single_resource?

api_action(type, id) do |klass|
physical_chassis = resource_search(id, type, klass)
api_log_info("Refreshing #{physical_chassis_ident(physical_chassis)}")
refresh_physical_chassis(physical_chassis)
end
end

private

def ensure_resource_exists(type, id)
raise NotFoundError, "#{type} with id:#{id} not found" unless collection_class(type).exists?(id)
def blink_loc_led_resource(type, id, _data)
perform_action(:blink_loc_led, type, id)
end

def refresh_physical_chassis(physical_chassis)
method_name = "refresh_ems"
role = "ems_operations"

act_refresh(physical_chassis, method_name, role)
rescue => err
action_result(false, err.to_s)
def turn_on_loc_led_resource(type, id, _data)
perform_action(:turn_on_loc_led, type, id)
end

def physical_chassis_ident(physical_chassis)
"Physical Chassis id:#{physical_chassis.id} name:'#{physical_chassis.name}'"
def turn_off_loc_led_resource(type, id, _data)
perform_action(:turn_off_loc_led, type, id)
end

def act_refresh(physical_chassis, method_name, role)
desc = "#{physical_chassis_ident(physical_chassis)} refreshing"
task_id = queue_object_action(physical_chassis, desc, :method_name => method_name, :role => role)
action_result(true, desc, :task_id => task_id)
def refresh_resource(type, id, _data = nil)
perform_action(:refresh_ems, type, id)
end
end
end
12 changes: 12 additions & 0 deletions config/api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1783,13 +1783,25 @@
:post:
- :name: refresh
:identifier: physical_chassis_refresh
- :name: blink_loc_led
:identifier: physical_chassis_blink_loc_led
- :name: turn_on_loc_led
:identifier: physical_chassis_turn_on_loc_led
- :name: turn_off_loc_led
:identifier: physical_chassis_turn_off_loc_led
:resource_actions:
:get:
- :name: read
:identifier: physical_chassis_show
:post:
- :name: refresh
:identifier: physical_chassis_refresh
- :name: blink_loc_led
:identifier: physical_chassis_blink_loc_led
- :name: turn_on_loc_led
:identifier: physical_chassis_turn_on_loc_led
- :name: turn_off_loc_led
:identifier: physical_chassis_turn_off_loc_led
:physical_racks:
:description: Physical Racks
:identifier: physical_rack
Expand Down
105 changes: 102 additions & 3 deletions spec/requests/physical_chassis_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@

post(api_one_physical_chassis_url(nil, physical_chassis), :params => gen_request(:refresh))

expect_single_action_result(:success => true, :message => /#{physical_chassis.id}.* refreshing/i, :href => api_one_physical_chassis_url(nil, physical_chassis))
expect_single_action_result(:success => true, :message => "Performing refresh_ems for Physical chassis id:#{physical_chassis.id} name: '#{physical_chassis.name}'", :href => api_one_physical_chassis_url(nil, physical_chassis))
end

it "refresh of multiple Physical Chassis" do
Expand All @@ -81,12 +81,12 @@
expected = {
"results" => a_collection_containing_exactly(
a_hash_including(
"message" => a_string_matching(/#{physical_chassis.id}.* refreshing/i),
"message" => "Performing refresh_ems for Physical chassis id:#{physical_chassis.id} name: '#{physical_chassis.name}'",
"success" => true,
"href" => api_one_physical_chassis_url(nil, physical_chassis)
),
a_hash_including(
"message" => a_string_matching(/#{physical_chassis_two.id}.* refreshing/i),
"message" => "Performing refresh_ems for Physical chassis id:#{physical_chassis_two.id} name: '#{physical_chassis_two.name}'",
"success" => true,
"href" => api_one_physical_chassis_url(nil, physical_chassis_two)
)
Expand Down Expand Up @@ -146,4 +146,103 @@
end
end
end

describe "turn on/off a physical chassis's location LED" do
let(:physical_chassis) { FactoryGirl.create(:physical_chassis) }
let(:actions) { %i(blink_loc_led turn_on_loc_led turn_off_loc_led) }

context "with valid action names" do
it "turns on a location LED successfully" do
api_basic_authorize('physical_chassis_turn_on_loc_led')
post(api_one_physical_chassis_url(nil, physical_chassis), :params => gen_request(:turn_on_loc_led))

expect(response).to have_http_status(:success)
expect(response.parsed_body).to include("success" => true)
end

it "turns off a location LED successfully" do
api_basic_authorize('physical_chassis_turn_off_loc_led')
post(api_one_physical_chassis_url(nil, physical_chassis), :params => gen_request(:turn_off_loc_led))

expect(response).to have_http_status(:success)
expect(response.parsed_body).to include("success" => true)
end

it "blinks a location LED successfully" do
api_basic_authorize('physical_chassis_blink_loc_led')
post(api_one_physical_chassis_url(nil, physical_chassis), :params => gen_request(:blink_loc_led))

expect(response).to have_http_status(:success)
expect(response.parsed_body).to include("success" => true)
end
end

context "without an appropriate role" do
it "fails to turn on a location LED" do
api_basic_authorize
post(api_one_physical_chassis_url(nil, physical_chassis), :params => gen_request(:turn_on_loc_led))

expect(response).to have_http_status(:forbidden)
expect(response.parsed_body["error"]).to include("kind" => "forbidden")
end

it "fails to turn off a location LED" do
api_basic_authorize
post(api_one_physical_chassis_url(nil, physical_chassis), :params => gen_request(:turn_off_loc_led))

expect(response).to have_http_status(:forbidden)
expect(response.parsed_body["error"]).to include("kind" => "forbidden")
end

it "fails to blink a location LED" do
api_basic_authorize
post(api_one_physical_chassis_url(nil, physical_chassis), :params => gen_request(:blink_loc_led))

expect(response).to have_http_status(:forbidden)
expect(response.parsed_body["error"]).to include("kind" => "forbidden")
end
end

context "with a non existent physical chassis" do
actions = %i(blink_loc_led turn_on_loc_led turn_off_loc_led)

actions.each do |action|
it "fails to #{action} a physical chassis" do
api_basic_authorize("physical_chassis_#{action}")

post(api_one_physical_chassis_url(nil, 999_999), :params => gen_request(action))

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

context "with an existent and a non existent physical server" do
actions = %i(blink_loc_led turn_on_loc_led turn_off_loc_led)

actions.each do |action|
it "for the action #{action} returns status 200 and a failure message for the non existent physical chassis" do
api_basic_authorize(action_identifier(:physical_chassis, action, :resource_actions, :post))

post(api_physical_chassis_url, :params => gen_request(action, [{"href" => api_one_physical_chassis_url(nil, physical_chassis)}, {"href" => api_one_physical_chassis_url(nil, 999_999)}]))

expected = {
"results" => a_collection_containing_exactly(
a_hash_including(
"message" => a_string_matching(/#{physical_chassis.id}/),
"success" => true,
"href" => api_one_physical_chassis_url(nil, physical_chassis)
),
a_hash_including(
"message" => a_string_matching(/#{999_999}/),
"success" => false
)
)
}
expect(response.parsed_body).to include(expected)
expect(response).to have_http_status(:ok)
end
end
end
end
end

0 comments on commit b1c8463

Please sign in to comment.