Skip to content

zac-amd/openstack-vm-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

25 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

OpenStack VM Lifecycle Management API

A RESTful API for managing OpenStack virtual machine lifecycle operations, built with FastAPI, SQLAlchemy, and the OpenStack SDK.

CI Python 3.10+ License: MIT

πŸš€ Features

  • VM Lifecycle Management: Create, list, get, update, and delete VMs
  • VM Actions: Start, stop, reboot (soft/hard), and sync state
  • Resource Management: List available flavors and images
  • API Key Authentication: Secure endpoint access
  • OpenStack Integration: Real OpenStack SDK or mock mode for testing
  • Async Architecture: Built with async/await for high performance
  • Auto-generated Documentation: Swagger UI and ReDoc
  • Containerized: Docker and Docker Compose support
  • CI/CD Ready: GitHub Actions workflow included

πŸ“‹ Table of Contents

πŸƒ Quick Start

Using Docker (Recommended)

# Clone the repository
git clone https://github.com/zac-amd/openstack-vm-api.git
cd openstack-vm-api

# Start with Docker Compose (mock mode)
docker-compose up -d

# API is now available at http://localhost:8000
# Documentation at http://localhost:8000/docs

Local Development

# Clone and install
git clone https://github.com/zac-amd/openstack-vm-api.git
cd openstack-vm-api
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate
pip install -r requirements.txt

# Configure
cp .env.example .env
# Edit .env with your settings

# Run
uvicorn app.main:app --reload

πŸ“¦ Installation

Prerequisites

  • Python 3.10+
  • pip
  • Docker & Docker Compose (optional)

Install Dependencies

# Production dependencies
pip install -r requirements.txt

# Development dependencies (includes testing tools)
pip install -e ".[dev]"

βš™οΈ Configuration

Configuration is managed through environment variables. Copy .env.example to .env and update:

# API Configuration
API_TITLE=OpenStack VM Lifecycle API
API_VERSION=1.0.0
DEBUG=false

# API Key Authentication
API_KEY=your-secure-api-key-here

# Database
DATABASE_URL=sqlite+aiosqlite:///./openstack_vm.db

# OpenStack Mode
USE_MOCK_OPENSTACK=true  # Set to false for real OpenStack

# OpenStack Credentials (when USE_MOCK_OPENSTACK=false)
OS_AUTH_URL=https://your-openstack:5000/v3
OS_PROJECT_NAME=your-project
OS_USERNAME=your-username
OS_PASSWORD=your-password
OS_USER_DOMAIN_NAME=Default
OS_PROJECT_DOMAIN_NAME=Default
OS_REGION_NAME=RegionOne

πŸ“š API Reference

Authentication

All endpoints (except /health and /) require an API key in the X-API-Key header:

curl -H "X-API-Key: your-api-key" http://localhost:8000/api/v1/vms

Endpoints

Method Endpoint Description
GET /api/v1/health Health check
GET /api/v1/ API information
Virtual Machines
POST /api/v1/vms Create a new VM
GET /api/v1/vms List all VMs (paginated)
GET /api/v1/vms/{uuid} Get VM details
PATCH /api/v1/vms/{uuid} Update VM
DELETE /api/v1/vms/{uuid} Delete VM
POST /api/v1/vms/{uuid}/start Start VM
POST /api/v1/vms/{uuid}/stop Stop VM
POST /api/v1/vms/{uuid}/reboot Reboot VM
POST /api/v1/vms/{uuid}/sync Sync VM state from OpenStack
Flavors
GET /api/v1/flavors List available flavors
GET /api/v1/flavors/{id} Get flavor details
Images
GET /api/v1/images List available images
GET /api/v1/images/{id} Get image details

Example: Create a VM

curl -X POST http://localhost:8000/api/v1/vms \
  -H "X-API-Key: your-api-key" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "my-web-server",
    "flavor_id": "m1.small",
    "image_id": "ubuntu-22.04",
    "description": "Web server instance"
  }'

Example: VM Lifecycle Operations

# Stop a VM
curl -X POST http://localhost:8000/api/v1/vms/{uuid}/stop \
  -H "X-API-Key: your-api-key"

# Start a VM
curl -X POST http://localhost:8000/api/v1/vms/{uuid}/start \
  -H "X-API-Key: your-api-key"

# Reboot a VM (soft)
curl -X POST http://localhost:8000/api/v1/vms/{uuid}/reboot \
  -H "X-API-Key: your-api-key" \
  -H "Content-Type: application/json" \
  -d '{"reboot_type": "SOFT"}'

πŸ“ Project Structure

openstack-vm-api/
β”œβ”€β”€ .github/
β”‚   └── workflows/
β”‚       └── ci.yml                 # GitHub Actions CI/CD pipeline
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ api/
β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   └── v1/
β”‚   β”‚       β”œβ”€β”€ __init__.py
β”‚   β”‚       β”œβ”€β”€ router.py          # API v1 router combining all endpoints
β”‚   β”‚       └── endpoints/
β”‚   β”‚           β”œβ”€β”€ __init__.py
β”‚   β”‚           β”œβ”€β”€ flavors.py     # Flavor endpoints (list, get)
β”‚   β”‚           β”œβ”€β”€ health.py      # Health check endpoint
β”‚   β”‚           β”œβ”€β”€ images.py      # Image endpoints (list, get)
β”‚   β”‚           └── vms.py         # VM lifecycle endpoints (CRUD + actions)
β”‚   β”œβ”€β”€ core/
β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   β”œβ”€β”€ exceptions.py          # Custom exception classes
β”‚   β”‚   β”œβ”€β”€ openstack_client.py    # OpenStack SDK wrapper (mock + real)
β”‚   β”‚   └── security.py            # API key authentication
β”‚   β”œβ”€β”€ models/
β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   └── vm.py                  # SQLAlchemy VM model with state machine
β”‚   β”œβ”€β”€ schemas/
β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   β”œβ”€β”€ common.py              # Shared schemas (health, errors, flavors, images)
β”‚   β”‚   └── vm.py                  # VM request/response schemas
β”‚   β”œβ”€β”€ services/
β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   └── vm_service.py          # VM business logic layer
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ config.py                  # Pydantic settings configuration
β”‚   β”œβ”€β”€ database.py                # Async SQLAlchemy database setup
β”‚   └── main.py                    # FastAPI application entry point
β”œβ”€β”€ tests/
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ conftest.py                # Pytest fixtures and configuration
β”‚   └── test_api_endpoints.py      # API integration tests
β”œβ”€β”€ .env.example                   # Environment variables template
β”œβ”€β”€ .gitignore                     # Git ignore patterns
β”œβ”€β”€ ARCHITECTURE.md                # Architecture documentation
β”œβ”€β”€ docker-compose.yml             # Docker Compose configuration
β”œβ”€β”€ Dockerfile                     # Multi-stage Docker build
β”œβ”€β”€ LICENSE                        # MIT License
β”œβ”€β”€ Makefile                       # Development commands
β”œβ”€β”€ pyproject.toml                 # Python project configuration
β”œβ”€β”€ README.md                      # This file
β”œβ”€β”€ requirements.txt               # Python dependencies
└── ROADMAP.md                     # Future features backlog

πŸ› οΈ Development

Using Make Commands

# Show available commands
make help

# Install dependencies
make install

# Run development server
make run

# Run tests
make test

# Format code
make format

# Lint code
make lint

Code Style

# Format code with Black
black app/ tests/

# Lint with Ruff
ruff check app/ tests/

# Type checking with MyPy
mypy app/

πŸ§ͺ Testing

# Run all tests
pytest

# Run with verbose output
pytest -v

# Run with coverage
pytest --cov=app --cov-report=html

# Run specific test file
pytest tests/test_api_endpoints.py -v

πŸš€ Deployment

Docker

# Build image
docker build -t openstack-vm-api:latest .

# Run container
docker run -d \
  -p 8000:8000 \
  -e API_KEY=your-secure-key \
  -e USE_MOCK_OPENSTACK=false \
  -e OS_AUTH_URL=https://openstack:5000/v3 \
  openstack-vm-api:latest

Docker Compose

# Production with environment variables
export API_KEY=your-secure-key
export USE_MOCK_OPENSTACK=false
docker-compose up -d

# View logs
docker-compose logs -f

# Stop
docker-compose down

πŸ“ Architecture

See ARCHITECTURE.md for detailed architecture documentation including:

  • System design and component diagrams
  • Design patterns (Repository, Service Layer, Strategy, State Machine)
  • Database schema design
  • VM state machine transitions
  • Security architecture
  • Technology choices and trade-offs
  • Scalability considerations

πŸ—ΊοΈ Roadmap

See ROADMAP.md for planned features and enhancements:

  • Phase 2: JWT authentication, RBAC, audit logging
  • Phase 3: PostgreSQL, Redis caching, Celery tasks
  • Phase 4: VM snapshots, floating IPs, Prometheus metrics
  • Phase 5: Multi-cloud support, Terraform provider

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ“§ Support


Built with ❀️ using FastAPI and OpenStack SDK

About

OpenStack virtual machine lifecycle operations

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors