Connect¶
In order to work with an OpenStack cloud you first need to create a
Connection to it using your credentials. A
connection can be created in a number of ways: using a configuration file,
environment variables, or explicit keyword arguments, as illustrated below.
It is recommended to use Config Files as the same configuration file can be shared across tools and languages. For a comprehensive overview of configuration handling in openstacksdk, see the Using os-client-config guide.
Create a connection via a configuration file¶
To create a connection via a configuration file, you need a YAML file called
clouds.yaml. A simple clouds.yaml file looks like the following:
clouds:
mycloud:
auth:
auth_url: https://identity.example.com/v3
username: "my-username"
password: "secret-password"
project_name: "my-project"
user_domain_name: "Default"
project_domain_name: "Default"
region_name: "RegionOne"
To create a Connection instance using this file,
use the connect() factory function and pass the name of the
cloud configuration:
def create_connection_from_config():
return openstack.connect(cloud=TEST_CLOUD)
For more details on the format of clouds.yaml, including standard file
locations and profile inheritance, refer to Config Files.
Command-line options (argparse)¶
If you are writing a command-line application, connect() can
integrate directly with Python’s standard argparse library.
When you pass an argparse.ArgumentParser instance via the options
parameter, openstacksdk registers its standard options (such as --os-cloud)
along with all keystoneauth authentication and session options (such as
--os-auth-url, --os-username, and --insecure). It then parses
sys.argv to configure the connection. This allows your application to
define its own CLI arguments on the parser while letting openstacksdk handle
all OpenStack configuration flags:
def create_connection_from_args():
parser = argparse.ArgumentParser()
return openstack.connect(options=parser)
Create a connection via environment variables¶
You can also build a connection from OS_-prefixed environment variables. If
you have the standard OpenStack environment variables set - for example, by
sourcing an openrc file - you can create a connection without any
additional configuration:
import openstack
conn = openstack.connect()
When no cloud name is given, connect() loads its configuration
from the environment. For the full list of supported environment variables,
refer to Environment Variables.
Create a connection via explicit arguments¶
To create a Connection instance via explicit
keyword arguments, pass the connection parameters directly to
connect():
def create_connection(
auth_url,
region,
project_name,
username,
password,
user_domain,
project_domain,
):
return openstack.connect(
auth_url=auth_url,
project_name=project_name,
username=username,
password=password,
region_name=region,
user_domain_name=user_domain,
project_domain_name=project_domain,
app_name='examples',
app_version='1.0',
)
Note
To enable logging, see the Logging user guide.
Next¶
Now that you can create a connection, continue with the User Guides to work with an OpenStack service.