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

Inventory abstraction for data fetching and storing in inventory collections #98

Merged
98 changes: 98 additions & 0 deletions app/models/manageiq/providers/amazon/inventory.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
class ManageIQ::Providers::Amazon::Inventory
require_nested :Factory
require_nested :HashCollection
require_nested :Targets

attr_reader :ems, :target, :inventory_collections, :options

def initialize(ems, target)
@ems = ems
@target = target
@options = Settings.ems_refresh[ems.class.ems_type]
@inventory_collections = {:_inventory_collection => true}

@known_flavors = Set.new

initialize_inventory_collections
end

def aws_ec2
@aws_ec2 ||= ems.connect
end

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

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

def instances
[]
end

def flavors
[]
end

def availability_zones
[]
end

def key_pairs
[]
end

def private_images
[]
end

def shared_images
[]
end

def public_images
[]
end

def stacks
[]
end

def stack_resources(_stack_name)
[]
end

def stack_template(_stack_name)
[]
end

def cloud_networks
[]
end

def cloud_subnets
[]
end

def security_groups
[]
end

def network_ports
[]
end

def load_balancers
[]
end

def health_check_members(_load_balancer_name)
[]
end

def floating_ips
[]
end
end
29 changes: 29 additions & 0 deletions app/models/manageiq/providers/amazon/inventory/factory.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class ManageIQ::Providers::Amazon::Inventory::Factory
class << self
def inventory(ems, target)
if target.kind_of?(EmsEvent)
event_target(ems, target) || target(ems, ems)
else
target(ems, target)
end
end

def target(ems, target)
case target
when ManageIQ::Providers::Amazon::CloudManager
ManageIQ::Providers::Amazon::Inventory::Targets::CloudManager.new(ems, target)
when ManageIQ::Providers::Amazon::NetworkManager
ManageIQ::Providers::Amazon::Inventory::Targets::NetworkManager.new(ems, target)
else
ManageIQ::Providers::Amazon::Inventory::Targets::CloudManager.new(ems, target)
end
end

def event_target(ems, target)
case target[:full_data]["configurationItem"]["resourceType"]
when "AWS::EC2::Instance"
ManageIQ::Providers::Amazon::Inventory::Targets::EventPayloadVm.new(ems, target)
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class ManageIQ::Providers::Amazon::Inventory::HashCollection
attr_reader :collection

def initialize(collection)
@collection = collection
end

def each
collection.each do |item|
item_data = item.respond_to?(:to_hash) ? item.to_hash : item.data.to_hash
yield(transform_keys(item_data))
end
end

def transform_keys(value)
case value
when Array
value.map { |x| transform_keys(x) }
when Hash
Hash[value.map { |k, v| [k.to_s.underscore, transform_keys(v)] }]
else
value
end
end
end
Loading