Skip to content

Commit

Permalink
add flavors actions to api
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Demichev committed Oct 9, 2017
1 parent 4422e45 commit 020b5a3
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 15 deletions.
1 change: 1 addition & 0 deletions app/controllers/api/providers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class ProvidersController < BaseController
include Subcollections::CustomAttributes
include Subcollections::LoadBalancers
include Subcollections::Vms
include Subcollections::Flavors

def create_resource(type, _id, data = {})
assert_id_not_specified(data, type)
Expand Down
31 changes: 31 additions & 0 deletions app/controllers/api/subcollections/flavors.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module Api
module Subcollections
module Flavors
def flavors_query_resource(object)
object.flavors
end

def flavors_create_resource(parent, _type, _id, data)
task_id = Flavor.create_flavor_queue(User.current_user.id, parent, data)
action_result(true, 'Creating Flavor', :task_id => task_id)
rescue => err
action_result(false, err.to_s)
end

def delete_resource_flavors(_parent, type, id, _data)
flavor = resource_search(id, type, collection_class(type))
task_id = flavor.delete_flavor_queue(User.current_user.id)
action_result(true, "Deleting #{flavor_ident(flavor)}", :task_id => task_id)
rescue => err
action_result(false, err.to_s)
end
alias flavors_delete_resource delete_resource_flavors

private

def flavor_ident(flavor)
"Flavor id:#{flavor.id} name: '#{flavor.name}'"
end
end
end
end
19 changes: 15 additions & 4 deletions config/api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -861,20 +861,30 @@
:description: Flavors
:identifier: flavor
:options:
- :collection
:verbs: *gp
- :subcollection
:verbs: *gpd
:klass: Flavor
:collection_actions:
:subcollection_actions:
:get:
- :name: read
:identifier: flavor_show_list
:post:
- :name: delete
:identifier: flavor_delete
- :name: query
:identifier: flavor_show_list
:resource_actions:
- :name: create
:identifier: flavor_create
:subresource_actions:
:get:
- :name: read
:identifier: flavor_show
:post:
- :name: delete
:identifier: flavor_delete
:delete:
- :name: delete
:identifier: flavor_delete
:floating_ips:
:description: Floating IPs
:identifier: floating_ip
Expand Down Expand Up @@ -1651,6 +1661,7 @@
- :custom_attributes
- :load_balancers
- :vms
- :flavors
:collection_actions:
:get:
- :name: read
Expand Down
12 changes: 1 addition & 11 deletions spec/requests/collections_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def test_collection_query(collection, collection_url, klass, attr = :id)
api_basic_authorize collection_action_identifier(collection, :read, :get)
else
api_basic_authorize
end
end©

get collection_url, :params => { :expand => "resources" }

Expand Down Expand Up @@ -114,11 +114,6 @@ def test_collection_bulk_query(collection, collection_url, klass, id = nil)
test_collection_query(:features, api_features_url, MiqProductFeature)
end

it "query Flavors" do
FactoryGirl.create(:flavor)
test_collection_query(:flavors, api_flavors_url, Flavor)
end

it "query Groups" do
expect(Tenant.exists?).to be_truthy
FactoryGirl.create(:miq_group)
Expand Down Expand Up @@ -411,11 +406,6 @@ def test_collection_bulk_query(collection, collection_url, klass, id = nil)
test_collection_bulk_query(:events, api_events_url, MiqEventDefinition)
end

it "bulk query Flavors" do
FactoryGirl.create(:flavor)
test_collection_bulk_query(:flavors, api_flavors_url, Flavor)
end

it "bulk query FloatingIps" do
FactoryGirl.create(:floating_ip)
test_collection_bulk_query(:floating_ips, api_floating_ips_url, FloatingIp)
Expand Down
143 changes: 143 additions & 0 deletions spec/requests/flavors_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
RSpec.describe "Flavors API" do
describe "as a subcollection of providers" do
describe "GET /api/providers/:c_id/flavors" do
it "can list the flavors of a provider" do
api_basic_authorize(action_identifier(:flavors, :read, :subcollection_actions, :get))
ems = FactoryGirl.create(:ems_cloud)
flavor = FactoryGirl.create(:flavor, :ext_management_system => ems)

get(api_provider_flavors_url(nil, ems))

expected = {
"count" => 1,
"name" => "flavors",
"resources" => [
{"href" => api_provider_flavor_url(nil, ems, flavor)}
]
}
expect(response.parsed_body).to include(expected)
expect(response).to have_http_status(:ok)
end

it "will not list flavors unless authorized" do
api_basic_authorize
ems = FactoryGirl.create(:ems_cloud)
FactoryGirl.create(:flavor, :ext_management_system => ems)

get(api_provider_flavors_url(nil, ems))

expect(response).to have_http_status(:forbidden)
end
end

describe "GET /api/providers/:c_id/flavors/:id" do
it "can show a provider's flavor" do
api_basic_authorize(action_identifier(:flavors, :read, :subresource_actions, :get))
ems = FactoryGirl.create(:ems_cloud)
flavor = FactoryGirl.create(:flavor, :ext_management_system => ems)

get(api_provider_flavor_url(nil, ems, flavor))

expected = {
"href" => api_provider_flavor_url(nil, ems, flavor),
"id" => flavor.id.to_s
}
expect(response.parsed_body).to include(expected)
expect(response).to have_http_status(:ok)
end

it "will not show a flavor unless authorized" do
api_basic_authorize
ems = FactoryGirl.create(:ems_cloud)
flavor = FactoryGirl.create(:flavor, :ext_management_system => ems)

get(api_provider_flavor_url(nil, ems, flavor))

expect(response).to have_http_status(:forbidden)
end
end

describe "POST /api/providers/:c_id/flavors" do
it "can queue the creation of a flavors" do
api_basic_authorize(action_identifier(:flavors, :create, :subcollection_actions))
ems = FactoryGirl.create(:ems_cloud)

post(api_provider_flavors_url(nil, ems), :params => { :name => "test-flavor" })

expected = {
"results" => [
a_hash_including(
"success" => true,
"message" => "Creating Flavor",
"task_id" => anything,
"task_href" => a_string_matching(api_tasks_url)
)
]
}
expect(response.parsed_body).to include(expected)
expect(response).to have_http_status(:ok)
end

it "will not create a flavor unless authorized" do
api_basic_authorize
ems = FactoryGirl.create(:ems_cloud)

post(api_provider_flavors_url(nil, ems), :params => { :name => "test-flavor" })

expect(response).to have_http_status(:forbidden)
end
end

describe "POST /api/providers/:c_id/flavors/:s_id with delete action" do
it "can queue a flavor for deletion" do
api_basic_authorize(action_identifier(:flavors, :delete, :subresource_actions))

ems = FactoryGirl.create(:ems_cloud)
flavor = FactoryGirl.create(:flavor, :ext_management_system => ems)

post(api_provider_flavor_url(nil, ems, flavor), :params => { :action => "delete" })

expected = {
"message" => "Deleting Flavor id:#{flavor.id} name: '#{flavor.name}'",
"success" => true,
"task_href" => a_string_matching(api_tasks_url),
"task_id" => anything
}
expect(response.parsed_body).to include(expected)
expect(response).to have_http_status(:ok)
end

it "will not delete a flavor unless authorized" do
api_basic_authorize
ems = FactoryGirl.create(:ems_cloud)
flavor = FactoryGirl.create(:flavor, :ext_management_system => ems)

post(api_provider_flavor_url(nil, ems, flavor), :params => { :action => "delete" })

expect(response).to have_http_status(:forbidden)
end
end

describe "DELETE /api/providers/:c_id/flavors/:s_id" do
it "can delete a flavor" do
api_basic_authorize(action_identifier(:flavors, :delete, :subresource_actions, :delete))
ems = FactoryGirl.create(:ems_cloud)
flavor = FactoryGirl.create(:flavor, :ext_management_system => ems)

delete(api_provider_flavor_url(nil, ems, flavor))

expect(response).to have_http_status(:no_content)
end

it "will not delete a flavor unless authorized" do
api_basic_authorize
ems = FactoryGirl.create(:ems_cloud)
flavor = FactoryGirl.create(:flavor, :ext_management_system => ems)

delete(api_provider_flavor_url(nil, ems, flavor))

expect(response).to have_http_status(:forbidden)
end
end
end
end

0 comments on commit 020b5a3

Please sign in to comment.