From 11cb5cb8ab478818619d5a9580ea0cac68f42ef0 Mon Sep 17 00:00:00 2001 From: Madhu Kanoor Date: Mon, 20 Mar 2017 15:55:33 -0400 Subject: [PATCH 1/2] Add a add_to_service method to Vm https://www.pivotaltracker.com/n/projects/1937537/stories/142057167 --- app/models/vm.rb | 4 ++++ .../service_models/miq_ae_service_vm.rb | 2 +- spec/models/vm_spec.rb | 9 +++++++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/app/models/vm.rb b/app/models/vm.rb index efe9774c103..35d36a29ea9 100644 --- a/app/models/vm.rb +++ b/app/models/vm.rb @@ -32,6 +32,10 @@ def validate_remote_console_vmrc_support _("VMRC remote console is not supported on %{vendor}.") % {:vendor => vendor}) end + def add_to_service(service) + service.add_resource!(self) + end + def self.find_all_by_mac_address_and_hostname_and_ipaddress(mac_address, hostname, ipaddress) return [] if mac_address.blank? && hostname.blank? && ipaddress.blank? diff --git a/lib/miq_automation_engine/service_models/miq_ae_service_vm.rb b/lib/miq_automation_engine/service_models/miq_ae_service_vm.rb index 6e8bbc94623..ce57d6c5b53 100644 --- a/lib/miq_automation_engine/service_models/miq_ae_service_vm.rb +++ b/lib/miq_automation_engine/service_models/miq_ae_service_vm.rb @@ -6,7 +6,7 @@ def remote_console_url=(url) def add_to_service(service) raise ArgumentError, "service must be a MiqAeServiceService" unless service.kind_of?(MiqAeMethodService::MiqAeServiceService) - ar_method { wrap_results(Service.find_by(:id => service.id).add_resource!(@object)) } + ar_method { wrap_results(@object.add_to_service(Service.find(service.id))) } end def remove_from_service diff --git a/spec/models/vm_spec.rb b/spec/models/vm_spec.rb index 01ce7c0da59..b828f2e32f1 100644 --- a/spec/models/vm_spec.rb +++ b/spec/models/vm_spec.rb @@ -262,4 +262,13 @@ expect(console.url).to eq(url) expect(console.url_secret).to be end + + it '#add_to_service' do + vm = FactoryGirl.create(:vm_vmware) + service = FactoryGirl.create(:service) + + vm.add_to_service(service) + service.reload + expect(service.vms.count).to eq(1) + end end From c13b5fdbf6af3fb61f7c4123a3e904ca4079ea04 Mon Sep 17 00:00:00 2001 From: Madhu Kanoor Date: Mon, 20 Mar 2017 17:12:47 -0400 Subject: [PATCH 2/2] changed to using find_by instead of find --- lib/miq_automation_engine/service_models/miq_ae_service_vm.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/miq_automation_engine/service_models/miq_ae_service_vm.rb b/lib/miq_automation_engine/service_models/miq_ae_service_vm.rb index ce57d6c5b53..fe8e71f4e91 100644 --- a/lib/miq_automation_engine/service_models/miq_ae_service_vm.rb +++ b/lib/miq_automation_engine/service_models/miq_ae_service_vm.rb @@ -6,7 +6,7 @@ def remote_console_url=(url) def add_to_service(service) raise ArgumentError, "service must be a MiqAeServiceService" unless service.kind_of?(MiqAeMethodService::MiqAeServiceService) - ar_method { wrap_results(@object.add_to_service(Service.find(service.id))) } + ar_method { wrap_results(@object.add_to_service(Service.find_by(:id => service.id))) } end def remove_from_service