Oslo Context Library

6.3.0 · active · verified Mon Apr 13

The `oslo.context` library is a core OpenStack component providing helpers to manage and maintain useful information about a request context. This context is typically populated within a WSGI pipeline and utilized by various modules, such as `oslo.log`, for consistent data access throughout a request's lifecycle. The library is currently at version 6.3.0 and follows the OpenStack release cadence.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a `RequestContext` object, make it available globally within the current thread using `make_current()`, and retrieve it with `get_current()`. It also shows how to create a specialized admin context. For real-world applications, `RequestContext` is typically initialized by WSGI middleware and automatically managed.

from oslo_context import context

# Create a basic RequestContext
req_context = context.RequestContext(
    auth_token='fake_auth_token',
    user_id='fake_user_id',
    project_id='fake_project_id',
    is_admin=False
)

# Store the context for the current thread
req_context.make_current()

# Later in the application, retrieve the context
current_context = context.get_current()

if current_context:
    print(f"Current User ID: {current_context.user_id}")
    print(f"Is Admin: {current_context.is_admin}")

# Example of creating an admin context
admin_context = context.get_admin_context(show_deleted=True)
admin_context.make_current()
print(f"Admin Context User ID: {context.get_current().user_id}")
print(f"Admin Context is Admin: {context.get_current().is_admin}")

# Clean up the context (important in testing or if not in a WSGI app)
context.clear_current()

view raw JSON →