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

Fix for $evm.execute not honoring dialog options #17844

Merged
merged 2 commits into from
Aug 13, 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
9 changes: 7 additions & 2 deletions app/models/dialog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,15 @@ def validate_field_data
result
end

def load_values_into_fields(values)
def load_values_into_fields(values, overwrite = true)
values = values.with_indifferent_access

dialog_field_hash.each_value do |field|
field.dialog = self
field.value = values[field.automate_key_name] || values[field.name]
new_value = values[field.automate_key_name] || values[field.name]
new_value ||= field.value unless overwrite

field.value = new_value
end
end

Expand Down
3 changes: 3 additions & 0 deletions app/models/resource_action_workflow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ def load_dialog(resource_action, values, options)
dialog.target_resource = @target
if options[:display_view_only]
dialog.init_fields_with_values_for_request(values)
elsif options[:provision_workflow]
dialog.initialize_value_context(values)
dialog.load_values_into_fields(values, false)
elsif options[:refresh] || options[:submit_workflow]
dialog.load_values_into_fields(values)
elsif options[:reconfigure]
Expand Down
17 changes: 10 additions & 7 deletions app/models/service_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,8 @@ def self.create_from_options(options)
end
private_class_method :create_from_options

def provision_request(user, options = nil, request_options = nil)
def provision_request(user, options = nil, request_options = {})
request_options[:provision_workflow] = true
result = order(user, options, request_options)
raise result[:errors].join(", ") if result[:errors].any?
result[:request]
Expand All @@ -394,7 +395,7 @@ def queue_order(user_id, options, request_options)
)
end

def order(user_or_id, options = nil, request_options = nil, schedule_time = nil)
def order(user_or_id, options = nil, request_options = {}, 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
Expand All @@ -421,13 +422,15 @@ def order(user_or_id, options = nil, request_options = nil, schedule_time = nil)
end
end

def provision_workflow(user, dialog_options = nil, request_options = nil)
def provision_workflow(user, dialog_options = nil, request_options = {})
dialog_options ||= {}
request_options ||= {}
request_options.delete(:provision_workflow) if request_options[:submit_workflow]

ra_options = {
:target => self,
:initiator => request_options[:initiator],
:submit_workflow => request_options[:submit_workflow]
:target => self,
:initiator => request_options[:initiator],
:submit_workflow => request_options[:submit_workflow],
:provision_workflow => request_options[:provision_workflow]
}

ResourceActionWorkflow.new(dialog_options, user, provision_action, ra_options).tap do |wf|
Expand Down
56 changes: 56 additions & 0 deletions spec/models/dialog_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,62 @@
end
end

describe "#load_values_into_fields" do
let(:dialog) { described_class.new(:dialog_tabs => [dialog_tab]) }
let(:dialog_tab) { DialogTab.new(:dialog_groups => [dialog_group]) }
let(:dialog_group) { DialogGroup.new(:dialog_fields => [dialog_field1]) }
let(:dialog_field1) { DialogField.new(:value => "123", :name => "field1") }

context "string values" do
it "sets field value" do
vars = {"field1" => "10.8.99.248"}
dialog.load_values_into_fields(vars)
expect(dialog_field1.value).to eq("10.8.99.248")
end

it "sets field value when prefixed with 'dialog'" do
vars = {"dialog_field1" => "10.8.99.248"}
dialog.load_values_into_fields(vars)
expect(dialog_field1.value).to eq("10.8.99.248")
end
end

context "symbol values" do
it "sets field value" do
vars = {:field1 => "10.8.99.248"}
dialog.load_values_into_fields(vars)
expect(dialog_field1.value).to eq("10.8.99.248")
end

it "sets field value when prefixed with 'dialog'" do
vars = {:dialog_field1 => "10.8.99.248"}
dialog.load_values_into_fields(vars)
expect(dialog_field1.value).to eq("10.8.99.248")
end
end

context "when the incoming values are missing keys" do
let(:dialog_group) { DialogGroup.new(:dialog_fields => [dialog_field1, dialog_field2]) }
let(:dialog_field2) { DialogField.new(:value => "321", :name => "field2") }

context "when overwrite is true" do
it "sets nil values" do
vars = {:field1 => "10.8.99.248"}
dialog.load_values_into_fields(vars)
expect(dialog_field2.value).to eq(nil)
end
end

context "when overwrite is false" do
it "does not set nil values" do
vars = {:field1 => "10.8.99.248"}
dialog.load_values_into_fields(vars, false)
expect(dialog_field2.value).to eq("321")
end
end
end
end

describe "#init_fields_with_values_for_request" do
let(:dialog) { described_class.new(:dialog_tabs => [dialog_tab]) }
let(:dialog_tab) { DialogTab.new(:dialog_groups => [dialog_group]) }
Expand Down
10 changes: 10 additions & 0 deletions spec/models/resource_action_workflow_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,16 @@
end
end

context "when the options are set to a provision workflow request" do
let(:options) { {:provision_workflow => true} }

it "initializes the value context and then loads the values into fields" do
expect(dialog).to receive(:initialize_value_context).with(values).ordered
expect(dialog).to receive(:load_values_into_fields).with(values, false).ordered
ResourceActionWorkflow.new(values, nil, resource_action, options)
end
end

context "when neither display_view_only nor refresh are true" do
let(:options) { {} }

Expand Down
34 changes: 30 additions & 4 deletions spec/models/service_template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -880,20 +880,25 @@

context "#provision_request" do
let(:arg1) { {'ordered_by' => 'fred'} }

context "with submit_workflow" do
let(:arg2) { {:initiator => 'control', :submit_workflow => true} }

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', :submit_workflow => true)
expect(resource_action_workflow).to receive(:request_options=).with(
:initiator => 'control', :submit_workflow => true
)

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', :submit_workflow => true)
expect(resource_action_workflow).to receive(:request_options=).with(
:initiator => 'control', :submit_workflow => true
)

expect { service_template.provision_request(user, arg1, arg2) }.to raise_error(RuntimeError)
end
Expand All @@ -905,18 +910,39 @@
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(resource_action_workflow).to receive(:request_options=).with(
:initiator => 'control', :provision_workflow => true
)

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(resource_action_workflow).to receive(:request_options=).with(
:initiator => 'control', :provision_workflow => true
)

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

context "without any request options" do
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(:provision_workflow => true)

expect(service_template.provision_request(user, arg1)).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(:provision_workflow => true)

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

Expand Down