OpenStack Heat Orchestration API Client Library
The `python-heatclient` library provides a Python API and a command-line interface for interacting with the OpenStack Heat orchestration service. It allows users to programmatically manage Heat stacks, which orchestrate complex cloud applications using templates. The library is actively maintained as part of the OpenStack project, with its current version being 5.1.0, requiring Python >=3.10.
Common errors
-
ModuleNotFoundError: No module named 'heatclient'
cause The `python-heatclient` library is not installed in the current Python environment.fixRun `pip install python-heatclient` to install the library. -
heatclient.common.exception.Unauthorized: The request you have made requires authentication. (HTTP 401)
cause The authentication credentials (username, password, project, domain, auth URL) provided to `keystoneauth1` are incorrect, expired, or missing.fixVerify your OpenStack credentials and `OS_AUTH_URL`. Ensure all required authentication parameters are correctly configured and passed to `keystoneauth1.loading.get_plugin_loader().load_from_options()`. -
heatclient.common.exception.ClientException: Could not find resource for service type orchestration.
cause The Heat service endpoint could not be discovered or is unavailable/misconfigured in the OpenStack service catalog.fixCheck the `OS_AUTH_URL` and ensure the Heat service (orchestration) is registered and accessible in your OpenStack deployment. Verify network connectivity to the Keystone and Heat API endpoints. -
TypeError: Client() got an unexpected keyword argument 'auth_token'
cause You are attempting to pass an authentication token directly to `heatclient.Client`, which expects a `keystoneauth1.session.Session` object.fixInstead of passing `auth_token`, create a `keystoneauth1.session.Session` object with your authentication details and pass it using the `session` keyword argument: `heat_client.Client('1', session=my_session)`.
Warnings
- breaking Python 2.7 support has been officially dropped. `python-heatclient` versions starting from Xena series (e.g., 2.x and later) and specifically the current 5.1.0 version, require Python 3.6 or newer, with the PyPI metadata explicitly stating `>=3.10`.
- gotcha OpenStack CLI commands are progressively consolidating into the unified `python-openstackclient`. While direct `heat` commands still exist, many functionalities are moving to `openstack stack ...` subcommands. Users familiar with older `heat` CLI tools should be aware of this shift.
- gotcha Authentication for the `heatclient` Python API requires constructing an authenticated session using `keystoneauth1`. Directly passing authentication tokens or credentials to `heatclient.Client` might be deprecated or incorrect in newer versions. Relying solely on shell environment variables (like `OS_USERNAME`) without explicitly loading them via `keystoneauth1` will not work for the programmatic client.
Install
-
pip install python-heatclient
Imports
- Client
import heatclient.client
from heatclient import client as heat_client
- Session
from keystoneclient.v3 import Client as Keystone_Client
from keystoneauth1.session import Session
- get_plugin_loader
from keystoneauth1.loading import get_plugin_loader
Quickstart
import os
from keystoneauth1 import loading
from keystoneauth1.session import Session
from heatclient import client as heat_client
# --- Authentication --- (replace with your OpenStack credentials or environment variables)
# For production, consider using environment variables (OS_AUTH_URL, OS_USERNAME, etc.)
# and 'keystoneauth1.loading.load_auth_from_options' or 'load_from_env'.
# For simplicity, using hardcoded values for quickstart, but highly discouraged in production.
AUTH_URL = os.environ.get('OS_AUTH_URL', 'http://your-openstack-ip/identity/v3')
USERNAME = os.environ.get('OS_USERNAME', 'admin')
PASSWORD = os.environ.get('OS_PASSWORD', 'your-password')
PROJECT_NAME = os.environ.get('OS_PROJECT_NAME', 'admin')
USER_DOMAIN_NAME = os.environ.get('OS_USER_DOMAIN_NAME', 'Default')
PROJECT_DOMAIN_NAME = os.environ.get('OS_PROJECT_DOMAIN_NAME', 'Default')
# Load the password authentication plugin
loader = loading.get_plugin_loader('password')
auth = loader.load_from_options(
auth_url=AUTH_URL,
username=USERNAME,
password=PASSWORD,
project_name=PROJECT_NAME,
user_domain_name=USER_DOMAIN_NAME,
project_domain_name=PROJECT_DOMAIN_NAME
)
# Create a Keystone session
sess = Session(auth=auth)
# Create a Heat client
# API version '1' is commonly used. 'public' endpoint type is standard.
heat_client_instance = heat_client.Client(
'1',
session=sess,
endpoint_type='public',
service_type='orchestration'
)
# --- Example Usage: List Stacks ---
try:
print('Listing Heat stacks:')
for stack in heat_client_instance.stacks.list():
print(f" - Name: {stack.stack_name}, Status: {stack.stack_status}, ID: {stack.id}")
except Exception as e:
print(f"Error listing stacks: {e}")