OpenStack Compute (Nova) Client
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
- deprecated The `nova` command-line script, which is installed alongside `python-novaclient`, is deprecated. Users are encouraged to use the Python API directly or the unified `openstackclient` for command-line interactions.
- gotcha OpenStack authentication, especially with Keystone v3, can be complex due to varying required parameters (e.g., domain IDs/names). Directly passing credentials to `novaclient.client.Client` can be brittle.
- gotcha OpenStack Nova API extensively uses microversions to introduce new features and changes without incrementing the major API version (e.g., API v2.0 has many microversions like 2.53, 2.94). Client behavior can differ significantly based on the microversion negotiated or specified.
- deprecated The `nova x509-create-cert` and `nova x509-get-root-cert` commands and the `novaclient.v2.certs` API binding are deprecated and scheduled for removal in future major releases after Nova server 16.0.
Install
-
pip install python-novaclient
Imports
- Client
from novaclient import client nova_client = client.Client(api_version='2.latest', ...)
Quickstart
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.")