Apache Libcloud

3.9.0 · active · verified Mon Apr 13

Apache Libcloud is a standard Python library that abstracts away differences among multiple cloud provider APIs, offering a unified interface to manage various cloud resources like servers, object storage, load balancers, and DNS. The current version is 3.9.0. The project follows an event-driven release schedule, with new versions released as enough changes accumulate, or for critical bug and security fixes.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to connect to a cloud provider (Rackspace in this example) using the Libcloud Compute API. It shows how to obtain a driver, authenticate using environment variables, and list available server sizes and images. Remember to replace placeholder credentials with your actual API keys and username, preferably loaded from environment variables for security.

import os
from libcloud.compute.types import Provider
from libcloud.compute.providers import get_driver

# Set your credentials using environment variables for security
RACKSPACE_USER = os.environ.get('RACKSPACE_USERNAME', 'your_username')
RACKSPACE_API_KEY = os.environ.get('RACKSPACE_API_KEY', 'your_api_key')

if RACKSPACE_USER == 'your_username' or RACKSPACE_API_KEY == 'your_api_key':
    print("Please set RACKSPACE_USERNAME and RACKSPACE_API_KEY environment variables.")
    exit(1)

# Obtain a reference to the Rackspace Compute driver
cls = get_driver(Provider.RACKSPACE)
driver = cls(RACKSPACE_USER, RACKSPACE_API_KEY, region='iad') # 'iad' for US-East (Virginia)

print("Connected to Rackspace Compute service.")

# List available sizes (flavors)
print("Available sizes:")
sizes = driver.list_sizes()
for size in sizes:
    print(f"  - {size.id}: {size.name} ({size.ram}MB RAM, {size.disk}GB Disk)")

# List available images
print("Available images:")
images = driver.list_images()
for image in images:
    print(f"  - {image.id}: {image.name}")

# Example: Select a size and image for node creation (adjust as needed)
# This is a placeholder and might require matching a specific image/size from your account/region
# For Rackspace, a common base image might be 'Ubuntu 18.04' or similar.
# For size, 'performance1-1' (1GB RAM) is common.
selected_size = next((s for s in sizes if s.id == 'performance1-1'), None)
selected_image = next((i for i in images if 'Ubuntu 18.04' in i.name), None)

if selected_size and selected_image:
    print(f"Attempting to create node with size: {selected_size.name} and image: {selected_image.name}")
    # Note: Creating a node usually incurs cost. This part is commented out.
    # try:
    #     node = driver.create_node(name='libcloud-test-node', size=selected_size, image=selected_image)
    #     print(f"Node created: {node.name} (ID: {node.id}, State: {node.state})")
    # except Exception as e:
    #     print(f"Error creating node: {e}")
else:
    print("Could not find suitable size or image for node creation. Please check available options.")

view raw JSON →