Allow session_client to take the same args as make_client

make_client is a great, simple yet flexible way to get a fully featured
Client object. simple_client is similar for Session objects, but lacks
the argparse and arbitrary kwargs that make_client - plus it has a weird
name. Since adding those two features to make_client did not make it too
confusing - do the same for simple_client. Also, rename it to
session_client (with a backwards-compat alias) and add it to the README
docs.

In the process of doing this, extract the "get me a cloud config"
functinality into an additional helper function - get_config.

Change-Id: Iadd24dfa021f870b3e5858bab8cd91fc96a373c2
This commit is contained in:
Monty Taylor 2016-02-22 06:30:31 -08:00
parent 7865abc22b
commit 7a4993da41
3 changed files with 38 additions and 13 deletions

View File

@ -362,8 +362,6 @@ will get you a fully configured `novaclient` instance.
.. code-block:: python
import argparse
import os_client_config
nova = os_client_config.make_client('compute')
@ -382,6 +380,21 @@ If you want to do the same thing but also support command line parsing.
If you want to get fancier than that in your python, then the rest of the
API is available to you. But often times, you just want to do the one thing.
Constructing Mounted Session Objects
------------------------------------
What if you want to make direct REST calls via a Session interface? You're
in luck. The same interface for `make_client` is supported for `session_client`
and will return you a keystoneauth Session object that is mounted on the
endpoint for the service you're looking for.
import os_client_config
session = os_client_config.session_client('compute', cloud='vexxhost')
response = session.get('/servers')
server_list = response.json()['servers']
Source
------

View File

@ -18,7 +18,18 @@ from os_client_config import cloud_config
from os_client_config.config import OpenStackConfig # noqa
def simple_client(service_key, cloud=None, region_name=None):
def get_config(service_key=None, options=None, **kwargs):
config = OpenStackConfig()
if options:
config.register_argparse_options(options, sys.argv, service_key)
parsed_options = options.parse_known_args(sys.argv)
else:
parsed_options = None
return config.get_one_cloud(options=parsed_options, **kwargs)
def session_client(service_key, options=None, **kwargs):
"""Simple wrapper function. It has almost no features.
This will get you a raw requests Session Adapter that is mounted
@ -31,8 +42,10 @@ def simple_client(service_key, cloud=None, region_name=None):
get_session_client on it. This function is to make it easy to poke
at OpenStack REST APIs with a properly configured keystone session.
"""
return OpenStackConfig().get_one_cloud(
cloud=cloud, region_name=region_name).get_session_client(service_key)
cloud = get_config(service_key=service_key, options=options, **kwargs)
return cloud.get_session_client(service_key)
# Backwards compat - simple_client was a terrible name
simple_client = session_client
def make_client(service_key, constructor=None, options=None, **kwargs):
@ -45,14 +58,7 @@ def make_client(service_key, constructor=None, options=None, **kwargs):
variables and clouds.yaml - and takes as **kwargs anything you'd expect
to pass in.
"""
cloud = get_config(service_key=service_key, options=options, **kwargs)
if not constructor:
constructor = cloud_config._get_client(service_key)
config = OpenStackConfig()
if options:
config.register_argparse_options(options, sys.argv, service_key)
parsed_options = options.parse_args(sys.argv)
else:
parsed_options = None
cloud = config.get_one_cloud(options=parsed_options, **kwargs)
return cloud.get_legacy_client(service_key, constructor)

View File

@ -0,0 +1,6 @@
---
features:
- Added kwargs and argparse processing for session_client.
deprecations:
- Renamed simple_client to session_client. simple_client
will remain as an alias for backwards compat.