Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhanced inventory collector target and parser classes #13907

Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/models/ems_refresh/refreshers/ems_refresher_mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def parse_targeted_inventory(ems, target, collector)
inventory_collections, = Benchmark.realtime_block(:parse_inventory) do
provider_module = ManageIQ::Providers::Inflector.provider_module(ems.class).name
inventory_target_class = "#{provider_module}::Inventory::Target::#{target.class.name.demodulize}".safe_constantize
inventory_target = inventory_target_class.new(target)
inventory_target = inventory_target_class.new(collector)

parser_class = "#{provider_module}::Inventory::Parser::#{target.class.name.demodulize}".safe_constantize
parser = parser_class.new
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,28 @@ def inventory_groups
collections[:inventory_groups] ||= ManagerRefresh::InventoryCollection.new(
:model_class => ManageIQ::Providers::AutomationManager::InventoryRootGroup,
:association => :inventory_root_groups,
:parent => @root,
:builder_params => {:manager => @root}
:parent => target,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

really like how @root (single refresh specific variable) was removed from the collection definition.

Would be nice if Persister definition was reusable. (future thought)

:builder_params => {:manager => manager}
)
end

def configured_systems
collections[:configured_systems] ||= ManagerRefresh::InventoryCollection.new(
:model_class => ManageIQ::Providers::AnsibleTower::AutomationManager::ConfiguredSystem,
:association => :configured_systems,
:parent => @root,
:parent => target,
:manager_ref => [:manager_ref],
:builder_params => {:manager => @root}
:builder_params => {:manager => manager}
)
end

def configuration_scripts
collections[:configuration_scripts] ||= ManagerRefresh::InventoryCollection.new(
:model_class => ManageIQ::Providers::AnsibleTower::AutomationManager::ConfigurationScript,
:association => :configuration_scripts,
:parent => @root,
:parent => target,
:manager_ref => [:manager_ref],
:builder_params => {:manager => @root}
:builder_params => {:manager => manager}
)
end

Expand All @@ -33,8 +33,8 @@ def configuration_script_sources
:model_class => ConfigurationScriptSource,
:association => :configuration_script_sources,
:manager_ref => [:manager_ref],
:parent => @root,
:builder_params => {:manager => @root}
:parent => target,
:builder_params => {:manager => manager}
)
end

Expand All @@ -43,8 +43,8 @@ def playbooks
:model_class => ManageIQ::Providers::AnsibleTower::AutomationManager::Playbook,
:association => :configuration_script_payloads,
:manager_ref => [:manager_ref],
:parent => @root,
:builder_params => {:manager => @root}
:parent => target,
:builder_params => {:manager => manager}
)
end

Expand Down

This file was deleted.

21 changes: 13 additions & 8 deletions app/models/manager_refresh/inventory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,25 @@ class Inventory
require_nested :Parser
require_nested :Target

attr_accessor :collector
attr_accessor :target
attr_accessor :parser
attr_accessor :collector, :parsers, :target

def initialize(target, collector, parser)
# @param target [ManagerRefresh::Inventory::Target] A target object
# @param collector [ManagerRefresh::Inventory::Collector] A collector object
# @param parsers [ManagerRefresh::Inventory::Parser|Array] A Parser object or an array of
# ManagerRefresh::Inventory::Parser objects
def initialize(target, collector, parsers)
@collector = collector
@target = target
@parser = parser
@parsers = parsers.kind_of?(Array) ? parsers : [parsers]
end

def inventory_collections
parser.collector = collector
parser.target = target
parser.parse
parsers.each do |parser|
parser.collector = collector
parser.target = target
parser.parse
end

target.inventory_collections
end
end
Expand Down
9 changes: 7 additions & 2 deletions app/models/manager_refresh/inventory/collector.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
class ManagerRefresh::Inventory::Collector
attr_accessor :manager
attr_accessor :target
attr_accessor :manager, :target

# @param manager [ManageIQ::Providers::BaseManager] A manager object
# @param target [ActiveRecord|Hash] A refresh Target object
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why could this be a Hash?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Object

def initialize(manager, target)
@manager = manager
@target = target
end

def options
@options ||= Settings.ems_refresh[manager.class.ems_type]
end
end
70 changes: 63 additions & 7 deletions app/models/manager_refresh/inventory/target.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,70 @@
class ManagerRefresh::Inventory::Target
# @param [ApplicationRecord] root
def initialize(root)
@root = root
end
attr_reader :collector, :collections

delegate :manager, :target, :to => :collector

def collections
@collections ||= {}
# @param collector [ManagerRefresh::Inventory::Collector] A collector object
def initialize(collector)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure if I like the collector being tied to this class.
This increases the coupling

@collector = collector
@collections = {}
initialize_inventory_collections
end

def inventory_collections
@collections.values
collections.values
end

def inventory_collections_names
collections.keys
end

protected

def initialize_inventory_collections
raise NotImplementedError, _("must be implemented in a subclass")
end

# Adds 1 ManagerRefresh::InventoryCollection under a target.collections using :association key as index
#
# @param inventory_collection_data [Hash] Hash used for ManagerRefresh::InventoryCollection initialize
def add_inventory_collection(inventory_collection_data)
data = inventory_collection_data
data[:parent] ||= manager

if !data.key?(:delete_method) && data[:model_class]
# Automatically infer what the delete method should be, unless the delete methods was given
data[:delete_method] = data[:model_class].new.respond_to?(:disconnect_inv) ? :disconnect_inv : nil
end

collections[data[:association]] = ::ManagerRefresh::InventoryCollection.new(data)
end

# Adds multiple inventory collections with the same data
#
# @param default [ManagerRefresh::InventoryCollectionDefault] Default
# @param inventory_collections [Array] Array of method names for passed default parameter
# @param inventory_collections_data [Hash] Hash used for ManagerRefresh::InventoryCollection initialize
def add_inventory_collections(default, inventory_collections, inventory_collections_data = {})
inventory_collections.each do |inventory_collection|
add_inventory_collection(default.send(inventory_collection, inventory_collections_data))
end
end

# Adds remaining inventory collections with the same data
#
# @param defaults [Array] Array of ManagerRefresh::InventoryCollectionDefault
# @param inventory_collections_data [Hash] Hash used for ManagerRefresh::InventoryCollection initialize
def add_remaining_inventory_collections(defaults, inventory_collections_data = {})
defaults.each do |default|
# Get names of all inventory collections defined in passed classes with Defaults
all_inventory_collections = default.methods - ::ManagerRefresh::InventoryCollectionDefault.methods
# Get names of all defined inventory_collections
defined_inventory_collections = inventory_collections_names

# Add all missing inventory_collections with defined init_data
add_inventory_collections(default,
all_inventory_collections - defined_inventory_collections,
inventory_collections_data)
end
end
end
27 changes: 0 additions & 27 deletions app/models/manager_refresh/refresh_parser_inventory_object.rb

This file was deleted.