[openstack-dev] Introducing simple client creation in os-client-config

Monty Taylor mordred at inaugust.com
Thu Feb 25 15:38:59 UTC 2016


Hey all,

I just responded to a mailing list thread about a question related to 
constructing a nova Client object - and I realized I'd been remiss about 
sending out an announcement of a couple of little but helpful features 
we landed in os-client-config - namely, simple factory functions to 
create Client objects and mounted Session objects.

As with all os-client-config things, they are fully aware of the 
standard OS_ env vars, as well as clouds.yaml config files - and can 
also delegate to argparse processing.

I wrote a blog post with examples:

http://inaugust.com/posts/simple-openstack-clients.html

but I'll include examples here too:

* Client Library Client Objects *

# Make a nova client object that uses env vars for auth info:

nova = os_client_config.make_client('compute')

# Make a glance client object for the cloud named 'mtvexx' in my 
clouds.yaml file:

client = os_client_config.make_client('image', cloud='mtvxx')

# Make a neutron client by passing in all of the values directly

client = os_client_config.make_client(
     'network',
     auth_url='https://example.com', username='my-user',
     password='awesome-password', project_name='my-project',
     region_name='the-best-region')

# Make a barbican client from env vars and all the standard arguments
# passed on the comand line:

import argparse
client = os_client_config.make_client(
     'key-manager', options=argparse.ArgumentParser())

# Swift has a Connection object, not a Client object, but that's fine
# We're an equal opportunity player
client = os_client_config.make_client('object-store')

I hope that covers most of the common end-user use cases with using
OpenStack Client Libraries. If you need more flexibility, you can always
create an os-client-config CloudConfig object and call
get_legacy_client, but I'm a big believer in one step instead of three
if you can get away with it:

config = os_client_config.OpenStackConfig()
cloud_config = config.get_one_cloud(cloud='vexxhost')
client = cloud_config.get_legacy_client('compute')

* Mounted Keystone Session *

What if what you want to do is make some direct REST calls to an 
OpenStack service, but you want to be able to do env vars or argparse or 
clouds.yaml files to configure your authentication? Well - you're in luck:

# Make a nova client object that uses env vars for auth info:

client = os_client_config.session_client('compute')

That will get you a keystoneauth Session object that has been "mounted" 
on the compute service. So you can do this:

response = session.get('/servers')
server_list = response.json()['servers']

session_client supports the exact same interface as make_client does, so 
all the above examples will work, but you'll get a ksa Session instead 
of a Client object.



More information about the OpenStack-dev mailing list