Skip to content

Commit

Permalink
add_provider_vms action for Services
Browse files Browse the repository at this point in the history
  • Loading branch information
Jillian Tullo committed Oct 13, 2017
1 parent febf29e commit c99352d
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 2 deletions.
13 changes: 13 additions & 0 deletions app/controllers/api/services_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,19 @@ def suspend_resource(type, id = nil, _data = nil)
end
end

def add_provider_vms_resource(type, id, data)
service = resource_search(id, type, collection_class(type))

provider_id = parse_id(data['provider'], :providers)
raise 'Must specify a valid provider href or id' unless provider_id
provider = resource_search(provider_id, :providers, collection_class(:providers))

task_id = service.add_provider_vms(provider, data['uid_ems']).miq_task_id
action_result(true, "Adding provider vms for #{service_ident(service)}", :task_id => task_id)
rescue => err
action_result(false, err.to_s)
end

private

def validate_resource(data)
Expand Down
4 changes: 4 additions & 0 deletions config/api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2400,6 +2400,8 @@
:identifier: service_edit
- :name: remove_resource
:identifier: service_edit
- :name: add_provider_vms
:identifier: service_edit
:resource_actions:
:get:
- :name: read
Expand Down Expand Up @@ -2429,6 +2431,8 @@
:identifier: service_edit
- :name: remove_resource
:identifier: service_edit
- :name: add_provider_vms
:identifier: service_edit
:delete:
- :name: delete
:identifier: service_delete
Expand Down
4 changes: 2 additions & 2 deletions spec/requests/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
get api_service_url(nil, svc1)

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 remove_all_resources))
expect(response.parsed_body["actions"].collect { |a| a["name"] }).to match_array(%w(edit add_resource remove_resource remove_all_resources add_provider_vms))
end
end

Expand All @@ -91,7 +91,7 @@ def expect_result_to_have_custom_actions_hash
get api_service_url(nil, svc1)

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 remove_all_resources))
expect(response.parsed_body["actions"].collect { |a| a["name"] }).to match_array(%w(edit button1 button2 button3 add_resource remove_resource remove_all_resources add_provider_vms))
end

it "supports the custom_actions attribute" do
Expand Down
62 changes: 62 additions & 0 deletions spec/requests/services_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1039,6 +1039,68 @@ def expect_svc_with_vms
end
end

describe 'add_provider_vms_resource' do
it 'cannot add_provider_vms without an appropriate role' do
api_basic_authorize

post(api_service_url(nil, svc), :params => { :action => 'add_provider_vms'})

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

it 'can add the provider vms to the queue' do
api_basic_authorize action_identifier(:services, :add_provider_vms)
svc.update_attributes!(:evm_owner => @user)

post(api_service_url(nil, svc), :params => { :action => 'add_provider_vms',
:provider => { :href => api_provider_url(nil, ems) }, :uid_ems => ['uids'] })

expected = {
'success' => true,
'message' => a_string_including('Adding provider vms for Service'),
'task_id' => a_kind_of(String),
'task_href' => a_string_including('/api/tasks/')
}
expect(response).to have_http_status(:ok)
expect(response.parsed_body).to include(expected)
end

it 'requires a valid provider href' do
api_basic_authorize action_identifier(:services, :add_provider_vms)

post(api_service_url(nil, svc), :params => { :action => 'add_provider_vms',
:uid_ems => ['uids'] })

expected = {
'success' => false,
'message' => a_string_including('Must specify a valid provider href or id')
}
expect(response).to have_http_status(:ok)
expect(response.parsed_body).to include(expected)
end

it 'can bulk add_provider_vms' do
api_basic_authorize action_identifier(:services, :add_provider_vms)
svc.update_attributes!(:evm_owner => @user)
svc2.update_attributes!(:evm_owner => @user)

post(api_services_url, :params => { :action => 'add_provider_vms',
:resources => [
{ :href => api_service_url(nil, svc), :provider => { :href => api_provider_url(nil, ems) }, :uid_ems => ['uids'] },
{ :href => api_service_url(nil, svc2), :provider => { :href => api_provider_url(nil, ems) }, :uid_ems => ['uids']}
]})

expected = {
'results' => [
{'success' => true, 'message' => a_string_including("Adding provider vms for Service id:#{svc.id}"), 'task_id' => a_kind_of(String), 'task_href' => a_string_including(api_tasks_url)},
{'success' => true, 'message' => a_string_including("Adding provider vms for Service id:#{svc2.id}"), 'task_id' => a_kind_of(String), 'task_href' => a_string_including(api_tasks_url)},
]
}
expect(response).to have_http_status(:ok)
expect(response.parsed_body).to include(expected)
end
end

describe "Generic Objects Subcollection" do
let(:generic_object_definition) { FactoryGirl.create(:generic_object_definition) }
let(:generic_object) { FactoryGirl.create(:generic_object, :generic_object_definition => generic_object_definition) }
Expand Down

0 comments on commit c99352d

Please sign in to comment.