Skip to content

Commit

Permalink
Merge pull request ManageIQ#438 from slemrmartin/inventory-collection…
Browse files Browse the repository at this point in the history
…-builder

Persister: InventoryCollection building through add_collection()
  • Loading branch information
agrare authored Jun 12, 2018
2 parents 8dde2ee + b50d394 commit b33b690
Show file tree
Hide file tree
Showing 12 changed files with 345 additions and 835 deletions.
31 changes: 9 additions & 22 deletions app/models/manageiq/providers/amazon/inventory/persister.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class ManageIQ::Providers::Amazon::Inventory::Persister < ManagerRefresh::Invent

# @param manager [ManageIQ::Providers::BaseManager] A manager object
# @param target [Object] A refresh Target object
# @param target [ManagerRefresh::Inventory::Collector] A Collector object
# @param collector [ManagerRefresh::Inventory::Collector] A Collector object
def initialize(manager, target = nil, collector = nil)
@manager = manager
@target = target
Expand All @@ -30,32 +30,19 @@ def initialize_tag_mapper
collections[:tags_to_resolve] = @tag_mapper.tags_to_resolve_collection
end

def cloud
ManageIQ::Providers::Amazon::InventoryCollectionDefault::CloudManager
end

def network
ManageIQ::Providers::Amazon::InventoryCollectionDefault::NetworkManager
end

def storage
ManageIQ::Providers::Amazon::InventoryCollectionDefault::StorageManager
end

def targeted
false
end

def strategy
nil
end

def shared_options
settings_options = options[:inventory_collections].try(:to_hash) || {}
def parent
manager.presence
end

settings_options.merge(
def shared_options
{
:strategy => strategy,
:targeted => targeted,
)
:targeted => targeted?,
:parent => parent
}
end
end
Original file line number Diff line number Diff line change
@@ -1,31 +1,9 @@
class ManageIQ::Providers::Amazon::Inventory::Persister::CloudManager < ManageIQ::Providers::Amazon::Inventory::Persister
include ManageIQ::Providers::Amazon::Inventory::Persister::Definitions::CloudCollections

def initialize_inventory_collections
initialize_tag_mapper

add_inventory_collections(
cloud,
%i(vms miq_templates hardwares operating_systems networks disks availability_zones
vm_and_template_labels vm_and_template_taggings
flavors key_pairs orchestration_stacks orchestration_stacks_resources
orchestration_stacks_outputs orchestration_stacks_parameters orchestration_templates)
)

add_inventory_collection(
cloud.vm_and_miq_template_ancestry(
:dependency_attributes => {
:vms => [collections[:vms]],
:miq_templates => [collections[:miq_templates]]
}
)
)

add_inventory_collection(
cloud.orchestration_stack_ancestry(
:dependency_attributes => {
:orchestration_stacks => [collections[:orchestration_stacks]],
:orchestration_stacks_resources => [collections[:orchestration_stacks_resources]]
}
)
)
initialize_cloud_inventory_collections
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
module ManageIQ::Providers::Amazon::Inventory::Persister::Definitions::CloudCollections
extend ActiveSupport::Concern

def initialize_cloud_inventory_collections
%i(vms
hardwares
operating_systems
networks
disks
availability_zones).each do |name|

add_collection(cloud, name)
end

add_miq_templates

add_flavors

add_key_pairs

add_vm_and_template_labels

add_vm_and_template_taggings

add_orchestration_stacks

%i(orchestration_stacks_resources
orchestration_stacks_outputs
orchestration_stacks_parameters
orchestration_templates).each do |name|

add_collection(cloud, name)
end

# Custom processing of Ancestry
add_vm_and_miq_template_ancestry

add_orchestration_stack_ancestry
end

# ------ IC provider specific definitions -------------------------

# TODO: Derive model class in core
def add_miq_templates(extra_properties = {})
add_collection(cloud, :miq_templates, extra_properties) do |builder|
builder.add_properties(:model_class => ::ManageIQ::Providers::Amazon::CloudManager::Template)
builder.add_builder_params(:template => true)
end
end

def add_flavors(extra_properties = {})
add_collection(cloud, :flavors, extra_properties) do |builder|
# Model we take just from a DB, there is no flavors API
builder.add_properties(:strategy => :local_db_find_references) if targeted?
end
end

def add_vm_and_template_labels
add_collection(cloud, :vm_and_template_labels) do |builder|
builder.add_targeted_arel(
lambda do |inventory_collection|
manager_uuids = inventory_collection.parent_inventory_collections.collect(&:manager_uuids).map(&:to_a).flatten
inventory_collection.parent.vm_and_template_labels.where(
'vms' => {:ems_ref => manager_uuids}
)
end
)
end
end

def add_vm_and_template_taggings
add_collection(cloud, :vm_and_template_taggings) do |builder|
builder.add_properties(
:model_class => Tagging,
:manager_ref => %i(taggable tag),
:parent_inventory_collections => %i(vms miq_templates)
)

builder.add_targeted_arel(
lambda do |inventory_collection|
manager_uuids = inventory_collection.parent_inventory_collections.collect(&:manager_uuids).map(&:to_a).flatten
ems = inventory_collection.parent
ems.vm_and_template_taggings.where(
'taggable_id' => ems.vms_and_templates.where(:ems_ref => manager_uuids)
)
end
)
end
end

def add_key_pairs(extra_properties = {})
add_collection(cloud, :key_pairs, extra_properties) do |builder|
builder.add_properties(:model_class => ::ManageIQ::Providers::Amazon::CloudManager::AuthKeyPair)
builder.add_properties(:manager_uuids => name_references(:key_pairs)) if targeted?
end
end

# TODO: mslemr - parent model_class used anywhere? If not, should be deleted
def add_orchestration_stacks(extra_properties = {})
add_collection(cloud, :orchestration_stacks, extra_properties) do |builder|
builder.add_properties(:model_class => ::ManageIQ::Providers::Amazon::CloudManager::OrchestrationStack)
end
end

def add_vm_and_miq_template_ancestry
add_collection(cloud, :vm_and_miq_template_ancestry, {}, {:auto_inventory_attributes => false, :without_model_class => true}) do |builder|
builder.add_dependency_attributes(
:vms => [collections[:vms]],
:miq_templates => [collections[:miq_templates]]
)
end
end

def add_orchestration_stack_ancestry
add_collection(cloud, :orchestration_stack_ancestry, {}, {:auto_inventory_attributes => false, :without_model_class => true}) do |builder|
builder.add_dependency_attributes(
:orchestration_stacks => [collections[:orchestration_stacks]],
:orchestration_stacks_resources => [collections[:orchestration_stacks_resources]]
)
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
module ManageIQ::Providers::Amazon::Inventory::Persister::Definitions::NetworkCollections
extend ActiveSupport::Concern

def initialize_network_inventory_collections
%i(cloud_networks
cloud_subnets
security_groups
load_balancers
load_balancer_pools
load_balancer_pool_members
load_balancer_pool_member_pools
load_balancer_listeners
load_balancer_listener_pools
load_balancer_health_checks
load_balancer_health_check_members).each do |name|

add_collection(network, name) do |builder|
builder.add_properties(:parent => manager.network_manager) if targeted?
end
end

add_cloud_subnet_network_ports

add_firewall_rules

add_floating_ips

add_network_ports
end

# ------ IC provider specific definitions -------------------------

def add_network_ports(extra_properties = {})
add_collection(network, :network_ports, extra_properties) do |builder|
if targeted?
builder.add_properties(:manager_uuids => references(:vms) + references(:network_ports) + references(:load_balancers))
builder.add_properties(:parent => manager.network_manager)
end
end
end

def add_floating_ips(extra_properties = {})
add_collection(network, :floating_ips, extra_properties) do |builder|
if targeted?
builder.add_properties(:manager_uuids => references(:floating_ips) + references(:load_balancers))
builder.add_properties(:parent => manager.network_manager)
end
end
end

def add_cloud_subnet_network_ports(extra_properties = {})
add_collection(network, :cloud_subnet_network_ports, extra_properties) do |builder|
builder.add_properties(:manager_ref_allowed_nil => %i(cloud_subnet))
builder.add_properties(:parent => manager.network_manager) if targeted?
end
end

def add_firewall_rules(extra_properties = {})
add_collection(network, :firewall_rules, extra_properties) do |builder|
builder.add_properties(:manager_ref_allowed_nil => %i(source_security_group))
builder.add_properties(:parent => manager.network_manager) if targeted?
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
module ManageIQ::Providers::Amazon::Inventory::Persister::Definitions::StorageCollections
extend ActiveSupport::Concern

def initialize_storage_inventory_collections
# should be defined by concrete persisters
end

# ------ IC provider specific definitions -------------------------

def add_cloud_volumes(extra_properties = {})
add_collection(storage, :cloud_volumes, extra_properties) do |builder|
builder.add_properties(:model_class => ::ManageIQ::Providers::Amazon::StorageManager::Ebs::CloudVolume)
builder.add_properties(:parent => manager.ebs_storage_manager) if targeted?

builder.add_builder_params(
:ems_id => block_storage_manager_id
)
end
end

def add_cloud_volume_snapshots(extra_properties = {})
add_collection(storage, :cloud_volume_snapshots, extra_properties) do |builder|
builder.add_properties(:model_class => ::ManageIQ::Providers::Amazon::StorageManager::Ebs::CloudVolumeSnapshot)
builder.add_properties(:parent => manager.ebs_storage_manager) if targeted?

builder.add_builder_params(
:ems_id => block_storage_manager_id
)
end
end

def add_cloud_object_store_containers(extra_properties = {})
add_collection(storage, :cloud_object_store_containers, extra_properties) do |builder|
builder.add_properties(:model_class => ::ManageIQ::Providers::Amazon::StorageManager::S3::CloudObjectStoreContainer)
builder.add_properties(:parent => manager.s3_storage_manager) if targeted?

builder.add_builder_params(
:ems_id => object_storage_manager_id
)
end
end

def add_cloud_object_store_objects(extra_properties = {})
add_collection(storage, :cloud_object_store_objects, extra_properties) do |builder|
builder.add_properties(:model_class => ::ManageIQ::Providers::Amazon::StorageManager::S3::CloudObjectStoreObject)
builder.add_properties(:parent => manager.s3_storage_manager) if targeted?

builder.add_builder_params(
:ems_id => object_storage_manager_id
)
end
end

protected

def block_storage_manager_id
manager.try(:ebs_storage_manager).try(:id) || manager.id
end

def object_storage_manager_id
manager.try(:s3_storage_manager).try(:id) || manager.id
end
end
Original file line number Diff line number Diff line change
@@ -1,16 +1,34 @@
class ManageIQ::Providers::Amazon::Inventory::Persister::NetworkManager < ManageIQ::Providers::Amazon::Inventory::Persister
include ManageIQ::Providers::Amazon::Inventory::Persister::Definitions::CloudCollections
include ManageIQ::Providers::Amazon::Inventory::Persister::Definitions::NetworkCollections

def initialize_inventory_collections
add_inventory_collections(
network,
%i(cloud_subnet_network_ports network_ports floating_ips cloud_subnets cloud_networks security_groups
firewall_rules load_balancers load_balancer_pools load_balancer_pool_members load_balancer_pool_member_pools
load_balancer_listeners load_balancer_listener_pools load_balancer_health_checks
load_balancer_health_check_members network_routers)
)
initialize_network_inventory_collections

initialize_cloud_inventory_collections
end

def initialize_network_inventory_collections
super

add_inventory_collections(cloud,
%i(vms orchestration_stacks availability_zones),
:parent => manager.parent_manager,
:strategy => :local_db_cache_all)
add_collection(network, :network_routers)
end

def initialize_cloud_inventory_collections
%i(vms
availability_zones).each do |name|

add_collection(cloud, name) do |builder|
builder.add_properties(
:parent => manager.parent_manager,
:strategy => :local_db_cache_all
)
end
end

add_orchestration_stacks(
:parent => manager.parent_manager,
:strategy => :local_db_cache_all
)
end
end
Loading

0 comments on commit b33b690

Please sign in to comment.