Skip to content

API Access from the Command Line Interface (CLI) and with Code

In addition to the Web Interface, OpenStack resources can be managed via the command line and with programming languages like Python.

This is ideal for automation, scripting, or integrating OpenStack with other tools.

Attention

For security reasons, you can access the ScienceCloud API only from the UZH internal network. Please use the UZH VPN if you are connecting from off-campus.


Set your environment variables

To set up your CLI environment, download an OpenStack RC file for your project from the Web Interface: go to API Access > Download OpenStack RC file > OpenStack RC file.

To import these variables into your shell environment, run the following command and enter your OpenStack password when prompted.

source /path/to/downloaded/projectname-openrc.sh

This approach keeps your credentials out of your code and leverages the environment variables set by the RC file.

Unset your environment variables

Once you are done, it is good practice to either close the terminal or manually unset the environment variables used to authenticate to the OpenStack API.

unset OS_AUTH_URL OS_PROJECT_NAME OS_USERNAME OS_PASSWORD OS_REGION_NAME OS_USER_DOMAIN_NAME OS_PROJECT_DOMAIN_NAME

Using the OpenStack CLI

The python-openstackclient CLI tools provide full access to most OpenStack services from your terminal. You can launch instances, manage volumes, configure networks, and more.

Installation of CLI tools

python3 -m pip install python-openstackclient

Example CLI commands

Once your CLI environment is authenticated, try a simple command:

openstack server list

📚 Full command list: CLI Reference

Using the Python SDK

You can manage OpenStack resources programmatically with Python using the openstacksdk package.

Installation of Python SDK

python3 -m pip install openstacksdk

📚 Full documentation: OpenStack SDK

Example Python script

Below is an example Python script that lists your project's servers, their status and their flavor using the OpenStack SDK.

Similar to the OpenStack CLI, download and source your project's authentication environment variables.

Danger

For security reasons, do not hardcode passwords or sensitive credentials directly in scripts.

import openstack

conn = openstack.connect() # uses your variables set from project-openrc.sh

print(f"{'Name':<25} {'Status':<20} {'Flavor':<20}")
print("-" * 65)

for server in conn.compute.servers():
    flavor = (
        server.flavor["original_name"]
        if server.flavor.get("original_name")
        else server.flavor["id"]
    )
    print(f"{server.name:<25} {server.status:<20} {flavor:<20}")

Troubleshooting

If you receive an error about missing required options (such as auth_url), make sure you have sourced your OpenStack RC file in the same shell session where you run your Python script. If running from an IDE or notebook, ensure the environment variables are set in that environment.