summaryrefslogtreecommitdiff
path: root/doc/source/user/using.rst
blob: aa1165e3bcc419fefbc83ca89f612fc20d4139fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
==========================================
 Using os-client-config in an Application
==========================================

Usage
-----

The simplest and least useful thing you can do is:

.. code-block:: python

  python -m os_client_config.config

Which will print out whatever if finds for your config. If you want to use
it from python, which is much more likely what you want to do, things like:

Get a named cloud.

.. code-block:: python

  import os_client_config

  cloud_config = os_client_config.OpenStackConfig().get_one_cloud(
      'internap', region_name='ams01')
  print(cloud_config.name, cloud_config.region, cloud_config.config)

Or, get all of the clouds.

.. code-block:: python

  import os_client_config

  cloud_config = os_client_config.OpenStackConfig().get_all_clouds()
  for cloud in cloud_config:
      print(cloud.name, cloud.region, cloud.config)

argparse
--------

If you're using os-client-config from a program that wants to process
command line options, there is a registration function to register the
arguments that both os-client-config and keystoneauth know how to deal
with - as well as a consumption argument.

.. code-block:: python

  import argparse
  import sys

  import os_client_config

  cloud_config = os_client_config.OpenStackConfig()
  parser = argparse.ArgumentParser()
  cloud_config.register_argparse_arguments(parser, sys.argv)

  options = parser.parse_args()

  cloud = cloud_config.get_one_cloud(argparse=options)

Constructing OpenStack SDK object
---------------------------------

If what you want to do is get an OpenStack SDK Connection and you want it to
do all the normal things related to clouds.yaml, `OS_` environment variables,
a helper function is provided. The following will get you a fully configured
`openstack.connection.Connection` instance.

.. code-block:: python

  import os_client_config

  sdk = os_client_config.make_sdk()

If you want to do the same thing but on a named cloud.

.. code-block:: python

  import os_client_config

  sdk = os_client_config.make_sdk(cloud='mtvexx')

If you want to do the same thing but also support command line parsing.

.. code-block:: python

  import argparse

  import os_client_config

  sdk = os_client_config.make_sdk(options=argparse.ArgumentParser())

It should be noted that OpenStack SDK has ways to construct itself that allow
for additional flexibility. If the helper function here does not meet your
needs, you should see the `from_config` method of
`openstack.connection.Connection <https://developer.openstack.org/sdks/python/openstacksdk/users/guides/connect_from_config.html>`_

Constructing shade objects
--------------------------

If what you want to do is get a
`shade <https://docs.openstack.org/infra/shade/>`_ OpenStackCloud object, a
helper function that honors clouds.yaml and `OS_` environment variables is
provided. The following will get you a fully configured `OpenStackCloud`
instance.

.. code-block:: python

  import os_client_config

  cloud = os_client_config.make_shade()

If you want to do the same thing but on a named cloud.

.. code-block:: python

  import os_client_config

  cloud = os_client_config.make_shade(cloud='mtvexx')

If you want to do the same thing but also support command line parsing.

.. code-block:: python

  import argparse

  import os_client_config

  cloud = os_client_config.make_shade(options=argparse.ArgumentParser())

Constructing REST API Clients
-----------------------------

What if you want to make direct REST calls via a Session interface? You're
in luck. A similar interface is available as with `openstacksdk` and `shade`.
The main difference is that you need to specify which service you want to
talk to and `make_rest_client` will return you a keystoneauth Session object
that is mounted on the endpoint for the service you're looking for.

.. code-block:: python

  import os_client_config

  session = os_client_config.make_rest_client('compute', cloud='vexxhost')

  response = session.get('/servers')
  server_list = response.json()['servers']

Constructing Legacy Client objects
----------------------------------

If you want get an old-style 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 helper function is also provided. The following
will get you a fully configured `novaclient` instance.

.. code-block:: python

  import os_client_config

  nova = os_client_config.make_client('compute')

If you want to do the same thing but on a named cloud.

.. code-block:: python

  import os_client_config

  nova = os_client_config.make_client('compute', cloud='mtvexx')

If you want to do the same thing but also support command line parsing.

.. code-block:: python

  import argparse

  import os_client_config

  nova = os_client_config.make_client(
      'compute', options=argparse.ArgumentParser())

If you want to get fancier than that in your python, then the rest of the
API is available to you. But often times, you just want to do the one thing.