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

Use base classes for Inventory, Collector, Persistor and Parser #139

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
57 changes: 35 additions & 22 deletions app/models/manageiq/providers/amazon/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,52 +5,65 @@ def build_inventory(ems, target)
when ManageIQ::Providers::Amazon::CloudManager
cloud_manager_inventory(ems, target)
when ManageIQ::Providers::Amazon::NetworkManager
ManageIQ::Providers::Amazon::Inventory.new(
inventory(
ems,
target,
:collector_class => ManageIQ::Providers::Amazon::Inventory::Collector::NetworkManager,
:target_class => ManageIQ::Providers::Amazon::Inventory::Target::NetworkManager,
:parsers_classes => [ManageIQ::Providers::Amazon::Inventory::Parser::NetworkManager]
ManageIQ::Providers::Amazon::Inventory::Collector::NetworkManager,
ManageIQ::Providers::Amazon::Inventory::Persister::NetworkManager,
[ManageIQ::Providers::Amazon::Inventory::Parser::NetworkManager]
)
when ManageIQ::Providers::Amazon::StorageManager::Ebs
ManageIQ::Providers::Amazon::Inventory.new(
inventory(
ems,
target,
:collector_class => ManageIQ::Providers::Amazon::Inventory::Collector::StorageManager::Ebs,
:target_class => ManageIQ::Providers::Amazon::Inventory::Target::StorageManager::Ebs,
:parsers_classes => [ManageIQ::Providers::Amazon::Inventory::Parser::StorageManager::Ebs]
ManageIQ::Providers::Amazon::Inventory::Collector::StorageManager::Ebs,
ManageIQ::Providers::Amazon::Inventory::Persister::StorageManager::Ebs,
[ManageIQ::Providers::Amazon::Inventory::Parser::StorageManager::Ebs]
)
when ManageIQ::Providers::Amazon::StorageManager::S3
ManageIQ::Providers::Amazon::Inventory.new(
inventory(
ems,
target,
:collector_class => ManageIQ::Providers::Amazon::Inventory::Collector::StorageManager::S3,
:target_class => ManageIQ::Providers::Amazon::Inventory::Target::StorageManager::S3,
:parsers_classes => [ManageIQ::Providers::Amazon::Inventory::Parser::StorageManager::S3]
ManageIQ::Providers::Amazon::Inventory::Collector::StorageManager::S3,
ManageIQ::Providers::Amazon::Inventory::Persister::StorageManager::S3,
[ManageIQ::Providers::Amazon::Inventory::Parser::StorageManager::S3]
)
when ManageIQ::Providers::Amazon::TargetCollection
ManageIQ::Providers::Amazon::Inventory.new(
inventory(
ems,
target,
:collector_class => ManageIQ::Providers::Amazon::Inventory::Collector::TargetCollection,
:target_class => ManageIQ::Providers::Amazon::Inventory::Target::TargetCollection,
:parsers_classes => [ManageIQ::Providers::Amazon::Inventory::Parser::CloudManager,
ManageIQ::Providers::Amazon::Inventory::Parser::NetworkManager,
ManageIQ::Providers::Amazon::Inventory::Parser::StorageManager::Ebs]
ManageIQ::Providers::Amazon::Inventory::Collector::TargetCollection,
ManageIQ::Providers::Amazon::Inventory::Persister::TargetCollection,
[ManageIQ::Providers::Amazon::Inventory::Parser::CloudManager,
ManageIQ::Providers::Amazon::Inventory::Parser::NetworkManager,
ManageIQ::Providers::Amazon::Inventory::Parser::StorageManager::Ebs]
)
else
# Fallback to ems refresh
cloud_manager_inventory(ems, target)
end
end

private

def cloud_manager_inventory(ems, target)
::ManageIQ::Providers::Amazon::Inventory.new(
inventory(
ems,
target,
:collector_class => ManageIQ::Providers::Amazon::Inventory::Collector::CloudManager,
:target_class => ManageIQ::Providers::Amazon::Inventory::Target::CloudManager,
:parsers_classes => [ManageIQ::Providers::Amazon::Inventory::Parser::CloudManager]
ManageIQ::Providers::Amazon::Inventory::Collector::CloudManager,
ManageIQ::Providers::Amazon::Inventory::Persister::CloudManager,
[ManageIQ::Providers::Amazon::Inventory::Parser::CloudManager]
)
end

def inventory(manager, raw_target, collector_class, target_class, parsers_classes)
collector = collector_class.new(manager, raw_target)
target = target_class.new(manager, raw_target)

::ManageIQ::Providers::Amazon::Inventory.new(
target,
collector,
parsers_classes.map(&:new)
)
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def parse_targeted_inventory(ems, _target, inventory)
_log.debug "#{log_header} Parsing inventory..."
hashes, = Benchmark.realtime_block(:parse_inventory) do
if refresher_options.try(:[], :inventory_object_refresh)
inventory.parse
inventory.inventory_collections
else
ManageIQ::Providers::Amazon::CloudManager::RefreshParser.ems_inv_to_hashes(ems, refresher_options)
end
Expand Down
24 changes: 2 additions & 22 deletions app/models/manageiq/providers/amazon/inventory.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,5 @@
class ManageIQ::Providers::Amazon::Inventory
class ManageIQ::Providers::Amazon::Inventory < ManagerRefresh::Inventory
require_nested :Collector
require_nested :Parser
require_nested :Target

attr_reader :ems, :options, :target, :collector, :parsers_classes

delegate :inventory_collections, :to => :target

def initialize(ems, raw_target, target_class: nil, collector_class: nil, parsers_classes: nil)
@ems = ems
@options = Settings.ems_refresh[ems.class.ems_type]

@collector = collector_class.new(@ems, @options, raw_target)
@target = target_class.new(@collector)

@parsers_classes = parsers_classes
end

def parse
parsers_classes.each { |parser_class| parser_class.new(@target).populate_inventory_collections }

inventory_collections.values
end
require_nested :Persister
end
18 changes: 7 additions & 11 deletions app/models/manageiq/providers/amazon/inventory/collector.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
class ManageIQ::Providers::Amazon::Inventory::Collector
class ManageIQ::Providers::Amazon::Inventory::Collector < ManagerRefresh::Inventory::Collector
require_nested :CloudManager
require_nested :NetworkManager
require_nested :TargetCollection

attr_reader :ems, :options, :target

attr_reader :instances, :instances_refs, :instances_deleted
attr_reader :flavors, :flavors_refs, :flavors_deleted
attr_reader :availability_zones, :availability_zones_refs, :availability_zones_deleted
Expand All @@ -22,10 +20,8 @@ class ManageIQ::Providers::Amazon::Inventory::Collector
attr_reader :cloud_volumes, :cloud_volumes_refs
attr_reader :cloud_volume_snapshots, :cloud_volume_snapshots_refs

def initialize(ems, options, target)
@ems = ems
@options = options
@target = target
def initialize(_manager, _target)
super

initialize_inventory_sources
end
Expand Down Expand Up @@ -89,19 +85,19 @@ def hash_collection
end

def aws_ec2
@aws_ec2 ||= ems.connect
@aws_ec2 ||= manager.connect
end

def aws_cloud_formation
@aws_cloud_formation ||= ems.connect(:service => :CloudFormation)
@aws_cloud_formation ||= manager.connect(:service => :CloudFormation)
end

def aws_elb
@aws_elb ||= ems.connect(:service => :ElasticLoadBalancing)
@aws_elb ||= manager.connect(:service => :ElasticLoadBalancing)
end

def aws_s3
@aws_s3 ||= ems.connect(:service => :S3)
@aws_s3 ||= manager.connect(:service => :S3)
end

def stack_resources(stack_name)
Expand Down
15 changes: 14 additions & 1 deletion app/models/manageiq/providers/amazon/inventory/parser.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
class ManageIQ::Providers::Amazon::Inventory::Parser
class ManageIQ::Providers::Amazon::Inventory::Parser < ManagerRefresh::Inventory::Parser
require_nested :CloudManager
require_nested :NetworkManager

include ManageIQ::Providers::Amazon::ParserHelperMethods

def process_inventory_collection(collection, key)
(collection || []).each do |item|
new_result = yield(item)
next if new_result.blank?

raise "InventoryCollection #{key} must be defined" unless persister.collections[key]

persister.collections[key] << persister.collections[key].new_inventory_object(new_result)
end
end
end
Loading