Skip to content

Commit

Permalink
Inventory Enterprise (Nuage) into CloudTenant (MIQ)
Browse files Browse the repository at this point in the history
With this commit we inventory Enterprise into CloudTenant model
instead into NetworkGroup model. We capture its name and description
and hook it to the network manager.

RFE BZ: https://bugzilla.redhat.com/show_bug.cgi?id=1574903

Signed-off-by: Miha Pleško <[email protected]>
  • Loading branch information
miha-plesko committed May 28, 2018
1 parent cdd28f9 commit 06b6a08
Show file tree
Hide file tree
Showing 20 changed files with 173 additions and 180 deletions.
2 changes: 1 addition & 1 deletion app/models/manageiq/providers/nuage/inventory/collector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ def initialize(_manager, _target)
end

def initialize_inventory_sources
@cloud_tenants = []
@cloud_subnets = []
@security_groups = []
@network_groups = []
@zones = {}
@domains = {}
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
class ManageIQ::Providers::Nuage::Inventory::Collector::NetworkManager < ManageIQ::Providers::Nuage::Inventory::Collector
def cloud_tenants
return @cloud_tenants if @cloud_tenants.any?
@cloud_tenants = vsd_client.get_enterprises
end

def cloud_subnets
return @cloud_subnets if @cloud_subnets.any?
@cloud_subnets = vsd_client.get_subnets
Expand All @@ -9,11 +14,6 @@ def security_groups
@security_groups = vsd_client.get_policy_groups
end

def network_groups
return @network_groups if @network_groups.any?
@network_groups = vsd_client.get_enterprises
end

def zones
return @zones if @zones.any?
@zones = vsd_client.get_zones.map { |zone| [zone['ID'], zone] } .to_h
Expand All @@ -28,8 +28,8 @@ def security_group(ems_ref)
security_groups.find { |sg| sg['ID'] == ems_ref }
end

def network_group(ems_ref)
network_groups.find { |ng| ng['ID'] == ems_ref }
def cloud_tenant(ems_ref)
cloud_tenants.find { |tenant| tenant['ID'] == ems_ref }
end

def zone(ems_ref)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ def security_groups
@security_groups_map.values.compact
end

def network_groups
return [] if references(:network_groups).blank?
references(:network_groups).collect { |ems_ref| network_group(ems_ref) }
@network_groups_map.values.compact
def cloud_tenants
return [] if references(:cloud_tenants).blank?
references(:cloud_tenants).collect { |ems_ref| cloud_tenant(ems_ref) }
@cloud_tenant_map.values.compact
end

def cloud_subnet(ems_ref)
Expand All @@ -37,9 +37,9 @@ def security_group(ems_ref)
@security_groups_map[ems_ref] = safe_call { vsd_client.get_policy_group(ems_ref) }
end

def network_group(ems_ref)
return @network_groups_map[ems_ref] if @network_groups_map.key?(ems_ref)
@network_groups_map[ems_ref] = safe_call { vsd_client.get_enterprise(ems_ref) }
def cloud_tenant(ems_ref)
return @cloud_tenant_map[ems_ref] if @cloud_tenant_map.key?(ems_ref)
@cloud_tenant_map[ems_ref] = safe_call { vsd_client.get_enterprise(ems_ref) }
end

def zone(ems_ref)
Expand All @@ -55,41 +55,41 @@ def domain(ems_ref)
private

def initialize_cache
@cloud_subnets_map = {}
@security_groups_map = {}
@network_groups_map = {}
@zones_map = {}
@domains_map = {}
@domains_per_network_group = {}
@cloud_subnets_map = {}
@security_groups_map = {}
@cloud_tenant_map = {}
@zones_map = {}
@domains_map = {}
@domains_per_tenant = {}
end

def domains_for_network_group(network_group_ems_ref)
ems_ref = network_group_ems_ref
@domains_per_network_group[ems_ref] ||= safe_list { vsd_client.get_domains_for_enterprise(ems_ref) }
@domains_per_network_group[ems_ref].each { |d| @domains_map[d['ID']] = d }
@domains_per_network_group[ems_ref]
def domains_for_tenant(tenant_ems_ref)
ems_ref = tenant_ems_ref
@domains_per_tenant[ems_ref] ||= safe_list { vsd_client.get_domains_for_enterprise(ems_ref) }
@domains_per_tenant[ems_ref].each { |d| @domains_map[d['ID']] = d }
@domains_per_tenant[ems_ref]
end

def cloud_subnets_for_network_group(network_group_ems_ref)
domains_for_network_group(network_group_ems_ref).each_with_object([]) do |d, arr|
def cloud_subnets_for_tenant(tenant_ems_ref)
domains_for_tenant(tenant_ems_ref).each_with_object([]) do |d, arr|
arr.push(safe_list { vsd_client.get_subnets_for_domain(d['ID']) })
end.flatten(1)
end

def security_groups_for_network_group(network_group_ems_ref)
domains_for_network_group(network_group_ems_ref).each_with_object([]) do |d, arr|
def security_groups_for_tenant(tenant_ems_ref)
domains_for_tenant(tenant_ems_ref).each_with_object([]) do |d, arr|
arr.push(safe_list { vsd_client.get_policy_groups_for_domain(d['ID']) })
end.flatten(1)
end

def network_group_ems_ref_for_cloud_subnet(cloud_subnet_ems_ref)
def tenant_ems_ref_for_cloud_subnet(cloud_subnet_ems_ref)
cloud_subnet = cloud_subnet(cloud_subnet_ems_ref)
return nil if cloud_subnet.nil?
domain_ems_ref = zone(cloud_subnet['parentID'])['parentID']
domain(domain_ems_ref)['parentID']
end

def network_group_ems_ref_for_security_group(security_group_ems_ref)
def tenant_ems_ref_for_security_group(security_group_ems_ref)
security_group = security_group(security_group_ems_ref)
return nil if security_group.nil?
domain(security_group['parentID'])['parentID']
Expand All @@ -106,8 +106,8 @@ def parse_targets!
add_simple_target!(:cloud_subnets, t.ems_ref)
when SecurityGroup
add_simple_target!(:security_groups, t.ems_ref)
when NetworkGroup
add_simple_target!(:network_groups, t.ems_ref)
when CloudTenant
add_simple_target!(:cloud_tenants, t.ems_ref)
end
end
end
Expand All @@ -124,21 +124,21 @@ def infer_related_ems_refs!
end

def infer_related_ems_refs_db!
if references(:network_groups).any?
network_groups = manager.network_groups.where(:ems_ref => references(:network_groups))
if references(:cloud_tenants).any?
tenants = manager.cloud_tenants.where(:ems_ref => references(:cloud_tenants))
.includes(:cloud_subnets, :security_groups)
network_groups.each do |ng|
ng.cloud_subnets.collect(&:ems_ref).compact.each { |ems_ref| add_simple_target!(:cloud_subnets, ems_ref) }
ng.security_groups.collect(&:ems_ref).compact.each { |ems_ref| add_simple_target!(:security_groups, ems_ref) }
tenants.each do |tenant|
tenant.cloud_subnets.collect(&:ems_ref).compact.each { |ems_ref| add_simple_target!(:cloud_subnets, ems_ref) }
tenant.security_groups.collect(&:ems_ref).compact.each { |ems_ref| add_simple_target!(:security_groups, ems_ref) }
end
end

if references(:cloud_subnets).any?
cloud_subnets = manager.cloud_subnets.where(:ems_ref => references(:cloud_subnets))
cloud_subnets.each do |cloud_subnet|
next if cloud_subnet.network_group.nil?
add_simple_target!(:network_groups, cloud_subnet.network_group.ems_ref)
cloud_subnet.network_group.security_groups.each do |security_group|
next if cloud_subnet.cloud_tenant.nil?
add_simple_target!(:cloud_tenants, cloud_subnet.cloud_tenant.ems_ref)
cloud_subnet.cloud_tenant.security_groups.each do |security_group|
add_simple_target!(:security_groups, security_group.ems_ref)
end
end
Expand All @@ -147,33 +147,33 @@ def infer_related_ems_refs_db!
if references(:security_groups).any?
security_groups = manager.security_groups.where(:ems_ref => references(:security_groups))
security_groups.each do |security_group|
add_simple_target!(:network_groups, security_group.network_group.ems_ref) unless security_group.network_group.nil?
add_simple_target!(:cloud_tenants, security_group.cloud_tenant.ems_ref) unless security_group.cloud_tenant.nil?
end
end
end

def infer_related_ems_refs_api!
references(:network_groups).each do |ng_ems_ref|
cloud_subnets_for_network_group(ng_ems_ref).each do |cloud_subnet|
references(:cloud_tenants).each do |tenant_ems_ref|
cloud_subnets_for_tenant(tenant_ems_ref).each do |cloud_subnet|
add_simple_target!(:cloud_subnets, cloud_subnet['ID'])
end

security_groups_for_network_group(ng_ems_ref).each do |policy_group|
security_groups_for_tenant(tenant_ems_ref).each do |policy_group|
add_simple_target!(:security_groups, policy_group['ID'])
end
end

references(:cloud_subnets).each do |cs_ems_ref|
ng_ems_ref = network_group_ems_ref_for_cloud_subnet(cs_ems_ref)
next if ng_ems_ref.nil?
add_simple_target!(:network_groups, ng_ems_ref)
security_groups_for_network_group(ng_ems_ref).each do |policy_group|
tenant_ems_ref = tenant_ems_ref_for_cloud_subnet(cs_ems_ref)
next if tenant_ems_ref.nil?
add_simple_target!(:cloud_tenants, tenant_ems_ref)
security_groups_for_tenant(tenant_ems_ref).each do |policy_group|
add_simple_target!(:security_groups, policy_group['ID'])
end
end

references(:security_groups).each do |sg_ems_ref|
add_simple_target!(:network_groups, network_group_ems_ref_for_security_group(sg_ems_ref))
add_simple_target!(:cloud_tenants, tenant_ems_ref_for_security_group(sg_ems_ref))
end
end

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
class ManageIQ::Providers::Nuage::Inventory::Parser::NetworkManager < ManageIQ::Providers::Nuage::Inventory::Parser
def parse
cloud_tenants
cloud_subnets
security_groups
network_groups
end

private

def cloud_tenants
collector.cloud_tenants.each do |enterprise|
persister.cloud_tenants.find_or_build(enterprise['ID']).assign_attributes(
:name => enterprise['name'],
:description => enterprise['description']
)
end
end

def cloud_subnets
collector.cloud_subnets.each do |subnet|
extra = map_extra_attributes(subnet['parentID']) || {}
Expand All @@ -17,7 +26,7 @@ def cloud_subnets
:gateway => subnet['gateway'],
:dhcp_enabled => false,
:extra_attributes => extra,
:network_group => persister.network_groups.lazy_find(extra["enterprise_id"])
:cloud_tenant => persister.cloud_tenants.lazy_find(extra["enterprise_id"])
)
end
end
Expand All @@ -28,17 +37,8 @@ def security_groups
domain = collector.domain(domain_id) || {}

persister.security_groups.find_or_build(sg['ID']).assign_attributes(
:name => sg['name'],
:network_group => persister.network_groups.lazy_find(domain['parentID'])
)
end
end

def network_groups
collector.network_groups.each do |ng|
persister.network_groups.find_or_build(ng['ID']).assign_attributes(
:name => ng['name'],
:status => 'active'
:name => sg['name'],
:cloud_tenant => persister.cloud_tenants.lazy_find(domain['parentID'])
)
end
end
Expand All @@ -49,12 +49,12 @@ def map_extra_attributes(zone_id)
domain_id = zone['parentID']
domain = collector.domain(domain_id)
return unless domain
network_group_id = domain['parentID']
network_group = collector.network_group(network_group_id)
return unless network_group
tenant_id = domain['parentID']
tenant = collector.cloud_tenant(tenant_id)
return unless tenant

{
"enterprise_name" => network_group['name'],
"enterprise_name" => tenant['name'],
"enterprise_id" => domain['parentID'],
"domain_name" => domain['name'],
"domain_id" => domain_id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class ManageIQ::Providers::Nuage::Inventory::Persister::NetworkManager < ManageI
def initialize_inventory_collections
add_inventory_collections(
network,
%i(cloud_subnets security_groups network_groups)
%i(cloud_tenants cloud_subnets security_groups)
)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class ManageIQ::Providers::Nuage::Inventory::Persister::TargetCollection < Manag
def initialize_inventory_collections
add_inventory_collections_with_references(
network,
%i(cloud_subnets security_groups network_groups),
%i(cloud_tenants cloud_subnets security_groups),
:parent => manager
)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def cloud_subnets(extra_attributes = {})
:gateway,
:dhcp_enabled,
:extra_attributes,
:network_group
:cloud_tenant
]
}

Expand All @@ -28,26 +28,30 @@ def security_groups(extra_attributes = {})
:ems_id,
:ems_ref,
:name,
:network_group
:cloud_tenant
]
}

super(attributes.merge!(extra_attributes))
end

def network_groups(extra_attributes = {})
def cloud_tenants(extra_attributes = {})
attributes = {
:model_class => ::ManageIQ::Providers::Nuage::NetworkManager::NetworkGroup,
:model_class => ::ManageIQ::Providers::Nuage::NetworkManager::CloudTenant,
:association => :cloud_tenants,
:builder_params => {
:ems_id => ->(persister) { persister.manager.id },
},
:inventory_object_attributes => [
:type,
:ems_id,
:ems_ref,
:name,
:status
:description
]
}

super(attributes.merge!(extra_attributes))
attributes.merge!(extra_attributes)
end
end
end
6 changes: 5 additions & 1 deletion app/models/manageiq/providers/nuage/network_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ class ManageIQ::Providers::Nuage::NetworkManager < ManageIQ::Providers::NetworkM
require_nested :RefreshWorker
require_nested :Refresher
require_nested :VsdClient
require_nested :CloudTenant
require_nested :CloudSubnet
require_nested :SecurityGroup
require_nested :NetworkGroup

supports :ems_network_new

Expand Down Expand Up @@ -44,4 +44,8 @@ def self.event_monitor_class
def name
self[:name]
end

def cloud_tenants
::CloudTenant.where(:ems_id => id)
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class ManageIQ::Providers::Nuage::NetworkManager::CloudTenant < ::CloudTenant
end
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ def parse_ems_event_targets(event)

case event.full_data["entityType"]
when 'enterprise'
add_targets(target_collection, :network_groups, event.full_data['entities'])
add_targets(target_collection, :cloud_tenants, event.full_data['entities'])
when 'subnet'
add_targets(target_collection, :cloud_subnets, event.full_data['entities'])
when 'policygroup'
add_targets(target_collection, :security_groups, event.full_data['entities'])
when 'domain'
add_targets(target_collection, :network_groups, event.full_data['entities'], :key => 'enterpriseID')
add_targets(target_collection, :cloud_tenants, event.full_data['entities'], :key => 'enterpriseID')
end

target_collection.targets
Expand Down

This file was deleted.

3 changes: 3 additions & 0 deletions spec/factories/cloud_tenant.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FactoryGirl.define do
factory :cloud_tenant_nuage, :class => "ManageIQ::Providers::Nuage::NetworkManager::CloudTenant"
end
3 changes: 0 additions & 3 deletions spec/factories/network_group.rb

This file was deleted.

Loading

0 comments on commit 06b6a08

Please sign in to comment.