Skip to main content

Utilities for Azure CosmosDB isolation and testing

Project description

CosmosDB Isolation Utilities

A unified command-line interface for managing Azure CosmosDB databases in isolation environments. This tool consolidates multiple utilities into a single CLI with consistent parameter handling and a clean separation between the interface and core implementation.

Features

  • Unified CLI: Single command-line tool with subcommands for all operations
  • Connection Testing: Test and validate CosmosDB connections
  • Container Management: View status, statistics, and manage containers
  • Data Export/Import: Dump containers to JSON and restore from JSON files
  • Database Operations: List and manage databases
  • Rich Output: Beautiful terminal output with progress bars and tables
  • Safety Features: Confirmation prompts and dry-run modes for destructive operations

Installation

From Source

git clone <repository-url>
cd cosmos-isolation-utils
pip install -e .

Dependencies

The tool requires the following Python packages:

  • azure-cosmos>=4.0.0 - Azure CosmosDB client
  • click>=8.0.0 - CLI framework
  • rich>=13.0.0 - Rich terminal output
  • urllib3>=1.26.0 - HTTP client

Usage

The unified CLI tool provides several subcommands, all sharing common connection parameters:

cosmos-isolation-utils -e <endpoint> -k <key> -d <database> <subcommand> [options]

Common Parameters

  • -e, --endpoint: CosmosDB endpoint URL (required)
  • -k, --key: CosmosDB primary key (required)
  • -d, --database: CosmosDB database name (required)
  • -a, --allow-insecure: Allow insecure HTTPS requests (suppress warnings)

Subcommands

1. Test Connection

Test the connection to a CosmosDB database and list available containers:

cosmos-isolation-utils -e <endpoint> -k <key> -d <database> test [options]

Options:

  • --create-database: Create database if it doesn't exist
  • -f, --force: Skip confirmation prompts

Example:

cosmos-isolation-utils -e "https://your-cosmosdb.documents.azure.com:443/" \
                      -k "your-primary-key" \
                      -d "testdb" \
                      test --create-database

2. Container Status

View the status and statistics of all containers in a database:

cosmos-isolation-utils -e <endpoint> -k <key> -d <database> status [options]

Options:

  • --detailed: Show detailed information for each container

Example:

cosmos-isolation-utils -e "https://your-cosmosdb.documents.azure.com:443/" \
                      -k "your-primary-key" \
                      -d "testdb" \
                      status --detailed

3. Dump Containers

Export all entries from containers to a JSON file:

cosmos-isolation-utils -e <endpoint> -k <key> -d <database> dump [options]

Options:

  • -c, --containers: Comma-separated list of container names or "all"
  • -o, --output: Output JSON file path (required)
  • -b, --batch-size: Batch size for processing (default: 100)
  • -p, --pretty: Pretty print JSON output

Examples:

# Dump all containers
cosmos-isolation-utils -e "https://your-cosmosdb.documents.azure.com:443/" \
                      -k "your-primary-key" \
                      -d "testdb" \
                      dump -c all -o all_containers.json

# Dump specific containers
cosmos-isolation-utils -e "https://your-cosmosdb.documents.azure.com:443/" \
                      -k "your-primary-key" \
                      -d "testdb" \
                      dump -c "users,orders" -o selected_containers.json

# List available containers
cosmos-isolation-utils -e "https://your-cosmosdb.documents.azure.com:443/" \
                      -k "your-primary-key" \
                      -d "testdb" \
                      dump -l

4. Upload Entries

Restore containers from a JSON dump file:

cosmos-isolation-utils -e <endpoint> -k <key> -d <database> upload [options]

Options:

  • -i, --input: Input JSON file path (required)
  • -b, --batch-size: Batch size for processing (default: 100)
  • -u, --upsert: Use upsert instead of create (overwrites existing items)
  • -r, --dry-run: Show what would be uploaded without actually uploading
  • -f, --force: Skip confirmation prompts
  • --create-containers: Automatically create containers if they don't exist
  • -c, --containers: Comma-separated list of specific containers to upload

Examples:

# Upload all containers from dump
cosmos-isolation-utils -e "https://your-cosmosdb.documents.azure.com:443/" \
                      -k "your-primary-key" \
                      -d "testdb" \
                      upload -i all_containers.json --create-containers

# Upload specific containers with dry-run
cosmos-isolation-utils -e "https://your-cosmosdb.documents.azure.com:443/" \
                      -k "your-primary-key" \
                      -d "testdb" \
                      upload -i all_containers.json -c "users,orders" --dry-run

5. Database Management

List and manage databases:

cosmos-isolation-utils -e <endpoint> -k <key> -d <database> delete-db [options]

Options:

  • -l, --list-databases: List all existing databases
  • -f, --force: Skip confirmation prompts for deletion

Example:

# List all databases
cosmos-isolation-utils -e "https://your-cosmosdb.documents.azure.com:443/" \
                      -k "your-primary-key" \
                      -d "testdb" \
                      delete-db -l

Project Structure

cosmos-isolation-utils/
├── cosmos_isolation_utils/
│   ├── __init__.py
│   ├── __main__.py          # Entry point for python -m
│   ├── __main__.py          # CLI interface and subcommands
│   ├── cosmos_client.py     # CosmosDB client wrapper
│   └── core/                # Core implementation logic
│       ├── __init__.py
│       ├── connection.py    # Connection testing
│       ├── status.py        # Container status
│       ├── dump.py          # Container export
│       ├── upload.py        # Container import
│       └── delete.py        # Database deletion
├── tests/                   # Test suite
├── pyproject.toml          # Project configuration
└── README.md               # This file

Architecture

The tool follows a clean separation of concerns:

  • CLI Layer (__main__.py): Handles command-line interface, parameter parsing, and user interaction
  • Core Layer (core/): Contains the actual business logic for each operation
  • Client Layer (cosmos_client.py): Provides a high-level interface to CosmosDB operations

This separation makes the code more maintainable and testable, while providing a consistent user experience across all operations.

Development

Setup Development Environment

# Create and activate virtual environment
python3 -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

# Install development dependencies
pip install -e ".[dev]"

Running Tests

# Using unittest discover (recommended)
python -m unittest discover tests/ -v

# Using make
make test

# Run specific test file
python -m unittest tests.test_cli -v

# Run test file directly
python tests/test_cli.py -v

# Run tests with coverage
make test-cov

Code Quality

The project uses:

  • pylint for code quality checks
  • coverage for test coverage
  • black for code formatting

Release Workflow

This project uses GitHub Actions with environment protection for secure package publishing to PyPI.

Release Process

  1. Create Release Branch: Push to a branch named release/X.Y.Z (e.g., release/1.2.3)
  2. Automated Checks: The workflow runs tests, builds the package, and publishes to Test PyPI
  3. Test Publication: Package is published to Test PyPI for validation
  4. Production Publication: After successful test publication, package is published to Production PyPI
  5. Release Tag: A Git tag is created for the release

Environment Setup

The workflow requires two GitHub environments to be configured:

  • test-pypi: For publishing to Test PyPI
  • production-pypi: For publishing to Production PyPI

See Environment Setup Guide for detailed configuration instructions.

Security Features

  • Environment Protection: Each environment requires approval from designated reviewers
  • Sequential Publishing: Test publication must succeed before production publication
  • Secret Isolation: PyPI credentials are scoped to specific environments
  • Approval Process: Production releases require explicit approval with optional wait timers

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests for new functionality
  5. Ensure all tests pass
  6. Submit a pull request

License

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

Support

For issues and questions, please use the GitHub issue tracker or contact the maintainers.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cosmos_isolation_utils-0.0.3.tar.gz (23.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cosmos_isolation_utils-0.0.3-py3-none-any.whl (29.0 kB view details)

Uploaded Python 3

File details

Details for the file cosmos_isolation_utils-0.0.3.tar.gz.

File metadata

  • Download URL: cosmos_isolation_utils-0.0.3.tar.gz
  • Upload date:
  • Size: 23.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.18

File hashes

Hashes for cosmos_isolation_utils-0.0.3.tar.gz
Algorithm Hash digest
SHA256 4f642c3d00dfa99a5c983048dd186da8bdf50f7d2b362377518bfc0f63beca09
MD5 0afcf6db8de8baed0873b03e02111a32
BLAKE2b-256 c7a00743e2056729ee4a72d5993e66702b038a041f87293dc637f50df236b773

See more details on using hashes here.

File details

Details for the file cosmos_isolation_utils-0.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for cosmos_isolation_utils-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 78dea7da5ceb8da5fef70c4842754b9a7d1ffb904d1e28e9921329b274f8d3af
MD5 474a4f5fd508998814d653dcde7a4f3b
BLAKE2b-256 041f40b8ed1abbcfee256ee914137b9af78872948e4d27d54b259af44751e795

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page