Python oslo_context.context.get_current() Examples
The following are 30
code examples of oslo_context.context.get_current().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
oslo_context.context
, or try the search function
.
Example #1
Source File: test_context.py From neutron-lib with Apache License 2.0 | 6 votes |
def test_neutron_context_overwrite(self): ctx1 = context.Context('user_id', 'tenant_id') self.assertEqual(ctx1.request_id, oslo_context.get_current().request_id) # If overwrite is not specified, request_id should be updated. ctx2 = context.Context('user_id', 'tenant_id') self.assertNotEqual(ctx2.request_id, ctx1.request_id) self.assertEqual(ctx2.request_id, oslo_context.get_current().request_id) # If overwrite is specified, request_id should be kept. ctx3 = context.Context('user_id', 'tenant_id', overwrite=False) self.assertNotEqual(ctx3.request_id, ctx2.request_id) self.assertEqual(ctx2.request_id, oslo_context.get_current().request_id)
Example #2
Source File: api.py From oslo.vmware with Apache License 2.0 | 6 votes |
def wait_for_task(self, task): """Waits for the given task to complete and returns the result. The task is polled until it is done. The method returns the task information upon successful completion. In case of any error, appropriate exception is raised. :param task: managed object reference of the task :returns: task info upon successful completion of the task :raises: VimException, VimFaultException, VimAttributeException, VimSessionOverLoadException, VimConnectionException """ ctx = context.get_current() loop = loopingcall.FixedIntervalLoopingCall(self._poll_task, task, ctx) evt = loop.start(self._task_poll_interval) LOG.debug("Waiting for the task: %s to complete.", task) return evt.wait()
Example #3
Source File: base.py From senlin with Apache License 2.0 | 6 votes |
def _build_conn_params(self, user, project): """Build trust-based connection parameters. :param user: the user for which the trust will be checked. :param project: the user for which the trust will be checked. """ service_creds = senlin_context.get_service_credentials() params = { 'username': service_creds.get('username'), 'password': service_creds.get('password'), 'auth_url': service_creds.get('auth_url'), 'user_domain_name': service_creds.get('user_domain_name') } cred = co.Credential.get(oslo_context.get_current(), user, project) if cred is None: raise exception.TrustNotFound(trustor=user) params['trust_id'] = cred.cred['openstack']['trust'] return params
Example #4
Source File: test_service.py From senlin with Apache License 2.0 | 6 votes |
def test_start_action_no_action_id(self, mock_acquire_action): mock_action = mock.Mock() mock_action.id = '0123' mock_action.action = 'CLUSTER_CREATE' mock_acquire_action.side_effect = [mock_action, None] svc = service.EngineService('HOST', 'TOPIC') svc.tg = self.mock_tg svc.start_action('4567') self.mock_tg.add_thread.assert_called_once_with( svc._start_with_trace, oslo_context.get_current(), None, actionm.ActionProc, svc.db_session, '0123' )
Example #5
Source File: formatters.py From oslo.log with Apache License 2.0 | 6 votes |
def _update_record_with_context(record): """Given a log record, update it with context information. The request context, if there is one, will either be passed with the incoming record or in the global thread-local store. """ context = record.__dict__.get( 'context', context_utils.get_current() ) if context: d = _dictify_context(context) # Copy the context values directly onto the record so they can be # used by the formatting strings. for k, v in d.items(): setattr(record, k, v) return context
Example #6
Source File: utils.py From masakari with Apache License 2.0 | 6 votes |
def spawn(func, *args, **kwargs): """Passthrough method for eventlet.spawn. This utility exists so that it can be stubbed for testing without interfering with the service spawns. It will also grab the context from the threadlocal store and add it to the store on the new thread. This allows for continuity in logging the context when using this method to spawn a new thread. """ _context = common_context.get_current() @functools.wraps(func) def context_wrapper(*args, **kwargs): # NOTE: If update_store is not called after spawn it won't be # available for the logger to pull from threadlocal storage. if _context is not None: _context.update_store() return func(*args, **kwargs) return eventlet.spawn(context_wrapper, *args, **kwargs)
Example #7
Source File: utils.py From masakari with Apache License 2.0 | 6 votes |
def spawn_n(func, *args, **kwargs): """Passthrough method for eventlet.spawn_n. This utility exists so that it can be stubbed for testing without interfering with the service spawns. It will also grab the context from the threadlocal store and add it to the store on the new thread. This allows for continuity in logging the context when using this method to spawn a new thread. """ _context = common_context.get_current() @functools.wraps(func) def context_wrapper(*args, **kwargs): # NOTE: If update_store is not called after spawn_n it won't be # available for the logger to pull from threadlocal storage. if _context is not None: _context.update_store() func(*args, **kwargs) eventlet.spawn_n(context_wrapper, *args, **kwargs)
Example #8
Source File: base.py From senlin with Apache License 2.0 | 6 votes |
def _build_conn_params(self, user, project): """Build connection params for specific user and project. :param user: The ID of the user for which a trust will be used. :param project: The ID of the project for which a trust will be used. :returns: A dict containing the required parameters for connection creation. """ cred = co.Credential.get(oslo_context.get_current(), user, project) if cred is None: raise exc.TrustNotFound(trustor=user) trust_id = cred.cred['openstack']['trust'] # This is supposed to be trust-based authentication params = copy.deepcopy(self.context) params['trust_id'] = trust_id return params
Example #9
Source File: test_context.py From tacker with Apache License 2.0 | 6 votes |
def test_tacker_context_overwrite(self): ctx1 = context.Context('user_id', 'tenant_id') self.assertEqual(oslo_context.get_current().request_id, ctx1.request_id) # If overwrite is not specified, request_id should be updated. ctx2 = context.Context('user_id', 'tenant_id') self.assertNotEqual(ctx2.request_id, ctx1.request_id) self.assertEqual(oslo_context.get_current().request_id, ctx2.request_id) # If overwrite is specified, request_id should be kept. ctx3 = context.Context('user_id', 'tenant_id', overwrite=False) self.assertNotEqual(ctx3.request_id, ctx2.request_id) self.assertEqual(oslo_context.get_current().request_id, ctx2.request_id)
Example #10
Source File: base.py From senlin with Apache License 2.0 | 6 votes |
def _build_conn_params(self, user, project): """Build connection params for specific user and project. :param user: The ID of the user for which a trust will be used. :param project: The ID of the project for which a trust will be used. :returns: A dict containing the required parameters for connection creation. """ service_creds = senlin_context.get_service_credentials() params = { 'username': service_creds.get('username'), 'password': service_creds.get('password'), 'auth_url': service_creds.get('auth_url'), 'user_domain_name': service_creds.get('user_domain_name') } cred = co.Credential.get(oslo_context.get_current(), user, project) if cred is None: raise exception.TrustNotFound(trustor=user) params['trust_id'] = cred.cred['openstack']['trust'] return params
Example #11
Source File: test_utils.py From masakari with Apache License 2.0 | 6 votes |
def test_spawn_n_context(self): self.assertIsNone(common_context.get_current()) ctxt = context.RequestContext('user', 'project') def _fake_spawn(func, *args, **kwargs): # call the method to ensure no error is raised func(*args, **kwargs) self.assertEqual(ctxt, args[0]) self.assertEqual('test', kwargs['kwarg1']) def fake(context, kwarg1=None): pass with mock.patch.object(eventlet, self.spawn_name, _fake_spawn): getattr(utils, self.spawn_name)(fake, ctxt, kwarg1='test') self.assertEqual(ctxt, common_context.get_current())
Example #12
Source File: test_utils.py From masakari with Apache License 2.0 | 6 votes |
def test_spawn_n_context_different_from_passed(self): self.assertIsNone(common_context.get_current()) ctxt = context.RequestContext('user', 'project') ctxt_passed = context.RequestContext('user', 'project', overwrite=False) self.assertEqual(ctxt, common_context.get_current()) def _fake_spawn(func, *args, **kwargs): # call the method to ensure no error is raised func(*args, **kwargs) self.assertEqual(ctxt_passed, args[0]) self.assertEqual('test', kwargs['kwarg1']) def fake(context, kwarg1=None): pass with mock.patch.object(eventlet, self.spawn_name, _fake_spawn): getattr(utils, self.spawn_name)(fake, ctxt_passed, kwarg1='test') self.assertEqual(ctxt, common_context.get_current())
Example #13
Source File: utils.py From zun with Apache License 2.0 | 6 votes |
def spawn_n(func, *args, **kwargs): """Passthrough method for eventlet.spawn_n. This utility exists so that it can be stubbed for testing without interfering with the service spawns. It will also grab the context from the threadlocal store and add it to the store on the new thread. This allows for continuity in logging the context when using this method to spawn a new thread. """ _context = common_context.get_current() @functools.wraps(func) def context_wrapper(*args, **kwargs): # NOTE: If update_store is not called after spawn_n it won't be # available for the logger to pull from threadlocal storage. if _context is not None: _context.update_store() func(*args, **kwargs) eventlet.spawn_n(context_wrapper, *args, **kwargs)
Example #14
Source File: message.py From senlin with Apache License 2.0 | 6 votes |
def _build_conn_params(self, user, project): """Build connection params for specific user and project. :param user: The ID of the user for which a trust will be used. :param project: The ID of the project for which a trust will be used. :returns: A dict containing the required parameters for connection creation. """ service_creds = senlin_context.get_service_credentials() params = { 'username': service_creds.get('username'), 'password': service_creds.get('password'), 'auth_url': service_creds.get('auth_url'), 'user_domain_name': service_creds.get('user_domain_name') } cred = co.Credential.get(oslo_context.get_current(), user, project) if cred is None: raise exception.TrustNotFound(trustor=user) params['trust_id'] = cred.cred['openstack']['trust'] return params
Example #15
Source File: test_context.py From tacker with Apache License 2.0 | 5 votes |
def test_tacker_context_get_admin_context_not_update_local_store(self): ctx = context.Context('user_id', 'tenant_id') req_id_before = oslo_context.get_current().request_id self.assertEqual(req_id_before, ctx.request_id) ctx_admin = context.get_admin_context() self.assertEqual(req_id_before, oslo_context.get_current().request_id) self.assertNotEqual(req_id_before, ctx_admin.request_id)
Example #16
Source File: service.py From senlin with Apache License 2.0 | 5 votes |
def execute(self, func, *args, **kwargs): """Run the given method in a thread.""" req_cnxt = oslo_context.get_current() self.tg.add_thread( self._start_with_trace, req_cnxt, self._serialize_profile_info(), func, *args, **kwargs )
Example #17
Source File: test_transaction.py From pypowervm with Apache License 2.0 | 5 votes |
def test_subtask_thread_local(self): """Security context and locks, if set, propagates to WrapperTasks.""" def verify_no_ctx(wrapper): self.assertIsNone(ctx.get_current()) tx.FeedTask('test_no_context', lpar.LPAR.getter( self.adpt)).add_functor_subtask(verify_no_ctx).execute() def verify_ctx(wrapper): _context = ctx.get_current() self.assertIsNotNone(_context) self.assertEqual('123', _context.request_id) # Copy the base set of locks to expect our_locks = list(locks) # Add our wrappers uuid since that will be set also. our_locks.append(wrapper.uuid) self.assertEqual(set(our_locks), set(tx._get_locks())) ctx.RequestContext(request_id='123') locks = ['L123', 'L456', 'L789'] tx._set_locks(locks) tx.FeedTask('test_set_context', lpar.LPAR.getter( self.adpt)).add_functor_subtask(verify_ctx).execute() # Context propagates even if FeedTask is executed in a subthread, as # long as our executor is used. # Make two to ensure they're run in separate threads ft1 = tx.FeedTask('subthread1', lpar.LPAR.getter( self.adpt)).add_functor_subtask(verify_ctx) ft2 = tx.FeedTask('subthread2', lpar.LPAR.getter( self.adpt)).add_functor_subtask(verify_ctx) self.assertRaises(tf_ex.WrappedFailure, tf_eng.run, tf_uf.Flow('subthread_flow').add(ft1, ft2), engine='parallel') tf_eng.run( tf_uf.Flow('subthread_flow').add(ft1, ft2), engine='parallel', executor=tx.ContextThreadPoolExecutor(2))
Example #18
Source File: castellan_secret_store.py From barbican with Apache License 2.0 | 5 votes |
def _set_params(self, conf): self.key_manager = key_manager.API(conf) self.context = context.get_current()
Example #19
Source File: dispatcher.py From senlin with Apache License 2.0 | 5 votes |
def notify(method, engine_id=None, **kwargs): """Send notification to dispatcher. Note that dispatcher is an engine internal communication. We are not using versioned object serialization at this level. :param method: remote method to call :param engine_id: dispatcher to notify; None implies broadcast """ client = messaging.get_rpc_client(consts.ENGINE_TOPIC, cfg.CONF.host) if engine_id: # Notify specific dispatcher identified by engine_id call_context = client.prepare(server=engine_id) else: # Broadcast to all disptachers call_context = client.prepare(fanout=True) try: # We don't use ctext parameter in action progress # actually. But since RPCClient.call needs this param, # we use oslo current context here. call_context.cast(oslo_context.get_current(), method, **kwargs) return True except oslo_messaging.MessagingTimeout: return False
Example #20
Source File: test_context.py From oslo.context with Apache License 2.0 | 5 votes |
def test_no_context(self): self.assertIsNone(context.get_current())
Example #21
Source File: test_context.py From neutron-lib with Apache License 2.0 | 5 votes |
def test_neutron_context_get_admin_context_not_update_local_store(self): ctx = context.Context('user_id', 'tenant_id') req_id_before = oslo_context.get_current().request_id self.assertEqual(ctx.request_id, req_id_before) ctx_admin = context.get_admin_context() self.assertEqual(req_id_before, oslo_context.get_current().request_id) self.assertNotEqual(req_id_before, ctx_admin.request_id)
Example #22
Source File: transaction.py From pypowervm with Apache License 2.0 | 5 votes |
def submit(self, fn, *args, **kwargs): context = ctx.get_current() # Get the list of locks held by this thread, we don't want sub # tasks locking the same thing! held_locks = list(_get_locks()) def wrapped(): # This is executed in the new thread. if context is not None: context.update_store() # Ensure the sub task knows about the parent's locks and doesn't # block on them. _set_locks(held_locks) return fn(*args, **kwargs) return super(ContextThreadPoolExecutor, self).submit(wrapped)
Example #23
Source File: test_fixture.py From oslo.context with Apache License 2.0 | 5 votes |
def test_store_current_resets_correctly(self): # By default a new context is stored. ctx = context.RequestContext() # the use of the fixture should put us in a reset state, not # doing so is a bug because when this fixture is consumed by # other test suites there is no guaruntee that all tests use # this fixture. self.useFixture(fixture.ClearRequestContext()) self.assertIsNone(context.get_current()) ctx = context.RequestContext() self.assertIs(context.get_current(), ctx) fixture.ClearRequestContext()._remove_cached_context() self.assertIsNone(context.get_current())
Example #24
Source File: test_context.py From oslo.context with Apache License 2.0 | 5 votes |
def test_store_current(self): # By default a new context is stored. ctx = context.RequestContext() self.assertIs(context.get_current(), ctx)
Example #25
Source File: test_context.py From oslo.context with Apache License 2.0 | 5 votes |
def test_admin_no_overwrite(self): # If there is already a context in the cache creating an admin # context will not overwrite it. ctx1 = context.RequestContext(overwrite=True) context.get_admin_context() self.assertIs(context.get_current(), ctx1) self.assertFalse(ctx1.is_admin)
Example #26
Source File: test_context.py From oslo.context with Apache License 2.0 | 5 votes |
def test_no_overwrite(self): # If there is already a context in the cache a new one will # not overwrite it if overwrite=False. ctx1 = context.RequestContext(overwrite=True) context.RequestContext(overwrite=False) self.assertIs(context.get_current(), ctx1)
Example #27
Source File: test_context.py From oslo.context with Apache License 2.0 | 5 votes |
def test_store_when_no_overwrite(self): # If no context exists we store one even if overwrite is false # (since we are not overwriting anything). ctx = context.RequestContext(overwrite=False) self.assertIs(context.get_current(), ctx)
Example #28
Source File: test_context.py From masakari with Apache License 2.0 | 5 votes |
def test_admin_no_overwrite(self): # If there is already a context in the cache creating an admin # context will not overwrite it. ctx1 = context.RequestContext('111', '222', overwrite=True) context.get_admin_context() self.assertIs(o_context.get_current(), ctx1)
Example #29
Source File: test_context.py From masakari with Apache License 2.0 | 5 votes |
def test_no_overwrite(self): # If there is already a context in the cache a new one will # not overwrite it if overwrite=False. ctx1 = context.RequestContext('111', '222', overwrite=True) context.RequestContext('333', '444', overwrite=False) self.assertIs(o_context.get_current(), ctx1)
Example #30
Source File: test_context.py From masakari with Apache License 2.0 | 5 votes |
def test_store_when_no_overwrite(self): # If no context exists we store one even if overwrite is false # (since we are not overwriting anything). ctx = context.RequestContext('111', '222', overwrite=False) self.assertIs(o_context.get_current(), ctx)