def exec_module(self, **kwargs): state = kwargs.pop('state', 'present') for name, value in kwargs.items(): setattr(self, name, value) # Validate whether the element type is valid entry_points = Search.object_types() for element in self.elements: for typeof, element_data in element.items(): if typeof not in entry_points: self.fail(msg='The specified element type: %s is not valid. ' 'Data provided: %s' % (typeof, element)) if 'name' not in element_data: self.fail(msg='The name field is required to operate on all ' 'elements. Data provided: %s' % element) try: if self.check_mode: return self.results for element in self.elements: for typeof, data in element.items(): try: instance, updated, created = lookup_class(typeof).update_or_create( with_status=True, **data) action = 'none' if updated: action = 'updated' elif created: action = 'created' if updated or created: self.results['changed'] = True self.results['state'].append(dict(name=instance.name, typeof=instance.typeof, action=action)) except SMCException as e: self.results['state'].append(dict(name=data.get('name'), typeof=typeof, action='error', reason=str(e))) except SMCException as err: self.fail(msg=str(err), exception=traceback.format_exc()) return self.results
def test_search_collections(self): entry_points = Search.object_types() self.assertIsInstance(entry_points, list) self.assertTrue(len(entry_points) > 0) # Object type with defined class results = list(Search('host').objects.all()) self.assertIsInstance(results, list) for result in results: self.assertIsInstance(result, Host) # Object type without defined class results = list(Search('ids_alert').objects.all()) self.assertIsInstance(results, list) for result in results: self.assertIsInstance(result, Element) with self.assertRaises(UnsupportedEntryPoint): Search('foo').objects.all() self.assertIsInstance(Search('host').objects, CollectionManager) # Access collection through element directly results = list(TCPService.objects.all()) # @UndefinedVariable self.assertIsInstance(results, list) for result in results: self.assertIsInstance(result, TCPService) filtered = list( TCPService.objects.filter('HTTP')) # @UndefinedVariable self.assertIsInstance(filtered, list) for result in filtered: self.assertIsInstance(result, TCPService) # No results result = list(Search('host').objects.filter('blahblah')) self.assertFalse(result) # Test limit result = list(TCPService.objects.all().limit(5)) # @UndefinedVariable self.assertTrue(len(result) == 5) # Get iterator and test collection iterator = TCPService.objects.iterator() # @UndefinedVariable results = list(iterator.all()) self.assertTrue(results) self.assertTrue(len(results) == iterator.count()) results = list(iterator.all().limit(5)) self.assertTrue(len(result) == 5) self.assertIsNotNone(iterator.first()) self.assertIsNotNone(iterator.last()) self.assertIsNotNone(iterator.first()) self.assertIsNotNone(iterator.last()) self.assertTrue(iterator.count() > 0) results = iterator.filter('HTTP') self.assertIsInstance(results, ElementCollection) self.assertTrue(list(results)) #limit in manager result = Host.objects.limit(3) # @UndefinedVariable self.assertTrue(len(list(result)) == 3) # Filter based on multiple entry points at once for x in list(Search('router,host').objects.all()): self.assertTrue(isinstance(x, (Host, Router))) iterator = Host.objects self.assertIsInstance(iterator, CollectionManager) Router.create('R1', address='10.10.10.1') Router.create('R2', address='110.10.10.1') router = Router.objects.iterator() results = list(router.filter('10.10.10.1')) self.assertTrue(len(results) == 2) query1 = router.filter(address='10.10.10.1') self.assertTrue(query1.count() == 2) # Not filtered until iterating results = list(query1) self.assertTrue(len(results) == 1) self.assertEqual(query1.first().name, 'R1') h1 = Host.create(name='host1', address='1.1.1.1', comment='host1comment') h2 = Host.create(name='host2', address='1.1.1.1') results = list( Host.objects.filter(address='1.1.1.1', comment='host1comment')) self.assertTrue(len(results) == 1) self.assertEqual(results[0].name, 'host1') results = list(Host.objects.filter(address='1.1.1.1', comment='foo')) self.assertFalse(results) h1.delete() h2.delete() # Test batch function it = TCPService.objects.batch(4) for _ in range(1, 5): self.assertTrue(len(next(it)) == 4) # Test batch running out it = Host.objects.batch(20) for x in it: self.assertTrue(len(x) <= 20) # Test cloning when using filter_key it = Router.objects.iterator() query1 = it.filter(address='10.10.10.1').limit(1) element = query1.first() self.assertEqual(element.name, 'R1') result = Router.objects.first() self.assertIsNotNone(result) for router in ['R1', 'R2']: Router(router).delete()