Skip to content

Commit

Permalink
Merge pull request #655 from agrare/move_vmware_specific_assign_ems_c…
Browse files Browse the repository at this point in the history
…reated_on

Move the vmware-specific assign_ems_created_on method
  • Loading branch information
chessbyte authored Oct 20, 2020
2 parents 1be076a + b64a6a8 commit bced590
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
37 changes: 37 additions & 0 deletions app/models/manageiq/providers/vmware/infra_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

0 comments on commit bced590

Please sign in to comment.