Skip to content

Commit

Permalink
Merge pull request #17594 from bdunne/scheduling_catalog_items
Browse files Browse the repository at this point in the history
Scheduling catalog items
  • Loading branch information
gtanzillo authored Jun 21, 2018
2 parents 362b5ff + 7a64f16 commit 245ce8b
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 34 deletions.
39 changes: 38 additions & 1 deletion app/models/service_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -392,11 +392,48 @@ 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
result = order(user, options, request_options)
raise result[:errors].join(", ") if result[:errors].any?
result[:request]
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)
dialog_options ||= {}
request_options ||= {}
Expand Down
19 changes: 11 additions & 8 deletions spec/models/miq_schedule_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -722,9 +722,14 @@
end

context "resource exists" do
let(:resource) { FactoryGirl.create(:host) }

before do
allow(Host).to receive(:find_by).with(:id => resource.id).and_return(resource)
end

it "and does not respond to the method" do
resource = FactoryGirl.create(:host)
schedule = FactoryGirl.create(:miq_schedule, :towhat => resource.class.name, :resource_id => resource, :sched_action => {:method => "test_method"})
schedule = FactoryGirl.create(:miq_schedule, :towhat => resource.class.name, :resource_id => resource.id, :sched_action => {:method => "test_method"})

expect($log).to receive(:warn) do |message|
expect(message).to include("no such action: [test_method], aborting schedule")
Expand All @@ -734,19 +739,17 @@
end

it "and responds to the method" do
resource = FactoryGirl.create(:host)
schedule = FactoryGirl.create(:miq_schedule, :towhat => resource.class.name, :resource_id => resource, :sched_action => {:method => "test_method"})
schedule = FactoryGirl.create(:miq_schedule, :towhat => resource.class.name, :resource_id => resource.id, :sched_action => {:method => "test_method"})

expect_any_instance_of(Host).to receive("test_method").once
expect(resource).to receive("test_method").once

MiqSchedule.queue_scheduled_work(schedule.id, nil, "abc", nil)
end

it "and responds to the method with arguments" do
resource = FactoryGirl.create(:host)
schedule = FactoryGirl.create(:miq_schedule, :towhat => resource.class.name, :resource_id => resource, :sched_action => {:method => "test_method", :args => ["abc", 123, :a => 1]})
schedule = FactoryGirl.create(:miq_schedule, :towhat => resource.class.name, :resource_id => resource.id, :sched_action => {:method => "test_method", :args => ["abc", 123, :a => 1]})

expect_any_instance_of(Host).to receive("test_method").once.with("abc", 123, :a => 1)
expect(resource).to receive("test_method").once.with("abc", 123, :a => 1)

MiqSchedule.queue_scheduled_work(schedule.id, nil, "abc", nil)
end
Expand Down
74 changes: 49 additions & 25 deletions spec/models/service_template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -810,35 +810,59 @@
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(:resource_action_options) { {:target => service_template, :initiator => 'control'} }
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, resource_action_options) }

before do
allow(ResourceActionWorkflow).to(receive(:new).and_return(resource_action_workflow))
end

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

expect(service_template.order(user)).to eq(miq_request)
end

it "successfully scheduled" do
EvmSpecHelper.local_miq_server
expect(resource_action_workflow).to receive(:validate_dialog).and_return([])

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

it "provision's a service template without errors" do
expect(resource_action_workflow).to receive(:validate_dialog).and_return([])
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(miq_request)
end

it "provision's a service template with errors" do
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 245ce8b

Please sign in to comment.