Offering Users and Async User Creation
The Waldur Site Agent provides robust support for managing offering users with asynchronous username generation and state management. This system enables non-blocking user processing and supports complex username generation scenarios through a pluggable backend architecture.
Overview
Offering users represent the relationship between Waldur users and marketplace offerings. The agent handles username generation, state transitions, and integration with backend systems to ensure users can access provisioned resources.
Async User Creation Workflow
State Machine
The async user creation follows a state-based workflow that prevents blocking operations:
stateDiagram-v2
[*] --> REQUESTED : User requests access
REQUESTED --> CREATING : begin_creating
CREATING --> OK : Username set (auto-transition)
CREATING --> PENDING_ACCOUNT_LINKING : Linking required
CREATING --> PENDING_ADDITIONAL_VALIDATION : Validation needed
CREATING --> ERROR_CREATING : BackendError
ERROR_CREATING --> CREATING : begin_creating (retry)
ERROR_CREATING --> PENDING_ACCOUNT_LINKING : Linking required
ERROR_CREATING --> PENDING_ADDITIONAL_VALIDATION : Validation needed
PENDING_ACCOUNT_LINKING --> OK : set_validation_complete
PENDING_ADDITIONAL_VALIDATION --> OK : set_validation_complete
PENDING_ACCOUNT_LINKING --> PENDING_ADDITIONAL_VALIDATION : Cross-transition
PENDING_ADDITIONAL_VALIDATION --> PENDING_ACCOUNT_LINKING : Cross-transition
OK --> [*] : User ready for resource access
State Descriptions
- REQUESTED: Initial state when user requests access to an offering
- CREATING: Transitional state during username generation process
- OK: Username successfully generated and user is ready for resource access
- PENDING_ACCOUNT_LINKING: Manual intervention required to link user accounts
- PENDING_ADDITIONAL_VALIDATION: Additional validation steps needed before proceeding
- ERROR_CREATING: Backend failure during username generation; retried on next sync cycle
Core Components
Main Functions
sync_offering_users()
Entry point function that processes all offering users across configured offerings.
Usage:
1 | |
Behavior:
- Iterates through all configured offerings
- Retrieves offering users from Waldur API
- Delegates processing to
update_offering_users()
update_offering_users()
Core processing function that handles username generation and state transitions.
Process:
- Early validation checks (empty users list, username generation policy)
- Username management backend validation (skips if UnknownUsernameManagementBackend)
- Efficient user grouping by state (single pass through users)
- Processes users in REQUESTED state via
_process_requested_users() - Handles users in pending states via
_process_pending_users() - Manages state transitions and centralized error handling
New Architecture: The function has been refactored into focused sub-functions:
_can_generate_usernames(): Policy validation_group_users_by_state(): Efficient user categorization_process_requested_users(): Handle new username requests_process_pending_users(): Process retry scenarios_update_user_username(): Individual user processing_handle_account_linking_error(): Account linking error management_handle_validation_error(): Validation error management_set_error_creating(): Marks user as ERROR_CREATING after backend failures
Username Management Backend System
The agent uses a pluggable backend architecture for username generation, allowing custom implementations for different identity providers and naming conventions.
Backend Validation
The system now includes early validation to skip processing when no valid username management backend is available:
- UnknownUsernameManagementBackend: Fallback backend that returns empty usernames
- Early Exit: Processing is skipped if
UnknownUsernameManagementBackendis detected - Performance Optimization: Prevents unnecessary API calls when username generation isn't possible
Base Abstract Class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | |
Plugin Registration
Register your backend via entry points in pyproject.toml:
1 2 | |
Built-in Backends
- base: Basic username management backend (plugins/basic_username_management/)
- UnknownUsernameManagementBackend: Fallback backend when configuration is missing or invalid
- Returns empty usernames for all requests
- Triggers early exit from processing to improve performance
- Used automatically when
username_management_backendis not properly configured
Configuration
Offering Configuration
Configure username management per offering in your agent configuration:
1 2 3 4 5 6 7 8 9 | |
Prerequisites
- Service Provider Username Generation: The offering must be configured
with
username_generation_policy = SERVICE_PROVIDERin Waldur - Backend Plugin: Appropriate username management backend must be installed and configured
- Permissions: API token user must have OFFERING.MANAGER role on the offering (grants permissions to manage offering users, orders, and agent identities)
Integration with Order Processing
The async user creation system is seamlessly integrated with the agent's order processing workflows:
Automatic Processing
Username generation is automatically triggered during:
- Resource creation orders
- User addition to existing resources
- Membership synchronization operations
Implementation in Processors
The OfferingBaseProcessor class provides _update_offering_users() method that:
- Calls username generation for users with blank usernames
- Refreshes offering user data after processing
- Filters users to only include those with valid usernames for resource operations
Example usage in order processing:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
Performance Improvements:
- Conditional refresh only when usernames are actually updated
- Early validation prevents unnecessary processing
- Efficient user state grouping reduces multiple iterations
- Backend validation prevents wasted API calls
Error Handling
Exception Types
The system defines specific exceptions for different error scenarios:
OfferingUserAccountLinkingRequiredError: Raised when manual account linking is requiredOfferingUserAdditionalValidationRequiredError: Raised when additional validation steps are neededBackendError: Generic backend failure; triggers ERROR_CREATING state transition- Other exceptions (e.g.
ValueError,HTTPError): Logged but do not trigger any state transition — the user silently stays in their current state. Plugin developers should wrap backend failures asBackendErrorto ensure the error state is reflected in Waldur.
Both linking/validation exceptions support an optional comment_url parameter to provide links to
forms, documentation, or other resources needed for error resolution.
Error Recovery
When exceptions occur during username generation:
- User state transitions to appropriate pending or error state
- Error details are logged with context
- Comment field is updated with error message and comment_url field with any provided URL
- Processing continues for other users
- Pending and error users are retried in subsequent runs
State transition handling by current user state:
- REQUESTED → CREATING: The agent first transitions the user to CREATING, then calls the backend.
If the backend raises a linking/validation error, the user transitions to the appropriate PENDING state.
If a
BackendErroroccurs, the user transitions to ERROR_CREATING. - CREATING / ERROR_CREATING: If the backend raises
OfferingUserAccountLinkingRequiredErrororOfferingUserAdditionalValidationRequiredError, the user transitions toPENDING_ACCOUNT_LINKINGorPENDING_ADDITIONAL_VALIDATIONrespectively. If aBackendErroroccurs, the user transitions to ERROR_CREATING so that admins can see the failure. On the next cycle, ERROR_CREATING users are moved back to CREATING viabegin_creatingand retried. - PENDING_ACCOUNT_LINKING: If the backend still raises
OfferingUserAccountLinkingRequiredError, the user stays in the current state (no redundant API call). If the backend raisesOfferingUserAdditionalValidationRequiredError, the user cross-transitions to PENDING_ADDITIONAL_VALIDATION. - PENDING_ADDITIONAL_VALIDATION: If the backend still raises
OfferingUserAdditionalValidationRequiredError, the user stays in the current state. If the backend raisesOfferingUserAccountLinkingRequiredError, the user cross-transitions to PENDING_ACCOUNT_LINKING. - PENDING_* → OK: When username generation succeeds for a PENDING user,
set_validation_completeis called (which clears service provider comments) before setting the username.
Username Reconciliation in Event Processing Mode
When the agent runs in event_process mode, offering user username synchronization is primarily driven
by real-time STOMP events. However, transient STOMP disconnections or message loss can cause missed
updates. To address this, the main event loop includes a periodic reconciliation timer.
How it works
- Interval: Defaults to 60 minutes, configurable via
WALDUR_SITE_AGENT_RECONCILIATION_PERIOD_MINUTES - Scope: Only runs for offerings with both
stomp_enabled: trueand amembership_sync_backend - Operation: Calls
sync_offering_user_usernames()which compares usernames between source and target offerings and patches any mismatches - Idempotent: Safe to run at any frequency — no side effects when data is already consistent
- Lightweight: Only syncs usernames, not a full membership reconciliation
Reconciliation interval setting
1 2 | |
Best Practices
Username Backend Implementation
- Idempotent Operations: Ensure
get_or_create_username()can be called multiple times safely - Error Handling: Raise appropriate exceptions for recoverable errors
- Logging: Include detailed logging for troubleshooting
- Validation: Validate generated usernames meet backend system requirements
- Performance Considerations: Implement efficient lookup mechanisms to avoid blocking operations
- Backend Validation: Return empty strings when username generation is not supported
Deployment Considerations
- Regular Sync: Run
waldur_sync_offering_usersregularly via cron or systemd timer - Monitoring: Monitor pending user states for manual intervention needs
- Backup Strategy: Consider username mapping backup for disaster recovery
- Testing: Test username generation logic thoroughly before production deployment
- Backend Configuration: Ensure proper
username_management_backendconfiguration to avoid UnknownUsernameManagementBackend fallback - Performance Tuning: Monitor processing times and adjust batch sizes if needed
- Error Recovery: Set up alerting for persistent pending states that may require manual intervention
Troubleshooting
Diagnostic Commands
1 2 3 4 5 6 7 8 9 | |