def get(self, *args, **kwargs): ci_id = self.request.GET.get('ci') self.rows = [] if ci_id: ci_names = dict([(x.id, x.name) for x in CI.objects.all()]) i = ImpactCalculator() st, pre = i.find_affected_nodes(int(ci_id)) nodes = [( key, ci_names[key], get_icon_for(CI.objects.get(pk=key))) for key in st.keys()] relations = [dict( child=x, parent=st.get(x), parent_name=ci_names[x], type=i.graph.edge_attributes((st.get(x), x))[0], child_name=ci_names[st.get(x)]) for x in st.keys() if x and st.get(x)] self.graph_data = dict( nodes=nodes, relations=relations) self.rows = [dict( icon=get_icon_for(CI.objects.get(pk=x)), ci=CI.objects.get(pk=x)) for x in pre] return super(BaseCMDBView, self).get(*args, **kwargs)
def test_import_devices(self): """ Test importing: - prefixes - parenthesis - layers Structure: top_venture <--child_venture <---role <---child_role <---- dc <----rack <----blade """ # ventures and roles objs = [self.top_venture, self.child_venture, self.role, self.child_role] for o in objs: ct = ContentType.objects.get_for_model(o) CIImporter().import_all_ci([ct], asset_id=o.id) for o in objs: ct = ContentType.objects.get_for_model(o) CIImporter().import_relations(ct, asset_id=o.id) # devices objs = [self.dc, self.rack, self.blade] # create ci for o in objs: ct = ContentType.objects.get_for_model(o) CIImporter().import_all_ci([ct], asset_id=o.id) # create relations for o in objs: ct = ContentType.objects.get_for_model(o) CIImporter().import_relations(ct, asset_id=o.id) # All ci should be in Hardware layer ci_dc = CI.objects.get(name='dc') ci_rack = CI.objects.get(name='rack') ci_blade = CI.objects.get(name='blade') ci_venture = CI.objects.get(name='child_venture') ci_role = CI.objects.get(name='child_role') self.assertEqual(ci_dc.layers.select_related()[0].name, 'Hardware') self.assertEqual(ci_rack.layers.select_related()[0].name, 'Hardware') self.assertEqual(ci_blade.layers.select_related()[0].name, 'Hardware') # Reimport of relations should not raise Exception, # and should not change relations count cis = [] for o in objs: ct = ContentType.objects.get_for_model(o) cis.extend( CIImporter().import_all_ci([ct], asset_id=o.id)) # Rack should be inside DC try: CIRelation.objects.get( parent=ci_dc, child=ci_rack, type=CI_RELATION_TYPES.CONTAINS.id) except CIRelation.DoesNotExist: self.fail('Cant find relation %s %s %s' % (ci_dc, ci_rack)) # Blade should be inside Rack CIRelation.objects.get( parent=ci_rack, child=ci_blade, type=CI_RELATION_TYPES.CONTAINS.id) # every device in composition chain should have relation # to Venture and Role as well. # test relations - # dc - no role no venture # rack - venture, no role # blade - venture and role venture_rels = CIRelation.objects.filter( child__in=[ci_dc, ci_rack, ci_blade], parent=ci_venture, type=CI_RELATION_TYPES.CONTAINS.id, ) # dc is *not* bound to venture self.assertEqual( set([(rel.parent.name, rel.child.name, rel.type) for rel in venture_rels]), set([(u'child_venture', u'rack', 1), (u'child_venture', u'blade', 1)]) ) role_rels = CIRelation.objects.filter( child__in=[ci_dc, ci_rack, ci_blade], parent=ci_role, type=CI_RELATION_TYPES.HASROLE.id, ) # only bottom level device has role, so one relation is made self.assertEqual( set([(rel.parent.name, rel.child.name, rel.type) for rel in role_rels]), set([(u'child_role', u'blade', 3)]), ) from ralph.cmdb.graphs import ImpactCalculator # summarize relations. self.assertEqual(CIRelation.objects.count(), 9) # calculate impact/spanning tree for CI structure root_ci = CI.objects.get(name='rack') calc = ImpactCalculator(root_ci) self.assertEqual( calc.find_affected_nodes(root_ci.id), ({2: 6, 5: 6, 6: None, 7: 6}, [6, 7, 2, 5]), )
def test_import_devices(self): """ Test importing: - prefixes - parenthesis - layers Structure: top_venture <--child_venture <---role <---child_role <----dc <----rack <----blade """ # Import all for o in self.objs: ct = ContentType.objects.get_for_model(o) CIImporter().import_all_ci([ct], asset_id=o.id) CIImporter().import_relations(ct, asset_id=o.id) # All ci should be in Hardware layer ci_dc = CI.objects.get(name='dc') ci_rack = CI.objects.get(name='rack') ci_blade = CI.objects.get(name='blade') ci_deleted = CI.objects.get(name='deleted blade') ci_top_venture = CI.objects.get(name='top_venture') ci_venture = CI.objects.get(name='child_venture') ci_role = CI.objects.get(name='child_role') ci_service1 = CI.objects.get(name='Service 1') ci_bline1 = CI.objects.get(name='INT-1') ci_bline2 = CI.objects.get(name='INT-2') self.assertEqual(ci_dc.layers.select_related()[0].name, 'Hardware') self.assertEqual(ci_rack.layers.select_related()[0].name, 'Hardware') self.assertEqual(ci_blade.layers.select_related()[0].name, 'Hardware') # All cis except soft-deleted devices should be active self.assertEqual(ci_dc.state, CI_STATE_TYPES.ACTIVE) self.assertEqual(ci_blade.state, CI_STATE_TYPES.ACTIVE) self.assertEqual(ci_deleted.state, CI_STATE_TYPES.INACTIVE) # Reimport of relations should not raise Exception, # and should not change relations count cis = [] for o in self.objs: ct = ContentType.objects.get_for_model(o) cis.extend( CIImporter().import_all_ci([ct], asset_id=o.id), ) # Rack should be inside DC try: CIRelation.objects.get( parent=ci_dc, child=ci_rack, type=CI_RELATION_TYPES.CONTAINS.id) except CIRelation.DoesNotExist: self.fail('Cant find relation %s %s' % (ci_dc, ci_rack)) # Blade should be inside Rack CIRelation.objects.get( parent=ci_rack, child=ci_blade, type=CI_RELATION_TYPES.CONTAINS.id, ) # every device in composition chain should have relation # to Venture and Role as well. # test relations - # dc - no role no venture # rack - venture, no role # blade - venture and role venture_rels = CIRelation.objects.filter( child__in=[ci_dc, ci_rack, ci_blade], parent=ci_venture, type=CI_RELATION_TYPES.CONTAINS.id, ) # dc is *not* bound to venture self.assertEqual( set( [ (rel.parent.name, rel.child.name, rel.type) for rel in venture_rels ] ), set([(u'child_venture', u'rack', 1), (u'child_venture', u'blade', 1)]) ) role_rels = CIRelation.objects.filter( child__in=[ci_dc, ci_rack, ci_blade], parent=ci_role, type=CI_RELATION_TYPES.HASROLE.id, ) # only bottom level device has role, so one relation is made self.assertEqual( set( [ (rel.parent.name, rel.child.name, rel.type) for rel in role_rels ] ), set([(u'child_role', u'blade', 3)]), ) # Service 1 should be in BusinessLine 1 service_rels = CIRelation.objects.filter( child=ci_service1, parent__in={ci_bline1, ci_bline2}, type=CI_RELATION_TYPES.CONTAINS.id, ) self.assertSetEqual( set( [ (rel.parent.name, rel.child.name, rel.type) for rel in service_rels ] ), {('INT-1', 'Service 1', 1)}, ) # child_venture should be in parent_venture venture_rel = CIRelation.objects.get( parent=ci_top_venture, child=ci_venture, ) self.assertEqual(venture_rel.type, CI_RELATION_TYPES.CONTAINS) from ralph.cmdb.graphs import ImpactCalculator # summarize relations. self.assertEqual(CIRelation.objects.count(), 13) # calculate impact/spanning tree for CI structure root_ci = CI.objects.get(name='rack') calc = ImpactCalculator(root_ci) map, nodes = calc.find_affected_nodes(root_ci.id) self.assertEqual(map, {2: 6, 5: 6, 6: None, 7: 6, 10: 6}) self.assertSetEqual(set(nodes), {2, 5, 6, 7, 10})
def test_import_devices(self): """ Test importing: - prefixes - parenthesis - layers Structure: top_venture <--child_venture <---role <---child_role <----dc <----rack <----blade """ # ventures and roles objs = [ self.top_venture, self.child_venture, self.role, self.child_role ] for o in objs: ct = ContentType.objects.get_for_model(o) CIImporter().import_all_ci([ct], asset_id=o.id) for o in objs: ct = ContentType.objects.get_for_model(o) CIImporter().import_relations(ct, asset_id=o.id) # devices objs = [self.dc, self.rack, self.blade] # create ci for o in objs: ct = ContentType.objects.get_for_model(o) CIImporter().import_all_ci([ct], asset_id=o.id) # create relations for o in objs: ct = ContentType.objects.get_for_model(o) CIImporter().import_relations(ct, asset_id=o.id) # All ci should be in Hardware layer ci_dc = CI.objects.get(name='dc') ci_rack = CI.objects.get(name='rack') ci_blade = CI.objects.get(name='blade') ci_venture = CI.objects.get(name='child_venture') ci_role = CI.objects.get(name='child_role') self.assertEqual(ci_dc.layers.select_related()[0].name, 'Hardware') self.assertEqual(ci_rack.layers.select_related()[0].name, 'Hardware') self.assertEqual(ci_blade.layers.select_related()[0].name, 'Hardware') # Reimport of relations should not raise Exception, # and should not change relations count cis = [] for o in objs: ct = ContentType.objects.get_for_model(o) cis.extend(CIImporter().import_all_ci([ct], asset_id=o.id), ) # Rack should be inside DC try: CIRelation.objects.get(parent=ci_dc, child=ci_rack, type=CI_RELATION_TYPES.CONTAINS.id) except CIRelation.DoesNotExist: self.fail('Cant find relation %s %s %s' % (ci_dc, ci_rack)) # Blade should be inside Rack CIRelation.objects.get( parent=ci_rack, child=ci_blade, type=CI_RELATION_TYPES.CONTAINS.id, ) # every device in composition chain should have relation # to Venture and Role as well. # test relations - # dc - no role no venture # rack - venture, no role # blade - venture and role venture_rels = CIRelation.objects.filter( child__in=[ci_dc, ci_rack, ci_blade], parent=ci_venture, type=CI_RELATION_TYPES.CONTAINS.id, ) # dc is *not* bound to venture self.assertEqual( set([(rel.parent.name, rel.child.name, rel.type) for rel in venture_rels]), set([(u'child_venture', u'rack', 1), (u'child_venture', u'blade', 1)])) role_rels = CIRelation.objects.filter( child__in=[ci_dc, ci_rack, ci_blade], parent=ci_role, type=CI_RELATION_TYPES.HASROLE.id, ) # only bottom level device has role, so one relation is made self.assertEqual( set([(rel.parent.name, rel.child.name, rel.type) for rel in role_rels]), set([(u'child_role', u'blade', 3)]), ) from ralph.cmdb.graphs import ImpactCalculator # summarize relations. self.assertEqual(CIRelation.objects.count(), 9) # calculate impact/spanning tree for CI structure root_ci = CI.objects.get(name='rack') calc = ImpactCalculator(root_ci) self.assertEqual( calc.find_affected_nodes(root_ci.id), ({ 2: 6, 5: 6, 6: None, 7: 6 }, [6, 7, 2, 5]), )