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

[GAPRINDASHVILI] Scheduling catalog items #17765

Merged
merged 4 commits into from
Jul 26, 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
41 changes: 39 additions & 2 deletions app/models/service_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -398,8 +398,8 @@ 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 = order(user, options, request_options)
raise result[:errors].join(", ") if result[:errors] && result[:errors].any?
result[:request]
end

Expand All @@ -408,6 +408,43 @@ def miq_schedules
MiqSchedule.where(:towhat => "ServiceTemplate", :id => schedule_ids)
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
70 changes: 47 additions & 23 deletions spec/models/service_template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -804,33 +804,57 @@
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', :submit_workflow => nil} }
let(:workflow) { instance_double(ResourceActionWorkflow) }
let(:resource_action_options) { {:target => service_template, :initiator => 'control', :submit_workflow => 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 "provisions a service template without errors" do
expect(ResourceActionWorkflow).to(receive(:new)
.with({'ordered_by' => 'fred'}, user, resource_action, hash).and_return(workflow))
expect(workflow).to receive(:submit_request).and_return(good_result)
expect(workflow).to receive(:request_options=).with(:initiator => 'control')

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

it "provisions a service template with errors" do
expect(ResourceActionWorkflow).to(receive(:new)
.with({'ordered_by' => 'fred'}, user, resource_action, hash).and_return(workflow))
expect(workflow).to receive(:submit_request).and_return(bad_result)
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 "provisions 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(:request_options=).with(:initiator => 'control')

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

it "provisions 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(:request_options=).with(:initiator => 'control')

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

Expand Down