Skip to content

Commit b8ada28

Browse files
author
Alexander Demichev
committed
add flavors actions to api
1 parent 48e856c commit b8ada28

File tree

4 files changed

+199
-1
lines changed

4 files changed

+199
-1
lines changed

app/controllers/api/providers_controller.rb

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class ProvidersController < BaseController
1919
include Subcollections::LoadBalancers
2020
include Subcollections::SecurityGroups
2121
include Subcollections::Vms
22+
include Subcollections::Flavors
2223

2324
def create_resource(type, _id, data = {})
2425
assert_id_not_specified(data, type)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
module Api
2+
module Subcollections
3+
module Flavors
4+
def flavors_query_resource(object)
5+
object.flavors
6+
end
7+
8+
def flavors_create_resource(parent, _type, _id, data)
9+
task_id = Flavor.create_flavor_queue(User.current_user.id, parent, data)
10+
action_result(true, 'Creating Flavor', :task_id => task_id)
11+
rescue => err
12+
action_result(false, err.to_s)
13+
end
14+
15+
def delete_resource_flavors(_parent, type, id, _data)
16+
flavor = resource_search(id, type, collection_class(type))
17+
task_id = flavor.delete_flavor_queue(User.current_user.id)
18+
action_result(true, "Deleting #{flavor_ident(flavor)}", :task_id => task_id)
19+
rescue => err
20+
action_result(false, err.to_s)
21+
end
22+
alias flavors_delete_resource delete_resource_flavors
23+
24+
private
25+
26+
def flavor_ident(flavor)
27+
"Flavor id:#{flavor.id} name: '#{flavor.name}'"
28+
end
29+
end
30+
end
31+
end

config/api.yml

+24-1
Original file line numberDiff line numberDiff line change
@@ -864,8 +864,30 @@
864864
:identifier: flavor
865865
:options:
866866
- :collection
867-
:verbs: *gp
867+
- :subcollection
868+
:verbs: *gpd
868869
:klass: Flavor
870+
:subcollection_actions:
871+
:get:
872+
- :name: read
873+
:identifier: flavor_show_list
874+
:post:
875+
- :name: delete
876+
:identifier: flavor_delete
877+
- :name: query
878+
:identifier: flavor_show_list
879+
- :name: create
880+
:identifier: flavor_create
881+
:subresource_actions:
882+
:get:
883+
- :name: read
884+
:identifier: flavor_show
885+
:post:
886+
- :name: delete
887+
:identifier: flavor_delete
888+
:delete:
889+
- :name: delete
890+
:identifier: flavor_delete
869891
:collection_actions:
870892
:get:
871893
- :name: read
@@ -1655,6 +1677,7 @@
16551677
- :load_balancers
16561678
- :security_groups
16571679
- :vms
1680+
- :flavors
16581681
:collection_actions:
16591682
:get:
16601683
- :name: read

spec/requests/flavors_spec.rb

+143
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
RSpec.describe "Flavors API" do
2+
describe "as a subcollection of providers" do
3+
describe "GET /api/providers/:c_id/flavors" do
4+
it "can list the flavors of a provider" do
5+
api_basic_authorize(action_identifier(:flavors, :read, :subcollection_actions, :get))
6+
ems = FactoryGirl.create(:ems_cloud)
7+
flavor = FactoryGirl.create(:flavor, :ext_management_system => ems)
8+
9+
get(api_provider_flavors_url(nil, ems))
10+
11+
expected = {
12+
"count" => 1,
13+
"name" => "flavors",
14+
"resources" => [
15+
{"href" => api_provider_flavor_url(nil, ems, flavor)}
16+
]
17+
}
18+
expect(response.parsed_body).to include(expected)
19+
expect(response).to have_http_status(:ok)
20+
end
21+
22+
it "will not list flavors unless authorized" do
23+
api_basic_authorize
24+
ems = FactoryGirl.create(:ems_cloud)
25+
FactoryGirl.create(:flavor, :ext_management_system => ems)
26+
27+
get(api_provider_flavors_url(nil, ems))
28+
29+
expect(response).to have_http_status(:forbidden)
30+
end
31+
end
32+
33+
describe "GET /api/providers/:c_id/flavors/:id" do
34+
it "can show a provider's flavor" do
35+
api_basic_authorize(action_identifier(:flavors, :read, :subresource_actions, :get))
36+
ems = FactoryGirl.create(:ems_cloud)
37+
flavor = FactoryGirl.create(:flavor, :ext_management_system => ems)
38+
39+
get(api_provider_flavor_url(nil, ems, flavor))
40+
41+
expected = {
42+
"href" => api_provider_flavor_url(nil, ems, flavor),
43+
"id" => flavor.id.to_s
44+
}
45+
expect(response.parsed_body).to include(expected)
46+
expect(response).to have_http_status(:ok)
47+
end
48+
49+
it "will not show a flavor unless authorized" do
50+
api_basic_authorize
51+
ems = FactoryGirl.create(:ems_cloud)
52+
flavor = FactoryGirl.create(:flavor, :ext_management_system => ems)
53+
54+
get(api_provider_flavor_url(nil, ems, flavor))
55+
56+
expect(response).to have_http_status(:forbidden)
57+
end
58+
end
59+
60+
describe "POST /api/providers/:c_id/flavors" do
61+
it "can queue the creation of a flavors" do
62+
api_basic_authorize(action_identifier(:flavors, :create, :subcollection_actions))
63+
ems = FactoryGirl.create(:ems_cloud)
64+
65+
post(api_provider_flavors_url(nil, ems), :params => { :name => "test-flavor" })
66+
67+
expected = {
68+
"results" => [
69+
a_hash_including(
70+
"success" => true,
71+
"message" => "Creating Flavor",
72+
"task_id" => anything,
73+
"task_href" => a_string_matching(api_tasks_url)
74+
)
75+
]
76+
}
77+
expect(response.parsed_body).to include(expected)
78+
expect(response).to have_http_status(:ok)
79+
end
80+
81+
it "will not create a flavor unless authorized" do
82+
api_basic_authorize
83+
ems = FactoryGirl.create(:ems_cloud)
84+
85+
post(api_provider_flavors_url(nil, ems), :params => { :name => "test-flavor" })
86+
87+
expect(response).to have_http_status(:forbidden)
88+
end
89+
end
90+
91+
describe "POST /api/providers/:c_id/flavors/:s_id with delete action" do
92+
it "can queue a flavor for deletion" do
93+
api_basic_authorize(action_identifier(:flavors, :delete, :subresource_actions))
94+
95+
ems = FactoryGirl.create(:ems_cloud)
96+
flavor = FactoryGirl.create(:flavor, :ext_management_system => ems)
97+
98+
post(api_provider_flavor_url(nil, ems, flavor), :params => { :action => "delete" })
99+
100+
expected = {
101+
"message" => "Deleting Flavor id:#{flavor.id} name: '#{flavor.name}'",
102+
"success" => true,
103+
"task_href" => a_string_matching(api_tasks_url),
104+
"task_id" => anything
105+
}
106+
expect(response.parsed_body).to include(expected)
107+
expect(response).to have_http_status(:ok)
108+
end
109+
110+
it "will not delete a flavor unless authorized" do
111+
api_basic_authorize
112+
ems = FactoryGirl.create(:ems_cloud)
113+
flavor = FactoryGirl.create(:flavor, :ext_management_system => ems)
114+
115+
post(api_provider_flavor_url(nil, ems, flavor), :params => { :action => "delete" })
116+
117+
expect(response).to have_http_status(:forbidden)
118+
end
119+
end
120+
121+
describe "DELETE /api/providers/:c_id/flavors/:s_id" do
122+
it "can delete a flavor" do
123+
api_basic_authorize(action_identifier(:flavors, :delete, :subresource_actions, :delete))
124+
ems = FactoryGirl.create(:ems_cloud)
125+
flavor = FactoryGirl.create(:flavor, :ext_management_system => ems)
126+
127+
delete(api_provider_flavor_url(nil, ems, flavor))
128+
129+
expect(response).to have_http_status(:no_content)
130+
end
131+
132+
it "will not delete a flavor unless authorized" do
133+
api_basic_authorize
134+
ems = FactoryGirl.create(:ems_cloud)
135+
flavor = FactoryGirl.create(:flavor, :ext_management_system => ems)
136+
137+
delete(api_provider_flavor_url(nil, ems, flavor))
138+
139+
expect(response).to have_http_status(:forbidden)
140+
end
141+
end
142+
end
143+
end

0 commit comments

Comments
 (0)