Skip to content

Commit 452a2a1

Browse files
author
Alexander Demichev
committed
add flavors create, delete to api
1 parent 170dee0 commit 452a2a1

File tree

3 files changed

+105
-1
lines changed

3 files changed

+105
-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(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

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

0 commit comments

Comments
 (0)