Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inventory Enterprise (Nuage) into CloudTenant (MIQ) #92

Merged
merged 1 commit into from
May 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,8 @@
class ManageIQ::Providers::Nuage::Inventory::Collector::NetworkManager < ManageIQ::Providers::Nuage::Inventory::Collector
def cloud_tenants
_cloud_tenants.values
end

def cloud_subnets
return @cloud_subnets if @cloud_subnets.any?
@cloud_subnets = vsd_client.get_subnets
Expand All @@ -9,34 +13,48 @@ 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
_zones.values
end

def domains
return @domains if @domains.any?
@domains = vsd_client.get_domains.map { |domain| [domain['ID'], domain] } .to_h
_domains.values
end

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[ems_ref]
end

def zone(ems_ref)
zones[ems_ref]
_zones[ems_ref]
end

def domain(ems_ref)
domains[ems_ref]
_domains[ems_ref]
end

private

def _cloud_tenants
return @cloud_tenants if @cloud_tenants.any?
@cloud_tenants = hash_by_id(vsd_client.get_enterprises)
end

def _zones
return @zones if @zones.any?
@zones = hash_by_id(vsd_client.get_zones)
end

def _domains
return @domains if @domains.any?
@domains = hash_by_id(vsd_client.get_domains)
end

def hash_by_id(list)
list.map { |el| [el['ID'], el] }.to_h
end
end
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
Loading