From 469bde675d52312d1a1772bac80c9c87631be14f Mon Sep 17 00:00:00 2001 From: william fitzgerald Date: Thu, 2 Aug 2018 13:18:44 -0400 Subject: [PATCH] Adding Inspectme functionality into log_object embedded method. Added the ability to log all attributes, associations, and tags. Inspectme is a great tool for debugging and now will be available from an embedded method. https://github.com/ManageIQ/manageiq-content/issues/350 Added tests --- .../Utils.class/__methods__/log_object.rb | 42 ++++++++++++ .../__methods__/log_object_spec.rb | 64 ++++++++++++++++++- 2 files changed, 105 insertions(+), 1 deletion(-) diff --git a/content/automate/ManageIQ/System/CommonMethods/Utils.class/__methods__/log_object.rb b/content/automate/ManageIQ/System/CommonMethods/Utils.class/__methods__/log_object.rb index 9e8df3bb0..c732fa23c 100644 --- a/content/automate/ManageIQ/System/CommonMethods/Utils.class/__methods__/log_object.rb +++ b/content/automate/ManageIQ/System/CommonMethods/Utils.class/__methods__/log_object.rb @@ -42,6 +42,48 @@ def self.log(obj, object_string = 'Automation Object', handle = $evm) obj.attributes.sort.each { |k, v| handle.log("info", " Attribute - #{k}: #{v}") } handle.log("info", "Listing #{object_string} Attributes - End") end + + def self.log_ar_objects(object_string = 'VMDB Object', handle = $evm) + handle.log("info", "#{object_string} log_ar_objects Begins") + handle.root.attributes.sort.each do |k, v| + log_ar_object(k, v, object_string, handle) if v.class.to_s =~ /MiqAeMethodService::MiqAeService/ + end + end + + def self.log_ar_object(key, object, object_string, handle = $evm) + handle.log("info", "key:<#{key}> object:<#{object}>") + attributes(object, object_string, handle) + associations(object, object_string, handle) + tags(object, object_string, handle) + end + + def self.attributes(obj, object_string, handle = $evm) + handle.log("info", " #{object_string} Begin Attributes [object.attributes]") + obj.attributes.sort.each do |k, v| + handle.log("info", " Attribute: #{k} = #{v.inspect}") + end + handle.log("info", " #{object_string} End Attributes [object.attributes]") + end + + def self.associations(obj, object_string, handle = $evm) + return if obj.associations.empty? + handle.log("info", " #{object_string} Begin Associations [object.associations]") + obj.associations.sort.each do |assc| + handle.log("info", " Associations - #{assc}") + end + handle.log("info", " #{object_string} End Associations [object.associations]") + end + + def self.tags(obj, object_string, handle = $evm) + return if obj.tags.nil? + handle.log("info", " #{object_string} Begin Tags [object.tags]") + obj.tags.sort.each do |tag_element| + tag_text = tag_element.split('/') + + handle.log("info", " Category:<#{tag_text.first.inspect}> Tag:<#{tag_text.last.inspect}>") + end + handle.log("info", " #{object_string} End Tags [object.tags]") + end end end end diff --git a/spec/content/automate/ManageIQ/System/CommonMethods/Utils.class/__methods__/log_object_spec.rb b/spec/content/automate/ManageIQ/System/CommonMethods/Utils.class/__methods__/log_object_spec.rb index 721daa66b..5164aa84f 100644 --- a/spec/content/automate/ManageIQ/System/CommonMethods/Utils.class/__methods__/log_object_spec.rb +++ b/spec/content/automate/ManageIQ/System/CommonMethods/Utils.class/__methods__/log_object_spec.rb @@ -3,15 +3,27 @@ let(:user) { FactoryGirl.create(:user_with_email_and_group) } let(:svc_model_user) { MiqAeMethodService::MiqAeServiceUser.find(user.id) } let(:ems) { FactoryGirl.create(:ext_management_system) } + let(:ar_object) { svc_model_user } let(:root) do Spec::Support::MiqAeMockObject.new( 'dialog_provider' => ems.id.to_s, - 'user' => svc_model_user, + 'ar_object' => ar_object, 'current' => current_object ) end + let(:small_environment_zone) { FactoryGirl.create(:small_environment) } + let(:parent_classification) { FactoryGirl.create(:classification, :description => "Environment", :name => "environment", :read_only => false) } + let(:classification) { FactoryGirl.create(:classification, :name => "prod", :description => "Production", :parent => parent_classification, :read_only => true) } + let(:vm1) do + small_environment_zone + + Vm.first + end + + let(:svc_model_vm1) { MiqAeMethodService::MiqAeServiceVm.find(vm1.id) } + let(:current_object) { Spec::Support::MiqAeMockObject.new('a' => 1, 'b' => 2) } let(:ae_service) do Spec::Support::MiqAeMockService.new(root).tap do |service| @@ -47,4 +59,54 @@ # described_class.log(ae_service, root) ManageIQ::Automate::System::CommonMethods::Utils::LogObject.log(root, 'My Object', ae_service) end + + context 'log ar_object' do + let(:ar_object) { svc_model_vm1 } + + it 'with default VMDB Object string' do + classification.assign_entry_to(vm1) + expect(ae_service).to receive(:log).with('info', /log_ar_objects/).exactly(1).times + expect(ae_service).to receive(:log).with('info', /key:/).exactly(1).times + + expect(ae_service).to receive(:log).with('info', / VMDB Object Begin Attributes/).exactly(1).times + expect(ae_service).to receive(:log).with('info', / Attribute:/).exactly(vm1.attributes.count).times + expect(ae_service).to receive(:log).with('info', / VMDB Object End Attributes/).exactly(1).times + + expect(ae_service).to receive(:log).with('info', / VMDB Object Begin Associations/).exactly(1).times + expect(ae_service).to receive(:log).with('info', / Associations -/).exactly(svc_model_vm1.associations.count).times + expect(ae_service).to receive(:log).with('info', / VMDB Object End Associations/).exactly(1).times + + expect(ae_service).to receive(:log).with('info', / VMDB Object Begin Tags /).exactly(1).times + expect(ae_service).to receive(:log).with('info', / Category:/).exactly(vm1.tags.count).times + expect(ae_service).to receive(:log).with('info', / VMDB Object End Tags/).exactly(1).times + + # described_class.log_ar_objects('VMDB Object', ae_service) + ManageIQ::Automate::System::CommonMethods::Utils::LogObject.log_ar_objects('VMDB Object', ae_service) + end + end + + context 'log ar_object' do + let(:ar_object) { svc_model_vm1 } + + it ' with My Database Object string' do + classification.assign_entry_to(vm1) + expect(ae_service).to receive(:log).with('info', /log_ar_objects/).exactly(1).times + expect(ae_service).to receive(:log).with('info', /key:/).exactly(1).times + + expect(ae_service).to receive(:log).with('info', / My Database Object Begin Attributes/).exactly(1).times + expect(ae_service).to receive(:log).with('info', / Attribute:/).exactly(vm1.attributes.count).times + expect(ae_service).to receive(:log).with('info', / My Database Object End Attributes/).exactly(1).times + + expect(ae_service).to receive(:log).with('info', / My Database Object Begin Associations/).exactly(1).times + expect(ae_service).to receive(:log).with('info', / Associations -/).exactly(svc_model_vm1.associations.count).times + expect(ae_service).to receive(:log).with('info', / My Database Object End Associations/).exactly(1).times + + expect(ae_service).to receive(:log).with('info', / My Database Object Begin Tags /).exactly(1).times + expect(ae_service).to receive(:log).with('info', / Category:/).exactly(vm1.tags.count).times + expect(ae_service).to receive(:log).with('info', / My Database Object End Tags/).exactly(1).times + + # described_class.log_ar_objects('My Database Object', ae_service) + ManageIQ::Automate::System::CommonMethods::Utils::LogObject.log_ar_objects('My Database Object', ae_service) + end + end end