Skip to content

Commit fdb9783

Browse files
author
Alexander Demichev
committed
add flavors create, delete to api
1 parent 254d7d8 commit fdb9783

File tree

3 files changed

+103
-1
lines changed

3 files changed

+103
-1
lines changed
+21
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,25 @@
11
module Api
22
class FlavorsController < BaseController
3+
def create_resource(_type, _id, data)
4+
task_id = Flavor.create_flavor_queue(User.current_user.id, EmsCloud.find_by_id(data['ems']['id']), data)
5+
action_result(true, 'Creating Flavor', :task_id => task_id)
6+
rescue => err
7+
action_result(false, err.to_s)
8+
end
9+
10+
def delete_resource(type, id, _data = {})
11+
flavor = resource_search(id, type, collection_class(:flavors))
12+
raise "Delete not supported for #{flavor_ident(flavor)}" unless flavor.respond_to?(:delete_flavor_queue)
13+
task_id = flavor.delete_flavor_queue(User.current_user.id)
14+
action_result(true, "Deleting #{flavor_ident(flavor)}", :task_id => task_id)
15+
rescue => err
16+
action_result(false, err.to_s)
17+
end
18+
19+
private
20+
21+
def flavor_ident(flavor)
22+
"Flavor id:#{flavor.id} name: '#{flavor.name}'"
23+
end
324
end
425
end

config/api.yml

+8-1
Original file line numberDiff line numberDiff line change
@@ -794,19 +794,26 @@
794794
:identifier: flavor
795795
:options:
796796
- :collection
797-
:verbs: *gp
797+
:verbs: *gpd
798798
:klass: Flavor
799799
:collection_actions:
800800
:get:
801801
- :name: read
802802
:identifier: flavor_show_list
803803
:post:
804+
- :name: delete
805+
:identifier: flavor_delete
804806
- :name: query
805807
:identifier: flavor_show_list
808+
- :name: create
809+
:identifier: flavor_create
806810
:resource_actions:
807811
:get:
808812
- :name: read
809813
:identifier: flavor_show
814+
:delete:
815+
- :name: delete
816+
:identifier: flavor_delete
810817
:floating_ips:
811818
:description: Floating IPs
812819
:identifier: floating_ip

spec/requests/flavors_spec.rb

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
RSpec.describe 'Flavors API' do
2+
let(:flavor) { FactoryGirl.create(:flavor_openstack) }
3+
4+
describe 'GET/api/flavors' do
5+
it 'lists all the flavor bases with an appropriate role' do
6+
flavor = FactoryGirl.create(:flavor_openstack)
7+
api_basic_authorize collection_action_identifier(:flavors, :read, :get)
8+
9+
run_get(flavors_url)
10+
11+
expected = {
12+
'count' => 1,
13+
'subcount' => 1,
14+
'name' => 'flavors',
15+
'resources' => [hash_including('href' => a_string_matching(flavors_url(flavor.compressed_id)))]
16+
}
17+
expect(response.parsed_body).to include(expected)
18+
expect(response).to have_http_status(:ok)
19+
end
20+
21+
it 'forbids access to flavor bases without an appropriate role' do
22+
api_basic_authorize
23+
24+
run_get(flavors_url)
25+
26+
expect(response).to have_http_status(:forbidden)
27+
end
28+
end
29+
30+
describe 'GET /api/flavors/:id' do
31+
it 'will show a flavor base' do
32+
33+
api_basic_authorize action_identifier(:flavors, :read, :resource_actions, :get)
34+
35+
run_get(flavors_url(flavor.id))
36+
37+
expected = {
38+
'href' => a_string_matching(flavors_url(flavor.compressed_id))
39+
}
40+
expect(response.parsed_body).to include(expected)
41+
expect(response).to have_http_status(:ok)
42+
end
43+
44+
it 'forbids access to a flavor base' do
45+
api_basic_authorize
46+
47+
run_get(flavors_url(flavor.id))
48+
49+
expect(response).to have_http_status(:forbidden)
50+
end
51+
end
52+
53+
describe 'DELETE /api/flavors/:id' do
54+
it 'will delete a flavor' do
55+
flavor = FactoryGirl.create(:flavor)
56+
api_basic_authorize action_identifier(:flavors, :delete, :resource_actions, :delete)
57+
58+
run_delete(flavors_url(flavor.id))
59+
60+
expect(response).to have_http_status(:no_content)
61+
end
62+
63+
it 'will not delete a flavor without an appropriate role' do
64+
flavor = FactoryGirl.create(:flavor)
65+
api_basic_authorize
66+
67+
run_delete(flavors_url(flavor.id))
68+
69+
expect(response).to have_http_status(:forbidden)
70+
end
71+
end
72+
73+
74+
end

0 commit comments

Comments
 (0)