Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow ordering of service templates resource #316

Merged
merged 1 commit into from
Feb 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions app/controllers/api/mixins/service_templates.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module Api
module Mixins
module ServiceTemplates
def order_service_template(id, data)
service_template = resource_search(id, :service_templates, ServiceTemplate)
raise BadRequestError, "#{service_template_ident(service_template)} cannot be ordered" unless service_template.orderable?
workflow = service_template.provision_workflow(User.current_user, data || {})
request_result = workflow.submit_request
errors = request_result[:errors]
if errors.present?
raise BadRequestError, "Failed to order #{service_template_ident(service_template)} - #{errors.join(", ")}"
end
request_result[:request]
end

private

def service_template_ident(st)
"Service Template id:#{st.id} name:'#{st.name}'"
end
end
end
end
5 changes: 5 additions & 0 deletions app/controllers/api/service_templates_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class ServiceTemplatesController < BaseController
include Subcollections::ResourceActions
include Subcollections::ServiceRequests
include Api::Mixins::Pictures
include Api::Mixins::ServiceTemplates

before_action :set_additional_attributes, :only => [:show]

Expand All @@ -24,6 +25,10 @@ def edit_resource(type, id, data)
raise BadRequestError, "Could not update Service Template - #{err}"
end

def order_resource(_type, id, data)
order_service_template(id, data)
end

private

def set_additional_attributes
Expand Down
18 changes: 4 additions & 14 deletions app/controllers/api/subcollections/service_templates.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module Api
module Subcollections
module ServiceTemplates
include Api::Mixins::ServiceTemplates

def service_templates_query_resource(object)
klass = collection_class(:service_templates)
object ? klass.where(:service_template_catalog_id => object.id) : {}
Expand All @@ -26,16 +28,8 @@ def service_templates_unassign_resource(object, type, id = nil, _data = nil)
end
end

def service_templates_order_resource(_object, type, id = nil, data = nil)
klass = collection_class(:service_templates)
service_template = resource_search(id, type, klass)
workflow = service_template.provision_workflow(User.current_user, data || {})
request_result = workflow.submit_request
errors = request_result[:errors]
if errors.present?
raise BadRequestError, "Failed to order #{service_template_ident(service_template)} - #{errors.join(", ")}"
end
request_result[:request]
def service_templates_order_resource(_object, _type, id = nil, data = nil)
order_service_template(id, data)
end

def service_templates_refresh_dialog_fields_resource(object, type, id = nil, data = nil)
Expand All @@ -54,10 +48,6 @@ def delete_resource_service_templates(_parent, type, id, data)

private

def service_template_ident(st)
"Service Template id:#{st.id} name:'#{st.name}'"
end

def service_template_subcollection_action(type, id)
klass = collection_class(:service_templates)
result =
Expand Down
6 changes: 6 additions & 0 deletions config/api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2487,6 +2487,8 @@
:identifier: catalogitem_delete
- :name: create
:identifier: atomic_catalogitem_new
- :name: order
:identifier: svc_catalog_provision
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't we need the :validate_action option here too ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@abellotti it doesn't go on the collection, only on the resource, which is consistent with the other places we have it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

:subcollection_actions:
:post:
- :name: edit
Expand All @@ -2510,6 +2512,10 @@
:identifier: catalogitem_edit
- :name: delete
:identifier: catalogitem_delete
- :name: order
:identifier: svc_catalog_provision
:options:
- :validate_action
:delete:
- :name: delete
:identifier: catalogitem_delete
Expand Down
91 changes: 91 additions & 0 deletions spec/requests/service_templates_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -445,4 +445,95 @@
expect(response.parsed_body).to include(expected)
end
end

describe "Service Templates order" do
let(:service_template) { FactoryGirl.create(:service_template, :with_provision_resource_action_and_dialog, :orderable) }

it "is forbidden without appropriate role" do
api_basic_authorize

post(api_service_template_url(nil, service_template), :params => { :action => "order" })

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

context "with an orderable template" do
it "can be ordered as a resource action" do
api_basic_authorize action_identifier(:service_templates, :order, :resource_actions, :post)

post(api_service_template_url(nil, service_template), :params => { :action => "order" })

expect(response).to have_http_status(:ok)
expect(response.parsed_body).to include('href' => a_string_including(api_service_requests_url))
end

it "can be ordered as an action on the collection" do
api_basic_authorize action_identifier(:service_templates, :order, :resource_actions, :post)

post(api_service_templates_url, :params => { :action => "order", :resources => [{:href => api_service_template_url(nil, service_template)}] })

expected = {
"results" => [a_hash_including("href" => a_string_including(api_service_requests_url))]
}
expect(response).to have_http_status(:ok)
expect(response.parsed_body).to include(expected)
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's add a test on the collection, ordering 2 templates. Thanks.


it "shows the action" do
api_basic_authorize(action_identifier(:service_templates, :order, :resource_actions, :post),
action_identifier(:service_templates, :read, :resource_actions, :get))

get(api_service_template_url(nil, service_template))

actions = response.parsed_body["actions"].collect { |action| action["name"] }
expect(actions).to include("order")
end

it "can order multiple service templates" do
service_template2 = FactoryGirl.create(:service_template, :with_provision_resource_action_and_dialog, :orderable)
api_basic_authorize action_identifier(:service_templates, :order, :resource_actions, :post)

post(api_service_templates_url, :params => { :action => "order", :resources =>
[{:href => api_service_template_url(nil, service_template)},
{:href => api_service_template_url(nil, service_template2)}]})

expected = {
"results" => [a_hash_including("href" => a_string_including(api_service_requests_url)),
a_hash_including("href" => a_string_including(api_service_requests_url))]
}
expect(response).to have_http_status(:ok)
expect(response.parsed_body).to include(expected)
end
end

context "with an unorderable template" do
let(:template_no_display) { FactoryGirl.create(:service_template, :display => false) }

it "cannot be ordered" do
api_basic_authorize action_identifier(:service_templates, :order, :resource_actions, :post)

post(api_service_template_url(nil, template_no_display), :params => { :action => "order" })

expected = {
"error" => a_hash_including(
"kind" => "bad_request",
"message" => /cannot be ordered/
)
}
expect(response).to have_http_status(:bad_request)
expect(response.parsed_body).to include(expected)
end

it "does not show the order action" do
api_basic_authorize(action_identifier(:service_templates, :order, :resource_actions, :post),
action_identifier(:service_templates, :read, :resource_actions, :get),
action_identifier(:service_templates, :edit, :resource_actions, :post))

get(api_service_template_url(nil, template_no_display))

actions = response.parsed_body["actions"].collect { |action| action["name"] }
expect(actions).to_not include("order")
end
end
end
end