Skip to content

Commit 5f8895a

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

File tree

3 files changed

+191
-1
lines changed

3 files changed

+191
-1
lines changed
+20
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,24 @@
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+
task_id = flavor.delete_flavor_queue(User.current_user.id)
13+
action_result(true, "Deleting #{flavor_ident(flavor)}", :task_id => task_id)
14+
rescue => err
15+
action_result(false, err.to_s)
16+
end
17+
18+
private
19+
20+
def flavor_ident(flavor)
21+
"Flavor id:#{flavor.id} name: '#{flavor.name}'"
22+
end
323
end
424
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

+163
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
RSpec.describe 'Flavors API' do
2+
let(:ems) { FactoryGirl.create(:ems_cloud) }
3+
let(:flavor) { FactoryGirl.create(:flavor_openstack, :ext_management_system => ems) }
4+
let(:flavor_2) { FactoryGirl.create(:flavor_openstack, :ext_management_system => ems) }
5+
6+
describe 'GET/api/flavors' do
7+
it 'lists all the flavor bases with an appropriate role' do
8+
flavor = FactoryGirl.create(:flavor_openstack)
9+
10+
api_basic_authorize collection_action_identifier(:flavors, :read, :get)
11+
12+
run_get(api_flavors_url)
13+
14+
expected = {
15+
'count' => 1,
16+
'subcount' => 1,
17+
'name' => 'flavors',
18+
'resources' => [hash_including('href' => a_string_matching(api_flavors_url(nil, flavor.compressed_id)))]
19+
}
20+
expect(response.parsed_body).to include(expected)
21+
expect(response).to have_http_status(:ok)
22+
end
23+
24+
it 'forbids access to flavor bases without an appropriate role' do
25+
api_basic_authorize
26+
27+
run_get(api_flavors_url)
28+
29+
expect(response).to have_http_status(:forbidden)
30+
end
31+
end
32+
33+
describe 'GET /api/flavors/:id' do
34+
it 'will show a flavor base' do
35+
api_basic_authorize action_identifier(:flavors, :read, :resource_actions, :get)
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+
api_basic_authorize action_identifier(:flavors, :delete, :resource_actions, :delete)
58+
59+
run_delete api_flavor_url(nil, flavor)
60+
61+
expect(response).to have_http_status(:no_content)
62+
end
63+
64+
it 'will not delete a flavor without an appropriate role' do
65+
api_basic_authorize
66+
67+
run_delete api_flavor_url(nil, flavor)
68+
69+
expect(response).to have_http_status(:forbidden)
70+
end
71+
end
72+
73+
describe 'POST /api/flavors' do
74+
let(:create_params) do
75+
{
76+
:action => 'create',
77+
:description => "Description",
78+
:name => "A Flavor",
79+
:related => {},
80+
:ems => { :id => ems.id },
81+
:type => 'ManageIQ::Providers::Openstack::CloudManager::Flavor',
82+
}
83+
end
84+
85+
it 'will delete multiple flavors' do
86+
api_basic_authorize collection_action_identifier(:flavors, :delete, :post)
87+
88+
expected = {
89+
'results' => [
90+
a_hash_including(
91+
'success' => true,
92+
'message' => a_string_including('Deleting Flavor'),
93+
'task_id' => a_kind_of(String)
94+
),
95+
a_hash_including(
96+
'success' => true,
97+
'message' => a_string_including('Deleting Flavor'),
98+
'task_id' => a_kind_of(String)
99+
)
100+
]
101+
}
102+
103+
run_post(api_flavors_url, :action => 'delete', :resources => [{ 'id' => flavor.id}, { 'id' => flavor_2.id }])
104+
105+
expect(response.parsed_body).to include(expected)
106+
expect(response).to have_http_status(:ok)
107+
end
108+
109+
it 'will forbid deletion to an flavor without appropriate role' do
110+
api_basic_authorize
111+
112+
run_post(api_flavors_url, :action => 'delete', :resources => [{ 'id' => flavor.id }])
113+
expect(response).to have_http_status(:forbidden)
114+
end
115+
116+
it 'can create an flavor' do
117+
api_basic_authorize collection_action_identifier(:flavors, :create, :post)
118+
119+
expected = {
120+
'results' => [a_hash_including(
121+
'success' => true,
122+
'message' => 'Creating Flavor',
123+
'task_id' => a_kind_of(String)
124+
)]
125+
}
126+
run_post(api_flavors_url, create_params)
127+
128+
expect(response).to have_http_status(:ok)
129+
expect(response.parsed_body).to include(expected)
130+
end
131+
132+
it 'can create flavors in bulk' do
133+
api_basic_authorize collection_action_identifier(:flavors, :create, :post)
134+
135+
expected = {
136+
'results' => [
137+
a_hash_including(
138+
'success' => true,
139+
'message' => 'Creating Flavor',
140+
'task_id' => a_kind_of(String)
141+
),
142+
a_hash_including(
143+
'success' => true,
144+
'message' => 'Creating Flavor',
145+
'task_id' => a_kind_of(String)
146+
)
147+
]
148+
}
149+
run_post(api_flavors_url, :resources => [create_params, create_params])
150+
151+
expect(response).to have_http_status(:ok)
152+
expect(response.parsed_body).to include(expected)
153+
end
154+
155+
it 'will forbid creation of an flavor without appropriate role' do
156+
api_basic_authorize
157+
158+
run_post(api_flavors_url, :action => 'create')
159+
160+
expect(response).to have_http_status(:forbidden)
161+
end
162+
end
163+
end

0 commit comments

Comments
 (0)