def test_get_middleware_by_uri(self): uri = '%s://' % TestUnregisteredMiddleware.__protocol__ self.assertRaises( Middleware.Error, Middleware.get_middleware_by_uri, uri) uri = '%s://' % TestRegisteredMiddleware.__protocol__ middleware = Middleware.get_middleware_by_uri(uri) self.assertEqual(type(middleware), TestRegisteredMiddleware) self.assertEqual(middleware.data_scope, DEFAULT_DATA_SCOPE) uri = '%s%s%s://' % ( TestRegisteredWithDataTypeMiddleware.__protocol__, SCHEME_SEPARATOR, TestRegisteredWithDataTypeMiddleware.__datatype__) middleware = Middleware.get_middleware_by_uri(uri) self.assertEqual( type(middleware), TestRegisteredWithDataTypeMiddleware) self.assertEqual(middleware.data_scope, DEFAULT_DATA_SCOPE) self.assertEqual(middleware.data_type, TestRegisteredWithDataTypeMiddleware.__datatype__) data_scope = 'test' uri = '%s%s%s%s%s://' % ( TestRegisteredWithDataTypeMiddleware.__protocol__, SCHEME_SEPARATOR, TestRegisteredWithDataTypeMiddleware.__datatype__, SCHEME_SEPARATOR, data_scope) middleware = Middleware.get_middleware_by_uri(uri) self.assertEqual( type(middleware), TestRegisteredWithDataTypeMiddleware) self.assertEqual(middleware.data_scope, data_scope) self.assertEqual(middleware.data_type, TestRegisteredWithDataTypeMiddleware.__datatype__)
def setUp(self): self.alarm_storage = Middleware.get_middleware_by_uri( 'storage-timed-testalarm://' ) self.config_storage = Middleware.get_middleware_by_uri( 'storage-timed-testconfig://' ) self.manager = Alerts() self.manager[Alerts.ALARM_STORAGE] = self.alarm_storage self.manager[Alerts.CONFIG_STORAGE] = self.config_storage self.config_storage.put_element( element={ '_id': 'test_config', 'crecord_type': 'statusmanagement', 'bagot_time': 3600, 'bagot_freq': 10, 'stealthy_time': 300, 'stealthy_show': 600, 'restore_event': True }, _id='test_config' ) self.alarm = { 'state': None, 'status': None, 'ack': None, 'canceled': None, 'ticket': None, 'resolved': None, 'steps': [], 'tags': [] }
def setUp(self): self.alarm_storage = Middleware.get_middleware_by_uri( 'storage-periodical-testalarm://' ) self.config_storage = Middleware.get_middleware_by_uri( 'storage-default-testconfig://' ) self.manager = Alerts() self.manager[Alerts.ALARM_STORAGE] = self.alarm_storage self.manager[Alerts.CONFIG_STORAGE] = self.config_storage self.config_storage.put_element( element={ '_id': 'test_config', 'crecord_type': 'statusmanagement', 'bagot_time': 3600, 'bagot_freq': 10, 'flapping_peristant_steps': 10, 'hard_limit': 100, 'stealthy_time': 300, 'stealthy_show': 600, 'restore_event': True, 'auto_snooze': False, 'snooze_default_time': 300, }, _id='test_config' )
def setUp(self): self.ctx_storage = Middleware.get_middleware_by_uri( 'storage-composite-test_context://', path=Context.DEFAULT_CONTEXT ) self.ctx_manager = Context() self.ctx_manager[Context.CTX_STORAGE] = self.ctx_storage self.perfdata_storage = Middleware.get_middleware_by_uri( 'storage-periodic-test_perfdata://' ) self.perfmeta_storage = Middleware.get_middleware_by_uri( 'storage-timed-test_perfmeta://' ) self.perf_manager = PerfData() self.perf_manager[PerfData.PERFDATA_STORAGE] = self.perfdata_storage self.perf_manager[PerfData.META_STORAGE] = self.perfmeta_storage self.perf_manager[PerfData.CONTEXT_MANAGER] = self.ctx_manager self.serie_storage = Middleware.get_middleware_by_uri( 'storage-default-test_serie://' ) self.serie_manager = Serie() self.serie_manager[Serie.SERIE_STORAGE] = self.serie_storage self.serie_manager[Serie.CONTEXT_MANAGER] = self.ctx_manager self.serie_manager[Serie.PERFDATA_MANAGER] = self.perf_manager
def storage(self, value): if value is None: value = 'storage-default-serie2://' if isinstance(value, basestring): value = Middleware.get_middleware_by_uri(value) self._storage = value
def __init__(self, *args, **kwargs): super(ViewsModule, self).__init__(*args, **kwargs) self.storage = Middleware.get_middleware_by_uri( 'storage-default://', table='object' ) self.rights = Rights()
def get_elements(protocol, data_type, data_scope, _id=None, **kwargs): storage = Middleware.get_middleware( protocol, data_type, data_scope, **kwargs ) storage.connect() return storage.get_elements(ids=_id) or []
def storage(self, value): if value is None: value = 'storage://' if isinstance(value, basestring): value = Middleware.get_middleware_by_uri(value) value.connect() self._storage = value
def test_resolve_middleware_by_uri(self): uri = '%s://' % TestUnregisteredMiddleware.__protocol__ self.assertRaises( Middleware.Error, Middleware.resolve_middleware_by_uri, uri) uri = '%s://' % TestRegisteredMiddleware.__protocol__ middleware_class = Middleware.resolve_middleware_by_uri(uri) self.assertEqual(middleware_class, TestRegisteredMiddleware) uri = '%s%s%s://' % ( TestRegisteredWithDataTypeMiddleware.__protocol__, SCHEME_SEPARATOR, TestRegisteredWithDataTypeMiddleware.__datatype__) middleware_class = Middleware.resolve_middleware_by_uri(uri) self.assertEqual( middleware_class, TestRegisteredWithDataTypeMiddleware)
def setUp(self): self.storage = Middleware.get_middleware_by_uri( 'mongodb-default-testsession://' ) self.storage.connect() self.manager = Session() self.manager[Session.SESSION_STORAGE] = self.storage self.user = '******'
def test_resolve_middleware(self): protocol = TestUnregisteredMiddleware.__protocol__ self.assertRaises( Middleware.Error, Middleware.resolve_middleware, protocol=protocol) protocol = TestRegisteredMiddleware.__protocol__ middleware_class = Middleware.resolve_middleware(protocol=protocol) self.assertEqual(middleware_class, TestRegisteredMiddleware) data_type = None middleware_class = Middleware.resolve_middleware( protocol=protocol, data_type=data_type) self.assertEqual(middleware_class, TestRegisteredMiddleware) data_type = TestRegisteredWithDataTypeMiddleware.__datatype__ middleware_class = Middleware.resolve_middleware( protocol=protocol, data_type=data_type) self.assertEqual( middleware_class, TestRegisteredWithDataTypeMiddleware)
def remove_elements( protocol, data_type, data_scope, _id=None, _filter=None, **kwargs ): storage = Middleware.get_middleware( protocol, data_type, data_scope, **kwargs ) storage.connect() storage.remove_elements(ids=_id, _filter=_filter)
def put_element( protocol, data_type, data_scope, _id=None, element=None, **kwargs ): storage = Middleware.get_middleware( protocol, data_type, data_scope, **kwargs ) storage.connect() if not storage.put_element(element, _id=_id): return HTTPError(500, 'Impossible to put element in storage')
def test_get_middleware(self): protocol = TestUnregisteredMiddleware.__protocol__ self.assertRaises( Middleware.Error, Middleware.get_middleware, protocol=protocol) protocol = TestRegisteredMiddleware.__protocol__ middleware = Middleware.get_middleware(protocol=protocol) self.assertEqual(type(middleware), TestRegisteredMiddleware) self.assertEqual(middleware.data_scope, DEFAULT_DATA_SCOPE) data_type = None middleware = Middleware.get_middleware( protocol=protocol, data_type=data_type) self.assertEqual(type(middleware), TestRegisteredMiddleware) self.assertEqual(middleware.data_scope, DEFAULT_DATA_SCOPE) data_scope = 'test' data_type = TestRegisteredWithDataTypeMiddleware.__datatype__ middleware = Middleware.get_middleware( protocol=protocol, data_type=data_type, data_scope=data_scope) self.assertEqual(middleware.data_scope, data_scope) self.assertEqual( type(middleware), TestRegisteredWithDataTypeMiddleware)
def find_elements( protocol, data_type, data_scope, query=None, projection=None, limit=0, skip=0, sort=None, with_count=False, **kwargs ): storage = Middleware.get_middleware( protocol, data_type, data_scope, **kwargs ) storage.connect() total = storage.count_elements(query=query) result = storage.find_elements( query=query, projection=projection, limit=limit, skip=skip, sort=sort, with_count=with_count ) return result, total
def get_middleware( self, protocol, data_type=None, data_scope=None, auto_connect=None, shared=None, sharing_scope=None, *args, **kwargs ): """ Load a middleware related to input uri. If shared, the result instance is shared among sharing_scope, protocol, data_type and data_scope. :param protocol: protocol to use :type protocol: str :param data_type: data type to use :type data_type: str :param data_scope: data scope to use :type data_scope: str :param auto_connect: middleware auto_connect parameter :type auto_connect: bool :param shared: if True, the result is a shared middleware instance among managers of the same class. If None, use self.shared. :type shared: bool :param sharing_scope: scope sharing :type sharing_scope: bool :return: middleware instance corresponding to input uri and data_scope. :rtype: Middleware """ # force data_scope to be self.data_scope if not None if self.data_scope is not None: data_scope = self.data_scope if auto_connect is None: auto_connect = self.auto_connect if shared is None: shared = self.shared if sharing_scope is None: sharing_scope = self.sharing_scope if shared: protocols = MiddlewareRegistry.__MIDDLEWARES__.setdefault(sharing_scope, {}) data_types = protocols.setdefault(protocol, {}) data_scopes = data_types.setdefault(data_type, {}) try: result = data_scopes.setdefault( data_scope, Middleware.get_middleware( protocol=protocol, data_type=data_type, data_scope=data_scope, auto_connect=auto_connect, *args, **kwargs ), ) except Exception: # clean memory in case of error if not data_scopes: del data_types[data_type] if not data_types: del protocols[protocol] if not protocols: del MiddlewareRegistry.__MIDDLEWARES__[sharing_scope] # and raise back exception raise else: # get a new middleware instance result = Middleware.get_middleware( protocol=protocol, data_type=data_type, data_scope=data_scope, auto_connect=auto_connect, *args, **kwargs ) if hasattr(result, MiddlewareRegistry.DB) and self.db is not None: result.db = self.db return result