Skip to content

Commit

Permalink
Add physical server refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
MaysaMacedo committed Nov 15, 2017
1 parent 5910269 commit 2076fac
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
22 changes: 22 additions & 0 deletions app/controllers/api/physical_servers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ def restart_mgmt_controller_resource(type, id, _data)
change_resource_state(:restart_mgmt_controller, type, id)
end

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

api_action(type, id) do |klass|
physical_server = resource_search(id, type, klass)
api_log_info("Refreshing #{physical_server_ident(physical_server)}")
refresh_physical_server(physical_server)
end
end

private

def change_resource_state(state, type, id)
Expand All @@ -61,5 +71,17 @@ def change_resource_state(state, type, id)
def server_ident(server)
"Server instance: #{server.id} name:'#{server.name}'"
end

def refresh_physical_server(physical_server)
desc = "#{physical_server_ident(physical_server)} refreshing"
task_id = queue_object_action(physical_server, 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_server_ident(physical_server)
"Physical Server id:#{physical_server.id} name:'#{physical_server.name}'"
end
end
end
4 changes: 4 additions & 0 deletions config/api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1527,6 +1527,8 @@
:identifier: physical_server_power_off
- :name: power_off_now
:identifier: physical_server_power_off_now
- :name: refresh
:identifier: physical_server_refresh
- :name: restart
:identifier: physical_server_restart
- :name: restart_now
Expand All @@ -1552,6 +1554,8 @@
:identifier: physical_server_power_off
- :name: power_off_now
:identifier: physical_server_power_off_now
- :name: refresh
:identifier: physical_server_refresh
- :name: restart
:identifier: physical_server_restart
- :name: restart_now
Expand Down
62 changes: 62 additions & 0 deletions spec/requests/physical_servers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -304,4 +304,66 @@
end
end
end

describe "Physical Server refresh action" do
context "with an invalid id" do
it "fails to refresh a physical server" do
api_basic_authorize(action_identifier(:physical_servers, :refresh, :resource_actions, :post))

post(api_physical_server_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 "fails to refresh a physical server" do
ps = FactoryGirl.create(:physical_server)

api_basic_authorize

post(api_physical_server_url(nil, ps), :params => gen_request(:refresh))

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

context "with an appropriate role" do
it "refresh of a single Physical Server" do
ps = FactoryGirl.create(:physical_server)

api_basic_authorize(action_identifier(:physical_servers, :refresh, :resource_actions, :post))

post(api_physical_server_url(nil, ps), :params => gen_request(:refresh))

expect_single_action_result(:success => true, :message => /#{ps.id}.* refreshing/i, :href => api_physical_server_url(nil, ps))
end

it "refresh of multiple Physical Servers" do
ps = FactoryGirl.create(:physical_server)
ps2 = FactoryGirl.create(:physical_server)

api_basic_authorize(action_identifier(:physical_servers, :refresh, :resource_actions, :post))

post(api_physical_servers_url, :params => gen_request(:refresh, [{"href" => api_physical_server_url(nil, ps)}, {"href" => api_physical_server_url(nil, ps2)}]))

expected = {
"results" => a_collection_containing_exactly(
a_hash_including(
"message" => a_string_matching(/#{ps.id}.* refreshing/i),
"success" => true,
"href" => api_physical_server_url(nil, ps)
),
a_hash_including(
"message" => a_string_matching(/#{ps2.id}.* refreshing/i),
"success" => true,
"href" => api_physical_server_url(nil, ps2)
)
)
}
expect(response.parsed_body).to include(expected)
expect(response).to have_http_status(:ok)
end
end
end
end

0 comments on commit 2076fac

Please sign in to comment.