From 46da4f325031246ae4e341f0bccf1b968a1c601e Mon Sep 17 00:00:00 2001 From: Martin Slemr Date: Thu, 21 Jun 2018 16:11:24 +0200 Subject: [PATCH] Better persister's IC definitions Optimizations --- .../definitions/cloud_collections.rb | 102 ++---------- .../definitions/network_collections.rb | 37 ++--- .../inventory/persister/network_manager.rb | 6 - .../cloud_manager.rb | 149 ------------------ .../network_manager.rb | 111 ------------- 5 files changed, 24 insertions(+), 381 deletions(-) delete mode 100644 app/models/manageiq/providers/azure/inventory_collection_default/cloud_manager.rb delete mode 100644 app/models/manageiq/providers/azure/inventory_collection_default/network_manager.rb diff --git a/app/models/manageiq/providers/azure/inventory/persister/definitions/cloud_collections.rb b/app/models/manageiq/providers/azure/inventory/persister/definitions/cloud_collections.rb index 3de696bc..98f332c0 100644 --- a/app/models/manageiq/providers/azure/inventory/persister/definitions/cloud_collections.rb +++ b/app/models/manageiq/providers/azure/inventory/persister/definitions/cloud_collections.rb @@ -1,29 +1,25 @@ module ManageIQ::Providers::Azure::Inventory::Persister::Definitions::CloudCollections extend ActiveSupport::Concern - # TODO: almost same as amazon def initialize_cloud_inventory_collections - %i(vms + %i(availability_zones + disks + flavors hardwares - operating_systems networks - disks - availability_zones).each do |name| + operating_systems + vm_and_template_labels + vm_and_template_taggings + vms).each do |name| add_collection(cloud, name) end add_miq_templates - add_flavors - add_key_pairs - add_resource_groups # not in amazon - - add_vm_and_template_labels - - add_vm_and_template_taggings + add_resource_groups add_orchestration_stacks @@ -38,42 +34,33 @@ def initialize_cloud_inventory_collections end # Custom processing of Ancestry - add_vm_and_miq_template_ancestry + %i(vm_and_miq_template_ancestry + orchestration_stack_ancestry).each do |name| - add_orchestration_stack_ancestry + add_collection(cloud, name) + end end # ------ IC provider specific definitions ------------------------- - # TODO: Derive model class in core - # Different from amazon def add_miq_templates add_collection(cloud, :miq_templates) do |builder| builder.add_properties(:model_class => ::ManageIQ::Providers::Azure::CloudManager::Template) - builder.add_builder_params( + builder.add_default_values( :ems_id => manager.id, :vendor => builder.vendor ) end end - # Missing in amazon def add_resource_groups add_collection(cloud, :resource_groups, {}, {:auto_inventory_attributes => false}) do |builder| builder.add_properties(:model_class => ::ManageIQ::Providers::Azure::ResourceGroup) - - builder.add_builder_params(:ems_id => manager.id) + builder.add_default_values(:ems_id => manager.id) end end - # Targeted doesn't have special strategy like amazon - def add_flavors - add_collection(cloud, :flavors) - end - - # TODO: almost same as amazon? (after targeted) - different model_class! - # TODO: Derive model class in core def add_key_pairs(extra_properties = {}) add_collection(cloud, :key_pairs, extra_properties) do |builder| builder.add_properties(:model_class => ::ManageIQ::Providers::Azure::CloudManager::AuthKeyPair) @@ -81,68 +68,9 @@ def add_key_pairs(extra_properties = {}) end end - # TODO: mslemr - parent model_class used anywhere? If not, should be deleted def add_orchestration_stacks add_collection(cloud, :orchestration_stacks) do |builder| - builder.add_properties( - :model_class => ::ManageIQ::Providers::Azure::CloudManager::OrchestrationStack, - :saver_strategy => 'default' # TODO(lsmola) can't batch unless we do smart batching - ) - end - end - - # TODO: mslemr - same as amazon! - 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 - - # TODO: mslemr - same as amazon! - 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 - - # TODO: mslemr - same as amazon! - def add_vm_and_miq_template_ancestry - add_collection(cloud, :vm_and_miq_template_ancestry, {}, {:auto_inventory_attributes => false, :auto_model_class => false, :without_model_class => true}) do |builder| - builder.add_dependency_attributes( - :vms => [collections[:vms]], - :miq_templates => [collections[:miq_templates]] - ) - end - end - - # TODO: mslemr - same as amazon! - def add_orchestration_stack_ancestry - add_collection(cloud, :orchestration_stack_ancestry, {}, {:auto_inventory_attributes => false, :auto_model_class => false, :without_model_class => true}) do |builder| - builder.add_dependency_attributes( - :orchestration_stacks => [collections[:orchestration_stacks]], - :orchestration_stacks_resources => [collections[:orchestration_stacks_resources]] - ) + builder.add_properties(:saver_strategy => 'default') # TODO(lsmola) can't batch unless we do smart batching end end end diff --git a/app/models/manageiq/providers/azure/inventory/persister/definitions/network_collections.rb b/app/models/manageiq/providers/azure/inventory/persister/definitions/network_collections.rb index 3a3627ad..fd1f6ca9 100644 --- a/app/models/manageiq/providers/azure/inventory/persister/definitions/network_collections.rb +++ b/app/models/manageiq/providers/azure/inventory/persister/definitions/network_collections.rb @@ -3,7 +3,10 @@ module ManageIQ::Providers::Azure::Inventory::Persister::Definitions::NetworkCol def initialize_network_inventory_collections %i(cloud_networks - security_groups + cloud_subnet_network_ports + floating_ips + network_ports + network_routers load_balancers load_balancer_pools load_balancer_pool_members @@ -11,46 +14,24 @@ def initialize_network_inventory_collections load_balancer_listeners load_balancer_listener_pools load_balancer_health_checks - load_balancer_health_check_members).each do |name| + load_balancer_health_check_members + security_groups).each do |name| add_collection(network, name) end - add_cloud_subnet_network_ports - - add_cloud_subnets # different in amazon + add_cloud_subnets add_firewall_rules - - add_floating_ips - - add_network_ports - - # Not in Amazon - %i(network_routers).each do |name| - add_collection(network, name) - end end # ------ IC provider specific definitions ------------------------- - def add_cloud_subnet_network_ports - add_collection(network, :cloud_subnet_network_ports) - end - - def add_network_ports - add_collection(network, :network_ports) - end - - def add_floating_ips - add_collection(network, :floating_ips) - end - # TODO: Different from amazon + # TODO: Builder params def add_cloud_subnets - add_collection(network, :cloud_subnets) do |builder| + add_collection(network, :cloud_subnets, :builder_params => nil) do |builder| builder.add_properties(:parent_inventory_collections => %i(cloud_networks)) - builder.add_properties(:builder_params => nil) builder.add_targeted_arel( lambda do |inventory_collection| diff --git a/app/models/manageiq/providers/azure/inventory/persister/network_manager.rb b/app/models/manageiq/providers/azure/inventory/persister/network_manager.rb index e74aac20..cea899c3 100644 --- a/app/models/manageiq/providers/azure/inventory/persister/network_manager.rb +++ b/app/models/manageiq/providers/azure/inventory/persister/network_manager.rb @@ -20,11 +20,5 @@ def initialize_cloud_inventory_collections ) end end - - # TODO: Not in amazon - # add_orchestration_stacks( - # :parent => manager.parent_manager, - # :strategy => :local_db_cache_all - # ) end end diff --git a/app/models/manageiq/providers/azure/inventory_collection_default/cloud_manager.rb b/app/models/manageiq/providers/azure/inventory_collection_default/cloud_manager.rb deleted file mode 100644 index d476b485..00000000 --- a/app/models/manageiq/providers/azure/inventory_collection_default/cloud_manager.rb +++ /dev/null @@ -1,149 +0,0 @@ -class ManageIQ::Providers::Azure::InventoryCollectionDefault::CloudManager < ManagerRefresh::InventoryCollectionDefault::CloudManager - class << self - def resource_groups(extra_attributes = {}) - attributes = { - :model_class => ::ManageIQ::Providers::Azure::ResourceGroup, - :association => :resource_groups, - :builder_params => { - :ems_id => ->(persister) { persister.manager.id }, - } - } - - attributes.merge!(extra_attributes) - end - - def vms(extra_attributes = {}) - attributes = { - :model_class => ::ManageIQ::Providers::Azure::CloudManager::Vm, - :builder_params => { - :ems_id => ->(persister) { persister.manager.id }, - :vendor => "azure", - } - } - super(attributes.merge!(extra_attributes)) - end - - def miq_templates(extra_attributes = {}) - attributes = { - :model_class => ::ManageIQ::Providers::Azure::CloudManager::Template, - :builder_params => { - :ems_id => ->(persister) { persister.manager.id }, - :vendor => "azure", - :template => true - } - } - - super(attributes.merge!(extra_attributes)) - end - - def availability_zones(extra_attributes = {}) - attributes = { - :model_class => ::ManageIQ::Providers::Azure::CloudManager::AvailabilityZone, - } - - super(attributes.merge!(extra_attributes)) - end - - def flavors(extra_attributes = {}) - attributes = { - :model_class => ::ManageIQ::Providers::Azure::CloudManager::Flavor, - } - - super(attributes.merge!(extra_attributes)) - end - - def key_pairs(extra_attributes = {}) - attributes = { - :model_class => ::ManageIQ::Providers::Azure::CloudManager::AuthKeyPair, - } - - super(attributes.merge!(extra_attributes)) - end - - def vm_and_template_labels(extra_attributes = {}) - attributes = { - :model_class => CustomAttribute, - :association => :vm_and_template_labels, - :manager_ref => %i(resource name), - :parent_inventory_collections => %i(vms miq_templates), - :inventory_object_attributes => %i( - resource - section - name - value - source - ) - } - - attributes[: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 - - attributes.merge!(extra_attributes) - end - - def vm_and_template_taggings(extra_attributes = {}) - attributes = { - :model_class => Tagging, - :association => :vm_and_template_taggings, - :manager_ref => %i(taggable tag), - :inventory_object_attributes => %i(taggable tag), - :parent_inventory_collections => %i(vms miq_templates), - } - - attributes[: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 - - attributes.merge!(extra_attributes) - end - - def orchestration_stacks(extra_attributes = {}) - attributes = { - :model_class => ::ManageIQ::Providers::Azure::CloudManager::OrchestrationStack, - :saver_strategy => "default" # TODO(lsmola) can't batch unless we do smart batching - } - - super(attributes.merge!(extra_attributes)) - end - - def orchestration_stacks_resources(extra_attributes = {}) - attributes = { - :manager_ref => %i(stack ems_ref) - } - - super(attributes.merge!(extra_attributes)) - end - - def orchestration_stacks_outputs(extra_attributes = {}) - attributes = { - :manager_ref => %i(stack ems_ref) - } - - super(attributes.merge!(extra_attributes)) - end - - def orchestration_stacks_parameters(extra_attributes = {}) - attributes = { - :manager_ref => %i(stack ems_ref) - } - - super(attributes.merge!(extra_attributes)) - end - - def orchestration_templates(extra_attributes = {}) - attributes = { - :model_class => ::ManageIQ::Providers::Azure::CloudManager::OrchestrationTemplate, - } - - super(attributes.merge!(extra_attributes)) - end - end -end diff --git a/app/models/manageiq/providers/azure/inventory_collection_default/network_manager.rb b/app/models/manageiq/providers/azure/inventory_collection_default/network_manager.rb deleted file mode 100644 index ca7ca2bf..00000000 --- a/app/models/manageiq/providers/azure/inventory_collection_default/network_manager.rb +++ /dev/null @@ -1,111 +0,0 @@ -class ManageIQ::Providers::Azure::InventoryCollectionDefault::NetworkManager < ManagerRefresh::InventoryCollectionDefault::NetworkManager - class << self - def network_ports(extra_attributes = {}) - attributes = { - :model_class => ::ManageIQ::Providers::Azure::NetworkManager::NetworkPort, - } - - super(attributes.merge!(extra_attributes)) - end - - def network_routers(extra_attributes = {}) - attributes = { - :model_class => ::ManageIQ::Providers::Azure::NetworkManager::NetworkRouter, - :association => :network_routers, - :builder_params => { - :ems_id => ->(persister) { persister.manager.try(:network_manager).try(:id) || persister.manager.id }, - } - } - - attributes.merge!(extra_attributes) - end - - def floating_ips(extra_attributes = {}) - attributes = { - :model_class => ::ManageIQ::Providers::Azure::NetworkManager::FloatingIp, - } - - super(attributes.merge!(extra_attributes)) - end - - def cloud_subnets(extra_attributes = {}) - attributes = { - :model_class => ::ManageIQ::Providers::Azure::NetworkManager::CloudSubnet, - :parent_inventory_collections => [:cloud_networks], - } - - attributes[:targeted_arel] = lambda do |inventory_collection| - manager_uuids = inventory_collection.parent_inventory_collections.flat_map { |c| c.manager_uuids.to_a } - inventory_collection.parent.cloud_subnets.joins(:cloud_network).where( - :cloud_networks => {:ems_ref => manager_uuids} - ) - end - - super(attributes.merge!(extra_attributes)) - end - - def cloud_networks(extra_attributes = {}) - attributes = { - :model_class => ::ManageIQ::Providers::Azure::NetworkManager::CloudNetwork, - } - - super(attributes.merge!(extra_attributes)) - end - - def security_groups(extra_attributes = {}) - attributes = { - :model_class => ::ManageIQ::Providers::Azure::NetworkManager::SecurityGroup, - } - - super(attributes.merge!(extra_attributes)) - end - - def firewall_rules(extra_attributes = {}) - attributes = { - :manager_ref_allowed_nil => [:source_security_group], - } - - super(attributes.merge!(extra_attributes)) - end - - def load_balancers(extra_attributes = {}) - attributes = { - :model_class => ::ManageIQ::Providers::Azure::NetworkManager::LoadBalancer, - } - - super(attributes.merge!(extra_attributes)) - end - - def load_balancer_pools(extra_attributes = {}) - attributes = { - :model_class => ::ManageIQ::Providers::Azure::NetworkManager::LoadBalancerPool, - } - - super(attributes.merge!(extra_attributes)) - end - - def load_balancer_pool_members(extra_attributes = {}) - attributes = { - :model_class => ::ManageIQ::Providers::Azure::NetworkManager::LoadBalancerPoolMember, - } - - super(attributes.merge!(extra_attributes)) - end - - def load_balancer_listeners(extra_attributes = {}) - attributes = { - :model_class => ::ManageIQ::Providers::Azure::NetworkManager::LoadBalancerListener, - } - - super(attributes.merge!(extra_attributes)) - end - - def load_balancer_health_checks(extra_attributes = {}) - attributes = { - :model_class => ::ManageIQ::Providers::Azure::NetworkManager::LoadBalancerHealthCheck, - } - - super(attributes.merge!(extra_attributes)) - end - end -end