Skip to content

Commit

Permalink
remove all resources
Browse files Browse the repository at this point in the history
spec consistency
  • Loading branch information
Jillian Tullo committed Apr 3, 2017
1 parent 88669e4 commit 815bd37
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 2 deletions.
9 changes: 9 additions & 0 deletions app/controllers/api/services_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ def remove_resource_resource(type, id, data)
action_result(false, err.to_s)
end

def remove_all_resources_resource(type, id, _data)
raise "Must specify a service href or id to remove resources from" unless id
svc = resource_search(id, type, collection_class(type))
svc.remove_all_resources
action_result(true, "Removed all resources from #{service_ident(svc)}")
rescue => err
action_result(false, err.to_s)
end

def reconfigure_resource(type, id = nil, data = nil)
raise BadRequestError, "Must specify an id for Reconfiguring a #{type} resource" unless id

Expand Down
4 changes: 4 additions & 0 deletions config/api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1931,6 +1931,8 @@
:identifier: service_tag
- :name: add_resource
:identifier: service_edit
- :name: remove_all_resources
:identifier: service_edit
- :name: remove_resource
:identifier: service_edit
:resource_actions:
Expand Down Expand Up @@ -1958,6 +1960,8 @@
:identifier: service_delete
- :name: add_resource
:identifier: service_edit
- :name: remove_all_resources
:identifier: service_edit
- :name: remove_resource
:identifier: service_edit
:delete:
Expand Down
4 changes: 2 additions & 2 deletions spec/requests/api/custom_actions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def expect_result_to_have_custom_actions_hash
run_get services_url(svc1.id)

expect_result_to_have_keys(%w(id href actions))
expect(response.parsed_body["actions"].collect { |a| a["name"] }).to match_array(%w(edit add_resource remove_resource))
expect(response.parsed_body["actions"].collect { |a| a["name"] }).to match_array(%w(edit add_resource remove_resource remove_all_resources))
end
end

Expand All @@ -91,7 +91,7 @@ def expect_result_to_have_custom_actions_hash
run_get services_url(svc1.id)

expect_result_to_have_keys(%w(id href actions))
expect(response.parsed_body["actions"].collect { |a| a["name"] }).to match_array(%w(edit button1 button2 button3 add_resource remove_resource))
expect(response.parsed_body["actions"].collect { |a| a["name"] }).to match_array(%w(edit button1 button2 button3 add_resource remove_resource remove_all_resources))
end

it "supports the custom_actions attribute" do
Expand Down
66 changes: 66 additions & 0 deletions spec/requests/api/services_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -928,4 +928,70 @@ def expect_svc_with_vms
expect(svc.reload.service_resources).to eq([])
end
end

describe 'remove_all_resources' do
let(:vm1) { FactoryGirl.create(:vm_vmware) }
let(:vm2) { FactoryGirl.create(:vm_vmware) }
let(:vm3) { FactoryGirl.create(:vm_vmware) }

before do
svc.add_resource(vm1)
svc.add_resource(vm2)
svc1.add_resource(vm3)
end

it 'cannot remove all resources without an appropriate role' do
api_basic_authorize

run_post(services_url, 'action' => 'remove_all_resources')

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

it 'can remove all resources from multiple services' do
api_basic_authorize collection_action_identifier(:services, :remove_all_resources)
request = {
'action' => 'remove_all_resources',
'resources' => [
{ 'href' => services_url(svc.id) },
{ 'href' => services_url(svc1.id) }
]
}

run_post(services_url, request)

expected = {
'results' => [
{ 'success' => true, 'message' => "Removed all resources from Service id:#{svc.id} name:'#{svc.name}'"},
{ 'success' => true, 'message' => "Removed all resources from Service id:#{svc1.id} name:'#{svc1.name}'"}
]
}
expect(response).to have_http_status(:ok)
expect(response.parsed_body).to eq(expected)
expect(svc.reload.service_resources).to eq([])
expect(svc1.reload.service_resources).to eq([])
end

it 'cannot remove all resources without an appropriate role' do
api_basic_authorize

run_post(services_url(svc.id), :action => 'remove_all_resources')

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

it 'can remove all resources from a service' do
api_basic_authorize collection_action_identifier(:services, :remove_all_resources)

run_post(services_url(svc.id), :action => 'remove_all_resources')

expected = {
'success' => true, 'message' => "Removed all resources from Service id:#{svc.id} name:'#{svc.name}'"
}

expect(response).to have_http_status(:ok)
expect(response.parsed_body).to eq(expected)
expect(svc.reload.service_resources).to eq([])
end
end
end

0 comments on commit 815bd37

Please sign in to comment.