From b64a6a8c36ae62e70786c4640e0e31654aaaf0c5 Mon Sep 17 00:00:00 2001 From: Adam Grare Date: Tue, 20 Oct 2020 09:24:53 -0400 Subject: [PATCH] Move the vmware-specific assign_ems_created_on method The VmOrTemplate.assign_ems_created_on method is very VMware specific, it looks for VMware events then queries the historical event history to find created events. --- .../providers/vmware/infra_manager.rb | 37 +++++++++++++++++++ .../infra_manager/inventory/persister.rb | 20 ++++++++++ 2 files changed, 57 insertions(+) diff --git a/app/models/manageiq/providers/vmware/infra_manager.rb b/app/models/manageiq/providers/vmware/infra_manager.rb index ebfd806ee..937b404d2 100644 --- a/app/models/manageiq/providers/vmware/infra_manager.rb +++ b/app/models/manageiq/providers/vmware/infra_manager.rb @@ -654,6 +654,43 @@ def find_vm_create_events(vms_list) found end + def assign_ems_created_on_queue(vm_ids) + MiqQueue.submit_job( + :class_name => self.class.name, + :instance_id => id, + :method_name => 'assign_ems_created_on', + :role => 'ems_operations', + :args => [vm_ids], + :priority => MiqQueue::MIN_PRIORITY + ) + end + + def assign_ems_created_on(vm_ids) + vms_to_update = vms_and_templates.where(:id => vm_ids, :ems_created_on => nil) + return if vms_to_update.empty? + + # Of the VMs without a VM create time, filter out the ones for which we + # already have a VM create event + vms_to_update = vms_to_update.reject do |v| + event = v.ems_events.find_by(:event_type => ["VmCreatedEvent", "VmDeployedEvent"]) + v.update_attribute(:ems_created_on, event.timestamp) if event && v.ems_created_on != event.timestamp + event + end + return if vms_to_update.empty? + + # Of the VMs still without an VM create time, use historical events, if + # available, to determine the VM create time + + vms_list = vms_to_update.collect { |v| {:id => v.id, :name => v.name, :uid_ems => v.uid_ems} } + found = find_vm_create_events(vms_list) + + # Loop through the found VMs and set their create times + found.each do |vmh| + v = vms_to_update.detect { |vm| vm.id == vmh[:id] } + v.update_attribute(:ems_created_on, vmh[:created_time]) + end + end + def get_files_on_datastore(datastore) with_provider_connection do |vim| begin diff --git a/app/models/manageiq/providers/vmware/infra_manager/inventory/persister.rb b/app/models/manageiq/providers/vmware/infra_manager/inventory/persister.rb index 704806d9c..a4e2b116e 100644 --- a/app/models/manageiq/providers/vmware/infra_manager/inventory/persister.rb +++ b/app/models/manageiq/providers/vmware/infra_manager/inventory/persister.rb @@ -45,6 +45,7 @@ def initialize_inventory_collections add_collection(infra, :vm_resource_pools) add_collection(infra, :root_folder_relationship) add_collection(infra, :orchestration_templates) + vms_and_templates_assign_created_on if ::Settings.ems_refresh.capture_vm_created_on_date end def vim_class_to_collection(managed_object) @@ -61,4 +62,23 @@ def vim_class_to_collection(managed_object) resource_pools end end + + private + + def vms_and_templates_assign_created_on + custom_save_block = lambda do |ems, inventory_collection| + vms_and_templates = inventory_collection.dependency_attributes[:vms_and_templates]&.first + return if vms_and_templates.nil? + + created_vm_ids = vms_and_templates.created_records.map { |rec| rec[:id] } + ems.assign_ems_created_on_queue(created_vm_ids) unless created_vm_ids.empty? + end + + settings = {:without_model_class => true, :auto_inventory_attributes => false} + + add_collection(infra, :vms_and_templates_assign_created_on, {}, settings) do |builder| + builder.add_custom_save_block(custom_save_block) + builder.add_dependency_attributes(:vms_and_templates => ->(persister) { [persister.vms_and_templates] }) + end + end end