class InventoryRefresh::Persister

Attributes

collections[R]
ingress_api_sent_at[RW]
manager[R]
persister_finished_at[RW]
persister_started_at[RW]
refresh_state_part_collected_at[RW]
refresh_state_part_sent_at[RW]
refresh_state_part_uuid[RW]
refresh_state_sent_at[RW]
refresh_state_started_at[RW]
refresh_state_uuid[RW]
refresh_time_tracking[RW]
retry_count[RW]
retry_max[RW]
sweep_scope[RW]
total_parts[RW]

Public Class Methods

from_hash(persister_data, manager) click to toggle source

Returns Persister object built from serialized data

@param persister_data [Hash] serialized Persister object in hash @return [ManageIQ::Providers::Inventory::Persister] Persister object built from serialized data

# File lib/inventory_refresh/persister.rb, line 142
def from_hash(persister_data, manager)
  new(manager).tap do |persister|
    persister_data['collections'].each do |collection|
      inventory_collection = persister.collections[collection['name'].try(:to_sym)]
      raise "Unrecognized InventoryCollection name: #{collection['name']}" if inventory_collection.blank?

      inventory_collection.from_hash(collection, persister.collections)
    end

    persister.refresh_state_uuid              = persister_data['refresh_state_uuid']
    persister.refresh_state_part_uuid         = persister_data['refresh_state_part_uuid']
    persister.refresh_state_part_collected_at = persister_data['refresh_state_part_collected_at']
    persister.refresh_state_part_sent_at      = persister_data['refresh_state_part_sent_at']
    persister.refresh_state_started_at        = persister_data['refresh_state_started_at']
    persister.refresh_state_sent_at           = persister_data['refresh_state_sent_at']
    persister.ingress_api_sent_at             = persister_data['ingress_api_sent_at']
    persister.retry_count                     = persister_data['retry_count']
    persister.retry_max                       = persister_data['retry_max']
    persister.total_parts                     = persister_data['total_parts']
    persister.sweep_scope                     = sweep_scope_from_hash(persister_data['sweep_scope'], persister.collections)
  end
end
from_json(json_data, manager) click to toggle source

Returns Persister object loaded from a passed JSON

@param json_data [String] input JSON data @return [ManageIQ::Providers::Inventory::Persister] Persister object loaded from a passed JSON

# File lib/inventory_refresh/persister.rb, line 134
def from_json(json_data, manager)
  from_hash(JSON.parse(json_data), manager)
end
new(manager) click to toggle source

@param manager [ManageIQ::Providers::BaseManager] A manager object

# File lib/inventory_refresh/persister.rb, line 14
def initialize(manager)
  @manager = manager

  @collections = {}

  self.persister_started_at = Time.now.utc.to_datetime.to_s

  initialize_inventory_collections
end

Private Class Methods

assert_sweep_scope!(sweep_scope) click to toggle source
# File lib/inventory_refresh/persister.rb, line 167
def assert_sweep_scope!(sweep_scope)
  return unless sweep_scope

  allowed_format_message = "Allowed format of sweep scope is Array<String> or Hash{String => Hash}, got #{sweep_scope}"

  if sweep_scope.kind_of?(Array)
    return if sweep_scope.all? { |x| x.kind_of?(String) || x.kind_of?(Symbol) }

    raise InventoryRefresh::Exception::SweeperScopeBadFormat, allowed_format_message
  elsif sweep_scope.kind_of?(Hash)
    return if sweep_scope.values.all? { |x| x.kind_of?(Array) }

    raise InventoryRefresh::Exception::SweeperScopeBadFormat, allowed_format_message
  end

  raise InventoryRefresh::Exception::SweeperScopeBadFormat, allowed_format_message
end
sweep_scope_from_hash(sweep_scope, available_inventory_collections) click to toggle source
# File lib/inventory_refresh/persister.rb, line 185
def sweep_scope_from_hash(sweep_scope, available_inventory_collections)
  assert_sweep_scope!(sweep_scope)

  return sweep_scope unless sweep_scope.kind_of?(Hash)

  sweep_scope.each_with_object({}) do |(k, v), obj|
    inventory_collection = available_inventory_collections[k.try(:to_sym)]
    raise "Unrecognized InventoryCollection name: #{k}" if inventory_collection.blank?

    serializer = InventoryRefresh::InventoryCollection::Serialization.new(inventory_collection)

    obj[k] = serializer.sweep_scope_from_hash(v, available_inventory_collections)
  end.symbolize_keys!
end

Public Instance Methods

add_collection(collection_name, builder_class = inventory_collection_builder, extra_properties = {}, settings = {}, &block) click to toggle source

Interface for creating InventoryCollection under @collections

@param builder_class [ManageIQ::Providers::Inventory::Persister::Builder] or subclasses @param collection_name [Symbol || Array] used as InventoryCollection:association @param extra_properties [Hash] props from InventoryCollection.initialize list

- adds/overwrites properties added by builder

@param settings [Hash] builder settings

- @see ManageIQ::Providers::Inventory::Persister::Builder.default_options
- @see make_builder_settings()

@example

add_collection(:vms, ManageIQ::Providers::Inventory::Persister::Builder::CloudManager) do |builder|
  builder.add_properties(
    :strategy => :local_db_cache_all,
  )
)

@see documentation github.com/ManageIQ/guides/tree/master/providers/persister/inventory_collections.md

# File lib/inventory_refresh/persister.rb, line 44
def add_collection(collection_name, builder_class = inventory_collection_builder, extra_properties = {}, settings = {}, &block)
  builder = builder_class.prepare_data(collection_name,
                                       self.class,
                                       builder_settings(settings),
                                       &block)

  builder.add_properties(extra_properties) if extra_properties.present?
  builder.evaluate_lambdas!(self)

  collections[collection_name] = builder.to_inventory_collection
end
define_collections_reader(collection_key) click to toggle source

Defines a new attr reader returning InventoryCollection object

# File lib/inventory_refresh/persister.rb, line 82
def define_collections_reader(collection_key)
  define_singleton_method(collection_key) do
    collections[collection_key]
  end
end
inventory_collection_builder() click to toggle source
# File lib/inventory_refresh/persister.rb, line 88
def inventory_collection_builder
  ::InventoryRefresh::InventoryCollection::Builder
end
inventory_collections() click to toggle source

@return [Array<InventoryRefresh::InventoryCollection>] array of InventoryCollection objects of the persister

# File lib/inventory_refresh/persister.rb, line 57
def inventory_collections
  collections.values
end
inventory_collections_names() click to toggle source

@return [Array<Symbol>] array of InventoryCollection object names of the persister

# File lib/inventory_refresh/persister.rb, line 62
def inventory_collections_names
  collections.keys
end
method_missing(method_name, *arguments, &block) click to toggle source

@return [InventoryRefresh::InventoryCollection] returns a defined InventoryCollection or undefined method

Calls superclass method
# File lib/inventory_refresh/persister.rb, line 67
def method_missing(method_name, *arguments, &block)
  if inventory_collections_names.include?(method_name)
    self.define_collections_reader(method_name)
    send(method_name)
  else
    super
  end
end
persist!() click to toggle source

Persists InventoryCollection objects into the DB

# File lib/inventory_refresh/persister.rb, line 93
def persist!
  InventoryRefresh::SaveInventory.save_inventory(manager, inventory_collections)
end
respond_to_missing?(method_name, _include_private = false) click to toggle source

@return [Boolean] true if InventoryCollection with passed method_name name is defined

Calls superclass method
# File lib/inventory_refresh/persister.rb, line 77
def respond_to_missing?(method_name, _include_private = false)
  inventory_collections_names.include?(method_name) || super
end
to_hash() click to toggle source

@return [Hash] entire Persister object serialized to hash

# File lib/inventory_refresh/persister.rb, line 104
def to_hash
  collections_data = collections.map do |_, collection|
    next if collection.data.blank? &&
            collection.skeletal_primary_index.index_data.blank?

    collection.to_hash
  end.compact

  {
    :refresh_state_uuid              => refresh_state_uuid,
    :refresh_state_part_uuid         => refresh_state_part_uuid,
    :refresh_state_part_collected_at => refresh_state_part_collected_at,
    :refresh_state_part_sent_at      => refresh_state_part_sent_at,
    :refresh_state_started_at        => refresh_state_started_at,
    :refresh_state_sent_at           => refresh_state_sent_at,
    :ingress_api_sent_at             => ingress_api_sent_at,
    :refresh_time_tracking           => refresh_time_tracking,
    :retry_count                     => retry_count,
    :retry_max                       => retry_max,
    :total_parts                     => total_parts,
    :sweep_scope                     => sweep_scope_to_hash(sweep_scope),
    :collections                     => collections_data,
  }
end
to_json() click to toggle source

Returns serialized Persisted object to JSON @return [String] serialized Persisted object to JSON

# File lib/inventory_refresh/persister.rb, line 99
def to_json
  JSON.dump(to_hash)
end

Protected Instance Methods

assert_graph_integrity?() click to toggle source
# File lib/inventory_refresh/persister.rb, line 229
def assert_graph_integrity?
  false
end
builder_settings(extra_settings = {}) click to toggle source

@param extra_settings [Hash]

:auto_inventory_attributes
  - auto creates inventory_object_attributes from target model_class setters
  - attributes used in InventoryObject.add_attributes
:without_model_class
  - if false and no model_class derived or specified, throws exception
  - doesn't try to derive model class automatically
  - @see method ManageIQ::Providers::Inventory::Persister::Builder.auto_model_class
# File lib/inventory_refresh/persister.rb, line 215
def builder_settings(extra_settings = {})
  opts = inventory_collection_builder.default_options

  opts[:shared_properties]         = shared_options
  opts[:auto_inventory_attributes] = true
  opts[:without_model_class]       = false

  opts.merge(extra_settings)
end
initialize_inventory_collections() click to toggle source
# File lib/inventory_refresh/persister.rb, line 203
def initialize_inventory_collections
  # can be implemented in a subclass
end
shared_options() click to toggle source

@return [Hash] kwargs shared for all InventoryCollection objects

# File lib/inventory_refresh/persister.rb, line 238
def shared_options
  {
    :strategy               => strategy,
    :parent                 => manager.presence,
    :assert_graph_integrity => assert_graph_integrity?,
    :use_ar_object          => use_ar_object?,
  }
end
strategy() click to toggle source
# File lib/inventory_refresh/persister.rb, line 225
def strategy
  nil
end
use_ar_object?() click to toggle source
# File lib/inventory_refresh/persister.rb, line 233
def use_ar_object?
  true
end

Private Instance Methods

sweep_scope_to_hash(sweep_scope) click to toggle source
# File lib/inventory_refresh/persister.rb, line 249
def sweep_scope_to_hash(sweep_scope)
  return sweep_scope unless sweep_scope.kind_of?(Hash)

  sweep_scope.each_with_object({}) do |(k, v), obj|
    inventory_collection = collections[k.try(:to_sym)]
    raise "Unrecognized InventoryCollection name: #{k}" if inventory_collection.blank?

    serializer = InventoryRefresh::InventoryCollection::Serialization.new(inventory_collection)

    obj[k] = serializer.sweep_scope_to_hash(v)
  end
end