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
This commit is contained in:
parent
ed2f34b06a
commit
5beaeef2c3
31
README.rst
31
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.
|
||||
|
@ -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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user