From b55bbebc6ab276e95dcaa386dea95bf89b2599d1 Mon Sep 17 00:00:00 2001 From: Lucy Fu Date: Thu, 21 Sep 2017 15:50:12 -0400 Subject: [PATCH 1/2] Get the workspace when calls are from automate engine. Generic object method calls are coming in from automate engine instead of Drb server via REST API. --- .../engine/miq_ae_engine/miq_ae_workspace_runtime.rb | 2 +- .../service_models/miq_ae_service_generic_object.rb | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/miq_automation_engine/engine/miq_ae_engine/miq_ae_workspace_runtime.rb b/lib/miq_automation_engine/engine/miq_ae_engine/miq_ae_workspace_runtime.rb index beffafbc0..0c80d6eed 100644 --- a/lib/miq_automation_engine/engine/miq_ae_engine/miq_ae_workspace_runtime.rb +++ b/lib/miq_automation_engine/engine/miq_ae_engine/miq_ae_workspace_runtime.rb @@ -45,8 +45,8 @@ def self.clear_stored_workspace def self.instantiate(uri, user, attrs = {}) User.current_user = user workspace = MiqAeWorkspaceRuntime.new(attrs) - workspace.instantiate(uri, user, nil) self.current = workspace + workspace.instantiate(uri, user, nil) workspace rescue MiqAeException ensure diff --git a/lib/miq_automation_engine/service_models/miq_ae_service_generic_object.rb b/lib/miq_automation_engine/service_models/miq_ae_service_generic_object.rb index 571451986..4df919f11 100644 --- a/lib/miq_automation_engine/service_models/miq_ae_service_generic_object.rb +++ b/lib/miq_automation_engine/service_models/miq_ae_service_generic_object.rb @@ -17,7 +17,9 @@ def remove_from_service(service) private def ae_user_identity - @ae_user = MiqAeEngine::DrbRemoteInvoker.workspace.ae_user + workspace = MiqAeEngine::MiqAeWorkspaceRuntime.current || MiqAeEngine::DrbRemoteInvoker.workspace + raise 'Workspace not found when running generic object' unless workspace + @ae_user = workspace.ae_user ar_method { @object.ae_user_identity(@ae_user, @ae_user.current_group, @ae_user.current_tenant) } end From 16fe96e161afd011749e1fe845e88e0ddb6d2a21 Mon Sep 17 00:00:00 2001 From: Lucy Fu Date: Thu, 21 Sep 2017 15:52:34 -0400 Subject: [PATCH 2/2] Add test case for generic object method calls from REST API. --- .../miq_ae_service_generic_object_spec.rb | 45 ++++++++++++------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/spec/service_models/miq_ae_service_generic_object_spec.rb b/spec/service_models/miq_ae_service_generic_object_spec.rb index 5b330cdd5..eb6bf9946 100644 --- a/spec/service_models/miq_ae_service_generic_object_spec.rb +++ b/spec/service_models/miq_ae_service_generic_object_spec.rb @@ -34,23 +34,38 @@ module MiqAeServiceGenericObjectSpec end describe "call method on generic object" do - before do - allow(MiqAeEngine::DrbRemoteInvoker).to receive('workspace').and_return(workspace) + shared_examples_for "calls into automate" do + it "runs successfully" do + options = { + :object_type => "GenericObject", + :object_id => go.id, + :instance_name => "GenericObject", + :user_id => user.id, + :miq_group_id => user.current_group.id, + :tenant_id => user.current_tenant.id, + :attrs => {:method_name => method_name} + } + svc_obj = MiqAeMethodService::MiqAeServiceGenericObject.find(go.id) + expect(MiqAeEngine).to receive('deliver').with(options).and_return(workspace) + + svc_obj.send(method_name) + end + end + + context "from Drb method" do + before do + allow(MiqAeEngine::DrbRemoteInvoker).to receive('workspace').and_return(workspace) + end + + it_behaves_like "calls into automate" end - it "calls into automate" do - options = { :object_type => "GenericObject", - :object_id => go.id, - :instance_name => "GenericObject", - :user_id => user.id, - :miq_group_id => user.current_group.id, - :tenant_id => user.current_tenant.id, - :attrs => {:method_name => method_name} - } - svc_obj = MiqAeMethodService::MiqAeServiceGenericObject.find(go.id) - expect(MiqAeEngine).to receive('deliver').with(options).and_return(workspace) - - svc_obj.send(method_name) + context "from workspace engine" do + before do + allow(MiqAeEngine::MiqAeWorkspaceRuntime).to receive('current').and_return(workspace) + end + + it_behaves_like "calls into automate" end end