Skip to content

Commit 6f2209e

Browse files
committed
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
1 parent bda6753 commit 6f2209e

File tree

3 files changed

+29
-13
lines changed

3 files changed

+29
-13
lines changed

doc/source/users/guides/connect_from_config.rst

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,24 @@ locations:
3232
* /etc/openstack
3333

3434
call :py:func:`~openstack.connection.from_config`. The ``from_config``
35-
function takes three optional arguments, such as **cloud_name**,
36-
which allows you to specify one set of cloud credentials in your
37-
``clouds.yaml`` file. Additionally, **cloud_config** and **options**
38-
allow you to pass in configiration data you may have already received
39-
from ``os-client-config``, as well as additional options that the
40-
``os-client-config`` library may need.
35+
function takes three optional arguments:
36+
37+
* **cloud_name** allows you to specify a cloud from your ``clouds.yaml`` file.
38+
* **cloud_config** allows you to pass in an existing
39+
``os_client_config.config.OpenStackConfig``` object.
40+
* **options** allows you to specify a namespace object with options to be
41+
added to the cloud config.
4142

4243
.. literalinclude:: ../examples/connect.py
4344
:pyobject: Opts
4445

4546
.. literalinclude:: ../examples/connect.py
4647
:pyobject: create_connection_from_config
4748

48-
.. note:: To enable logging, set ``debug=True`` in the ``Opts`` object.
49+
.. literalinclude:: ../examples/connect.py
50+
:pyobject: create_connection_from_args
51+
52+
.. note:: To enable logging, set ``debug=True`` in the ``options`` object.
4953

5054
User Defined Location
5155
*********************

examples/connect.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
For a full guide see TODO(etoews):link to docs on developer.openstack.org
1717
"""
1818

19+
import argparse
1920
import os
2021

2122
import os_client_config
@@ -48,9 +49,8 @@ def _get_resource_value(resource_key, default):
4849
except KeyError:
4950
return default
5051

51-
opts = Opts(cloud_name=TEST_CLOUD)
5252
occ = os_client_config.OpenStackConfig()
53-
cloud = occ.get_one_cloud(opts.cloud, argparse=opts)
53+
cloud = occ.get_one_cloud(TEST_CLOUD)
5454

5555
SERVER_NAME = 'openstacksdk-example'
5656
IMAGE_NAME = _get_resource_value('image_name', 'cirros-0.3.4-x86_64-uec')
@@ -67,9 +67,20 @@ def _get_resource_value(resource_key, default):
6767

6868

6969
def create_connection_from_config():
70+
opts = Opts(cloud_name=TEST_CLOUD)
71+
occ = os_client_config.OpenStackConfig()
72+
cloud = occ.get_one_cloud(opts.cloud)
7073
return connection.from_config(cloud_config=cloud, options=opts)
7174

7275

76+
def create_connection_from_args():
77+
parser = argparse.ArgumentParser()
78+
config = os_client_config.OpenStackConfig()
79+
config.register_argparse_arguments(parser, sys.argv[1:])
80+
args = parser.parse_args()
81+
return connection.from_config(options=args)
82+
83+
7384
def create_connection(auth_url, region, project_name, username, password):
7485
prof = profile.Profile()
7586
prof.set_region(profile.Profile.ALL, region)

openstack/connection.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,11 @@ def from_config(cloud_name=None, cloud_config=None, options=None):
8686
determining which cloud's configuration details
8787
will be used in creation of the
8888
`Connection` instance.
89-
:param options: An argparse Namespace object; allows direct passing
90-
in of argparse options to be added to the cloud config.
91-
This value is passed to the `argparse` argument of
92-
`os_client_config.config.OpenStackConfig.get_one_cloud`.
89+
:param options: A namespace object; allows direct passing in of options to
90+
be added to the cloud config. This does not have to be an
91+
instance of argparse.Namespace, despite the naming of the
92+
the `os_client_config.config.OpenStackConfig.get_one_cloud`
93+
argument to which it is passed.
9394
9495
:rtype: :class:`~openstack.connection.Connection`
9596
"""

0 commit comments

Comments
 (0)