harbor_batch/CLAUDE.md
seahi e3401ec380 Initial commit: Harbor batch management tools
Add scripts for Harbor user and project management:
- register.py: Bulk user registration from Excel
- delete_users.py: Complete user deletion with resource cleanup
- delete_projects.py: Targeted deletion of student projects (stu01-stu49)
- CLAUDE.md: Project documentation and API guidance
- requirements.txt: Python dependencies

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-06-17 22:06:31 +08:00

110 lines
3.3 KiB
Markdown

# CLAUDE.md
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
## Project Overview
This is a Harbor container registry user management tool written in Python. It provides functionality to:
- Register/create Harbor users from Excel data (`register.py`)
- Delete Harbor users individually or in batches (`delete_users.py`, `delete_users_fixed.py`)
The application interacts with Harbor's REST API v2.0 at `https://harbor.seahi.me`.
## Setup and Environment
### Dependencies Installation
```bash
pip install -r requirements.txt
```
### Virtual Environment Setup
```bash
# Activate existing virtual environment
source venv/bin/activate # On Unix/macOS
# or
venv\Scripts\activate # On Windows
```
## Core Architecture
### Data Flow
1. **Input**: Excel file (`users.xlsx`) with user data (username, realname, password, email)
2. **Authentication**: HTTP Basic Auth with Harbor admin credentials
3. **API Operations**: REST API calls to Harbor's `/api/v2.0/users` endpoint
4. **Error Handling**: Comprehensive exception handling with Chinese language user feedback
### Key Components
- **Authentication**: `get_admin_credentials()` - Interactive credential collection with getpass
- **User Operations**:
- `create_harbor_user()` - User creation via POST (register.py:20)
- `delete_harbor_user()` - User deletion via DELETE (requires user ID lookup)
- `get_user_id_by_username()` - User ID resolution from username
- **Excel Processing**: `pd.read_excel()` with `skiprows=1` to handle header row
- **Connection Testing**: `test_api_connection()` validates API connectivity and auth (delete_users_fixed.py:20)
- **Interactive Modes**: `delete_users_fixed.py` provides menu-driven user interaction
### File Structure
- `register.py` - User registration from Excel
- `delete_users.py` - Basic user deletion tool
- `delete_users_fixed.py` - Enhanced deletion tool with better error handling and interactive modes
- `users.xlsx` - Excel data source (not in version control)
- `requirements.txt` - Python dependencies
## Common Commands
### Environment Setup
```bash
# Activate virtual environment (required before running scripts)
source venv/bin/activate
# Install dependencies
pip install -r requirements.txt
```
### Run Scripts
```bash
# Register users from Excel file
python register.py
# Delete users (recommended enhanced version)
python delete_users_fixed.py
# Delete users (basic version)
python delete_users.py
```
### Development
```bash
# Check Python syntax
python -m py_compile register.py
python -m py_compile delete_users_fixed.py
# Run with verbose output for debugging
python -v register.py
```
## API Configuration
- Harbor URL: `https://harbor.seahi.me`
- API Version: v2.0
- Authentication: HTTP Basic Auth
- SSL Verification: Disabled (`verify=False`)
- HTTPS Warnings: Suppressed via urllib3
## Excel File Format
Expected structure for `users.xlsx`:
- Row 1: Headers (skipped)
- Column 0: Username
- Column 1: Real name
- Column 2: Password
- Column 4: Email address
## Error Handling Patterns
- HTTP 409: User already exists (treated as success for registration)
- HTTP 401: Authentication failure
- HTTP 404: User not found
- HTTP 412: User has dependencies (cannot delete)
- Connection timeouts and network errors are handled gracefully