From 7a4993da4190d92cb2f023d84e0e9311db38f0bf Mon Sep 17 00:00:00 2001 From: Monty Taylor Date: Mon, 22 Feb 2016 06:30:31 -0800 Subject: [PATCH] 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 --- README.rst | 17 +++++++++-- os_client_config/__init__.py | 28 +++++++++++-------- .../session-client-b581a6e5d18c8f04.yaml | 6 ++++ 3 files changed, 38 insertions(+), 13 deletions(-) create mode 100644 releasenotes/notes/session-client-b581a6e5d18c8f04.yaml diff --git a/README.rst b/README.rst index f078f3cee..54a5631ed 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 52fcb8511..6f78d2fca 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 000000000..11219016b --- /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.