Esempio n. 1
0
 def add_or_update_ci_post_save(sender, instance, raw, using, **kwargs):
     from ralph.cmdb.importer import CIImporter
     ci = cdb.CI.get_by_content_object(instance)
     if not ci:
         CIImporter().import_single_object(instance)
         CIImporter().import_single_object_relations(instance)
     else:
         CIImporter().update_single_object(ci, instance)
Esempio n. 2
0
 def setUp(self):
     v = Venture(symbol='test_venture')
     v.save()
     r = VentureRole(name='test_role', venture=v)
     r.save()
     # ci for custom path mapping
     c = Venture(symbol='custom_ci', name='custom_ci')
     c.save()
     for i in (v, r, c):
         CIImporter().import_single_object(i)
         CIImporter().import_single_object_relations(i)
Esempio n. 3
0
File: tests.py Progetto: pb-it/ralph
    def test_fisheye(self):
        """
        Create Venture/Role and import as CI/CIRelations.
        Now import fisheye xml from samples/* files and compare
        if changes are properly saved into the database,
        and reconcilated.
        """
        x = Venture(symbol='test_venture')
        x.save()
        y = VentureRole(name='test_role', venture=x)
        y.save()
        allegro_ci = CI(name='Allegro', type_id=CI_TYPES.VENTURE.id)
        allegro_ci.save()

        ct=ContentType.objects.get_for_model(x)
        test_venture, = CIImporter.import_all_ci([ct],asset_id=x.id)

        ct=ContentType.objects.get_for_model(y)
        test_role, = CIImporter.import_all_ci([ct],asset_id=y.id)

        CIImporter.import_relations(ContentType.objects.get_for_model(y),asset_id=y.id)

        with mock.patch('ralph.cmdb.integration.fisheye.Fisheye') as Fisheye:
            Fisheye.side_effect = MockFisheye
            x = pgi(fisheye_class=Fisheye)
            x.import_git()

        self.assertEqual(CIChangeGit.objects.filter(
            author__contains='*****@*****.**',
            #file_paths__contains='/test_venture'
            ).count(), 2)

        self.assertEqual(CIChange.objects.filter(
            ci=test_role,
            type=CI_CHANGE_TYPES.CONF_GIT.id,
        ).count(), 2)
Esempio n. 4
0
    def test_fisheye(self):
        """
        Create Venture/Role and import as CI/CIRelations.
        Now import fisheye xml from samples/* files and compare
        if changes are properly saved into the database,
        and reconcilated.
        """
        x = Venture(symbol='test_venture')
        x.save()
        y = VentureRole(name='test_role', venture=x)
        y.save()
        allegro_ci = CI(name='Allegro', type_id=CI_TYPES.VENTURE.id)
        allegro_ci.save()

        ct=ContentType.objects.get_for_model(x)
        test_venture, = CIImporter().import_all_ci([ct],asset_id=x.id)

        ct=ContentType.objects.get_for_model(y)
        test_role, = CIImporter().import_all_ci([ct],asset_id=y.id)

        CIImporter().import_relations(ContentType.objects.get_for_model(y),asset_id=y.id)

        with mock.patch('ralph.cmdb.integration.lib.fisheye.Fisheye') as Fisheye:
            Fisheye.side_effect = MockFisheye
            x = pgi(fisheye_class=Fisheye)
            x.import_git()

        self.assertEqual(CIChangeGit.objects.filter(
            author__contains='*****@*****.**',
            #file_paths__contains='/test_venture'
            ).count(), 2)

        self.assertEqual(CIChange.objects.filter(
            ci=test_role,
            type=CI_CHANGE_TYPES.CONF_GIT.id,
        ).count(), 2)
Esempio n. 5
0
    def setUp(self):
        self.user = create_user(
            'api_user',
            '*****@*****.**',
            'password',
            is_superuser=True
        )
        self.puppet_cv = "v%s" % random.randrange(0, 1000)
        self.post_data_puppet = {
            'configuration_version': self.puppet_cv,
            'host': 's11111.dc2',
            'kind': 'apply',
            'status': 'failed',
            'time': '2012-11-14 13:00:00',
        }

        self.git_changeset = "change:%s" % random.randrange(0, 1000)
        self.git_comment = "comment:%s" % random.randrange(0, 1000)
        self.post_data_git = {
            'author': 'Jan Kowalski',
            'changeset': self.git_changeset,
            'comment': self.git_comment,
            'file_paths': '/some/path',
        }

        temp_venture = Venture.objects.create(name='TempTestVenture')
        if settings.AUTOCI:
            self.ci = CI.get_by_content_object(temp_venture)
        else:
            CIImporter().import_single_object(temp_venture)
            self.ci = CI.objects.create(
                name='TempTestVentureCI',
                uid=CI.get_uid_by_content_object(temp_venture),
                type_id=4,
            )

        self.cmdb_new_value = 'nv_%s' % random.randrange(0, 1000)
        self.cmdb_old_value = 'ov_%s' % random.randrange(0, 1000)
        self.post_data_cmdb_change = {
            'ci': '/api/v0.9/ci/%d/' % self.ci.pk,
            'comment': 'test api',
            'field_name': 'child',
            'new_value': self.cmdb_new_value,
            'old_value': self.cmdb_old_value,
            'time': '2012-11-15 12:00:00',
        }
        cache.clear()
Esempio n. 6
0
    def handle(self, *args, **options):
        usage = "usage: %prog --action=[purge|import] \
            --kind=[ci/user-relations/all-relations/system-relations] \
            --content-types"

        if not options.get('action') or options.get(
                'action') not in self.actions:
            print(usage)
            print("Specify valid action: " + ','.join(self.actions))
            return
        if not options.get('kind') or options.get('kind') not in self.kinds:
            print(usage)
            print("You must specify valid kind: " + '|'.join(self.kinds))
            return
        content_types_names = dict([(x.app_label + '.' + x.model, x)
                                    for x in self.content_types])
        content_types_to_import = []
        id_to_import = None
        if options.get('ids'):
            id_to_import = int(options.get('ids'))
        if options.get('content_types'):
            t = options.get('content_types').split(',')
            for ct in t:
                if not content_types_names.get(ct, None):
                    print("Invalid content type: %s: " % ct)
                    return
                else:
                    content_types_to_import.append(
                        content_types_names.get(ct, None))
        else:
            content_types_to_import = self.content_types
        cimp = CIImporter()
        if options.get('action') == 'purge':
            if options.get('kind') == 'ci':
                cimp.purge_all_ci(content_types_to_import)
            elif options.get('kind') == 'all-relations':
                cimp.purge_all_relations()
            elif options.get('kind') == 'user-relations':
                cimp.purge_user_relations()
            elif options.get('kind') == 'system-relations':
                cimp.purge_system_relations()
        elif options.get('action') == 'import':
            if options.get('kind') == 'ci':
                cimp.import_all_ci(content_types_to_import, id_to_import)
            elif options.get('kind') == 'all-relations':
                for content_type in content_types_to_import:
                    cimp.import_relations(content_type, id_to_import)
            else:
                print("Invalid kind for this action")
                return
        logger.info('Ready.')
Esempio n. 7
0
 def handle(self, *args, **options):
     usage = "usage: %prog --action=[purge|import] \
         --kind=[ci/user-relations/all-relations/system-relations] \
         --content-types"
     if not options.get('action') or options.get(
             'action') not in self.actions:
         print(usage)
         print("Specify valid action: " + ','.join(self.actions))
         return
     if not options.get('kind') or options.get('kind') not in self.kinds:
         print(usage)
         print("You must specify valid kind: " + '|'.join(self.kinds))
         return
     content_types_names = dict(
         [(x.app_label + '.' + x.model, x) for x in self.content_types])
     content_types_to_import = []
     id_to_import = None
     if options.get('ids'):
         id_to_import = int(options.get('ids'))
     if options.get('content_types'):
         t = options.get('content_types').split(',')
         for ct in t:
             if not content_types_names.get(ct, None):
                 print("Invalid content type: %s: " % ct)
                 return
             else:
                 content_types_to_import.append(
                     content_types_names.get(ct, None))
     else:
         content_types_to_import = self.content_types
     cimp = CIImporter()
     if options.get('action') == 'purge':
         if options.get('kind') == 'ci':
             cimp.purge_all_ci(content_types_to_import)
         elif options.get('kind') == 'all-relations':
             cimp.purge_all_relations()
         elif options.get('kind') == 'user-relations':
             cimp.purge_user_relations()
         elif options.get('kind') == 'system-relations':
             cimp.purge_system_relations()
     elif options.get('action') == 'import':
         if options.get('kind') == 'ci':
             cimp.import_all_ci(content_types_to_import, id_to_import)
         elif options.get('kind') == 'all-relations':
             for content_type in content_types_to_import:
                 cimp.import_relations(content_type, id_to_import)
         else:
             print("Invalid kind for this action")
             return
     logger.info('Ready.')
Esempio n. 8
0
File: tests.py Progetto: pb-it/ralph
    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(uid='dd-1')
        ci_rack = CI.objects.get(uid='dd-2')
        ci_blade = CI.objects.get(uid='dd-3')

        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)
        # Relations count should be 6
        self.assertEqual(len(CIRelation.objects.all()), 6)
Esempio n. 9
0
    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})
Esempio n. 10
0
    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]),
        )
Esempio n. 11
0
 def setUp(self):
     engine = settings.ISSUETRACKERS['default']['ENGINE']
     if engine != '':
         raise ImproperlyConfigured(
             '''Expected ISSUETRACKERS['default']['ENGINE']='' got: %r''' %
             engine)
     # usual stuff
     self.top_venture = Venture(name='top_venture')
     self.top_venture.save()
     self.child_venture = Venture(name='child_venture',
                                  parent=self.top_venture)
     self.child_venture.save()
     self.role = VentureRole(name='role', venture=self.child_venture)
     self.role.save()
     self.child_role = VentureRole(
         name='child_role',
         venture=self.child_venture,
         parent=self.role,
     )
     self.child_role.save()
     to = CIOwner(
         first_name='Bufallo',
         last_name='Kudłaczek',
     )
     to.save()
     bo = CIOwner(
         first_name='Bill',
         last_name='Bąbelek',
     )
     bo.save()
     ct = ContentType.objects.get_for_model(self.top_venture)
     CIImporter().import_all_ci([ct])
     CIOwnership(owner=to,
                 ci=CI.get_by_content_object(self.child_venture),
                 type=CIOwnershipType.technical.id).save()
     CIOwnership(owner=bo,
                 ci=CI.get_by_content_object(self.child_venture),
                 type=CIOwnershipType.business.id).save()
     dm = self.add_model('DC model sample', DeviceType.data_center.id)
     self.dc = Device.create(sn='sn1', model=dm)
     self.dc.name = 'dc'
     self.dc.save()
     dm = self.add_model('Rack model sample', DeviceType.rack_server.id)
     self.rack = Device.create(
         venture=self.child_venture,
         sn='sn2',
         model=dm,
     )
     self.rack.parent = self.dc
     self.rack.name = 'rack'
     self.rack.save()
     dm = self.add_model('Blade model sample', DeviceType.blade_server.id)
     self.blade = Device.create(venture=self.child_venture,
                                venturerole=self.child_role,
                                sn='sn3',
                                model=dm)
     self.blade.name = 'blade'
     self.blade.parent = self.rack
     self.blade.save()
     self.deployment = Deployment()
     self.deployment.hostname = 'test_host2'
     self.deployment.device = self.blade
     self.deployment.mac = '10:9a:df:6f:af:01'
     self.deployment.ip = '192.168.1.1'
     self.deployment.hostname = 'test'
     self.deployment.save()
Esempio n. 12
0
    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')

        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)
        # Relations count should be 6
        self.assertEqual(len(CIRelation.objects.all()), 6)