Skip to content

Commit

Permalink
Merge pull request #13739 from Ladas/change_inventory_collection_inte…
Browse files Browse the repository at this point in the history
…rface_to_kwargs

Change inventory collection interface to kwargs
  • Loading branch information
agrare authored Feb 8, 2017
2 parents af4f115 + a0d9636 commit 136c681
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 88 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class ManageIQ::Providers::AnsibleTower::Inventory::Target::AutomationManager < ManagerRefresh::Inventory::Target
def inventory_groups
collections[:inventory_groups] ||= ManagerRefresh::InventoryCollection.new(
ManageIQ::Providers::AutomationManager::InventoryRootGroup,
:model_class => ManageIQ::Providers::AutomationManager::InventoryRootGroup,
:association => :inventory_root_groups,
:parent => @root,
:builder_params => {:manager => @root}
Expand All @@ -10,7 +10,7 @@ def inventory_groups

def configured_systems
collections[:configured_systems] ||= ManagerRefresh::InventoryCollection.new(
ManageIQ::Providers::AnsibleTower::AutomationManager::ConfiguredSystem,
:model_class => ManageIQ::Providers::AnsibleTower::AutomationManager::ConfiguredSystem,
:association => :configured_systems,
:parent => @root,
:manager_ref => [:manager_ref],
Expand All @@ -20,7 +20,7 @@ def configured_systems

def configuration_scripts
collections[:configuration_scripts] ||= ManagerRefresh::InventoryCollection.new(
ManageIQ::Providers::AnsibleTower::AutomationManager::ConfigurationScript,
:model_class => ManageIQ::Providers::AnsibleTower::AutomationManager::ConfigurationScript,
:association => :configuration_scripts,
:parent => @root,
:manager_ref => [:manager_ref],
Expand Down
22 changes: 18 additions & 4 deletions app/models/manager_refresh/inventory_collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class InventoryCollection

delegate :each, :size, :to => :to_a

def initialize(model_class, manager_ref: nil, association: nil, parent: nil, strategy: nil, saved: nil,
def initialize(model_class: nil, manager_ref: nil, association: nil, parent: nil, strategy: nil, saved: nil,
custom_save_block: nil, delete_method: nil, data_index: nil, data: nil, dependency_attributes: nil,
attributes_blacklist: nil, attributes_whitelist: nil, complete: nil, update_only: nil,
check_changed: nil, custom_manager_uuid: nil, custom_db_finder: nil, arel: nil, builder_params: {})
Expand Down Expand Up @@ -211,7 +211,9 @@ def filtered_dependency_attributes
end

def fixed_attributes
presence_validators = model_class.validators.detect { |x| x.kind_of? ActiveRecord::Validations::PresenceValidator }
if model_class
presence_validators = model_class.validators.detect { |x| x.kind_of? ActiveRecord::Validations::PresenceValidator }
end
# Attributes that has to be always on the entity, so attributes making unique index of the record + attributes
# that have presence validation
fixed_attributes = manager_ref
Expand Down Expand Up @@ -257,7 +259,7 @@ def whitelist_attributes!(attributes)
def clone
# A shallow copy of InventoryCollection, the copy will share @data of the original collection, otherwise we would
# be copying a lot of records in memory.
self.class.new(model_class,
self.class.new(:model_class => model_class,
:manager_ref => manager_ref,
:association => association,
:parent => parent,
Expand All @@ -272,30 +274,40 @@ def clone
end

def association_to_foreign_key_mapping
return {} unless model_class

@association_to_foreign_key_mapping ||= model_class.reflect_on_all_associations.each_with_object({}) do |x, obj|
obj[x.name] = x.foreign_key
end
end

def foreign_key_to_association_mapping
return {} unless model_class

@foreign_key_to_association_mapping ||= model_class.reflect_on_all_associations.each_with_object({}) do |x, obj|
obj[x.foreign_key] = x.name
end
end

def association_to_foreign_type_mapping
return {} unless model_class

@association_to_foreign_type_mapping ||= model_class.reflect_on_all_associations.each_with_object({}) do |x, obj|
obj[x.name] = x.foreign_type if x.polymorphic?
end
end

def foreign_type_to_association_mapping
return {} unless model_class

@foreign_type_to_association_mapping ||= model_class.reflect_on_all_associations.each_with_object({}) do |x, obj|
obj[x.foreign_type] = x.name if x.polymorphic?
end
end

def base_class_name
return "" unless model_class

@base_class_name ||= model_class.base_class.name
end

Expand All @@ -305,7 +317,9 @@ def to_s

strategy_name = ", strategy: #{strategy}" if strategy

"InventoryCollection:<#{@model_class}>#{whitelist}#{blacklist}#{strategy_name}"
name = model_class || association

"InventoryCollection:<#{name}>#{whitelist}#{blacklist}#{strategy_name}"
end

def inspect
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,11 +354,11 @@
# Initialize the InventoryCollections
@data = {}
@data[:vms] = ::ManagerRefresh::InventoryCollection.new(
ManageIQ::Providers::CloudManager::Vm,
:model_class => ManageIQ::Providers::CloudManager::Vm,
:parent => @ems,
:association => :vms)
@data[:hardwares] = ::ManagerRefresh::InventoryCollection.new(
Hardware,
:model_class => Hardware,
:parent => @ems,
:association => :hardwares,
:manager_ref => [:vm_or_template])
Expand Down Expand Up @@ -472,35 +472,35 @@ def initialize_data_and_inventory_collections
# Initialize the InventoryCollections
@data = {}
@data[:vms] = ::ManagerRefresh::InventoryCollection.new(
ManageIQ::Providers::CloudManager::Vm,
:model_class => ManageIQ::Providers::CloudManager::Vm,
:parent => @ems,
:association => :vms)
@data[:key_pairs] = ::ManagerRefresh::InventoryCollection.new(
ManageIQ::Providers::CloudManager::AuthKeyPair,
:model_class => ManageIQ::Providers::CloudManager::AuthKeyPair,
:parent => @ems,
:association => :key_pairs,
:manager_ref => [:name])
@data[:miq_templates] = ::ManagerRefresh::InventoryCollection.new(
ManageIQ::Providers::CloudManager::Template,
:model_class => ManageIQ::Providers::CloudManager::Template,
:parent => @ems,
:association => :miq_templates)
@data[:hardwares] = ::ManagerRefresh::InventoryCollection.new(
Hardware,
:model_class => Hardware,
:parent => @ems,
:association => :hardwares,
:manager_ref => [:vm_or_template])
@data[:disks] = ::ManagerRefresh::InventoryCollection.new(
Disk,
:model_class => Disk,
:parent => @ems,
:association => :disks,
:manager_ref => [:hardware, :device_name])
@data[:networks] = ::ManagerRefresh::InventoryCollection.new(
Network,
:model_class => Network,
:parent => @ems,
:association => :networks,
:manager_ref => [:hardware, :description])
@data[:flavors] = ::ManagerRefresh::InventoryCollection.new(
Flavor,
:model_class => Flavor,
:parent => @ems,
:association => :flavors,
:manager_ref => [:name])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -433,15 +433,15 @@
it 'test network_port -> stack -> resource -> stack' do
@data = {}
@data[:orchestration_stacks] = ::ManagerRefresh::InventoryCollection.new(
ManageIQ::Providers::CloudManager::OrchestrationStack,
:model_class => ManageIQ::Providers::CloudManager::OrchestrationStack,
:parent => @ems,
:association => :orchestration_stacks)
@data[:orchestration_stacks_resources] = ::ManagerRefresh::InventoryCollection.new(
OrchestrationStackResource,
:model_class => OrchestrationStackResource,
:parent => @ems,
:association => :orchestration_stacks_resources)
@data[:network_ports] = ::ManagerRefresh::InventoryCollection.new(
NetworkPort,
:model_class => NetworkPort,
:parent => @ems.network_manager,
:association => :network_ports)

Expand Down Expand Up @@ -503,15 +503,15 @@
it 'test network_port -> stack -> resource -> stack reverted' do
@data = {}
@data[:network_ports] = ::ManagerRefresh::InventoryCollection.new(
NetworkPort,
:model_class => NetworkPort,
:parent => @ems.network_manager,
:association => :network_ports)
@data[:orchestration_stacks_resources] = ::ManagerRefresh::InventoryCollection.new(
OrchestrationStackResource,
:model_class => OrchestrationStackResource,
:parent => @ems,
:association => :orchestration_stacks_resources)
@data[:orchestration_stacks] = ::ManagerRefresh::InventoryCollection.new(
ManageIQ::Providers::CloudManager::OrchestrationStack,
:model_class => ManageIQ::Providers::CloudManager::OrchestrationStack,
:parent => @ems,
:association => :orchestration_stacks)

Expand Down Expand Up @@ -575,7 +575,7 @@
# data dependencies and saving it according to the tree.
@data = {}
@data[:network_ports] = ::ManagerRefresh::InventoryCollection.new(
NetworkPort,
:model_class => NetworkPort,
:parent => @ems.network_manager,
:association => :network_ports)

Expand Down Expand Up @@ -616,15 +616,15 @@
# edge correctly and this cycle is solvable.
@data = {}
@data[:orchestration_stacks] = ::ManagerRefresh::InventoryCollection.new(
ManageIQ::Providers::CloudManager::OrchestrationStack,
:model_class => ManageIQ::Providers::CloudManager::OrchestrationStack,
:parent => @ems,
:association => :orchestration_stacks)
@data[:orchestration_stacks_resources] = ::ManagerRefresh::InventoryCollection.new(
OrchestrationStackResource,
:model_class => OrchestrationStackResource,
:parent => @ems,
:association => :orchestration_stacks_resources)
@data[:network_ports] = ::ManagerRefresh::InventoryCollection.new(
NetworkPort,
:model_class => NetworkPort,
:parent => @ems.network_manager,
:association => :network_ports)

Expand Down Expand Up @@ -686,15 +686,15 @@
it 'test network_port -> stack -> resource -> stack and network_port -> resource -> stack -> resource -> stack ' do
@data = {}
@data[:orchestration_stacks] = ::ManagerRefresh::InventoryCollection.new(
ManageIQ::Providers::CloudManager::OrchestrationStack,
:model_class => ManageIQ::Providers::CloudManager::OrchestrationStack,
:parent => @ems,
:association => :orchestration_stacks)
@data[:orchestration_stacks_resources] = ::ManagerRefresh::InventoryCollection.new(
OrchestrationStackResource,
:model_class => OrchestrationStackResource,
:parent => @ems,
:association => :orchestration_stacks_resources)
@data[:network_ports] = ::ManagerRefresh::InventoryCollection.new(
NetworkPort,
:model_class => NetworkPort,
:parent => @ems.network_manager,
:association => :network_ports)

Expand Down Expand Up @@ -982,11 +982,11 @@ def initialize_inventory_collections
# Initialize the InventoryCollections
@data = {}
@data[:orchestration_stacks] = ::ManagerRefresh::InventoryCollection.new(
ManageIQ::Providers::CloudManager::OrchestrationStack,
:model_class => ManageIQ::Providers::CloudManager::OrchestrationStack,
:parent => @ems,
:association => :orchestration_stacks)
@data[:orchestration_stacks_resources] = ::ManagerRefresh::InventoryCollection.new(
OrchestrationStackResource,
:model_class => OrchestrationStackResource,
:parent => @ems,
:association => :orchestration_stacks_resources)
end
Expand All @@ -996,11 +996,11 @@ def initialize_inventory_collections_reversed
# the order of the InventoryCollections
@data = {}
@data[:orchestration_stacks_resources] = ::ManagerRefresh::InventoryCollection.new(
OrchestrationStackResource,
:model_class => OrchestrationStackResource,
:parent => @ems,
:association => :orchestration_stacks_resources)
@data[:orchestration_stacks] = ::ManagerRefresh::InventoryCollection.new(
ManageIQ::Providers::CloudManager::OrchestrationStack,
:model_class => ManageIQ::Providers::CloudManager::OrchestrationStack,
:parent => @ems,
:association => :orchestration_stacks)
end
Expand Down
Loading

0 comments on commit 136c681

Please sign in to comment.