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 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
6 changes: 3 additions & 3 deletions app/models/ems_refresh/refreshers/ems_refresher_mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,13 @@ def parse_targeted_inventory(ems, target, collector)
_log.debug "#{log_header} Parsing inventory..."
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)
persister_class = "#{provider_module}::Inventory::Persister::#{target.class.name.demodulize}".safe_constantize
Copy link
Member

Choose a reason for hiding this comment

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

Are you using strings/safe_constantize to avoid class not found?

(basically asking will this work if you don't .name, but just reference the class)?

Copy link
Member

@durandom durandom Feb 16, 2017

Choose a reason for hiding this comment

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

Good point, and I'm chiming in, because the original code was mine, so I'm to blame 😉
So, if the class is not found, it would return nil and nil.new will blow up.
So we should assume the class is there, check if its not and raise an exception like you should really implement this - so the dev knows what he's missing

persister = persister_class.new(ems, target)

parser_class = "#{provider_module}::Inventory::Parser::#{target.class.name.demodulize}".safe_constantize
parser = parser_class.new

i = ManagerRefresh::Inventory.new(inventory_target, collector, parser)
i = ManagerRefresh::Inventory.new(persister, collector, parser)
i.inventory_collections
end
_log.debug "#{log_header} Parsing inventory...Complete"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,57 +1,57 @@
class ManageIQ::Providers::AnsibleTower::Inventory::Parser::AutomationManager < ManagerRefresh::Inventory::Parser
def parse
inventory_groups
inventory_root_groups
configured_systems
configuration_scripts
configuration_script_sources
credentials
end

def inventory_groups
def inventory_root_groups
collector.inventories.each do |inventory|
inventory_object = target.inventory_groups.find_or_build(inventory.id.to_s)
inventory_object = persister.inventory_root_groups.find_or_build(inventory.id.to_s)
inventory_object[:name] = inventory.name
end
end

def configured_systems
collector.hosts.each do |host|
inventory_object = target.configured_systems.find_or_build(host.id)
inventory_object = persister.configured_systems.find_or_build(host.id)
inventory_object[:hostname] = host.name
inventory_object[:virtual_instance_ref] = host.instance_id
inventory_object[:inventory_root_group] = target.inventory_groups.lazy_find(host.inventory_id.to_s)
inventory_object[:inventory_root_group] = persister.inventory_root_groups.lazy_find(host.inventory_id.to_s)
inventory_object[:counterpart] = Vm.find_by(:uid_ems => host.instance_id)
end
end

def configuration_scripts
collector.job_templates.each do |job_template|
inventory_object = target.configuration_scripts.find_or_build(job_template.id.to_s)
inventory_object = persister.configuration_scripts.find_or_build(job_template.id.to_s)
inventory_object[:description] = job_template.description
inventory_object[:name] = job_template.name
inventory_object[:survey_spec] = job_template.survey_spec_hash
inventory_object[:variables] = job_template.extra_vars_hash
inventory_object[:inventory_root_group] = target.inventory_groups.lazy_find(job_template.inventory_id.to_s)
inventory_object[:inventory_root_group] = persister.inventory_root_groups.lazy_find(job_template.inventory_id.to_s)

inventory_object[:authentications] = []
%w(credential_id cloud_credential_id network_credential_id).each do |credential_attr|
next unless job_template.respond_to?(credential_attr)
credential_id = job_template.public_send(credential_attr).to_s
next if credential_id.blank?
inventory_object[:authentications] << target.credentials.lazy_find(credential_id)
inventory_object[:authentications] << persister.credentials.lazy_find(credential_id)
end
end
end

def configuration_script_sources
collector.projects.each do |project|
inventory_object = target.configuration_script_sources.find_or_build(project.id.to_s)
inventory_object = persister.configuration_script_sources.find_or_build(project.id.to_s)
inventory_object[:description] = project.description
inventory_object[:name] = project.name

project.playbooks.each do |playbook_name|
# FIXME: its not really nice how I have to build a manager_ref / uuid here
inventory_object_playbook = target.playbooks.find_or_build("#{project.id}__#{playbook_name}")
inventory_object_playbook = persister.configuration_script_payloads.find_or_build("#{project.id}__#{playbook_name}")
inventory_object_playbook[:configuration_script_source] = inventory_object
inventory_object_playbook[:name] = playbook_name
end
Expand All @@ -60,7 +60,7 @@ def configuration_script_sources

def credentials
collector.credentials.each do |credential|
inventory_object = target.credentials.find_or_build(credential.id.to_s)
inventory_object = persister.credentials.find_or_build(credential.id.to_s)
inventory_object[:name] = credential.name
inventory_object[:userid] = credential.username
# credential.description
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class ManageIQ::Providers::AnsibleTower::Inventory::Persister::AutomationManager < ManagerRefresh::Inventory::Persister
def automation
ManageIQ::Providers::AnsibleTower::InventoryCollectionDefault::AutomationManager
end

def initialize_inventory_collections
add_inventory_collections(
automation,
%i(inventory_root_groups configured_systems configuration_scripts configuration_script_sources configuration_script_payloads),
:builder_params => {:manager => manager}
)

add_inventory_collections(
automation,
%i(credentials),
:builder_params => {:resource => manager}
)
end
end

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
class ManageIQ::Providers::AnsibleTower::InventoryCollectionDefault::AutomationManager < ManagerRefresh::InventoryCollectionDefault
class << self
def inventory_root_groups(extra_attributes = {})
attributes = {
:model_class => ManageIQ::Providers::AutomationManager::InventoryRootGroup,
:association => :inventory_root_groups,
}
attributes.merge!(extra_attributes)
end

def configured_systems(extra_attributes = {})
attributes = {
:model_class => ManageIQ::Providers::AnsibleTower::AutomationManager::ConfiguredSystem,
:association => :configured_systems,
:manager_ref => [:manager_ref],
}
attributes.merge!(extra_attributes)
end

def configuration_scripts(extra_attributes = {})
attributes = {
:model_class => ManageIQ::Providers::AnsibleTower::AutomationManager::ConfigurationScript,
:association => :configuration_scripts,
:manager_ref => [:manager_ref],
}
attributes.merge!(extra_attributes)
end

def configuration_script_sources(extra_attributes = {})
attributes = {
:model_class => ConfigurationScriptSource,
:association => :configuration_script_sources,
:manager_ref => [:manager_ref],
}
attributes.merge!(extra_attributes)
end

def configuration_script_payloads(extra_attributes = {})
attributes = {
:model_class => ManageIQ::Providers::AnsibleTower::AutomationManager::Playbook,
:association => :configuration_script_payloads,
:manager_ref => [:manager_ref],
}
attributes.merge!(extra_attributes)
end

def credentials(extra_attributes = {})
attributes = {
:model_class => ManageIQ::Providers::AutomationManager::Authentication,
:association => :credentials,
:manager_ref => [:manager_ref],
}
attributes.merge!(extra_attributes)
end
end
end

This file was deleted.

27 changes: 16 additions & 11 deletions app/models/manager_refresh/inventory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,28 @@ module ManagerRefresh
class Inventory
require_nested :Collector
require_nested :Parser
require_nested :Target
require_nested :Persister

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

def initialize(target, collector, parser)
# @param persister [ManagerRefresh::Inventory::Persister] A Persister 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(persister, collector, parsers)
@collector = collector
@target = target
@parser = parser
@persister = persister
@parsers = parsers.kind_of?(Array) ? parsers : [parsers]
end

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

persister.inventory_collections
end
end
end
13 changes: 9 additions & 4 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_reader :manager, :target

def initialize(manager, target)
# @param manager [ManageIQ::Providers::BaseManager] A manager object
# @param target [Object] A refresh Target object
def initialize(manager, refresh_target)
@manager = manager
@target = target
@target = refresh_target
end

def options
@options ||= Settings.ems_refresh[manager.class.ems_type]
end
end
2 changes: 1 addition & 1 deletion app/models/manager_refresh/inventory/parser.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class ManagerRefresh::Inventory::Parser
attr_accessor :collector
attr_accessor :target
attr_accessor :persister

def parse
raise NotImplementedError, _("must be implemented in a subclass")
Expand Down
Loading