Implement bulk_purge_user service action in user service layer
- Dominant language
- Python
- Stars
- 670
- Forks
- 183
- Avg merge
- 17h 7m
- Merged PRs (30d)
- 358
Description
## Story
As a developer, I want to implement the bulk_purge_user service action so that multiple users can be efficiently purged with proper business logic and transaction management.
## Technical Details
Implement `bulk_purge_user` action in `/src/ai/backend/manager/services/user/service.py` following the existing service action patterns.
## Implementation Requirements
### Service Action Definition
```python
class BulkPurgeUserAction:
user_info_ctx: UserInfoContext
emails: list[str]
purge_shared_vfolders: bool = False
delegate_endpoint_ownership: bool = False
class BulkPurgeUserResult:
succeeded_users: list[str]
failed_users: dict[str, str] # email -> error reason
```
### Service Implementation
- Location: `/src/ai/backend/manager/services/user/service.py`
- Create new `bulk_purge_user` method in UserService
- Implement transaction management for atomic operations
- Handle batch processing efficiently
- Reuse existing purge_user logic where appropriate
- Implement proper error handling and rollback
### Key Features
1. **Transaction Safety**: All-or-nothing approach with proper rollback
1. **Performance Optimization**: Batch database operations where possible
1. **Error Handling**: Collect and report individual user failures
1. **Consistency**: Maintain same behavior as single user purge
1. **Resource Cleanup**: Ensure all related resources are properly cleaned up
### Processing Logic
```python
async def bulk_purge_user(self, action: BulkPurgeUserAction) -> BulkPurgeUserResult:
# 1. Validate all users exist and can be purged
# 2. Start transaction
# 3. For each user:
# - Delete keypairs
# - Handle vfolders based on policies
# - Delete user record
# - Handle endpoints if delegate_endpoint_ownership
# 4. Commit or rollback based on results
# 5. Return detailed results
```
## Acceptance Criteria
- [ ] bulk_purge_user action implemented in UserService
- [ ] Transaction safety for all-or-nothing operations
- [ ] Efficient batch processing of multiple users
- [ ] Proper error handling with detailed failure reasons
- [ ] Consistency with existing purge_user behavior
- [ ] Unit tests for service action
- [ ] Integration tests for transaction rollback scenarios
## Dependencies
- Depends on repository layer bulk operations
## Parent Epic
BA-2510
JIRA Issue: BA-2512
Contributor guide
Assessment
This issue has not been assessed yet.