File: usage.rst

package info (click to toggle)
python-os-api-ref 1.5.0%2Bdfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 404 kB
  • sloc: python: 925; makefile: 8
file content (340 lines) | stat: -rw-r--r-- 10,468 bytes parent folder | download | duplicates (4)
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
Usage
=====

``os-api-ref`` is designed to be used inside of a sphinx tree that is
devoted solely to the documentation of the API.

Modify your ``source/conf.py`` file to include ``os_api_ref`` in the
list of sphinx extensions. This extension assumes you are also using
``openstackdocstheme`` for some of the styling, and may not fully work if you
are not.

.. code-block:: python

   # Add any Sphinx extension module names here, as strings. They can be
   # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom ones.

   extensions = [
       'os_api_ref',
   ]


Stanzas
=======

rest_method
-----------

The ``rest_method`` stanza is a way to declare that a section is about
a particular REST method. It takes the form of:

.. code-block:: rst

   .. rest_method:: <METHODNAME> <url>

``METHODNAME`` should be one of the commonly used REST methods or HTTP verbs.
This stanza should be the first element in a ``section`` that has some
descriptive title about the method. An example from the Nova
documentation is:

.. code-block:: rst

   List Servers
   ============

   .. rest_method:: GET /v2.1/{tenant_id}/servers

   Lists IDs, names, and links for all servers.


   Servers contain a status attribute that indicates the current server
   state. You can filter on the server status when you complete a list
   servers request. The server status is returned in the response
   body. The possible server status values are:

   ...

This is going to do a slightly unexpected transform where the
``rest_method`` is pivoted up and into the section title to produce an
HTML line of the form: <METHOD> <URL> <SECTIONTITLE> <SHOW/HIDE
BUTTON>.

The entire contents of the ``List Servers`` section will then be
hidden by default, with a button to open it on demand.

rest_parameters
---------------

The ``rest_parameters`` stanza is a solution to the problem of tables
in ``rst``.

A REST API that uses JSON has a large number of structured parameters
that include type, location (i.e. is this in the query, the header,
the path, the body), whether or not this parameter is required, as well as
the desire to provide a long description about each parameter. And, assuming
some consistent modeling, that parameter will show up in multiple calls. A
``server_id`` used in the path is always going to have the same
meaning.

It is natural to want to display this data in a tabular way to show
all these dimensions. However, tables in ``rst`` are quite cumbersome, and
repeating the same data over and over again is error prone.

The ``rest_parameters`` stanza solves this by having the inline markup
be a yaml list of ``name: value`` pairs. ``name`` is the name of the
parameter. ``value`` is the key to lookup the rest of the details for
this parameter in a parameters file.  In each method,
there should be one ``rest_parameters`` stanza for the request, and
another ``rest_parameters`` stanza for the response.

.. code-block:: rst

   .. rest_parameters:: parameters.yaml

      - tenant_id: tenant_id
      - changes-since: changes-since
      - image: image_query
      - flavor: flavor_query
      - name: server_name_query
      - status: server_status_query
      - host: host_query
      - limit: limit
      - marker: marker

And corresponding entries in ``parameters.yaml``:

.. code-block:: yaml

   tenant_id:
     description: |
       The UUID of the tenant in a multi-tenancy cloud.
     in: path
     required: true
     type: string
   ...
   changes-since:
     description: |
       Filters the response by a date and time when the image last changed status.
       Use this query parameter to check for changes since a previous request rather
       than re-downloading and re-parsing the full status at each polling interval.
       If data has changed, the call returns only the items changed since the ``changes-since``
       time. If data has not changed since the ``changes-since`` time, the call returns an
       empty list.\nTo enable you to keep track of changes, this filter also displays images
       that were deleted if the ``changes-since`` value specifies a date in the last 30 days.
       Items deleted more than 30 days ago might be returned, but it is not guaranteed.
       The date and time stamp format is `ISO 8601 <https://en.wikipedia.org/wiki/ISO_8601>`_:

       ::

          CCYY-MM-DDThh:mm:ss±hh:mm

       The ``±hh:mm`` value, if included, returns the time zone as an offset from UTC.
       For example, ``2015-08-27T09:49:58-05:00``.
       If you omit the time zone, the UTC time zone is assumed.
     in: query
     required: false
     type: string
   server_status_query:
     description: |
       Filters the response by a server status, as a string. For example, ``ACTIVE``.
     in: query
     required: false
     type: string

Every ``rest_parameters`` stanza specifies the lookup file it will
use. This gives you the freedom to decide how you would like to split
up your parameters, ranging from a single global file, to a dedicated
file for every stanza, or anywhere in between.

parameters file format
----------------------

The parameters file is inspired by the OpenAPI (aka: Swagger)
specification.  The OpenAPI specification provides a property object
which categorizes the parameters by type and describes how the parameter is used.
The following fields exist for every entry:

in
  where this parameter exists. One of ``header``, ``path``,
  ``query``, ``body``.

description
  a free form description of the parameter. This can be
  multiline (if using the | or > tags in yaml), and supports ``rst``
  format syntax.

required
  whether this parameter is required or not. If ``required:
  false`` the parameter name will be rendered with an (Optional)
  keyword next to it

type
  the javascript/json type of the field. one of ``boolean``, ``int``,
  ``float``, ``string``, ``uuid``, ``array``, ``object``.

min_version
  the microversion that this parameter was introduced at. Will render
  a *new in $version* stanza in the html output.

max_version
  the last version that includes this parameter. Will render
  a *Available until $version* stanza in the html output.


rest_status_code
----------------

The ``rest_status_code`` stanza is how you can show what HTTP status codes your
API uses and what they indicate.

.. code-block:: rst

   .. rest_status_code:: <success|error> <location of http-status.yaml file>

This stanza should be the first element after the narrative section of the
method description.

An example from the Designate documentation is:

.. code-block:: rst
   :emphasize-lines: 11-25

   Create Zone
   ===========

   .. rest_method::  POST /v2/zones

   Create a zone

   Response codes
   --------------

   .. rest_status_code:: success status.yaml

      - 200
      - 100
      - 201


   .. rest_status_code:: error status.yaml

      - 405
      - 403
      - 401
      - 400
      - 500
      - 409: duplicate_zone

And corresponding entries in ``http-status.yaml``:

.. code-block:: yaml

    100:
      default: |
        An unusual code for an API
    200:
      default: |
        Request was successful.
    201:
      default: >
        Request has been fulfilled and new resource created. The ``Location`` header
        has the URL to the new item.
    400:
      default: |
        Some content in the request was invalid
      zone_data_error: |
        Some of the data for the zone in the request is unavailable to the service.
    401:
      default: |
        User must authenticate before making a request.
    403:
      default: |
         Policy does not allow current user to do this operation.
    405:
      default: |
        Method is not valid for this endpoint and resource.
    409:
     default: |
        This resource has an action in progress that would conflict with this request.
     duplicate_zone: |
        There is already a zone with this name.
    500:
      default: |
        Something went wrong with the service which prevents it from fulfilling the request.

This RST example creates two HTML tables of response codes, one for success and one for
errors.

status file format
------------------

This is a simple yaml file, with a single object of status codes and the
reasons that each would be used.

Each status code **must** have a default entry in the status yaml file. The default entry is used
in the ``rest_status_code`` stanza when a code is listed with no value or lookup key.

There may be situations where the reason for a code may be different across
endpoints, or a different message may be appropriate.

In this case, adding a entry at the same level as the ``default`` and
referencing that in the stanza like so:

.. code-block:: yaml

   - 409: duplicate_zone

This will override the default message with the newly defined one.

You can get a copy of a starter status file from the os-api-ref repository,
by downloading :download:`http-status.yaml <http-status.yaml>`.

rest_expand_all
---------------

The ``rest_expand_all`` stanza is used to place a control in the
document that will be a global Show / Hide for all sections. There are
times when this is extremely nice to have.


Including Sample Files
======================

To refer to a sample file in a ``rst`` file, use the
``rst`` directive, ``literalinclude``. Typically, the content sent
or received is of type JSON, so the language role is set to javascript.
The example immediately follows the parameter listing in the ``rst`` file.
An example of an included Nova response sample file:

.. code-block:: rst

   .. literalinclude:: ../../doc/api_samples/os-evacuate/server-evacuate-resp.json
      :language: javascript


Runtime Warnings
================

The extension tries to help when it can by pointing out that something isn't
matching up correctly. The following warnings are generated when
issues are found:

* parameters file is not found
* parameters file is not valid yaml, i.e.
  missing colon after the name
* a lookup value in the ``rst`` file is not found in the parameters file
* the parameters file is not sorted as outlined in the rules below

The sorting rules for parameters file is that first elements should be
sorted by ``in``, going from earliest to latest processed.

#. header
#. path
#. query
#. body

After that, the parameters should be sorted by name, lower case alpha
numerically.

The sort enforcement is because in large parameters files it helps
prevent unintended duplicates.