A standard Python library that abstracts away differences among multiple cloud provider APIs
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
The core driver system provides the foundation for Libcloud's unified interface across all cloud providers. It implements a factory pattern that allows you to obtain provider-specific drivers through a consistent API.
class DriverType:
"""Enumeration of supported service types"""
BACKUP = BackupProvider
COMPUTE = ComputeProvider
CONTAINER = ContainerProvider
DNS = DnsProvider
LOADBALANCER = LoadBalancerProvider
STORAGE = StorageProviderdef get_driver(type: DriverType, provider: str) -> BaseDriverGet a driver instance for the specified type and provider.
Parameters:
type: The service type from DriverType enumprovider: The provider identifier stringReturns:
Raises:
DriverTypeNotFoundError: If the driver type is not supportedExample:
from libcloud.base import get_driver, DriverType
from libcloud.compute.types import Provider
# Using universal factory
driver = get_driver(DriverType.COMPUTE, Provider.EC2)Each service provides its own factory function for convenience:
# Compute
def get_compute_driver(provider: str) -> NodeDriver
# Storage
def get_storage_driver(provider: str) -> StorageDriver
# DNS
def get_dns_driver(provider: str) -> DNSDriver
# Load Balancer
def get_loadbalancer_driver(provider: str) -> Driver
# Container
def get_container_driver(provider: str) -> ContainerDriver
# Backup
def get_backup_driver(provider: str) -> BackupDriverExample:
from libcloud.compute.providers import get_driver
from libcloud.compute.types import Provider
# Using service-specific factory
driver = get_driver(Provider.EC2)class BaseDriver:
"""Base class for all Libcloud drivers"""
name: str
website: str
type: Type
api_name: str
def __init__(self, key: str, secret: str = None, secure: bool = True, host: str = None, port: int = None, **kwargs)All driver classes inherit from BaseDriver and provide service-specific methods.
def enable_debug(fo: TextIOWrapper) -> NoneEnable library-wide debugging to a file-like object. This logs all HTTP requests and responses to help with troubleshooting.
Parameters:
fo: File-like object to write debug information toExample:
import libcloud
import sys
# Enable debug logging to stderr
libcloud.enable_debug(sys.stderr)
# Enable debug logging to a file
with open('libcloud.log', 'w') as f:
libcloud.enable_debug(f)You can also enable debugging via environment variable:
export LIBCLOUD_DEBUG=/dev/stderr
# or
export LIBCLOUD_DEBUG=libcloud.logDriverTypeFactoryMap: Dict[Type, Callable]Internal mapping of driver types to their factory functions. Used by the universal get_driver() function.
class DriverTypeNotFoundError(KeyError):
"""Raised when an unsupported driver type is requested"""
def __init__(self, type: Type)__version__: strThe current version of the Libcloud library.
Example:
import libcloud
print(f"Libcloud version: {libcloud.__version__}")from libcloud.compute.types import Provider
from libcloud.compute.providers import get_driver
# Get driver class
cls = get_driver(Provider.EC2)
# Initialize driver with credentials
driver = cls('access_key', 'secret_key', region='us-east-1')
# Use driver methods
nodes = driver.list_nodes()from libcloud.base import get_driver, DriverType, DriverTypeNotFoundError
from libcloud.common.types import InvalidCredsError
try:
driver = get_driver(DriverType.COMPUTE, 'invalid_provider')
except DriverTypeNotFoundError as e:
print(f"Driver not found: {e}")
try:
cls = get_driver(DriverType.COMPUTE, Provider.EC2)
driver = cls('invalid_key', 'invalid_secret')
nodes = driver.list_nodes()
except InvalidCredsError as e:
print(f"Invalid credentials: {e}")Install with Tessl CLI
npx tessl i tessl/pypi-apache-libcloud