From 5beaeef2c3140f84b2e5a57a789460d4db9ff766 Mon Sep 17 00:00:00 2001 From: Monty Taylor Date: Thu, 3 Dec 2015 11:26:12 -0600 Subject: [PATCH] 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 --- README.rst | 31 +++++++++++++++++++++++++++++++ os_client_config/__init__.py | 23 +++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/README.rst b/README.rst index d8fde5924..156c7607a 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 00e6ff514..ac585f248 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)