Skip to content

Commit 81d60c7

Browse files
committed
README: Add guide on raw HTTP layer
Highlight that one can do e.g. `conn.compute.get('/servers')`. You almost certainly *don't want* to do this, but it can be helpful (see: our support for Nova's os-hypervisors API in OSC). The flow of the README is modified slightly so we go sequentially from high-level layers to low-level layers. Rubrics (header-like elements that don't produce anchors or appear in tables of contents) are also added to produce improve information hierarchy. Change-Id: Ifd4a5a2c753f6698fa4384a197e81cc5383ef312 Signed-off-by: Stephen Finucane <[email protected]>
1 parent 6dbc7e9 commit 81d60c7

File tree

1 file changed

+95
-18
lines changed

1 file changed

+95
-18
lines changed

README.rst

+95-18
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ https://docs.openstack.org/openstacksdk/latest/contributor/history.html
2020
Getting started
2121
---------------
2222

23+
.. rubric:: Authentication and connection management
24+
2325
openstacksdk aims to talk to any OpenStack cloud. To do this, it requires a
2426
configuration file. openstacksdk favours ``clouds.yaml`` files, but can also
2527
use environment variables. The ``clouds.yaml`` file should be provided by your
@@ -38,29 +40,43 @@ cloud provider or deployment tooling. An example:
3840
3941
openstacksdk will look for ``clouds.yaml`` files in the following locations:
4042

43+
* If set, the path indicated by the ``OS_CLIENT_CONFIG_FILE`` environment
44+
variable
4145
* ``.`` (the current directory)
4246
* ``$HOME/.config/openstack``
4347
* ``/etc/openstack``
4448

45-
openstacksdk consists of three layers. Most users will make use of the *proxy*
46-
layer. Using the above ``clouds.yaml``, consider listing servers:
49+
You can create a connection using the ``openstack.connect`` function. The cloud
50+
name can be either passed directly to this function or specified using the
51+
``OS_CLOUD`` environment variable. If you don't have a ``clouds.yaml`` file and
52+
instead use environment variables for configuration then you can use the
53+
special ``envvars`` cloud name to load configuration from the environment. For
54+
example:
4755

4856
.. code-block:: python
4957
5058
import openstack
5159
52-
# Initialize and turn on debug logging
53-
openstack.enable_logging(debug=True)
60+
# Initialize connection from a clouds.yaml by passing a cloud name
61+
conn_from_cloud_name = openstack.connect(cloud='mordred')
5462
55-
# Initialize connection
56-
conn = openstack.connect(cloud='mordred')
63+
# Initialize connection from a clouds.yaml using the OS_CLOUD envvar
64+
conn_from_os_cloud = openstack.connect()
5765
58-
# List the servers
59-
for server in conn.compute.servers():
60-
print(server.to_dict())
66+
# Initialize connection from environment variables
67+
conn_from_env_vars = openstack.connect(cloud='envvars')
68+
69+
.. note::
6170

62-
openstacksdk also contains a higher-level *cloud* layer based on logical
63-
operations:
71+
How this is all achieved is described in more detail `below
72+
<openstack.config>`__.
73+
74+
.. rubric:: The cloud layer
75+
76+
openstacksdk consists of four layers which all build on top of each other. The
77+
highest level layer is the *cloud* layer. Cloud layer methods are available via
78+
the top level ``Connection`` object returned by ``openstack.connect``. For
79+
example:
6480

6581
.. code-block:: python
6682
@@ -76,8 +92,10 @@ operations:
7692
for server in conn.list_servers():
7793
print(server.to_dict())
7894
79-
The benefit of this layer is mostly seen in more complicated operations that
80-
take multiple steps and where the steps vary across providers. For example:
95+
The cloud layer is based on logical operations that can potentially touch
96+
multiple services. The benefit of this layer is mostly seen in more complicated
97+
operations that take multiple steps and where the steps vary across providers.
98+
For example:
8199

82100
.. code-block:: python
83101
@@ -101,9 +119,38 @@ take multiple steps and where the steps vary across providers. For example:
101119
conn.create_server(
102120
'my-server', image=image, flavor=flavor, wait=True, auto_ip=True)
103121
104-
Finally, there is the low-level *resource* layer. This provides support for the
105-
basic CRUD operations supported by REST APIs and is the base building block for
106-
the other layers. You typically will not need to use this directly:
122+
.. rubric:: The proxy layer
123+
124+
The next layer is the *proxy* layer. Most users will make use of this layer.
125+
The proxy layer is service-specific, so methods will be available under
126+
service-specific connection attributes of the ``Connection`` object such as
127+
``compute``, ``block_storage``, ``image`` etc. For example:
128+
129+
.. code-block:: python
130+
131+
import openstack
132+
133+
# Initialize and turn on debug logging
134+
openstack.enable_logging(debug=True)
135+
136+
# Initialize connection
137+
conn = openstack.connect(cloud='mordred')
138+
139+
# List the servers
140+
for server in conn.compute.servers():
141+
print(server.to_dict())
142+
143+
.. note::
144+
145+
A list of supported services is given `below <supported-services>`__.
146+
147+
.. rubric:: The resource layer
148+
149+
Below this there is the *resource* layer. This provides support for the basic
150+
CRUD operations supported by REST APIs and is the base building block for the
151+
other layers. You typically will not need to use this directly but it can be
152+
helpful for operations where you already have a ``Resource`` object to hand.
153+
For example:
107154

108155
.. code-block:: python
109156
@@ -121,6 +168,34 @@ the other layers. You typically will not need to use this directly:
121168
for server in openstack.compute.v2.server.Server.list(session=conn.compute):
122169
print(server.to_dict())
123170
171+
.. rubric:: The raw HTTP layer
172+
173+
Finally, there is the *raw HTTP* layer. This exposes raw HTTP semantics and
174+
is effectively a wrapper around the `requests`__ API with added smarts to
175+
handle stuff like authentication and version management. As such, you can use
176+
the ``requests`` API methods you know and love, like ``get``, ``post`` and
177+
``put``, and expect to receive a ``requests.Response`` object in response
178+
(unlike the other layers, which mostly all return objects that subclass
179+
``openstack.resource.Resource``). Like the *resource* layer, you will typically
180+
not need to use this directly but it can be helpful to interact with APIs that
181+
have not or will not be supported by openstacksdk. For example:
182+
183+
.. code-block:: python
184+
185+
import openstack
186+
187+
# Initialize and turn on debug logging
188+
openstack.enable_logging(debug=True)
189+
190+
# Initialize connection
191+
conn = openstack.connect(cloud='mordred')
192+
193+
# List servers
194+
for server in openstack.compute.get('/servers').json():
195+
print(server)
196+
197+
.. __: https://requests.readthedocs.io/en/latest/
198+
124199
.. _openstack.config:
125200

126201
Configuration
@@ -145,14 +220,14 @@ environment by running ``openstack.config.loader``. For example:
145220
146221
More information at https://docs.openstack.org/openstacksdk/latest/user/config/configuration.html
147222

223+
.. _supported-services:
224+
148225
Supported services
149226
------------------
150227

151228
The following services are currently supported. A full list of all available
152229
OpenStack service can be found in the `Project Navigator`__.
153230

154-
.. __: https://www.openstack.org/software/project-navigator/openstack-components#openstack-services
155-
156231
.. note::
157232

158233
Support here does not guarantee full-support for all APIs. It simply means
@@ -302,6 +377,8 @@ OpenStack service can be found in the `Project Navigator`__.
302377
- ✔
303378
- ✔ (``openstack.instance_ha``)
304379

380+
.. __: https://www.openstack.org/software/project-navigator/openstack-components#openstack-services
381+
305382
Links
306383
-----
307384

0 commit comments

Comments
 (0)