Skip to content

Commit

Permalink
Allow ordering of service templates directly
Browse files Browse the repository at this point in the history
For the new ServiceTemplateTransformationPlan, ordering must happen directly on the service template. Currently, the API only allows ordering through a service catalog.
  • Loading branch information
Jillian Tullo committed Feb 8, 2018
1 parent f304c85 commit b5693a1
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 14 deletions.
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_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_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_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
: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
76 changes: 76 additions & 0 deletions spec/requests/service_templates_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -445,4 +445,80 @@
expect(response.parsed_body).to include(expected)
end
end

describe "Service Templates order" do
let(:catalog) { FactoryGirl.create(:service_template_catalog) }
let(:service_template) { FactoryGirl.create(:service_template, :resource_actions => [ra1], :display => true, :service_template_catalog => catalog) }

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

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
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

0 comments on commit b5693a1

Please sign in to comment.