-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Alexander Demichev
committed
Sep 21, 2017
1 parent
170dee0
commit 7e54907
Showing
3 changed files
with
191 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,24 @@ | ||
module Api | ||
class FlavorsController < BaseController | ||
def create_resource(_type, _id, data) | ||
task_id = Flavor.create_flavor_queue(User.current_user.id, EmsCloud.find(data['ems']['id']), data) | ||
action_result(true, 'Creating Flavor', :task_id => task_id) | ||
rescue => err | ||
action_result(false, err.to_s) | ||
end | ||
|
||
def delete_resource(type, id, _data = {}) | ||
flavor = resource_search(id, type, collection_class(:flavors)) | ||
task_id = flavor.delete_flavor_queue(User.current_user.id) | ||
action_result(true, "Deleting #{flavor_ident(flavor)}", :task_id => task_id) | ||
rescue => err | ||
action_result(false, err.to_s) | ||
end | ||
|
||
private | ||
|
||
def flavor_ident(flavor) | ||
"Flavor id:#{flavor.id} name: '#{flavor.name}'" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
RSpec.describe 'Flavors API' do | ||
let(:ems) { FactoryGirl.create(:ems_cloud) } | ||
let(:flavor) { FactoryGirl.create(:flavor_openstack, :ext_management_system => ems) } | ||
let(:flavor_2) { FactoryGirl.create(:flavor_openstack, :ext_management_system => ems) } | ||
|
||
describe 'GET/api/flavors' do | ||
it 'lists all the flavor bases with an appropriate role' do | ||
flavor = FactoryGirl.create(:flavor_openstack) | ||
|
||
api_basic_authorize collection_action_identifier(:flavors, :read, :get) | ||
|
||
run_get(api_flavors_url) | ||
|
||
expected = { | ||
'count' => 1, | ||
'subcount' => 1, | ||
'name' => 'flavors', | ||
'resources' => [hash_including('href' => a_string_matching(api_flavors_url(nil, flavor.compressed_id)))] | ||
} | ||
expect(response.parsed_body).to include(expected) | ||
expect(response).to have_http_status(:ok) | ||
end | ||
|
||
it 'forbids access to flavor bases without an appropriate role' do | ||
api_basic_authorize | ||
|
||
run_get(api_flavors_url) | ||
|
||
expect(response).to have_http_status(:forbidden) | ||
end | ||
end | ||
|
||
describe 'GET /api/flavors/:id' do | ||
it 'will show a flavor base' do | ||
api_basic_authorize action_identifier(:flavors, :read, :resource_actions, :get) | ||
|
||
run_get(api_flavor_url(nil, flavor)) | ||
|
||
expected = { | ||
'href' => a_string_matching(api_flavors_url(nil, flavor.compressed_id)) | ||
} | ||
expect(response.parsed_body).to include(expected) | ||
expect(response).to have_http_status(:ok) | ||
end | ||
|
||
it 'forbids access to a flavor base' do | ||
api_basic_authorize | ||
|
||
run_get(api_flavors_url) | ||
|
||
expect(response).to have_http_status(:forbidden) | ||
end | ||
end | ||
|
||
describe 'DELETE /api/flavors/:id' do | ||
it 'will delete a flavor' do | ||
api_basic_authorize action_identifier(:flavors, :delete, :resource_actions, :delete) | ||
|
||
run_delete api_flavor_url(nil, flavor) | ||
|
||
expect(response).to have_http_status(:no_content) | ||
end | ||
|
||
it 'will not delete a flavor without an appropriate role' do | ||
api_basic_authorize | ||
|
||
run_delete api_flavor_url(nil, flavor) | ||
|
||
expect(response).to have_http_status(:forbidden) | ||
end | ||
end | ||
|
||
describe 'POST /api/flavors' do | ||
let(:create_params) do | ||
{ | ||
:action => 'create', | ||
:description => "Description", | ||
:name => "A Flavor", | ||
:related => {}, | ||
:ems => { :id => ems.id }, | ||
:type => 'ManageIQ::Providers::Openstack::CloudManager::Flavor', | ||
} | ||
end | ||
|
||
it 'will delete multiple flavors' do | ||
api_basic_authorize collection_action_identifier(:flavors, :delete, :post) | ||
|
||
expected = { | ||
'results' => [ | ||
a_hash_including( | ||
'success' => true, | ||
'message' => a_string_including('Deleting Flavor'), | ||
'task_id' => a_kind_of(String) | ||
), | ||
a_hash_including( | ||
'success' => true, | ||
'message' => a_string_including('Deleting Flavor'), | ||
'task_id' => a_kind_of(String) | ||
) | ||
] | ||
} | ||
|
||
run_post(api_flavors_url, :action => 'delete', :resources => [{ 'id' => flavor.id}, { 'id' => flavor_2.id }]) | ||
|
||
expect(response.parsed_body).to include(expected) | ||
expect(response).to have_http_status(:ok) | ||
end | ||
|
||
it 'will forbid deletion to an flavor without appropriate role' do | ||
api_basic_authorize | ||
|
||
run_post(api_flavors_url, :action => 'delete', :resources => [{ 'id' => flavor.id }]) | ||
expect(response).to have_http_status(:forbidden) | ||
end | ||
|
||
it 'can create an flavor' do | ||
api_basic_authorize collection_action_identifier(:flavors, :create, :post) | ||
|
||
expected = { | ||
'results' => [a_hash_including( | ||
'success' => true, | ||
'message' => 'Creating Flavor', | ||
'task_id' => a_kind_of(String) | ||
)] | ||
} | ||
run_post(api_flavors_url, create_params) | ||
|
||
expect(response).to have_http_status(:ok) | ||
expect(response.parsed_body).to include(expected) | ||
end | ||
|
||
it 'can create flavors in bulk' do | ||
api_basic_authorize collection_action_identifier(:flavors, :create, :post) | ||
|
||
expected = { | ||
'results' => [ | ||
a_hash_including( | ||
'success' => true, | ||
'message' => 'Creating Flavor', | ||
'task_id' => a_kind_of(String) | ||
), | ||
a_hash_including( | ||
'success' => true, | ||
'message' => 'Creating Flavor', | ||
'task_id' => a_kind_of(String) | ||
) | ||
] | ||
} | ||
run_post(api_flavors_url, :resources => [create_params, create_params]) | ||
|
||
expect(response).to have_http_status(:ok) | ||
expect(response.parsed_body).to include(expected) | ||
end | ||
|
||
it 'will forbid creation of an flavor without appropriate role' do | ||
api_basic_authorize | ||
|
||
run_post(api_flavors_url, :action => 'create') | ||
|
||
expect(response).to have_http_status(:forbidden) | ||
end | ||
end | ||
end |