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

Moving Inventory::Builder's functions to Inventory class #17907

Merged
merged 4 commits into from
Aug 31, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ def builder_class_for(klass)
"#{provider_module}::Builder".constantize
end

def inventory_class_for(klass)
provider_module = ManageIQ::Providers::Inflector.provider_module(klass)
"#{provider_module}::Inventory".constantize
end

# Legacy inventory parser
#
# @param ems [ManageIQ::Providers::BaseManager] Manager we want to parse
Expand Down Expand Up @@ -40,7 +45,13 @@ def collect_inventory_for_targets(ems, targets)
_log.info("Filtering inventory for #{target.class} [#{target_name}] id: [#{target.id}]...")

if ems.inventory_object_refresh?
inventory = builder_class_for(ems.class).build_inventory(ems, target)
# Tmp construction until all providers will be refactored
# to use `inventory_class_for`
begin
inventory = builder_class_for(ems.class).build_inventory(ems, target)
rescue NameError
inventory = inventory_class_for(ems.class).build(ems, target)
end
end

_log.info("Filtering inventory...Complete")
Expand Down
95 changes: 75 additions & 20 deletions app/models/manageiq/providers/inventory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,13 @@ class Inventory

attr_accessor :collector, :parsers, :persister

# Based on the given provider/manager class, this returns correct parser class
#
# @param klass class of the Provider/Manager
# @return [Class] Correct class name of the Parser
def self.parser_class_for(klass)
provider_module = ManageIQ::Providers::Inflector.provider_module(klass)
"#{provider_module}::Inventory::Parser::#{klass.name.demodulize}".safe_constantize
rescue ManageIQ::Providers::Inflector::ObjectNotNamespacedError => _err
nil
end

# Based on the given provider/manager class, this returns correct persister class
#
# @param klass class of the Provider/Manager
# @return [Class] Correct class name of the persister
def self.persister_class_for(klass)
provider_module = ManageIQ::Providers::Inflector.provider_module(klass)
"#{provider_module}::Inventory::Persister::#{klass.name.demodulize}".safe_constantize
rescue ManageIQ::Providers::Inflector::ObjectNotNamespacedError => _err
nil
# Entry point for building inventory
def self.build(ems, target)
new(
class_for(ems, target, 'Persister').new(ems, target),
class_for(ems, target, 'Collector').new(ems, target),
Copy link
Member

Choose a reason for hiding this comment

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

Instead of the general class_for here how about we call collector_class_for and persister_class_for here to give providers a change to override them?

parser_classes_for(ems, target).map(&:new)
)
end

# @param persister [ManageIQ::Providers::Inventory::Persister] A Persister object
Expand Down Expand Up @@ -57,5 +44,73 @@ def parse
def inventory_collections
parse.inventory_collections
end

# Based on the given provider/manager class, this returns correct persister class
#
# @param klass class of the Provider/Manager
# @return [Class] Correct class name of the persister
def self.persister_class_for(klass)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@agrare I only changed order of these methods in file, no change there

Copy link
Member

Choose a reason for hiding this comment

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

I'm good with this for now, I think these should all be private and then all of the yardoc comments are unnecessary

Copy link
Member

Choose a reason for hiding this comment

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

Actually where is persister_class_for used?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Ah okay 👍

provider_module = ManageIQ::Providers::Inflector.provider_module(klass)
"#{provider_module}::Inventory::Persister::#{klass.name.demodulize}".safe_constantize
rescue ManageIQ::Providers::Inflector::ObjectNotNamespacedError => _err
nil
end

# Based on the given provider/manager class, this returns correct parser class
#
# @param klass class of the Provider/Manager
# @return [Class] Correct class name of the Parser
def self.parser_class_for(klass)
provider_module = ManageIQ::Providers::Inflector.provider_module(klass)
"#{provider_module}::Inventory::Parser::#{klass.name.demodulize}".safe_constantize
rescue ManageIQ::Providers::Inflector::ObjectNotNamespacedError => _err
nil
end

# @param ems [ExtManagementSystem]
# @param target [ExtManagementSystem, ManagerRefresh::TargetCollection]
# @param type [String] 'Persister' | 'Collector' | 'Parser'
# @param manager_name [String, nil] @see default_manager_name
def self.class_for(ems, target, type, manager_name = nil)
provider_module = ManageIQ::Providers::Inflector.provider_module(ems.class)

manager_name = parsed_manager_name(target) if manager_name.nil?

klass = "#{provider_module}::Inventory::#{type}::#{manager_name}".safe_constantize
# if class for given target doesn't exist, try to use class for default_manager (if defined)
if klass.nil? && default_manager_name.present? && manager_name != default_manager_name
klass = class_for(ems, target, type, default_manager_name)
end
klass
rescue ManageIQ::Providers::Inflector::ObjectNotNamespacedError => _err
nil
end

# Fallback manager name when persister/parser/collector not determined from refreshes' target
# @return [String, nil] i.e. 'CloudManager'
def self.default_manager_name
nil
end

# Last part of persister/parser/collector class name
# For example 'CloudManager' or 'StorageManager::Ebs'
def self.parsed_manager_name(target)
case target
when ManagerRefresh::TargetCollection
'TargetCollection'
else
klass = target.class == Class ? target : target.class
suffix_arr = klass.name.split('::') - ManageIQ::Providers::Inflector.provider_module(klass).name.split("::")
suffix_arr.join('::')
end
rescue ManageIQ::Providers::Inflector::ObjectNotNamespacedError => _err
nil
end

# Multiple parser classes
# Can be implemented in subclass when custom set needed (mainly for TargetCollection)
def self.parser_classes_for(ems, target)
[class_for(ems, target, 'Parser')]
end
end
end