Clarify argparse connections

The user guide shows that options passed to connection.from_config do
not have to be an argparse Namespace, contrary to the comments in
connection.from_config. This change corrects those comments and adds
a user guide example showing how argparse may be used.

Change-Id: I2a9e92cbf0aab16476001be772034e9698a24c9f
Closes-Bug: #1629331
This commit is contained in:
Matthew Edmonds 2016-09-30 14:20:55 -04:00
parent bda6753837
commit 6f2209e238
3 changed files with 29 additions and 13 deletions

View File

@ -32,12 +32,13 @@ locations:
* /etc/openstack * /etc/openstack
call :py:func:`~openstack.connection.from_config`. The ``from_config`` call :py:func:`~openstack.connection.from_config`. The ``from_config``
function takes three optional arguments, such as **cloud_name**, function takes three optional arguments:
which allows you to specify one set of cloud credentials in your
``clouds.yaml`` file. Additionally, **cloud_config** and **options** * **cloud_name** allows you to specify a cloud from your ``clouds.yaml`` file.
allow you to pass in configiration data you may have already received * **cloud_config** allows you to pass in an existing
from ``os-client-config``, as well as additional options that the ``os_client_config.config.OpenStackConfig``` object.
``os-client-config`` library may need. * **options** allows you to specify a namespace object with options to be
added to the cloud config.
.. literalinclude:: ../examples/connect.py .. literalinclude:: ../examples/connect.py
:pyobject: Opts :pyobject: Opts
@ -45,7 +46,10 @@ from ``os-client-config``, as well as additional options that the
.. literalinclude:: ../examples/connect.py .. literalinclude:: ../examples/connect.py
:pyobject: create_connection_from_config :pyobject: create_connection_from_config
.. note:: To enable logging, set ``debug=True`` in the ``Opts`` object. .. literalinclude:: ../examples/connect.py
:pyobject: create_connection_from_args
.. note:: To enable logging, set ``debug=True`` in the ``options`` object.
User Defined Location User Defined Location
********************* *********************

View File

@ -16,6 +16,7 @@ Connect to an OpenStack cloud.
For a full guide see TODO(etoews):link to docs on developer.openstack.org For a full guide see TODO(etoews):link to docs on developer.openstack.org
""" """
import argparse
import os import os
import os_client_config import os_client_config
@ -48,9 +49,8 @@ def _get_resource_value(resource_key, default):
except KeyError: except KeyError:
return default return default
opts = Opts(cloud_name=TEST_CLOUD)
occ = os_client_config.OpenStackConfig() occ = os_client_config.OpenStackConfig()
cloud = occ.get_one_cloud(opts.cloud, argparse=opts) cloud = occ.get_one_cloud(TEST_CLOUD)
SERVER_NAME = 'openstacksdk-example' SERVER_NAME = 'openstacksdk-example'
IMAGE_NAME = _get_resource_value('image_name', 'cirros-0.3.4-x86_64-uec') IMAGE_NAME = _get_resource_value('image_name', 'cirros-0.3.4-x86_64-uec')
@ -67,9 +67,20 @@ EXAMPLE_IMAGE_NAME = 'openstacksdk-example-public-image'
def create_connection_from_config(): def create_connection_from_config():
opts = Opts(cloud_name=TEST_CLOUD)
occ = os_client_config.OpenStackConfig()
cloud = occ.get_one_cloud(opts.cloud)
return connection.from_config(cloud_config=cloud, options=opts) return connection.from_config(cloud_config=cloud, options=opts)
def create_connection_from_args():
parser = argparse.ArgumentParser()
config = os_client_config.OpenStackConfig()
config.register_argparse_arguments(parser, sys.argv[1:])
args = parser.parse_args()
return connection.from_config(options=args)
def create_connection(auth_url, region, project_name, username, password): def create_connection(auth_url, region, project_name, username, password):
prof = profile.Profile() prof = profile.Profile()
prof.set_region(profile.Profile.ALL, region) prof.set_region(profile.Profile.ALL, region)

View File

@ -86,10 +86,11 @@ def from_config(cloud_name=None, cloud_config=None, options=None):
determining which cloud's configuration details determining which cloud's configuration details
will be used in creation of the will be used in creation of the
`Connection` instance. `Connection` instance.
:param options: An argparse Namespace object; allows direct passing :param options: A namespace object; allows direct passing in of options to
in of argparse options to be added to the cloud config. be added to the cloud config. This does not have to be an
This value is passed to the `argparse` argument of instance of argparse.Namespace, despite the naming of the
`os_client_config.config.OpenStackConfig.get_one_cloud`. the `os_client_config.config.OpenStackConfig.get_one_cloud`
argument to which it is passed.
:rtype: :class:`~openstack.connection.Connection` :rtype: :class:`~openstack.connection.Connection`
""" """