Skip to content

Commit

Permalink
Refactor ordering a service template, allow scheduling and queueing
Browse files Browse the repository at this point in the history
  • Loading branch information
bdunne committed Jun 15, 2018
1 parent 5565e64 commit 6d625ea
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 26 deletions.
42 changes: 39 additions & 3 deletions app/models/service_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -391,9 +391,45 @@ def self.create_from_options(options)
private_class_method :create_from_options

def provision_request(user, options = nil, request_options = nil)
result = provision_workflow(user, options, request_options).submit_request
raise result[:errors].join(", ") if result[:errors].any?
result[:request]
result = order(user, options, request_options)
result[:request] || raise(result[:errors].join(", "))
end

def queue_order(user_id, options, request_options)
MiqQueue.submit_job(
:class_name => name,
:instance_id => id,
:method_name => "order",
:args => [user_id, options, request_options],
)
end

def order(user_or_id, options = nil, request_options = nil, schedule_time = nil)
user = user_or_id.kind_of?(User) ? user_or_id : User.find(user_or_id)
workflow = provision_workflow(user, options, request_options)
if schedule_time
require 'time'
time = Time.parse(schedule_time).utc

errors = workflow.validate_dialog
return {:errors => errors} unless errors.blank?

schedule = MiqSchedule.create!(
:name => "Order #{self.class.name} #{id}",
:description => "Order #{self.class.name} #{id}",
:sched_action => {:args => [user.id, options, request_options], :method => "queue_order"},
:resource_id => id,
:towhat => "ServiceTemplate",
:run_at => {
:interval => {:unit => "once"},
:start_time => time,
:tz => "UTC",
},
)
{:schedule => schedule}
else
workflow.submit_request
end
end

def provision_workflow(user, dialog_options = nil, request_options = nil)
Expand Down
74 changes: 51 additions & 23 deletions spec/models/service_template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -810,35 +810,63 @@
end
end

context "#provision_request" do
context "#order" do
let(:user) { FactoryGirl.create(:user, :userid => "barney") }
let(:resource_action) { FactoryGirl.create(:resource_action, :action => "Provision") }
let(:service_template) { FactoryGirl.create(:service_template, :resource_actions => [resource_action]) }
let(:hash) { {:target => service_template, :initiator => 'control'} }
let(:workflow) { instance_double(ResourceActionWorkflow) }
let(:workflow) { instance_double(ResourceActionWorkflow, :validate_dialog => nil) }
let(:miq_request) { FactoryGirl.create(:service_template_provision_request) }
let(:good_result) { { :errors => [], :request => miq_request } }
let(:bad_result) { { :errors => %w(Error1 Error2), :request => miq_request } }
let(:arg1) { {'ordered_by' => 'fred'} }
let(:arg2) { {:initiator => 'control'} }

it "provision's a service template without errors" do
expect(ResourceActionWorkflow).to(receive(:new)
.with({}, user, resource_action, hash).and_return(workflow))
expect(workflow).to receive(:submit_request).and_return(good_result)
expect(workflow).to receive(:set_value).with('ordered_by', 'fred')
expect(workflow).to receive(:request_options=).with(:initiator => 'control')

expect(service_template.provision_request(user, arg1, arg2)).to eq(miq_request)
end

it "provision's a service template with errors" do
expect(ResourceActionWorkflow).to(receive(:new)
.with({}, user, resource_action, hash).and_return(workflow))
expect(workflow).to receive(:submit_request).and_return(bad_result)
expect(workflow).to receive(:set_value).with('ordered_by', 'fred')
expect(workflow).to receive(:request_options=).with(:initiator => 'control')
expect { service_template.provision_request(user, arg1, arg2) }.to raise_error(RuntimeError)
let(:resource_action_workflow) { ResourceActionWorkflow.new({}, user, resource_action) }

it "success no optional args" do
expect(ResourceActionWorkflow).to(receive(:new).and_return(resource_action_workflow))
expect(resource_action_workflow).to receive(:validate_dialog).and_return(nil)
expect(resource_action_workflow).to receive(:submit_request).and_return(miq_request)

expect(service_template.order(user)).to eq(:request => miq_request)
end

it "successfully scheduled" do
EvmSpecHelper.local_miq_server
expect(ResourceActionWorkflow).to(receive(:new).and_return(resource_action_workflow))
expect(resource_action_workflow).to receive(:validate_dialog).and_return(nil)

result = service_template.order(user, {}, {}, Time.now.utc.to_s)

expect(result.keys).to eq([:schedule]) # No errors
expect(result[:schedule]).to have_attributes(
:name => "Order ServiceTemplate #{service_template.id}",
:sched_action => {:args => [user.id, {}, {}], :method => "queue_order"},
:towhat => "ServiceTemplate",
:resource_id => service_template.id
)
end

context "#provision_request" do
let(:arg1) { {'ordered_by' => 'fred'} }
let(:arg2) { {:initiator => 'control'} }
let(:resource_action_workflow) { ResourceActionWorkflow.new({}, user, resource_action, hash) }

it "provision's a service template without errors" do
expect(ResourceActionWorkflow).to(receive(:new).with({}, user, resource_action, hash).and_return(resource_action_workflow))
expect(resource_action_workflow).to receive(:validate_dialog).twice.and_return(nil)
expect(resource_action_workflow).to receive(:make_request).and_return(miq_request)
expect(resource_action_workflow).to receive(:set_value).with('ordered_by', 'fred')
expect(resource_action_workflow).to receive(:request_options=).with(:initiator => 'control')

expect(service_template.provision_request(user, arg1, arg2)).to eq(:errors => nil, :request => miq_request)
end

it "provision's a service template with errors" do
expect(ResourceActionWorkflow).to(receive(:new).with({}, user, resource_action, hash).and_return(resource_action_workflow))
expect(resource_action_workflow).to receive(:validate_dialog).and_return(%w(Error1 Error2))
expect(resource_action_workflow).to receive(:set_value).with('ordered_by', 'fred')
expect(resource_action_workflow).to receive(:request_options=).with(:initiator => 'control')

expect { service_template.provision_request(user, arg1, arg2) }.to raise_error(RuntimeError)
end
end
end

Expand Down

0 comments on commit 6d625ea

Please sign in to comment.