summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMonty Taylor <mordred@inaugust.com>2015-12-03 11:26:12 -0600
committerMonty Taylor <mordred@inaugust.com>2015-12-07 02:42:47 +0000
commit5beaeef2c3140f84b2e5a57a789460d4db9ff766 (patch)
treeb469da33b4e5d3e4e1674a758de8291847415e0d
parented2f34b06a7d581fb5fdd9811e3f8a7f748a2ce4 (diff)
downloados-client-config-5beaeef2c3140f84b2e5a57a789460d4db9ff766.tar.gz
Add simple helper function for client construction
Often times you don't want to take advantage of all the flexibility, you simple want the basic works-like-it-should thing. Add a warpper around get_legacy_client to do tht one thing. Change-Id: I086dc4a8e762d4e8e56e01cabe2386577f2ceec8
-rw-r--r--README.rst31
-rw-r--r--os_client_config/__init__.py23
2 files changed, 54 insertions, 0 deletions
diff --git a/README.rst b/README.rst
index d8fde59..156c760 100644
--- a/README.rst
+++ b/README.rst
@@ -319,3 +319,34 @@ with - as well as a consumption argument.
options = parser.parse_args()
cloud = cloud_config.get_one_cloud(argparse=options)
+
+Constructing OpenStack Client objects
+-------------------------------------
+
+If all you want to do is get a Client object from a python-*client library,
+and you want it to do all the normal things related to clouds.yaml, `OS_`
+environment variables, a hepler function is provided.
+
+::
+
+ import argparse
+
+ from novaclient import client
+ import os_client_config
+
+ nova = os_client_config.make_client('compute', client.Client)
+
+If you want to do the same thing but also support command line parsing.
+
+::
+
+ import argparse
+
+ from novaclient import client
+ import os_client_config
+
+ nova = os_client_config.make_client(
+ 'compute', client.Client, options=argparse.ArgumentParser())
+
+If you want to get fancier than that in your python, then the rest of the
+API is avaiable to you. But often times, you just want to do the one thing.
diff --git a/os_client_config/__init__.py b/os_client_config/__init__.py
index 00e6ff5..ac585f2 100644
--- a/os_client_config/__init__.py
+++ b/os_client_config/__init__.py
@@ -12,6 +12,8 @@
# License for the specific language governing permissions and limitations
# under the License.
+import sys
+
from os_client_config.config import OpenStackConfig # noqa
@@ -30,3 +32,24 @@ def simple_client(service_key, cloud=None, region_name=None):
"""
return OpenStackConfig().get_one_cloud(
cloud=cloud, region_name=region_name).get_session_client('compute')
+
+
+def make_client(service_key, constructor, options=None, **kwargs):
+ """Simple wrapper for getting a client instance from a client lib.
+
+ OpenStack Client Libraries all have a fairly consistent constructor
+ interface which os-client-config supports. In the simple case, there
+ is one and only one right way to construct a client object. If as a user
+ you don't want to do fancy things, just use this. It honors OS_ environment
+ variables and clouds.yaml - and takes as **kwargs anything you'd expect
+ to pass in.
+ """
+ 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 = config.get_one_cloud(options=parsed_options, **kwargs)
+ return cloud_config.get_legacy_client(service_key, constructor)