From d0dc3e348cab13d7e39eb0251626ca938ebbca5c Mon Sep 17 00:00:00 2001 From: Ladislav Smola Date: Fri, 20 Jan 2017 12:18:17 +0100 Subject: [PATCH] Use :id instead of :_object for NetworkManager saving Use :id instead of :_object for NetworkManager saving. We can also delete processing of cross-manager links ( orchestration_stacks, vms, availability_zones, cloud_tenants) since those will be always AR objects. Only :device can be both Hash and AR object, so it's handled appropriatelly. The ignored cross-manager links processing was needed only when there were CloudManagers without NetworkManagers, so this is not backportable to Darga. Conflicts: app/models/ems_refresh/save_inventory_network.rb --- .../ems_refresh/save_inventory_network.rb | 132 +++++++++--------- 1 file changed, 68 insertions(+), 64 deletions(-) diff --git a/app/models/ems_refresh/save_inventory_network.rb b/app/models/ems_refresh/save_inventory_network.rb index ca970e6eeea..fa7c7f10472 100644 --- a/app/models/ems_refresh/save_inventory_network.rb +++ b/app/models/ems_refresh/save_inventory_network.rb @@ -72,12 +72,6 @@ def save_cloud_networks_inventory(ems, hashes, target = nil) [] end - hashes.each do |h| - %i(cloud_tenant orchestration_stack).each do |relation| - h[relation] = h.fetch_path(relation, :_object) if h.fetch_path(relation, :_object) - end - end - save_inventory_multi(ems.cloud_networks, hashes, deletes, @@ -96,10 +90,6 @@ def save_network_groups_inventory(ems, hashes, target = nil) [] end - hashes.each do |h| - h[:orchestration_stack_id] = h.fetch_path(:orchestration_stack, :id) - end - save_inventory_multi(ems.network_groups, hashes, deletes, @@ -111,14 +101,11 @@ def save_network_groups_inventory(ems, hashes, target = nil) def save_cloud_subnets_inventory(network, hashes) hashes.each do |h| - %i(availability_zone parent_cloud_subnet).each do |relation| - h[relation] = h.fetch_path(relation, :_object) if h.fetch_path(relation, :_object) - end - + h[:cloud_network_id] = h.fetch_path(:cloud_network, :id) if h.key?(:cloud_network) h[:ems_id] = network.ems_id end - save_inventory_multi(network.cloud_subnets, hashes, :use_association, [:ems_ref], nil, [:network_router]) + save_inventory_multi(network.cloud_subnets, hashes, :use_association, [:ems_ref], nil, [:network_router, :cloud_network]) network.save! store_ids_for_new_records(network.cloud_subnets, hashes, :ems_ref) @@ -135,15 +122,16 @@ def save_security_groups_inventory(ems, hashes, target = nil) end hashes.each do |h| - %i(cloud_tenant cloud_network orchestration_stack network_group).each do |relation| - h[relation] = h.fetch_path(relation, :_object) if h.fetch_path(relation, :_object) - end + h[:cloud_network_id] = h.fetch_path(:cloud_network, :id) + h[:network_group_id] = h.fetch_path(:network_group, :id) end - save_inventory_multi(ems.security_groups, hashes, + save_inventory_multi(ems.security_groups, + hashes, deletes, [:ems_ref], - :firewall_rules) + :firewall_rules, + [:cloud_network, :network_group]) store_ids_for_new_records(ems.security_groups, hashes, :ems_ref) # Reset the source_security_group_id for the firewall rules after all @@ -168,12 +156,11 @@ def save_floating_ips_inventory(ems, hashes, target = nil) end hashes.each do |h| - %i(vm cloud_tenant cloud_network network_port).each do |relation| - h[relation] = h.fetch_path(relation, :_object) if h.fetch_path(relation, :_object) - end + h[:cloud_network_id] = h.fetch_path(:cloud_network, :id) + h[:network_port_id] = h.fetch_path(:network_port, :id) end - save_inventory_multi(ems.floating_ips, hashes, deletes, [:ems_ref]) + save_inventory_multi(ems.floating_ips, hashes, deletes, [:ems_ref], nil, [:cloud_network, :network_port]) store_ids_for_new_records(ems.floating_ips, hashes, :ems_ref) end @@ -211,15 +198,16 @@ def save_network_routers_inventory(ems, hashes, target = nil) end hashes.each do |h| - %i(cloud_tenant cloud_network network_group).each do |relation| - h[relation] = h.fetch_path(relation, :_object) if h.fetch_path(relation, :_object) - end + h[:cloud_network_id] = h.fetch_path(:cloud_network, :id) + h[:network_group_id] = h.fetch_path(:network_group, :id) end save_inventory_multi(ems.network_routers, hashes, deletes, - [:ems_ref]) + [:ems_ref], + nil, + [:cloud_network, :network_group]) store_ids_for_new_records(ems.network_routers, hashes, :ems_ref) end @@ -251,15 +239,24 @@ def save_network_ports_inventory(ems, hashes, target = nil, mode = :refresh) hashes.compact! hashes.each do |h| - %i(cloud_tenant device cloud_subnet).each do |relation| - h[relation] = h.fetch_path(relation, :_object) if h.fetch_path(relation, :_object) + device = h.fetch_path(:device) + if device.kind_of?(Hash) + h.delete(:device) + + h[:device_id] = device[:id] + h[:device_type] = device[:type].constantize.base_class.name end - h[:security_groups] = (h.fetch_path(:security_groups) || []).map { |x| x.try(:[], :_object) }.compact.uniq + h[:security_group_ids] = (h.delete(:security_groups) || []).map { |x| x.try(:[], :id) }.compact.uniq h[:source] = mode end - save_inventory_multi(ems.network_ports, hashes, deletes, [:ems_ref], :cloud_subnet_network_ports) + save_inventory_multi(ems.network_ports, + hashes, + deletes, + [:ems_ref], + :cloud_subnet_network_ports, + [:cloud_subnet]) store_ids_for_new_records(ems.network_ports, hashes, :ems_ref) end @@ -268,12 +265,15 @@ def save_cloud_subnet_network_ports_inventory(network_port, hashes) deletes = network_port.cloud_subnet_network_ports.reload.dup hashes.each do |h| - %i(cloud_subnet).each do |relation| - h[relation] = h.fetch_path(relation, :_object) if h.fetch_path(relation, :_object) - end + h[:cloud_subnet_id] = h.fetch_path(:cloud_subnet, :id) end - save_inventory_multi(network_port.cloud_subnet_network_ports, hashes, deletes, [:cloud_subnet, :address]) + save_inventory_multi(network_port.cloud_subnet_network_ports, + hashes, + deletes, + [:cloud_subnet_id, :address], + nil, + [:cloud_subnet]) end def save_load_balancer_pool_members_inventory(ems, hashes, target = nil) @@ -286,12 +286,6 @@ def save_load_balancer_pool_members_inventory(ems, hashes, target = nil) [] end - hashes.each do |h| - %i(vm).each do |relation| - h[relation] = h.fetch_path(relation, :_object) if h.fetch_path(relation, :_object) - end - end - save_inventory_multi(ems.load_balancer_pool_members, hashes, deletes, @@ -321,12 +315,13 @@ def save_load_balancer_pool_member_pools_inventory(load_balancer_pool, hashes) deletes = load_balancer_pool.load_balancer_pool_member_pools.reload.dup hashes.each do |h| - %i(load_balancer_pool_member).each do |relation| - h[relation] = h.fetch_path(relation, :_object) if h.fetch_path(relation, :_object) - end + h[:load_balancer_pool_member_id] = h.fetch_path(:load_balancer_pool_member, :id) end - save_inventory_multi(load_balancer_pool.load_balancer_pool_member_pools, hashes, deletes, + save_inventory_multi(load_balancer_pool.load_balancer_pool_member_pools, + hashes, deletes, + [:load_balancer_pool_member_id], + nil, [:load_balancer_pool_member]) end @@ -341,16 +336,15 @@ def save_load_balancer_listeners_inventory(ems, hashes, target = nil) end hashes.each do |h| - %i(load_balancer).each do |relation| - h[relation] = h.fetch_path(relation, :_object) if h.fetch_path(relation, :_object) - end + h[:load_balancer_id] = h.fetch_path(:load_balancer, :id) end save_inventory_multi(ems.load_balancer_listeners, hashes, deletes, [:ems_ref], - :load_balancer_listener_pools) + :load_balancer_listener_pools, + [:load_balancer]) store_ids_for_new_records(ems.load_balancer_listeners, hashes, :ems_ref) end @@ -358,12 +352,15 @@ def save_load_balancer_listener_pools_inventory(load_balancer_listener, hashes) deletes = load_balancer_listener.load_balancer_listener_pools.reload.dup hashes.each do |h| - %i(load_balancer_pool).each do |relation| - h[relation] = h.fetch_path(relation, :_object) if h.fetch_path(relation, :_object) - end + h[:load_balancer_pool_id] = h.fetch_path(:load_balancer_pool, :id) end - save_inventory_multi(load_balancer_listener.load_balancer_listener_pools, hashes, deletes, [:load_balancer_pool]) + save_inventory_multi(load_balancer_listener.load_balancer_listener_pools, + hashes, + deletes, + [:load_balancer_pool_id], + nil, + [:load_balancer_pool]) end def save_load_balancer_health_checks_inventory(ems, hashes, target = nil) @@ -377,16 +374,16 @@ def save_load_balancer_health_checks_inventory(ems, hashes, target = nil) end hashes.each do |h| - %i(load_balancer load_balancer_listener).each do |relation| - h[relation] = h.fetch_path(relation, :_object) if h.fetch_path(relation, :_object) - end + h[:load_balancer_id] = h.fetch_path(:load_balancer, :id) + h[:load_balancer_listener_id] = h.fetch_path(:load_balancer_listener, :id) end save_inventory_multi(ems.load_balancer_health_checks, hashes, deletes, [:ems_ref], - :load_balancer_health_check_members) + :load_balancer_health_check_members, + [:load_balancer, :load_balancer_listener]) store_ids_for_new_records(ems.load_balancer_health_checks, hashes, :ems_ref) end @@ -394,19 +391,26 @@ def save_load_balancer_health_check_members_inventory(load_balancer_health_check deletes = load_balancer_health_check.load_balancer_health_check_members.reload.dup hashes.each do |h| - %i(load_balancer_pool_member).each do |relation| - h[relation] = h.fetch_path(relation, :_object) if h.fetch_path(relation, :_object) - end + h[:load_balancer_pool_member_id] = h.fetch_path(:load_balancer_pool_member, :id) end - save_inventory_multi(load_balancer_health_check.load_balancer_health_check_members, hashes, deletes, + save_inventory_multi(load_balancer_health_check.load_balancer_health_check_members, + hashes, + deletes, + [:load_balancer_pool_member_id], + nil, [:load_balancer_pool_member]) end def link_cloud_subnets_to_network_routers(hashes) + return if hashes.blank? + + cloud_subnets = CloudSubnet.where(:id => hashes.map { |x| x[:id] }.compact.uniq).find_each.index_by(&:id) + hashes.each do |hash| - network_router = hash.fetch_path(:network_router, :_object) - hash[:_object].update_attributes(:network_router => network_router) + network_router = hash.fetch_path(:network_router, :id) + cloud_subnet = cloud_subnets[hash[:id]] + cloud_subnet.update_attributes(:network_router_id => network_router) if cloud_subnet end end end