diff --git a/app/models/automation_request.rb b/app/models/automation_request.rb index 3df9ad9cf27b..c90c50c6d8b2 100644 --- a/app/models/automation_request.rb +++ b/app/models/automation_request.rb @@ -29,7 +29,7 @@ def self.create_from_ws(version, user, uri_parts, parameters, requester) options[:schedule_type] = parameters['schedule_time'].present? ? "schedule" : "immediately" options[:schedule_time] = parameters['schedule_time'].to_i.days.from_now if parameters['schedule_time'] - object_parameters = parse_out_objects(parameters) + object_parameters, parameters = parse_out_objects(parameters) attrs = MiqRequestWorkflow.parse_ws_string(parameters) attrs.merge!(object_parameters) @@ -42,18 +42,10 @@ def self.create_from_ws(version, user, uri_parts, parameters, requester) end def self.create_from_scheduled_task(user, uri_parts, parameters) - [:namespace, :class_name].each { |key| uri_parts.delete(key) if uri_parts.key?(key) } - approval = {'auto_approve' => true} - uri_parts.stringify_keys! - parameters.stringify_keys! - create_from_ws("1.1", user, uri_parts, parameters, approval) - end - - def self.parse_out_objects(parameters) - object_hash = parameters.select { |key, _v| key.to_s.include?(MiqAeEngine::MiqAeObject::CLASS_SEPARATOR) } - object_hash.each do |key, _v| - parameters.delete(key) - end + uri_parts = uri_parts.stringify_keys + parameters = parameters.stringify_keys + %w[namespace class_name].each { |key| uri_parts.delete(key) } + create_from_ws("1.1", user, uri_parts, parameters, 'auto_approve' => true) end def self.zone(options) @@ -79,4 +71,12 @@ def my_role(_action = nil) def log_request_success(_requester_id, _mode) # currently we do not log successful automation requests end + + def self.parse_out_objects(parameters) + parameters = parameters.dup + object_hash = parameters.select { |key, _v| key.to_s.include?(MiqAeEngine::MiqAeObject::CLASS_SEPARATOR) } + parameters.extract!(*object_hash.keys) + return object_hash, parameters + end + private_class_method :parse_out_objects end diff --git a/spec/models/automation_request_spec.rb b/spec/models/automation_request_spec.rb index ea7f8ca12f2d..1ee02326a862 100644 --- a/spec/models/automation_request_spec.rb +++ b/spec/models/automation_request_spec.rb @@ -20,18 +20,6 @@ expect(AutomationRequest.request_task_class).to eq(AutomationTask) end - context ".parse_out_objects" do - it "isolates objects including the '::' class separator" do - object_parameters = {'VmOrTemplate::vm' => 10, 'var2' => @ae_var2.to_s, 'var3' => @ae_var3.to_s} - non_object_parameters = {'var2' => @ae_var2.to_s, 'var3' => @ae_var3.to_s} - object_hash = AutomationRequest.parse_out_objects(object_parameters) - non_object_hash = AutomationRequest.parse_out_objects(non_object_parameters) - expect(object_hash).to eq 'VmOrTemplate::vm' => 10 - expect(non_object_hash).to be_a Hash - expect(non_object_hash).to be_empty - end - end - context ".create_from_ws" do it "with empty requester string" do ar = AutomationRequest.create_from_ws(@version, admin, @uri_parts, @parameters, {}) @@ -51,6 +39,14 @@ expect(ar.options[:attrs][:userid]).to eq(admin.userid) expect(ar.options[:schedule_type]).to eq("immediately") expect(ar.options[:schedule_time]).to eq(nil) + expect(ar.options[:attrs].keys).to eq([:var1, :var2, :var3, :userid]) + + end + + it "with object in parameters" do + @parameters = {'var1' => @ae_var1.to_s, 'var2' => @ae_var2.to_s, 'var3' => @ae_var3.to_s, 'VmOrTemplate::vm' => 10} + ar = AutomationRequest.create_from_ws(@version, admin, @uri_parts, @parameters, {}) + expect(ar.options[:attrs].keys).to include("VmOrTemplate::vm") end it "creates request with schedule" do @@ -65,6 +61,7 @@ @object_parameters = {'VmOrTemplate::vm' => 10, 'var2' => @ae_var2.to_s, 'var3' => @ae_var3.to_s} ar = AutomationRequest.create_from_ws(@version, admin, @uri_parts, @object_parameters, {}) expect(ar.options[:attrs]).to include("VmOrTemplate::vm" => 10, :var2 => @ae_var2.to_s) + expect(@object_parameters).to include("VmOrTemplate::vm" => 10) end it "does not allow overriding userid who is NOT in the database" do diff --git a/spec/models/miq_schedule_spec.rb b/spec/models/miq_schedule_spec.rb index 6fa2459fc893..31b462d8b4cf 100644 --- a/spec/models/miq_schedule_spec.rb +++ b/spec/models/miq_schedule_spec.rb @@ -566,6 +566,7 @@ context "valid action_automation_request" do let(:admin) { FactoryBot.create(:user_miq_request_approver) } + let(:ems) { FactoryBot.create(:ext_management_system) } let(:automate_sched) do MiqSchedule.create(:name => "test_method", :resource_type => "AutomationRequest", :userid => admin.userid, :enabled => true, @@ -575,7 +576,8 @@ :filter => {:uri_parts => {:namespace => 'ss', :instance => 'vv', :message => 'mm'}, - :parameters => {"param" => "8"}}) + :parameters => {"param" => "8", + "ExtManagementSystem::ext_management_system" => ems.id}}) end it "should create a request from a scheduled task" do @@ -587,6 +589,7 @@ FactoryBot.create(:user_admin, :userid => 'admin') automate_sched.action_automation_request(AutomationRequest, '') expect(AutomationRequest.where(:description => "Automation Task", :userid => admin.userid).count).to eq(1) + expect(automate_sched.filter[:parameters].keys).to include("ExtManagementSystem::ext_management_system") end end