Skip to content

Commit

Permalink
Merge pull request #17621 from slemrmartin/ic-builder-optimalization
Browse files Browse the repository at this point in the history
InventoryCollection Builder improvements
  • Loading branch information
agrare authored Jul 11, 2018
2 parents e484d1a + f46fb65 commit 9036816
Show file tree
Hide file tree
Showing 10 changed files with 507 additions and 171 deletions.
74 changes: 45 additions & 29 deletions app/models/manager_refresh/inventory_collection/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ class InventoryCollection
class Builder
class MissingModelClassError < StandardError; end

require_nested :AutomationManager
require_nested :CloudManager
require_nested :InfraManager
require_nested :AutomationManager
require_nested :NetworkManager
require_nested :PhysicalInfraManager
require_nested :StorageManager
require_nested :PersisterHelper

Expand All @@ -28,7 +29,7 @@ def self.default_options

# Entry point
# Creates builder and builds data for inventory collection
# @param name [Symbol] InventoryCollection.association value
# @param name [Symbol, Array] InventoryCollection.association value. <name> method not called when Array
# (optional) method with this name also used for concrete inventory collection specific properties
# @param persister_class [Class] used for "guessing" model_class
# @param options [Hash]
Expand All @@ -49,7 +50,7 @@ def initialize(name, persister_class, options = self.class.default_options)

@properties = {}
@inventory_object_attributes = []
@builder_params = {}
@default_values = {}
@dependency_attributes = {}

@options = options
Expand All @@ -65,14 +66,15 @@ def initialize(name, persister_class, options = self.class.default_options)
# Yields for overwriting provider-specific properties
def construct_data
add_properties(:association => @name)
add_properties(:model_class => auto_model_class) unless @options[:without_model_class]

add_properties(@adv_settings, :if_missing)
add_properties(@shared_properties, :if_missing)

send(@name.to_sym) if respond_to?(@name.to_sym)
send(@name.to_sym) if @name.respond_to?(:to_sym) && respond_to?(@name.to_sym)

add_inventory_attributes(auto_inventory_attributes) if @options[:auto_inventory_attributes]
if @properties[:model_class].nil?
add_properties(:model_class => auto_model_class) unless @options[:without_model_class]
end
end

# Creates InventoryCollection
Expand Down Expand Up @@ -125,29 +127,40 @@ def remove_inventory_attributes(array)

# Clears all inventory object attributes
def clear_inventory_attributes!
@options[:auto_inventory_attributes] = false
@inventory_object_attributes = []
end

# Adds key/values to builder params (part of @properties)
def add_builder_params(params = {}, mode = :overwrite)
@builder_params = merge_hashes(@builder_params, params, mode)
# Adds key/values to default values (InventoryCollection.builder_params) (part of @properties)
def add_default_values(params = {}, mode = :overwrite)
@default_values = merge_hashes(@default_values, params, mode)
end
alias add_builder_params add_default_values

# Evaluates lambda blocks
def evaluate_lambdas!(persister)
evaluate_builder_params_lambdas!(persister)
@default_values = evaluate_lambdas_on(@default_values, persister)
@dependency_attributes = evaluate_lambdas_on(@dependency_attributes, persister)
end

# Adds key/values to dependency_attributes (part of @properties)
def add_dependency_attributes(attrs = {}, mode = :overwrite)
@dependency_attributes = merge_hashes(@dependency_attributes, attrs, mode)
end

# Deletes key from dependency_attributes
def remove_dependency_attributes(key)
@dependency_attributes.delete(key)
end

# Returns whole InventoryCollection properties
# TODO: default values converted to builder_params, change InventoryCollection and usages in next PR
def to_hash
add_inventory_attributes(auto_inventory_attributes) if @options[:auto_inventory_attributes]

@properties.merge(
:inventory_object_attributes => @inventory_object_attributes,
:builder_params => @builder_params,
:builder_params => @default_values,
:dependency_attributes => @dependency_attributes
)
end
Expand Down Expand Up @@ -210,6 +223,12 @@ def auto_model_class
end
end

# Enables/disables auto_model_class and exception check
# @param skip [Boolean]
def skip_model_class(skip = true)
@options[:without_model_class] = skip
end

# Inventory object attributes are derived from setters
#
# Can be disabled by options :auto_inventory_attributes => false
Expand All @@ -222,26 +241,23 @@ def auto_inventory_attributes
end
end

# Evaluates lambda blocks in @builder_params
def evaluate_builder_params_lambdas!(persister)
if @builder_params
@builder_params = @builder_params.transform_values do |value|
if value.respond_to?(:call)
value.call(persister)
else
value
end
end
end
end

def network_manager_collections?
self.class.network_manager_collections?
# Enables/disables auto_inventory_attributes
# @param skip [Boolean]
def skip_auto_inventory_attributes(skip = true)
@options[:auto_inventory_attributes] = !skip
end

# InventoryCollection definitions for NetworkManager?
def self.network_manager_collections?
false
# Evaluates lambda blocks in @default_values and @dependency_attributes
# @param values [Hash]
# @param persister [ManagerRefresh::Inventory::Persister]
def evaluate_lambdas_on(values, persister)
values&.transform_values do |value|
if value.respond_to?(:call)
value.call(persister)
else
value
end
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,40 @@ class Builder
class AutomationManager < ::ManagerRefresh::InventoryCollection::Builder
def configuration_scripts
default_manager_ref
default_builder_params
add_common_default_values
end

def configuration_script_payloads
add_properties(
:manager_ref => %i(configuration_script_source manager_ref)
)
default_builder_params
add_common_default_values
end

def configuration_script_sources
default_manager_ref
default_builder_params
add_common_default_values
end

def configuration_workflows
default_manager_ref
default_builder_params
add_common_default_values
end

def configured_systems
default_manager_ref
default_builder_params
add_common_default_values
end

def credentials
default_manager_ref
add_builder_params(
add_default_values(
:resource => ->(persister) { persister.manager }
)
end

def inventory_root_groups
default_builder_params
add_common_default_values
end

def vms
Expand All @@ -51,12 +51,6 @@ def vms
def default_manager_ref
add_properties(:manager_ref => %i(manager_ref))
end

def default_builder_params
add_builder_params(
:manager => ->(persister) { persister.manager }
)
end
end
end
end
Expand Down
Loading

0 comments on commit 9036816

Please sign in to comment.