diff --git a/app/models/manager_refresh/inventory_collection/builder.rb b/app/models/manager_refresh/inventory_collection/builder.rb index f5cc34909d6..a2b7c4779c4 100644 --- a/app/models/manager_refresh/inventory_collection/builder.rb +++ b/app/models/manager_refresh/inventory_collection/builder.rb @@ -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 @@ -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. 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] @@ -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 @@ -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 @@ -125,17 +127,20 @@ 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) @@ -143,11 +148,19 @@ 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 @@ -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 @@ -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 diff --git a/app/models/manager_refresh/inventory_collection/builder/automation_manager.rb b/app/models/manager_refresh/inventory_collection/builder/automation_manager.rb index 7051dc0ac66..2be5c32d17b 100644 --- a/app/models/manager_refresh/inventory_collection/builder/automation_manager.rb +++ b/app/models/manager_refresh/inventory_collection/builder/automation_manager.rb @@ -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 @@ -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 diff --git a/app/models/manager_refresh/inventory_collection/builder/cloud_manager.rb b/app/models/manager_refresh/inventory_collection/builder/cloud_manager.rb index fe024acbe97..d18ed77147e 100644 --- a/app/models/manager_refresh/inventory_collection/builder/cloud_manager.rb +++ b/app/models/manager_refresh/inventory_collection/builder/cloud_manager.rb @@ -3,11 +3,11 @@ class InventoryCollection class Builder class CloudManager < ::ManagerRefresh::InventoryCollection::Builder def availability_zones - shared_builder_params + add_common_default_values end def flavors - shared_builder_params + add_common_default_values end def key_pairs @@ -15,7 +15,7 @@ def key_pairs :model_class => ::ManageIQ::Providers::CloudManager::AuthKeyPair, :manager_ref => %i(name) ) - add_builder_params( + add_default_values( :resource_id => ->(persister) { persister.manager.id }, :resource_type => ->(persister) { persister.manager.class.base_class } ) @@ -28,15 +28,41 @@ def vm_and_template_labels :manager_ref => %i(resource name), :parent_inventory_collections => %i(vms miq_templates) ) + + 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 + + def vm_and_template_taggings + add_properties( + :model_class => Tagging, + :manager_ref => %i(taggable tag), + :parent_inventory_collections => %i(vms miq_templates) + ) + + 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 def orchestration_stacks add_properties( - :model_class => ::ManageIQ::Providers::CloudManager::OrchestrationStack, :attributes_blacklist => %i(parent), ) - shared_builder_params + add_common_default_values end def orchestration_stacks_resources @@ -77,99 +103,110 @@ def orchestration_templates end def orchestration_stack_ancestry - orchestration_stack_ancestry_save_block = lambda do |_ems, inventory_collection| - stacks_inventory_collection = inventory_collection.dependency_attributes[:orchestration_stacks].try(:first) - - return if stacks_inventory_collection.blank? - - stacks_parents = stacks_inventory_collection.data.each_with_object({}) do |x, obj| - parent_id = x.data[:parent].try(:load).try(:id) - obj[x.id] = parent_id if parent_id - end - - model_class = stacks_inventory_collection.model_class - - stacks_parents_indexed = model_class - .select(%i(id ancestry)) - .where(:id => stacks_parents.values).find_each.index_by(&:id) - - ActiveRecord::Base.transaction do - model_class.select(%i(id ancestry)) - .where(:id => stacks_parents.keys).find_each do |stack| - parent = stacks_parents_indexed[stacks_parents[stack.id]] - stack.update_attribute(:parent, parent) - end - end - end + skip_auto_inventory_attributes + skip_model_class add_properties( :custom_save_block => orchestration_stack_ancestry_save_block ) + + add_dependency_attributes( + :orchestration_stacks => ->(persister) { [persister.collections[:orchestration_stacks]] }, + :orchestration_stacks_resources => ->(persister) { [persister.collections[:orchestration_stacks_resources]] } + ) end def vm_and_miq_template_ancestry - vm_and_miq_template_ancestry_save_block = lambda do |_ems, inventory_collection| - vms_inventory_collection = inventory_collection.dependency_attributes[:vms].try(:first) - miq_templates_inventory_collection = inventory_collection.dependency_attributes[:miq_templates].try(:first) - - return if vms_inventory_collection.blank? || miq_templates_inventory_collection.blank? - - # Fetch IDs of all vms and genealogy_parents, only if genealogy_parent is present - vms_genealogy_parents = vms_inventory_collection.data.each_with_object({}) do |x, obj| - unless x.data[:genealogy_parent].nil? - genealogy_parent_id = x.data[:genealogy_parent].load.try(:id) - obj[x.id] = genealogy_parent_id if genealogy_parent_id - end - end - - miq_template_genealogy_parents = miq_templates_inventory_collection.data.each_with_object({}) do |x, obj| - unless x.data[:genealogy_parent].nil? - genealogy_parent_id = x.data[:genealogy_parent].load.try(:id) - obj[x.id] = genealogy_parent_id if genealogy_parent_id - end - end - - ActiveRecord::Base.transaction do - # associate parent templates to child instances - parent_miq_templates = miq_templates_inventory_collection.model_class - .select([:id]) - .where(:id => vms_genealogy_parents.values).find_each.index_by(&:id) - vms_inventory_collection.model_class - .select([:id]) - .where(:id => vms_genealogy_parents.keys).find_each do |vm| - parent = parent_miq_templates[vms_genealogy_parents[vm.id]] - vm.with_relationship_type('genealogy') { vm.parent = parent } - end - end - - ActiveRecord::Base.transaction do - # associate parent instances to child templates - parent_vms = vms_inventory_collection.model_class - .select([:id]) - .where(:id => miq_template_genealogy_parents.values).find_each.index_by(&:id) - miq_templates_inventory_collection.model_class - .select([:id]) - .where(:id => miq_template_genealogy_parents.keys).find_each do |miq_template| - parent = parent_vms[miq_template_genealogy_parents[miq_template.id]] - miq_template.with_relationship_type('genealogy') { miq_template.parent = parent } - end - end - end + skip_auto_inventory_attributes + skip_model_class add_properties( :custom_save_block => vm_and_miq_template_ancestry_save_block ) + + add_dependency_attributes( + :vms => ->(persister) { [persister.collections[:vms]] }, + :miq_templates => ->(persister) { [persister.collections[:miq_templates]] } + ) end end private - def shared_builder_params - add_builder_params(:ems_id => default_ems_id) + def orchestration_stack_ancestry_save_block + lambda do |_ems, inventory_collection| + stacks_inventory_collection = inventory_collection.dependency_attributes[:orchestration_stacks].try(:first) + + return if stacks_inventory_collection.blank? + + stacks_parents = stacks_inventory_collection.data.each_with_object({}) do |x, obj| + parent_id = x.data[:parent].try(:load).try(:id) + obj[x.id] = parent_id if parent_id + end + + model_class = stacks_inventory_collection.model_class + + stacks_parents_indexed = model_class.select(%i(id ancestry)) + .where(:id => stacks_parents.values).find_each.index_by(&:id) + + ActiveRecord::Base.transaction do + model_class.select(%i(id ancestry)) + .where(:id => stacks_parents.keys).find_each do |stack| + parent = stacks_parents_indexed[stacks_parents[stack.id]] + stack.update_attribute(:parent, parent) + end + end + end end - def default_ems_id - ->(persister) { persister.manager.id } + def vm_and_miq_template_ancestry_save_block + lambda do |_ems, inventory_collection| + vms_inventory_collection = inventory_collection.dependency_attributes[:vms].try(:first) + miq_templates_inventory_collection = inventory_collection.dependency_attributes[:miq_templates].try(:first) + + return if vms_inventory_collection.blank? || miq_templates_inventory_collection.blank? + + # Fetch IDs of all vms and genealogy_parents, only if genealogy_parent is present + vms_genealogy_parents = vms_inventory_collection.data.each_with_object({}) do |x, obj| + unless x.data[:genealogy_parent].nil? + genealogy_parent_id = x.data[:genealogy_parent].load.try(:id) + obj[x.id] = genealogy_parent_id if genealogy_parent_id + end + end + + miq_template_genealogy_parents = miq_templates_inventory_collection.data.each_with_object({}) do |x, obj| + unless x.data[:genealogy_parent].nil? + genealogy_parent_id = x.data[:genealogy_parent].load.try(:id) + obj[x.id] = genealogy_parent_id if genealogy_parent_id + end + end + + ActiveRecord::Base.transaction do + # associate parent templates to child instances + parent_miq_templates = miq_templates_inventory_collection.model_class + .select([:id]) + .where(:id => vms_genealogy_parents.values).find_each.index_by(&:id) + vms_inventory_collection.model_class + .select([:id]) + .where(:id => vms_genealogy_parents.keys).find_each do |vm| + parent = parent_miq_templates[vms_genealogy_parents[vm.id]] + vm.with_relationship_type('genealogy') { vm.parent = parent } + end + end + + ActiveRecord::Base.transaction do + # associate parent instances to child templates + parent_vms = vms_inventory_collection.model_class + .select([:id]) + .where(:id => miq_template_genealogy_parents.values).find_each.index_by(&:id) + miq_templates_inventory_collection.model_class + .select([:id]) + .where(:id => miq_template_genealogy_parents.keys).find_each do |miq_template| + parent = parent_vms[miq_template_genealogy_parents[miq_template.id]] + miq_template.with_relationship_type('genealogy') { miq_template.parent = parent } + end + end + end end end end diff --git a/app/models/manager_refresh/inventory_collection/builder/container_manager.rb b/app/models/manager_refresh/inventory_collection/builder/container_manager.rb new file mode 100644 index 00000000000..40d9613378f --- /dev/null +++ b/app/models/manager_refresh/inventory_collection/builder/container_manager.rb @@ -0,0 +1,237 @@ +module ManagerRefresh + class InventoryCollection + class Builder + class ContainerManager < ::ManagerRefresh::InventoryCollection::Builder + # TODO: (agrare) Targeted refreshes will require adjusting the associations / arels. (duh) + def container_projects + add_properties( + :secondary_refs => {:by_name => %i(name)}, + :delete_method => :disconnect_inv + ) + add_common_default_values + end + + def container_quotas + add_properties( + :attributes_blacklist => %i(namespace), + :delete_method => :disconnect_inv + ) + add_common_default_values + end + + def container_quota_scopes + add_properties( + :manager_ref => %i(container_quota scope) + ) + end + + def container_quota_items + add_properties( + :manager_ref => %i(container_quota resource quota_desired quota_enforced quota_observed), + :delete_method => :disconnect_inv + ) + end + + def container_limits + add_properties( + :attributes_blacklist => %i(namespace) + ) + add_common_default_values + end + + def container_limit_items + add_properties( + :manager_ref => %i(container_limit resource item_type) + ) + end + + def container_nodes + add_properties( + :model_class => ::ContainerNode, + :secondary_refs => {:by_name => %i(name)}, + :delete_method => :disconnect_inv + ) + add_common_default_values + end + + def computer_systems + add_properties(:manager_ref => %i(managed_entity)) + end + + def computer_system_hardwares + add_properties( + :model_class => ::Hardware, + :manager_ref => %i(computer_system) + ) + end + + def computer_system_operating_systems + add_properties( + :model_class => ::OperatingSystem, + :manager_ref => %i(computer_system) + ) + end + + # images have custom_attributes but that's done conditionally in openshift parser + def container_images + add_properties( + # TODO: (bpaskinc) old save matches on [:image_ref, :container_image_registry_id] + # TODO: (bpaskinc) should match on digest when available + # TODO: (mslemr) provider-specific class exists (openshift), but specs fail with them (?) + :model_class => ::ContainerImage, + :manager_ref => %i(image_ref), + :delete_method => :disconnect_inv, + :custom_reconnect_block => custom_reconnect_block + ) + add_common_default_values + end + + def container_image_registries + add_properties(:manager_ref => %i(host port)) + add_common_default_values + end + + def container_groups + add_properties( + :model_class => ContainerGroup, + :secondary_refs => {:by_container_project_and_name => %i(container_project name)}, + :attributes_blacklist => %i(namespace), + :delete_method => :disconnect_inv, + :custom_reconnect_block => custom_reconnect_block + ) + add_common_default_values + end + + def container_volumes + add_properties(:manager_ref => %i(parent name)) + end + + def containers + add_properties( + :model_class => Container, + # parser sets :ems_ref => "#{pod_id}_#{container.name}_#{container.image}" + :delete_method => :disconnect_inv, + :custom_reconnect_block => custom_reconnect_block + ) + add_common_default_values + end + + def container_port_configs + # parser sets :ems_ref => "#{pod_id}_#{container_name}_#{port_config.containerPort}_#{port_config.hostPort}_#{port_config.protocol}" + end + + def container_env_vars + add_properties( + # TODO: (agrare) old save matches on all :name, :value, :field_path - does this matter? + :manager_ref => %i(container name) + ) + end + + def security_contexts + add_properties(:manager_ref => %i(resource)) + end + + def container_replicators + add_properties( + :secondary_refs => {:by_container_project_and_name => %i(container_project name)}, + :attributes_blacklist => %i(namespace) + ) + add_common_default_values + end + + def container_services + add_properties( + :secondary_refs => {:by_container_project_and_name => %i(container_project name)}, + :attributes_blacklist => %i(namespace), + :saver_strategy => "default" # TODO: (fryguy) (perf) Can't use batch strategy because of usage of M:N container_groups relation + ) + add_common_default_values + end + + def container_service_port_configs + add_properties(:manager_ref => %i(ems_ref protocol)) # TODO: (lsmola) make protocol part of the ems_ref?) + end + + def container_routes + add_properties(:attributes_blacklist => %i(namespace)) + add_common_default_values + end + + def container_templates + add_properties( + :model_class => ::ContainerTemplate, + :attributes_blacklist => %i(namespace) + ) + add_common_default_values + end + + def container_template_parameters + add_properties(:manager_ref => %i(container_template name)) + end + + def container_builds + add_properties( + :secondary_refs => {:by_namespace_and_name => %i(namespace name)} + ) + add_common_default_values + end + + def container_build_pods + add_properties( + # TODO: (bpaskinc) convert namespace column -> container_project_id? + :manager_ref => %i(namespace name), + :secondary_refs => {:by_namespace_and_name => %i(namespace name)}, + ) + add_common_default_values + end + + def persistent_volumes + add_default_values(:parent => ->(persister) { persister.manager }) + end + + def persistent_volume_claims + add_properties( + :secondary_refs => {:by_container_project_and_name => %i(container_project name)}, + :attributes_blacklist => %i(namespace) + ) + add_common_default_values + end + + protected + + def custom_reconnect_block + # TODO(lsmola) once we have DB unique indexes, we can stop using manual reconnect, since it adds processing time + lambda do |inventory_collection, inventory_objects_index, attributes_index| + relation = inventory_collection.model_class.where(:ems_id => inventory_collection.parent.id).archived + + # Skip reconnect if there are no archived entities + return if relation.archived.count <= 0 + raise "Allowed only manager_ref size of 1, got #{inventory_collection.manager_ref}" if inventory_collection.manager_ref.count > 1 + + inventory_objects_index.each_slice(1000) do |batch| + relation.where(inventory_collection.manager_ref.first => batch.map(&:first)).each do |record| + index = inventory_collection.object_index_with_keys(inventory_collection.manager_ref_to_cols, record) + + # We need to delete the record from the inventory_objects_index and attributes_index, otherwise it + # would be sent for create. + inventory_object = inventory_objects_index.delete(index) + hash = attributes_index.delete(index) + + # Make the entity active again, otherwise we would be duplicating nested entities + hash[:deleted_on] = nil + + record.assign_attributes(hash.except(:id, :type)) + if !inventory_collection.check_changed? || record.changed? + record.save! + inventory_collection.store_updated_records(record) + end + + inventory_object.id = record.id + end + end + end + end + end + end + end +end diff --git a/app/models/manager_refresh/inventory_collection/builder/infra_manager.rb b/app/models/manager_refresh/inventory_collection/builder/infra_manager.rb index 7728c8091c0..8493ce52403 100644 --- a/app/models/manager_refresh/inventory_collection/builder/infra_manager.rb +++ b/app/models/manager_refresh/inventory_collection/builder/infra_manager.rb @@ -56,16 +56,27 @@ def custom_attributes ) end + def vm_and_template_ems_custom_fields + skip_auto_inventory_attributes + + add_properties( + :model_class => ::CustomAttribute, + :manager_ref => %i(name) + ) + + add_inventory_attributes(%i(section name value source resource)) + end + def ems_folders add_properties( :manager_ref => %i(uid_ems), :attributes_blacklist => %i(ems_children), ) - shared_builder_params + add_common_default_values end def datacenters - shared_builder_params + add_common_default_values end def resource_pools @@ -73,7 +84,7 @@ def resource_pools :manager_ref => %i(uid_ems), :attributes_blacklist => %i(ems_children), ) - shared_builder_params + add_common_default_values end def ems_clusters @@ -82,7 +93,7 @@ def ems_clusters ) add_inventory_attributes(%i(datacenter_id)) - shared_builder_params + add_common_default_values end def storages @@ -94,7 +105,7 @@ def storages end def hosts - shared_builder_params + add_common_default_values add_custom_reconnect_block( lambda do |inventory_collection, inventory_objects_index, attributes_index| @@ -200,16 +211,6 @@ def snapshot_parent :custom_save_block => snapshot_parent_save_block ) end - - private - - def shared_builder_params - add_builder_params(:ems_id => default_ems_id) - end - - def default_ems_id - ->(persister) { persister.manager.id } - end end end end diff --git a/app/models/manager_refresh/inventory_collection/builder/network_manager.rb b/app/models/manager_refresh/inventory_collection/builder/network_manager.rb index 2a14baccb5c..084b0917385 100644 --- a/app/models/manager_refresh/inventory_collection/builder/network_manager.rb +++ b/app/models/manager_refresh/inventory_collection/builder/network_manager.rb @@ -26,35 +26,35 @@ def network_ports :saver_strategy => :default ) - shared_builder_params + add_common_default_values end def network_groups - shared_builder_params + add_common_default_values end def network_routers - shared_builder_params + add_common_default_values end def floating_ips - shared_builder_params + add_common_default_values end def cloud_tenants - shared_builder_params + add_common_default_values end def cloud_subnets - shared_builder_params + add_common_default_values end def cloud_networks - shared_builder_params + add_common_default_values end def security_groups - shared_builder_params + add_common_default_values end def firewall_rules @@ -65,7 +65,7 @@ def firewall_rules end def load_balancers - shared_builder_params + add_common_default_values end def load_balancer_pools @@ -83,7 +83,7 @@ def load_balancer_pools end ) - shared_builder_params + add_common_default_values end def load_balancer_pool_members @@ -106,7 +106,7 @@ def load_balancer_pool_members end ) - shared_builder_params + add_common_default_values end def load_balancer_pool_member_pools @@ -142,7 +142,7 @@ def load_balancer_listeners end ) - shared_builder_params + add_common_default_values end def load_balancer_listener_pools @@ -177,7 +177,7 @@ def load_balancer_health_checks end ) - shared_builder_params + add_common_default_values end def load_balancer_health_check_members @@ -199,18 +199,13 @@ def load_balancer_health_check_members protected - def shared_builder_params - add_builder_params(:ems_id => default_ems_id) + def add_common_default_values + add_default_values(:ems_id => default_ems_id) end def default_ems_id ->(persister) { persister.manager.try(:network_manager).try(:id) || persister.manager.id } end - - # InventoryCollection definitions for NetworkManager? - def self.network_manager_collections? - true - end end end end diff --git a/app/models/manager_refresh/inventory_collection/builder/persister_helper.rb b/app/models/manager_refresh/inventory_collection/builder/persister_helper.rb index 0cc6eed4f18..d1c05b2e0a8 100644 --- a/app/models/manager_refresh/inventory_collection/builder/persister_helper.rb +++ b/app/models/manager_refresh/inventory_collection/builder/persister_helper.rb @@ -4,7 +4,7 @@ module ManagerRefresh::InventoryCollection::Builder::PersisterHelper # Interface for creating InventoryCollection under @collections # # @param builder_class [ManagerRefresh::InventoryCollection::Builder] or subclasses - # @param collection_name [Symbol] used as InventoryCollection:association + # @param collection_name [Symbol || Array] used as InventoryCollection:association # @param extra_properties [Hash] props from InventoryCollection.initialize list # - adds/overwrites properties added by builder # @@ -19,7 +19,7 @@ module ManagerRefresh::InventoryCollection::Builder::PersisterHelper # ) # ) # - # @see ManagerRefresh::InventoryCollection::Builder + # @see documentation https://github.com/ManageIQ/guides/tree/master/providers/persister/inventory_collections.md # def add_collection(builder_class, collection_name, extra_properties = {}, settings = {}, &block) builder = builder_class.prepare_data(collection_name, @@ -61,6 +61,15 @@ def automation ::ManagerRefresh::InventoryCollection::Builder::AutomationManager end + # builder class for add_collection() + def physical_infra + ::ManagerRefresh::InventoryCollection::Builder::PhysicalInfraManager + end + + def container + ::ManagerRefresh::InventoryCollection::Builder::ContainerManager + end + # @param extra_settings [Hash] # :auto_inventory_attributes # - auto creates inventory_object_attributes from target model_class setters @@ -72,7 +81,7 @@ def automation def make_builder_settings(extra_settings = {}) opts = ::ManagerRefresh::InventoryCollection::Builder.default_options - opts[:adv_settings] = options[:inventory_collections].try(:to_hash) || {} + opts[:adv_settings] = options.try(:[], :inventory_collections).try(:to_hash) || {} opts[:shared_properties] = shared_options opts[:auto_inventory_attributes] = true opts[:without_model_class] = false @@ -98,12 +107,12 @@ def shared_options # Returns list of target's ems_refs # @return [Array] def references(collection) - target.manager_refs_by_association.try(:[], collection).try(:[], :ems_ref).try(:to_a) || [] + target.try(:manager_refs_by_association).try(:[], collection).try(:[], :ems_ref).try(:to_a) || [] end # Returns list of target's name # @return [Array] def name_references(collection) - target.manager_refs_by_association.try(:[], collection).try(:[], :name).try(:to_a) || [] + target.try(:manager_refs_by_association).try(:[], collection).try(:[], :name).try(:to_a) || [] end end diff --git a/app/models/manager_refresh/inventory_collection/builder/physical_infra_manager.rb b/app/models/manager_refresh/inventory_collection/builder/physical_infra_manager.rb new file mode 100644 index 00000000000..a7d655c828b --- /dev/null +++ b/app/models/manager_refresh/inventory_collection/builder/physical_infra_manager.rb @@ -0,0 +1,41 @@ +module ManagerRefresh + class InventoryCollection + class Builder + class PhysicalInfraManager < ::ManagerRefresh::InventoryCollection::Builder + def physical_servers + add_common_default_values + end + + def physical_server_details + add_properties( + :model_class => ::AssetDetail, + :manager_ref => %i(resource), + :parent_inventory_collections => %i(physical_servers) + ) + end + + def computer_systems + add_properties( + :manager_ref => %i(managed_entity), + :parent_inventory_collections => %i(physical_servers) + ) + end + + def hardwares + add_properties( + :manager_ref => %i(computer_system), + :parent_inventory_collections => %i(physical_servers) + ) + end + + def physical_racks + add_common_default_values + end + + def physical_chassis + add_common_default_values + end + end + end + end +end diff --git a/app/models/manager_refresh/inventory_collection/builder/shared.rb b/app/models/manager_refresh/inventory_collection/builder/shared.rb index b0377deb066..59ce2f26719 100644 --- a/app/models/manager_refresh/inventory_collection/builder/shared.rb +++ b/app/models/manager_refresh/inventory_collection/builder/shared.rb @@ -43,7 +43,7 @@ def vms def miq_templates vm_template_shared - add_builder_params( + add_default_values( :template => true ) end @@ -58,7 +58,7 @@ def vm_template_shared :custom_reconnect_block => INVENTORY_RECONNECT_BLOCK ) - add_builder_params( + add_default_values( :ems_id => ->(persister) { persister.manager.id } ) end @@ -91,5 +91,11 @@ def disks :parent_inventory_collections => %i(vms) ) end + + protected + + def add_common_default_values + add_default_values(:ems_id => ->(persister) { persister.manager.id }) + end end end diff --git a/spec/models/manager_refresh/inventory_collection/builder_spec.rb b/spec/models/manager_refresh/inventory_collection/builder_spec.rb index 96f3802e9bc..5e090855108 100644 --- a/spec/models/manager_refresh/inventory_collection/builder_spec.rb +++ b/spec/models/manager_refresh/inventory_collection/builder_spec.rb @@ -135,22 +135,22 @@ def create_persister expect(data[:some_tmp_param]).to eq :some_value end - # --- builder params --- + # --- default values --- - it 'adds builder_params repeatedly' do + it 'adds default_values repeatedly' do data = cloud.prepare_data(:tmp, persister_class) do |builder| - builder.add_builder_params(:ems_id => 10) - builder.add_builder_params(:ems_id => 20) - builder.add_builder_params(:tmp_id => 30) + builder.add_default_values(:ems_id => 10) + builder.add_default_values(:ems_id => 20) + builder.add_default_values(:tmp_id => 30) end.to_hash expect(data[:builder_params][:ems_id]).to eq 20 expect(data[:builder_params][:tmp_id]).to eq 30 end - it 'transforms lambdas in builder_params' do + it 'transforms lambdas in default_values' do bldr = cloud.prepare_data(:tmp, persister_class) do |builder| - builder.add_builder_params(:ems_id => ->(persister) { persister.manager.id }) + builder.add_default_values(:ems_id => ->(persister) { persister.manager.id }) end bldr.evaluate_lambdas!(@persister)