Skip to content

Commit 8425e11

Browse files
author
Alexander Demichev
committed
add flavors actions to api
1 parent 4422e45 commit 8425e11

File tree

5 files changed

+190
-14
lines changed

5 files changed

+190
-14
lines changed

app/controllers/api/providers_controller.rb

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class ProvidersController < BaseController
1818
include Subcollections::CustomAttributes
1919
include Subcollections::LoadBalancers
2020
include Subcollections::Vms
21+
include Subcollections::Flavors
2122

2223
def create_resource(type, _id, data = {})
2324
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

+15-4
Original file line numberDiff line numberDiff line change
@@ -861,20 +861,30 @@
861861
:description: Flavors
862862
:identifier: flavor
863863
:options:
864-
- :collection
865-
:verbs: *gp
864+
- :subcollection
865+
:verbs: *gpd
866866
:klass: Flavor
867-
:collection_actions:
867+
:subcollection_actions:
868868
:get:
869869
- :name: read
870870
:identifier: flavor_show_list
871871
:post:
872+
- :name: delete
873+
:identifier: flavor_delete
872874
- :name: query
873875
:identifier: flavor_show_list
874-
:resource_actions:
876+
- :name: create
877+
:identifier: flavor_create
878+
:subresource_actions:
875879
:get:
876880
- :name: read
877881
:identifier: flavor_show
882+
:post:
883+
- :name: delete
884+
:identifier: flavor_delete
885+
:delete:
886+
- :name: delete
887+
:identifier: flavor_delete
878888
:floating_ips:
879889
:description: Floating IPs
880890
:identifier: floating_ip
@@ -1651,6 +1661,7 @@
16511661
- :custom_attributes
16521662
- :load_balancers
16531663
- :vms
1664+
- :flavors
16541665
:collection_actions:
16551666
:get:
16561667
- :name: read

spec/requests/collections_spec.rb

-10
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,6 @@ def test_collection_bulk_query(collection, collection_url, klass, id = nil)
114114
test_collection_query(:features, api_features_url, MiqProductFeature)
115115
end
116116

117-
it "query Flavors" do
118-
FactoryGirl.create(:flavor)
119-
test_collection_query(:flavors, api_flavors_url, Flavor)
120-
end
121-
122117
it "query Groups" do
123118
expect(Tenant.exists?).to be_truthy
124119
FactoryGirl.create(:miq_group)
@@ -411,11 +406,6 @@ def test_collection_bulk_query(collection, collection_url, klass, id = nil)
411406
test_collection_bulk_query(:events, api_events_url, MiqEventDefinition)
412407
end
413408

414-
it "bulk query Flavors" do
415-
FactoryGirl.create(:flavor)
416-
test_collection_bulk_query(:flavors, api_flavors_url, Flavor)
417-
end
418-
419409
it "bulk query FloatingIps" do
420410
FactoryGirl.create(:floating_ip)
421411
test_collection_bulk_query(:floating_ips, api_floating_ips_url, FloatingIp)

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)