Skip to content

Commit

Permalink
move custom reconnect block to the base class and use it for both Vm …
Browse files Browse the repository at this point in the history
…and MiqTemplate
  • Loading branch information
Alexander Zagaynov committed Feb 1, 2018
1 parent 197cfd8 commit e77a19d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 33 deletions.
31 changes: 29 additions & 2 deletions app/models/manager_refresh/inventory_collection_default.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,29 @@
class ManagerRefresh::InventoryCollectionDefault
VM_OR_TEMPLATE_RECONNECT_BLOCK = lambda do |inventory_collection, inventory_objects_index, attributes_index|
relation = inventory_collection.model_class.where(:ems_id => nil)

return if relation.count <= 0

inventory_objects_index.each_slice(100) do |batch|
relation.where(inventory_collection.manager_ref.first => batch.map(&:first)).each do |record|
index = inventory_collection.object_index_with_keys(inventory_collection.manager_ref_to_cols, record)

# We need to delete the record from the inventory_objects_index and attributes_index, otherwise it
# would be sent for create.
inventory_object = inventory_objects_index.delete(index)
hash = attributes_index.delete(index)

record.assign_attributes(hash.except(:id, :type))
if !inventory_collection.check_changed? || record.changed?
record.save!
inventory_collection.store_updated_records(record)
end

inventory_object.id = record.id
end
end
end.freeze

class << self
def vms(extra_attributes = {})
attributes = {
Expand Down Expand Up @@ -42,7 +67,8 @@ def vms(extra_attributes = {})
:ems_id => ->(persister) { persister.manager.id },
:name => "unknown",
:location => "unknown",
}
},
:custom_reconnect_block => VM_OR_TEMPLATE_RECONNECT_BLOCK,
}

attributes.merge!(extra_attributes)
Expand Down Expand Up @@ -82,7 +108,8 @@ def miq_templates(extra_attributes = {})
:name => "unknown",
:location => "unknown",
:template => true
}
},
:custom_reconnect_block => VM_OR_TEMPLATE_RECONNECT_BLOCK,
}

attributes.merge!(extra_attributes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,37 +310,6 @@ def hosts(extra_attributes = {})
attributes.merge!(extra_attributes)
end

def vms(extra_attributes = {})
attributes = {
:custom_reconnect_block => lambda do |inventory_collection, inventory_objects_index, attributes_index|
relation = inventory_collection.model_class.where(:ems_id => nil)

return if relation.count <= 0

inventory_objects_index.each_slice(100) do |batch|
relation.where(inventory_collection.manager_ref.first => batch.map(&:first)).each do |record|
index = inventory_collection.object_index_with_keys(inventory_collection.manager_ref_to_cols, record)

# We need to delete the record from the inventory_objects_index and attributes_index, otherwise it
# would be sent for create.
inventory_object = inventory_objects_index.delete(index)
hash = attributes_index.delete(index)

record.assign_attributes(hash.except(:id, :type))
if !inventory_collection.check_changed? || record.changed?
record.save!
inventory_collection.store_updated_records(record)
end

inventory_object.id = record.id
end
end
end
}

super(attributes.merge!(extra_attributes))
end

def host_storages(extra_attributes = {})
attributes = {
:model_class => ::HostStorage,
Expand Down

0 comments on commit e77a19d

Please sign in to comment.