Class: InventoryRefresh::TargetCollection

Inherits:
Object
  • Object
show all
Defined in:
lib/inventory_refresh/target_collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(manager: nil, manager_id: nil, event: nil, targets: []) ⇒ TargetCollection

Returns a new instance of TargetCollection.

Parameters:

  • manager (ManageIQ::Providers::BaseManager) (defaults to: nil)

    manager owning the TargetCollection

  • manager_id (Integer) (defaults to: nil)

    primary key of manager owning the TargetCollection

  • event (EmsEvent) (defaults to: nil)

    EmsEvent associated with the TargetCollection

  • targets (Array<InventoryRefresh::Target, ApplicationRecord>) (defaults to: [])

    Array of InventoryRefresh::Target objects or ApplicationRecord objects



14
15
16
17
18
19
# File 'lib/inventory_refresh/target_collection.rb', line 14

def initialize(manager: nil, manager_id: nil, event: nil, targets: [])
  @manager    = manager
  @manager_id = manager_id
  @event      = event
  @targets    = targets
end

Instance Attribute Details

#targetsObject (readonly)

Returns the value of attribute targets.



5
6
7
# File 'lib/inventory_refresh/target_collection.rb', line 5

def targets
  @targets
end

Instance Method Details

#add_target(association:, manager_ref:, manager: nil, manager_id: nil, event_id: nil, options: {}) ⇒ Object

Parameters:

  • association (Symbol)

    An existing association on Manager, that lists objects represented by a Target, naming should be the same of association of a counterpart InventoryCollection object

  • manager_ref (Hash)

    A Hash that can be used to find_by on a given association and returning a unique object. The keys should be the same as the keys of the counterpart InventoryObject

  • manager (ManageIQ::Providers::BaseManager) (defaults to: nil)

    The Manager owning the Target

  • manager_id (Integer) (defaults to: nil)

    A primary key of the Manager owning the Target

  • event_id (Integer) (defaults to: nil)

    A primary key of the EmsEvent associated with the Target

  • options (Hash) (defaults to: {})

    A free form options hash



29
30
31
32
33
34
35
36
# File 'lib/inventory_refresh/target_collection.rb', line 29

def add_target(association:, manager_ref:, manager: nil, manager_id: nil, event_id: nil, options: {})
  self << InventoryRefresh::Target.new(:association => association,
                                     :manager_ref => manager_ref,
                                     :manager     => manager || @manager,
                                     :manager_id  => manager_id || @manager_id || @manager.try(:id),
                                     :event_id    => event_id || @event.try(:id),
                                     :options     => options)
end

#idString

Returns A String containing an id of each target in the TargetCollection.

Returns:

  • (String)

    A String containing an id of each target in the TargetCollection



44
45
46
# File 'lib/inventory_refresh/target_collection.rb', line 44

def id
  "Collection of targets with id: #{targets.collect(&:name)}"
end

#manager_refs_by_associationObject

Returns targets in a format:

{
  :vms => {:ems_ref => Set.new(["vm_ref_1", "vm_ref2"])},
  :network_ports => {:ems_ref => Set.new(["network_port_1", "network_port2"])
}

Then we can quickly access all objects affected by:

NetworkPort.where(target_collection.manager_refs_by_association[:network_ports].to_a) =>
  return AR objects with ems_refs ["network_port_1", "network_port2"]

And we can get a list of ids for the API query by:

target_collection.manager_refs_by_association[:network_ports][:ems_ref].to_a =>
  ["network_port_1", "network_port2"]

Only targets of a type InventoryRefresh::Target are processed, any other targets present should be converted to InventoryRefresh::Target, e.g. in the Inventory::Collector code.



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/inventory_refresh/target_collection.rb', line 63

def manager_refs_by_association
  @manager_refs_by_association ||= targets.select { |x| x.kind_of?(InventoryRefresh::Target) }.each_with_object({}) do |x, obj|
    if obj[x.association].blank?
      obj[x.association] = x.manager_ref.each_with_object({}) { |(key, value), hash| hash[key] = Set.new([value]) }
    else
      obj[x.association].each do |key, value|
        value << x.manager_ref[key]
      end
    end
  end
end

#manager_refs_by_association_resetObject

Resets the cached @manager_refs_by_association to enforce reload when calling :manager_refs_by_association method



76
77
78
# File 'lib/inventory_refresh/target_collection.rb', line 76

def manager_refs_by_association_reset
  @manager_refs_by_association = nil
end

#nameString

Returns A String containing a summary.

Returns:

  • (String)

    A String containing a summary



39
40
41
# File 'lib/inventory_refresh/target_collection.rb', line 39

def name
  "Collection of #{targets.size} targets"
end

#name_references(collection) ⇒ Array<String>

Returns list of names

Returns:

  • (Array<String>)


88
89
90
# File 'lib/inventory_refresh/target_collection.rb', line 88

def name_references(collection)
  manager_refs_by_association.try(:[], collection).try(:[], :name)&.to_a || []
end

#references(collection) ⇒ Array<String>

Returns list of ems_refs

Returns:

  • (Array<String>)


82
83
84
# File 'lib/inventory_refresh/target_collection.rb', line 82

def references(collection)
  manager_refs_by_association.try(:[], collection).try(:[], :ems_ref)&.to_a || []
end