Skip to content

Example Code

Here is a code snippet to be regarded as a starting point in order to use the various python clients available to interact with the different OpenStack services, once authenticated via Kerberos.

Packages required: keystoneauth1, keystoneauth1[kerberos] and the specific client you need, python-novaclient for the sake of this example:

When run from aiadm, since all the OS environment variables will be set for you, this code will print all the available instances for the current project (which you can check with echo $OS_PROJECT_NAME):

import os

from keystoneauth1 import session
from keystoneauth1.extras.kerberos import MappedKerberos
from novaclient import client

NOVA_API_VERSION = '2'

auth = MappedKerberos(auth_url=os.environ.get('OS_AUTH_URL'),
                      project_name=os.environ.get('OS_PROJECT_NAME'),
                      project_domain_id=os.environ.get('OS_PROJECT_DOMAIN_ID'),
                      identity_provider=os.environ.get('OS_IDENTITY_PROVIDER'),
                      protocol=os.environ.get('OS_PROTOCOL'),
                      mutual_auth=os.environ.get('OS_MUTUAL_AUTH'))

sess = session.Session(auth=auth)
nova = client.Client(NOVA_API_VERSION, session=sess)
nova.servers.list()

Being the output:

[<Server: my-server-test>,
 <Server: my-k8s-cluster-001-eorcbetzolub-minion-0>,
 <Server: my-k8s-cluster-001-eorcbetzolub-minion-1>,
 <Server: my-k8s-cluster-001-eorcbetzolub-master-0>]