Class: InventoryRefresh::Persister

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(manager) ⇒ Persister

Returns a new instance of Persister.

Parameters:

  • manager (ManageIQ::Providers::BaseManager)

    A manager object



14
15
16
17
18
19
20
21
22
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *arguments, &block) ⇒ InventoryRefresh::InventoryCollection

Returns a defined InventoryCollection or undefined method

Returns:



67
68
69
70
71
72
73
74
# 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

Instance Attribute Details

#collectionsObject (readonly)

Returns the value of attribute collections.



6
7
8
# File 'lib/inventory_refresh/persister.rb', line 6

def collections
  @collections
end

#ingress_api_sent_atObject

Returns the value of attribute ingress_api_sent_at.



9
10
11
# File 'lib/inventory_refresh/persister.rb', line 9

def ingress_api_sent_at
  @ingress_api_sent_at
end

#managerObject (readonly)

Returns the value of attribute manager.



6
7
8
# File 'lib/inventory_refresh/persister.rb', line 6

def manager
  @manager
end

#persister_finished_atObject

Returns the value of attribute persister_finished_at.



9
10
11
# File 'lib/inventory_refresh/persister.rb', line 9

def persister_finished_at
  @persister_finished_at
end

#persister_started_atObject

Returns the value of attribute persister_started_at.



9
10
11
# File 'lib/inventory_refresh/persister.rb', line 9

def persister_started_at
  @persister_started_at
end

#refresh_state_part_collected_atObject

Returns the value of attribute refresh_state_part_collected_at.



9
10
11
# File 'lib/inventory_refresh/persister.rb', line 9

def refresh_state_part_collected_at
  @refresh_state_part_collected_at
end

#refresh_state_part_sent_atObject

Returns the value of attribute refresh_state_part_sent_at.



9
10
11
# File 'lib/inventory_refresh/persister.rb', line 9

def refresh_state_part_sent_at
  @refresh_state_part_sent_at
end

#refresh_state_part_uuidObject

Returns the value of attribute refresh_state_part_uuid.



8
9
10
# File 'lib/inventory_refresh/persister.rb', line 8

def refresh_state_part_uuid
  @refresh_state_part_uuid
end

#refresh_state_sent_atObject

Returns the value of attribute refresh_state_sent_at.



9
10
11
# File 'lib/inventory_refresh/persister.rb', line 9

def refresh_state_sent_at
  @refresh_state_sent_at
end

#refresh_state_started_atObject

Returns the value of attribute refresh_state_started_at.



9
10
11
# File 'lib/inventory_refresh/persister.rb', line 9

def refresh_state_started_at
  @refresh_state_started_at
end

#refresh_state_uuidObject

Returns the value of attribute refresh_state_uuid.



8
9
10
# File 'lib/inventory_refresh/persister.rb', line 8

def refresh_state_uuid
  @refresh_state_uuid
end

#refresh_time_trackingObject

Returns the value of attribute refresh_time_tracking.



8
9
10
# File 'lib/inventory_refresh/persister.rb', line 8

def refresh_time_tracking
  @refresh_time_tracking
end

#retry_countObject

Returns the value of attribute retry_count.



8
9
10
# File 'lib/inventory_refresh/persister.rb', line 8

def retry_count
  @retry_count
end

#retry_maxObject

Returns the value of attribute retry_max.



8
9
10
# File 'lib/inventory_refresh/persister.rb', line 8

def retry_max
  @retry_max
end

#sweep_scopeObject

Returns the value of attribute sweep_scope.



8
9
10
# File 'lib/inventory_refresh/persister.rb', line 8

def sweep_scope
  @sweep_scope
end

#total_partsObject

Returns the value of attribute total_parts.



8
9
10
# File 'lib/inventory_refresh/persister.rb', line 8

def total_parts
  @total_parts
end

Class Method Details

.from_hash(persister_data, manager) ⇒ ManageIQ::Providers::Inventory::Persister

Returns Persister object built from serialized data

Parameters:

  • persister_data (Hash)

    serialized Persister object in hash

Returns:

  • (ManageIQ::Providers::Inventory::Persister)

    Persister object built from serialized data



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# 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) ⇒ ManageIQ::Providers::Inventory::Persister

Returns Persister object loaded from a passed JSON

Parameters:

  • json_data (String)

    input JSON data

Returns:

  • (ManageIQ::Providers::Inventory::Persister)

    Persister object loaded from a passed JSON



134
135
136
# File 'lib/inventory_refresh/persister.rb', line 134

def from_json(json_data, manager)
  from_hash(JSON.parse(json_data), manager)
end

Instance Method Details

#add_collection(collection_name, builder_class = inventory_collection_builder, extra_properties = {}, settings = {}, &block) ⇒ Object

Interface for creating InventoryCollection under @collections

Examples:

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

Parameters:

  • builder_class (ManageIQ::Providers::Inventory::Persister::Builder) (defaults to: inventory_collection_builder)

    or subclasses

  • collection_name (Symbol || Array)

    used as InventoryCollection:association

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

    props from InventoryCollection.initialize list

    • adds/overwrites properties added by builder

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

    builder settings

    • @see ManageIQ::Providers::Inventory::Persister::Builder.default_options

    • @see make_builder_settings()

See Also:

  • https://github.com/ManageIQ/guides/tree/master/providers/persister/inventory_collections.md


44
45
46
47
48
49
50
51
52
53
54
# 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) ⇒ Object

Defines a new attr reader returning InventoryCollection object



82
83
84
85
86
# 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_builderObject



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

def inventory_collection_builder
  ::InventoryRefresh::InventoryCollection::Builder
end

#inventory_collectionsArray<InventoryRefresh::InventoryCollection>

Returns array of InventoryCollection objects of the persister.

Returns:



57
58
59
# File 'lib/inventory_refresh/persister.rb', line 57

def inventory_collections
  collections.values
end

#inventory_collections_namesArray<Symbol>

Returns array of InventoryCollection object names of the persister.

Returns:

  • (Array<Symbol>)

    array of InventoryCollection object names of the persister



62
63
64
# File 'lib/inventory_refresh/persister.rb', line 62

def inventory_collections_names
  collections.keys
end

#persist!Object

Persists InventoryCollection objects into the DB



93
94
95
# 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) ⇒ Boolean

Returns true if InventoryCollection with passed method_name name is defined.

Returns:

  • (Boolean)

    true if InventoryCollection with passed method_name name is defined



77
78
79
# 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_hashHash

Returns entire Persister object serialized to hash.

Returns:

  • (Hash)

    entire Persister object serialized to hash



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# 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_jsonString

Returns serialized Persisted object to JSON

Returns:

  • (String)

    serialized Persisted object to JSON



99
100
101
# File 'lib/inventory_refresh/persister.rb', line 99

def to_json
  JSON.dump(to_hash)
end