Skip to main content

Community Python SDK for MTN Cloud (Morpheus) - Deploy and manage cloud resources with ease

Project description

MTN Cloud Python SDK

PyPI version Tests Python 3.10+ License: MIT

A community-maintained Python SDK for MTN Cloud (Morpheus).

⚠️ Disclaimer: This is an unofficial community project and is not affiliated with, endorsed by, or supported by MTN. This software is provided "as is", without warranty of any kind. Use at your own risk. See the LICENSE for full terms.

Features

  • 🚀 Simple, Pythonic API - Intuitive interface for all cloud operations
  • 📦 Typed Models - Full Pydantic models with IDE autocomplete
  • 🔄 Automatic Retries - Built-in retry logic with exponential backoff
  • 🔐 Flexible Auth - Token or username/password authentication
  • Resource Managers - Organized access to instances, networks, volumes
  • 🛡️ Error Handling - Specific exceptions for different error types

Installation

pip install mtn-cloud

Quick Start

from mtn_cloud import MTNCloud

# Initialize with token
cloud = MTNCloud(token="your-api-token")

# Or use environment variable MTN_CLOUD_TOKEN
cloud = MTNCloud()

# Check connection
user = cloud.whoami()
print(f"Connected as: {user.username}")

# List instances
for instance in cloud.instances.list():
    print(f"{instance.name}: {instance.status} ({instance.primary_ip})")

Authentication

Using API Token (Recommended)

from mtn_cloud import MTNCloud

# Pass token directly
cloud = MTNCloud(token="your-api-token")

# Or set environment variable
# export MTN_CLOUD_TOKEN="your-api-token"
cloud = MTNCloud()

Using Username/Password

cloud = MTNCloud(
    username="user@example.com",
    password="your-password"
)

Getting Your API Token

  1. Log in to MTN Cloud Console
  2. Go to User SettingsAPI Access
  3. Generate a new access token

Usage Examples

Managing Instances

from mtn_cloud import MTNCloud
from mtn_cloud.models import InstanceConfig, InstanceVolume, InstanceNetwork

cloud = MTNCloud(token="xxx")

# List all instances
instances = cloud.instances.list()

# Filter instances
running = cloud.instances.list(status="running")
by_name = cloud.instances.list(name="web-server")

# Get a specific instance
instance = cloud.instances.get(123)
print(f"Name: {instance.name}")
print(f"Status: {instance.status}")
print(f"IP: {instance.primary_ip}")

# Get instance by name
instance = cloud.instances.get_by_name("my-app")

# Create a new instance
instance = cloud.instances.create(
    name="web-server-01",
    cloud_id=1,
    group_id=1,
    instance_type_code="MTN-CS10",
    layout_id=327,
    plan_id=6923,
    config=InstanceConfig(
        resource_pool_id="pool-214",
        availability_zone="Lagos-AZ-1-fd1",
        security_group="default",
    ),
    volumes=[
        InstanceVolume(name="root", size=20),
    ],
    network_interfaces=[
        InstanceNetwork(network_id=298, ip_address="192.168.100.50"),
    ],
    labels=["production", "web"],
)

# Wait for instance to be running
instance = cloud.instances.wait_until_running(instance.id, timeout=300)

# Instance actions
instance.stop()
instance.start()
instance.restart()

# Or use the resource manager
cloud.instances.stop(123)
cloud.instances.start(123)

# Resize instance
cloud.instances.resize(123, plan_id=6924)

# Delete instance
cloud.instances.delete(123)

# Force delete with volume preservation
cloud.instances.delete(123, force=True, preserve_volumes=True)

Working with Networks

# List all networks
networks = cloud.networks.list()

# List networks for a specific cloud
networks = cloud.networks.list(cloud_id=1)

# Get network by ID
network = cloud.networks.get(298)
print(f"Network: {network.name}")
print(f"CIDR: {network.cidr}")
print(f"Gateway: {network.gateway}")

# Get network by name
network = cloud.networks.get_by_name("my-network")

Managing Groups and Clouds

# List groups
for group in cloud.groups.list():
    print(f"{group.name}: {group.instance_count} instances")

# Get group by name
group = cloud.groups.get_by_name("MTNNG_CLOUD_AZ_1")

# List clouds/zones
for c in cloud.clouds.list():
    print(f"{c.name}: {c.type_code}")

# Get cloud by name
zone = cloud.clouds.get_by_name("MTNNG_CLOUD_AZ_1")

Service Plans

# List all plans
plans = cloud.plans.list()

for plan in plans:
    print(f"{plan.name}: {plan.cores} cores, {plan.memory_gb}GB RAM")

# Find a plan by requirements
plan = cloud.plans.find(cores=2, memory_gb=4)

Error Handling

The SDK provides specific exceptions for different error types:

from mtn_cloud import (
    MTNCloud,
    MTNCloudError,
    AuthenticationError,
    NotFoundError,
    ForbiddenError,
    ValidationError,
    RateLimitError,
    TimeoutError,
)

cloud = MTNCloud(token="xxx")

try:
    instance = cloud.instances.get(99999)
except NotFoundError as e:
    print(f"Instance not found: {e}")
except AuthenticationError as e:
    print(f"Auth failed: {e}")
except ForbiddenError as e:
    print(f"Access denied: {e}")
except ValidationError as e:
    print(f"Invalid request: {e}")
    print(f"Errors: {e.errors}")
except RateLimitError as e:
    print(f"Rate limited. Retry after: {e.retry_after}s")
except TimeoutError as e:
    print(f"Request timed out: {e}")
except MTNCloudError as e:
    print(f"API error: {e}")

Configuration

Environment Variables

Variable Description Default
MTN_CLOUD_TOKEN API access token -
MTN_CLOUD_URL API base URL https://console.cloud.mtn.ng
MTN_CLOUD_TIMEOUT Request timeout (seconds) 30
MTN_CLOUD_MAX_RETRIES Max retry attempts 3
MTN_CLOUD_VERIFY_SSL Verify SSL certs true

Programmatic Configuration

from mtn_cloud import MTNCloud, MTNCloudConfig

# Using config object
config = MTNCloudConfig(
    token="xxx",
    timeout=60,
    max_retries=5,
    debug=True,
)
cloud = MTNCloud(config=config)

# Or pass arguments directly
cloud = MTNCloud(
    token="xxx",
    timeout=60,
    verify_ssl=False,  # Not recommended for production
)

Context Manager

Use as a context manager for automatic cleanup:

with MTNCloud(token="xxx") as cloud:
    instances = cloud.instances.list()
    # Session is automatically closed when exiting

Advanced Usage

Waiting for Instance States

# Wait for specific status
instance = cloud.instances.wait_for_status(
    instance_id=123,
    target_status="running",
    timeout=300,
    poll_interval=5,
)

# Convenience methods
instance = cloud.instances.wait_until_running(123)
instance = cloud.instances.wait_until_stopped(123)

Instance Actions from Instance Object

# Get instance
instance = cloud.instances.get(123)

# Actions are available directly on the instance
instance.stop()
instance.start()
instance.restart()
instance.delete()

# Refresh data from API
instance.refresh()

Checking Connection

# Quick connection check
if cloud.ping():
    print("Connected!")
else:
    print("Connection failed")

API Reference

MTNCloud

Main client class.

Property Description
instances Instance resource manager
networks Network resource manager
groups Group resource manager
clouds Cloud/zone resource manager
plans Service plan resource manager
Method Description
whoami() Get current user
ping() Check connection
close() Close HTTP session

InstancesResource

Method Description
list(**filters) List instances
get(id) Get instance by ID
get_by_name(name) Get instance by name
create(...) Create new instance
update(id, ...) Update instance
delete(id) Delete instance
start(id) Start instance
stop(id) Stop instance
restart(id) Restart instance
suspend(id) Suspend instance
resize(id, plan_id) Resize instance
wait_for_status(...) Wait for status
wait_until_running(id) Wait until running
wait_until_stopped(id) Wait until stopped

Development

Setup

git clone https://github.com/mahveotm/mtn-cloud-python
cd mtn-cloud-python
pip install -e ".[dev]"

Running Tests

pytest
pytest --cov=mtn_cloud

Code Quality

# Format code
ruff format src tests

# Lint code
ruff check src tests

# Lint and auto-fix
ruff check src tests --fix

# Type checking
mypy src

License

MIT License - see LICENSE for details.

This is an unofficial community project. Not affiliated with MTN.

Author

Marvellous Osuolale - GitHub

Links

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

mtn_cloud-0.1.2.tar.gz (28.6 kB view details)

Uploaded Source

Built Distribution

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

mtn_cloud-0.1.2-py3-none-any.whl (34.7 kB view details)

Uploaded Python 3

File details

Details for the file mtn_cloud-0.1.2.tar.gz.

File metadata

  • Download URL: mtn_cloud-0.1.2.tar.gz
  • Upload date:
  • Size: 28.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for mtn_cloud-0.1.2.tar.gz
Algorithm Hash digest
SHA256 dfd3e3fac6471ffe824db9af1bf043f768cdb2edee1ff897ee1b3a5a97c040f0
MD5 997601de523a5e0d06be54e8a454b8de
BLAKE2b-256 2b0d0a38b28e0d0d072ff6e3f6d4d15063377eb01e6aa465b02a0b3008f67f7c

See more details on using hashes here.

Provenance

The following attestation bundles were made for mtn_cloud-0.1.2.tar.gz:

Publisher: publish.yml on mahveotm/mtn-cloud-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mtn_cloud-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: mtn_cloud-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 34.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for mtn_cloud-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0feae2ffc0c114ad2a36f768ca2e1fac3b1e131f342621e3a4ba0a564b14a0a2
MD5 c5709a66b9d7a18e8fe4dec8a9527c11
BLAKE2b-256 d032c465dbee05d46f253b4f3062981d8094cd94c5338ef1cb1d9a6fa71d2134

See more details on using hashes here.

Provenance

The following attestation bundles were made for mtn_cloud-0.1.2-py3-none-any.whl:

Publisher: publish.yml on mahveotm/mtn-cloud-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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