diff --git a/app/models/mixins/inter_region_api_method_relay.rb b/app/models/mixins/inter_region_api_method_relay.rb index 789ae722da9..1300a89f983 100644 --- a/app/models/mixins/inter_region_api_method_relay.rb +++ b/app/models/mixins/inter_region_api_method_relay.rb @@ -29,9 +29,7 @@ def api_relay_method(method, action = method) if in_current_region? super(*meth_args, &meth_block) else - InterRegionApiMethodRelay.exec_api_call(region_number, collection_name, action, api_args) do - [{:id => id}] - end + InterRegionApiMethodRelay.exec_api_call(region_number, collection_name, action, api_args, id) end end end @@ -71,14 +69,11 @@ def self.api_client_connection_for_region(region_number, user = User.current_use ) end - def self.exec_api_call(region, collection_name, action, api_args = nil, &resource_block) + def self.exec_api_call(region, collection_name, action, api_args = nil, id = nil) api_args ||= {} collection = api_client_connection_for_region(region).public_send(collection_name) - result = if resource_block - collection.public_send(action, api_args, &resource_block) - else - collection.public_send(action, api_args) - end + collection_or_instance = id ? collection.find(id) : collection + result = collection_or_instance.public_send(action, api_args) case result when ManageIQ::API::Client::ActionResult raise InterRegionApiMethodRelayError, result.message if result.failed? diff --git a/spec/models/mixins/inter_region_api_method_relay_spec.rb b/spec/models/mixins/inter_region_api_method_relay_spec.rb index fb7bdca0778..390757c2bac 100644 --- a/spec/models/mixins/inter_region_api_method_relay_spec.rb +++ b/spec/models/mixins/inter_region_api_method_relay_spec.rb @@ -83,13 +83,12 @@ def self.test_class_method_action(_arg) end def expect_api_call(expected_action, expected_args = nil) - expect(described_class).to receive(:exec_api_call) do |region_num, collection, action, args, &block| + expect(described_class).to receive(:exec_api_call) do |region_num, collection, action, args, instance_id| expect(region_num).to eq(region) expect(collection).to eq(collection_name) expect(action).to eq(expected_action) expect(args).to eq(expected_args) if expected_args - - expect(block.call).to eq([{:id => id}]) + expect(instance_id).to eq(id) end end @@ -230,54 +229,27 @@ def expect_api_call(expected_action, expected_args = nil) }.to raise_error(described_class::InterRegionApiMethodRelayError) end - context "when no block is passed" do - it "calls the given action with the given args" do - args = {:my => "args", :here => 123} - expect(api_collection).to receive(action).with(args).and_return(api_success_result) - described_class.exec_api_call(region, collection_name, action, args) - end - - it "defaults the args to an empty hash" do - expect(api_collection).to receive(action).with({}).and_return(api_success_result) - described_class.exec_api_call(region, collection_name, action) - end - - it "defaults the args to an empty hash when nil is explicitly passed as args" do - expect(api_collection).to receive(action).with({}).and_return(api_success_result) - described_class.exec_api_call(region, collection_name, action, nil) - end + it "calls the given action with the given args" do + args = {:my => "args", :here => 123} + expect(api_collection).to receive(action).with(args).and_return(api_success_result) + described_class.exec_api_call(region, collection_name, action, args) end - context "when a block is passed" do - let(:resource_proc) { -> { "some stuff" } } - - it "calls the given action with the given args" do - expected_args = {:my => "args", :here => 123} - expect(api_collection).to receive(action) do |args, &block| - expect(args).to eq(expected_args) - expect(block.call).to eq("some stuff") - end.and_return(api_success_result) - - described_class.exec_api_call(region, collection_name, action, expected_args, &resource_proc) - end - - it "defaults the args to an empty hash" do - expect(api_collection).to receive(action) do |args, &block| - expect(args).to eq({}) - expect(block.call).to eq("some stuff") - end.and_return(api_success_result) - - described_class.exec_api_call(region, collection_name, action, &resource_proc) - end + it "defaults the args to an empty hash" do + expect(api_collection).to receive(action).with({}).and_return(api_success_result) + described_class.exec_api_call(region, collection_name, action) + end - it "defaults the args to an empty hash when nil is explicitly passed as args" do - expect(api_collection).to receive(action) do |args, &block| - expect(args).to eq({}) - expect(block.call).to eq("some stuff") - end.and_return(api_success_result) + it "defaults the args to an empty hash when nil is explicitly passed as args" do + expect(api_collection).to receive(action).with({}).and_return(api_success_result) + described_class.exec_api_call(region, collection_name, action, nil) + end - described_class.exec_api_call(region, collection_name, action, nil, &resource_proc) - end + it "calls a method on an instance if id is passed" do + instance = double("instance") + expect(api_collection).to receive(:find).with(4).and_return(instance) + expect(instance).to receive(action).and_return(api_success_result) + described_class.exec_api_call(region, collection_name, action, nil, 4) end end end