OpenStack Compute (Nova) Client

18.12.0 · active · verified Tue Apr 14

python-novaclient is a client library for the OpenStack Compute (Nova) API, enabling Python applications to programmatically interact with OpenStack's compute resources. It provides a comprehensive Python API (the `novaclient` module) to manage virtual servers, images, flavors, and other Nova-related services. This library is actively developed as part of the OpenStack ecosystem, with releases typically synchronized with OpenStack major versions, and the current version is 18.12.0.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the Nova client and perform basic operations like listing available compute flavors and running servers. It uses environment variables for authentication, which is a common practice in OpenStack CLI tools and scripts. For production applications, using `keystoneauth1.session.Session` is the recommended and more robust authentication method.

import os
from novaclient import client

# OpenStack credentials (often sourced from an 'openrc' file)
OS_USERNAME = os.environ.get('OS_USERNAME', 'your_username')
OS_PASSWORD = os.environ.get('OS_PASSWORD', 'your_password')
OS_PROJECT_NAME = os.environ.get('OS_PROJECT_NAME', 'your_project_name')
OS_AUTH_URL = os.environ.get('OS_AUTH_URL', 'http://your_keystone_ip:5000/v3')
OS_REGION_NAME = os.environ.get('OS_REGION_NAME', 'RegionOne')
OS_USER_DOMAIN_NAME = os.environ.get('OS_USER_DOMAIN_NAME', 'Default')
OS_PROJECT_DOMAIN_NAME = os.environ.get('OS_PROJECT_DOMAIN_NAME', 'Default')

# Initialize the Nova client
# For robust authentication, consider using keystoneauth1.session.Session
# and passing it to the client for 'session' parameter.
# Example: from keystoneauth1.identity import v3; from keystoneauth1 import session
# auth = v3.Password(auth_url=OS_AUTH_URL, username=OS_USERNAME, ...)
# sess = session.Session(auth=auth)
# nova = client.Client(api_version='2.latest', session=sess, region_name=OS_REGION_NAME)

# Using direct parameters for simplicity in quickstart, but keystoneauth1 is better for production
nova = client.Client(
    api_version='2.latest',
    username=OS_USERNAME,
    password=OS_PASSWORD,
    project_name=OS_PROJECT_NAME,
    auth_url=OS_AUTH_URL,
    region_name=OS_REGION_NAME,
    user_domain_name=OS_USER_DOMAIN_NAME,
    project_domain_name=OS_PROJECT_DOMAIN_NAME
)

# List available flavors (instance types)
try:
    flavors = nova.flavors.list()
    print(f"Available Flavors ({len(flavors)}):")
    for flavor in flavors:
        print(f"  - {flavor.name} (ID: {flavor.id}, RAM: {flavor.ram}MB, VCPUs: {flavor.vcpus})")

    # List available servers
    servers = nova.servers.list()
    print(f"\nRunning Servers ({len(servers)}):")
    if not servers:
        print("  No servers found.")
    for server in servers:
        print(f"  - {server.name} (ID: {server.id}, Status: {server.status})")

except Exception as e:
    print(f"An error occurred: {e}")
    print("Please ensure your OpenStack credentials (environment variables) are correctly set.")

view raw JSON →