summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMonty Taylor <mordred@inaugust.com>2016-02-22 06:30:31 -0800
committerMonty Taylor <mordred@inaugust.com>2016-02-22 09:29:18 -0800
commit7a4993da4190d92cb2f023d84e0e9311db38f0bf (patch)
tree698f24510b309e6e23006bb21c17e56c4d65379d
parent7865abc22b7289b2679f6848395d4850d544d1f0 (diff)
downloados-client-config-7a4993da4190d92cb2f023d84e0e9311db38f0bf.tar.gz
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
-rw-r--r--README.rst17
-rw-r--r--os_client_config/__init__.py28
-rw-r--r--releasenotes/notes/session-client-b581a6e5d18c8f04.yaml6
3 files changed, 38 insertions, 13 deletions
diff --git a/README.rst b/README.rst
index f078f3c..54a5631 100644
--- a/README.rst
+++ b/README.rst
@@ -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
------
diff --git a/os_client_config/__init__.py b/os_client_config/__init__.py
index 52fcb85..6f78d2f 100644
--- a/os_client_config/__init__.py
+++ b/os_client_config/__init__.py
@@ -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)
diff --git a/releasenotes/notes/session-client-b581a6e5d18c8f04.yaml b/releasenotes/notes/session-client-b581a6e5d18c8f04.yaml
new file mode 100644
index 0000000..1121901
--- /dev/null
+++ b/releasenotes/notes/session-client-b581a6e5d18c8f04.yaml
@@ -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.