Skip to content

Commit

Permalink
Merge pull request #14070 from mkanoor/service_initiator
Browse files Browse the repository at this point in the history
Set the initiator from the workflow/request
  • Loading branch information
gmcculloug authored Mar 6, 2017
2 parents 8681229 + 1c171b8 commit ba06004
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 8 deletions.
3 changes: 2 additions & 1 deletion app/models/miq_action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,8 @@ def action_audit(_action, rec, inputs)

def action_run_ansible_playbook(action, rec, _inputs)
service_template = ServiceTemplate.find(action.options[:service_template_id])
options = { :dialog_hosts => target_hosts(action, rec) }
options = { :dialog_hosts => target_hosts(action, rec),
:initiator => 'control' }
service_template.provision_request(target_user(rec), options)
end

Expand Down
4 changes: 3 additions & 1 deletion app/models/resource_action_workflow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def initialize(values, requester, resource_action, options = {})
@settings = {}
@requester = requester
@target = options[:target]
@initiator = options[:initiator]
@dialog = load_dialog(resource_action, values)

@settings[:resource_action_id] = resource_action.id unless resource_action.nil?
Expand Down Expand Up @@ -80,7 +81,8 @@ def load_resource_action(values = nil)
def create_values_hash
{
:dialog => @dialog.automate_values_hash,
:workflow_settings => @settings
:workflow_settings => @settings,
:initiator => @initiator
}
end

Expand Down
5 changes: 4 additions & 1 deletion app/models/service_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ def create_service(service_task, parent_svc = nil)
# convert template class name to service class name by naming convention
nh[:type] = self.class.name.sub('Template', '')

nh[:initiator] = service_task.options[:initiator] if service_task.options[:initiator]

# Determine service name
# target_name = self.get_option(:target_name)
# nh['name'] = target_name unless target_name.blank?
Expand Down Expand Up @@ -453,8 +455,9 @@ def update_from_options(options)

def provision_workflow(user, options = nil)
options ||= {}
ra_options = { :target => self, :initiator => options[:initiator] }
ResourceActionWorkflow.new({}, user,
provision_action, :target => self).tap do |wf|
provision_action, ra_options).tap do |wf|
options.each { |key, value| wf.set_value(key, value) }
end
end
Expand Down
6 changes: 3 additions & 3 deletions spec/models/miq_action_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ def stub_csv(data)
{ :service_template_id => stap.id,
:use_event_target => true }
end
let(:options) { {:dialog_hosts => ip1 } }
let(:options) { {:dialog_hosts => ip1, :initiator => 'control' } }

it_behaves_like "#workflow check"
end
Expand All @@ -430,7 +430,7 @@ def stub_csv(data)
{ :service_template_id => stap.id,
:use_localhost => true }
end
let(:options) { {:dialog_hosts => 'localhost' } }
let(:options) { {:dialog_hosts => 'localhost', :initiator => 'control' } }

it_behaves_like "#workflow check"
end
Expand All @@ -440,7 +440,7 @@ def stub_csv(data)
{ :service_template_id => stap.id,
:hosts => "ip1, ip2" }
end
let(:options) { {:dialog_hosts => 'ip1, ip2' } }
let(:options) { {:dialog_hosts => 'ip1, ip2', :initiator => 'control' } }

it_behaves_like "#workflow check"
end
Expand Down
28 changes: 26 additions & 2 deletions spec/models/service_template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,28 @@
end
end

context "initiator" do
shared_examples_for 'initiator example' do |initiator, match|
it 'test initiator' do
svc_template = FactoryGirl.create(:service_template, :name => 'Svc A')
options = {:dialog => {}}
options[:initiator] = initiator if initiator
svc_task = instance_double("service_task", :options => options)
svc = svc_template.create_service(svc_task, nil)

expect(svc.initiator).to eq(match)
end
end

context "initiator specified" do
it_behaves_like 'initiator example', 'fred', 'fred'
end

context "initiator not specified" do
it_behaves_like 'initiator example', nil, 'user'
end
end

context "with multiple services" do
before(:each) do
@svc_a = FactoryGirl.create(:service_template, :name => 'Svc A')
Expand Down Expand Up @@ -606,13 +628,15 @@
resource_action = FactoryGirl.create(:resource_action, :action => "Provision")
service_template = FactoryGirl.create(:service_template,
:resource_actions => [resource_action])
hash = {:target => service_template, :initiator => 'control'}
workflow = instance_double(ResourceActionWorkflow)
expect(ResourceActionWorkflow).to(receive(:new)
.with({}, user, resource_action, :target => service_template).and_return(workflow))
.with({}, user, resource_action, hash).and_return(workflow))
expect(workflow).to receive(:submit_request)
expect(workflow).to receive(:set_value).with('ordered_by', 'fred')
expect(workflow).to receive(:set_value).with(:initiator, 'control')

service_template.provision_request(user, 'ordered_by' => 'fred')
service_template.provision_request(user, 'ordered_by' => 'fred', :initiator => 'control')
end
end
end
Expand Down

0 comments on commit ba06004

Please sign in to comment.