OpenStack Neutron Client (Deprecated)
python-neutronclient is a client library for the OpenStack Networking (Neutron) API. It provides Python API bindings and previously included a command-line interface. As of version 11.8.0, the project is deprecated; its CLI code has been removed, and no new features will be added. All new development and migrations under OpenStack governance are directed towards using OpenStackSDK and OpenStack Client.
Warnings
- breaking The `python-neutronclient` project is officially deprecated. Its command-line interface (CLI) code has been deleted, and no new features will be added to the Python API bindings. Users are strongly encouraged to migrate to `openstacksdk` for Python API interactions and `openstackclient` for CLI operations.
- deprecated The `neutron` CLI (provided by older versions of `python-neutronclient`) was deprecated from the OpenStack Ocata release. While `python-neutronclient` provided extensions for the unified `openstackclient`, the primary CLI for networking functionality is now `openstackclient`.
- gotcha Authenticating with OpenStack Identity (Keystone) v2.0 is deprecated. Using v2.0 may lead to authentication issues or limited functionality as OpenStack services increasingly require v3.0.
- gotcha Common authentication failures, such as `Unauthorized` or `EndpointNotFound`, often result from incorrect or missing OpenStack environment variables (e.g., `OS_AUTH_URL`, `OS_USERNAME`, `OS_PASSWORD`, `OS_PROJECT_NAME`, `OS_USER_DOMAIN_NAME`, `OS_PROJECT_DOMAIN_NAME`) or an expired authentication token.
Install
-
pip install python-neutronclient
Imports
- Client
from neutronclient.v2_0 import client
Quickstart
import os
from keystoneauth1 import identity
from keystoneauth1 import session
from neutronclient.v2_0 import client
# Set these environment variables or replace with actual values
OS_AUTH_URL = os.environ.get('OS_AUTH_URL', 'http://auth.example.com:5000/v3')
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_USER_DOMAIN_NAME = os.environ.get('OS_USER_DOMAIN_NAME', 'Default')
OS_PROJECT_DOMAIN_NAME = os.environ.get('OS_PROJECT_DOMAIN_NAME', 'Default')
# Authenticate using Keystone v3 password flow
auth = identity.Password(
auth_url=OS_AUTH_URL,
username=OS_USERNAME,
password=OS_PASSWORD,
project_name=OS_PROJECT_NAME,
user_domain_name=OS_USER_DOMAIN_NAME,
project_domain_name=OS_PROJECT_DOMAIN_NAME
)
sess = session.Session(auth=auth)
# Create a Neutron client instance
neutron_client = client.Client(session=sess)
# Example: List networks
try:
networks = neutron_client.list_networks()
print("Networks:")
for net in networks['networks']:
print(f" - {net['name']} ({net['id']})")
except Exception as e:
print(f"Error listing networks: {e}")