Debian Codegraph @

doc/source/contributor/microversions.rst

raw
1 API Microversions
2 =================
3
4 Background
5 ----------
6
7 Cyborg uses a framework we call 'API Microversions' for allowing changes
8 to the API while preserving backward compatibility. The basic idea is
9 that a user has to explicitly ask for their request to be treated with
10 a particular version of the API. So breaking changes can be added to
11 the API without breaking users who don't specifically ask for it. This
12 is done with an HTTP header ``OpenStack-API-Version`` which has as its
13 value a string containing the name of the service, ``accelerator``, and
14 a monotonically increasing semantic version number starting from ``2.0``.
15 The full form of the header takes the form::
16
17 OpenStack-API-Version: accelerator 2.0
18
19 If a user makes a request without specifying a version, they will get the
20 ``_MIN_VERSION_STRING`` (defined in ``cyborg/api/controllers/v2/versions.py``)
21 as the default version. This value is currently ``2.0`` and is expected to
22 remain so for quite a long time.
23
24 There is a special value ``latest`` which can be specified, which will
25 allow a client to always receive the most recent version
26 (``_MAX_VERSION_STRING`` defined in ``cyborg/api/controllers/v2/versions.py``)
27 of API responses from the server.
28
29 .. warning::
30
31 The ``latest`` value is mostly meant for integration testing and
32 would be dangerous to rely on in client code since Cyborg microversions
33 are not following sever and therefore backward compatibility is not
34 guaranteed. Clients, like python-cyborgclient, should always require a
35 specific microversion but limit what is acceptable to the version range
36 that it understands at the time.
37
38 For full details please read the `Ussuri spec for microversions
39 <https://specs.openstack.org/openstack/cyborg-specs/specs/ussuri/implemented/cyborg-api.html>`_
40 and `Microversion Specification
41 <http://specs.openstack.org/openstack/api-wg/guidelines/microversion_specification.html>`_.
42
43 When do I need a new Microversion?
44 ----------------------------------
45
46 A microversion is needed when the contract to the user is
47 changed. The user contract covers many kinds of information such as:
48
49 - the Request
50
51 - the list of resource urls which exist on the accelerator
52
53 Example: adding a new accelerator_requests/{ID}/foo which didn't exist in a
54 previous version of the code
55
56 - the list of query parameters that are valid on urls
57
58 Example: adding a new parameter ``is_yellow``
59 accelerator_requests/{ID}?is_yellow=True
60
61 - the list of query parameter values for non free form fields
62
63 Example: parameter filter_by takes a small set of constants/enums "A",
64 "B", "C". Adding support for new enum "D".
65
66 - new headers accepted on a request
67
68 - the list of attributes and data structures accepted.
69
70 Example: adding a new attribute 'description' to the accelerator
71 request body
72
73 - the Response
74
75 - the list of attributes and data structures returned
76
77 Example: adding a new attribute 'description' to the output
78 of accelerator_requests/{ID}
79
80 - the allowed values of non free form fields
81
82 Example: adding a new allowed ``state`` to accelerator_requests/{ID}
83
84 - the list of status codes allowed for a particular request
85
86 Example: an API previously could return 200, 400, 403, 404 and the
87 change would make the API now also be allowed to return 409.
88
89 See [#f2]_ for the 400, 403, 404 and 415 cases.
90
91 - new headers returned on a response.
92
93 - changing a status code on a particular response.
94
95 Example: changing the return code of an API from 501 to 400.
96
97 .. note:: Fixing a bug so that a 400+ code is returned rather than a
98 500 or 503 does not require a microversion change. It's assumed
99 that clients are not expected to handle a 500 or 503 response and
100 therefore should not need to opt-in to microversion changes that
101 fixes a 500 or 503 response from happening.
102 According to the OpenStack API Working Group, a **500 Internal
103 Server Error** should **not** be returned to the user for failures
104 due to user error that can be fixed by changing the request on the
105 client side. See [#f1]_.
106
107 The following flow chart attempts to walk through the process of "do
108 we need a microversion".
109
110 .. graphviz::
111
112 digraph states {
113
114 label="Do I need a microversion?"
115
116 silent_fail[shape="diamond", style="", group=g1, label="Did we silently
117 fail to do what is asked?"];
118 ret_500[shape="diamond", style="", group=g1, label="Did we return a 500
119 before?"];
120 new_error[shape="diamond", style="", group=g1, label="Are we changing what
121 status code is returned?"];
122 new_attr[shape="diamond", style="", group=g1, label="Did we add or remove
123 an attribute to a payload?"];
124 new_param[shape="diamond", style="", group=g1, label="Did we add or remove
125 an accepted query string parameter or value?"];
126 new_resource[shape="diamond", style="", group=g1, label="Did we add or
127 remove a resource url?"];
128
129
130 no[shape="box", style=rounded, label="No microversion needed"];
131 yes[shape="box", style=rounded, label="Yes, you need a microversion"];
132 no2[shape="box", style=rounded, label="No microversion needed, it's
133 a bug"];
134
135 silent_fail -> ret_500[label=" no"];
136 silent_fail -> no2[label="yes"];
137
138 ret_500 -> no2[label="yes [1]"];
139 ret_500 -> new_error[label=" no"];
140
141 new_error -> new_attr[label=" no"];
142 new_error -> yes[label="yes"];
143
144 new_attr -> new_param[label=" no"];
145 new_attr -> yes[label="yes"];
146
147 new_param -> new_resource[label=" no"];
148 new_param -> yes[label="yes"];
149
150 new_resource -> no[label=" no"];
151 new_resource -> yes[label="yes"];
152
153 {rank=same; yes new_attr}
154 {rank=same; no2 ret_500}
155 {rank=min; silent_fail}
156
157 }
158
159
160 **Footnotes**
161
162 .. [#f1] When fixing 500 errors that previously caused stack traces, try
163 to map the new error into the existing set of errors that API call
164 could previously return (400 if nothing else is appropriate). Changing
165 the set of allowed status codes from a request is changing the
166 contract, and should be part of a microversion (except in [#f2]_).
167
168 The reason why we are so strict on contract is that we'd like
169 application writers to be able to know, for sure, what the contract is
170 at every microversion in Cyborg. If they do not, they will need to write
171 conditional code in their application to handle ambiguities.
172
173 When in doubt, consider application authors. If it would work with no
174 client side changes on both Cyborg versions, you probably don't need a
175 microversion. If, on the other hand, there is any ambiguity, a
176 microversion is probably needed.
177
178 .. [#f2] The exception to not needing a microversion when returning a
179 previously unspecified error code is the 400, 403, 404 and 415 cases.
180 This is considered OK to return even if previously unspecified in the
181 code since it's implied given keystone authentication can fail with a
182 403 and API validation can fail with a 400 for invalid json request body.
183 Request to url/resource that does not exist always fails with 404.
184 Invalid content types are handled before API methods are called which
185 results in a 415.
186
187 .. note:: When in doubt about whether or not a microversion is required
188 for changing an error response code, consult the `Cyborg team`_.
189
190 .. _`Cyborg team`: https://review.opendev.org/#/admin/groups/1243,members
191
192
193 When a microversion is not needed
194 ---------------------------------
195
196 A microversion is not needed in the following situation:
197
198 - the response
199
200 - Changing the error message without changing the response code
201 does not require a new microversion.
202
203 - Removing an inapplicable HTTP header, for example, suppose the Retry-After
204 HTTP header is being returned with a 4xx code. This header should only be
205 returned with a 503 or 3xx response, so it may be removed without bumping
206 the microversion.
207
208 - An obvious regression bug in an admin-only API where the bug can still
209 be fixed upstream on active stable branches. Admin-only APIs are less of
210 a concern for interoperability and generally a regression in behavior can
211 be dealt with as a bug fix when the documentation clearly shows the API
212 behavior was unexpectedly regressed.
213
214 In Code
215 -------
216
217 In ``cyborg/api/controllers/v2/versions.py`` we define some constants below:
218
219 - ``BASE_VERSION``: value is ``2`` which is intended to be used as the
220 Cyborg API version.
221
222 - ``MINOR_0_INITIAL_VERSION``: value is ``0`` to be used as the initial
223 value of microversion.
224
225 - ``MINOR_X_Y``: ``Y`` is the change you want to make, ``X`` is the min
226 version to support ``Y``. For example, ``MINOR_1_PROJECT_ID`` means
227 that the request ``project_id`` is supported from microversion ``2.1``.
228
229 - ``MINOR_MAX_VERSION``: the max version, which equals to latest.
230
231 - ``_MIN_VERSION_STRING``: the combination of ``BASE_VERSION`` and
232 ``MINOR_0_INITIAL_VERSION``, which means the min version of Cyborg API.
233
234 - ``_MAX_VERSION_STRING`` with the combination of ``BASE_VERSION`` and
235 ``MINOR_MAX_VERSION``, which means the max version of Cyborg API.
236
237 In ``cyborg/api/controllers/v2/utils.py``, we define the check function of
238 microversion.
239
240 For the example of ``allow_project_id()`` function, we compare the request
241 version and the defined ``MINOR_1_PROJECT_ID`` to check whether the
242 request is allowed. If the user's request with the version which is lower
243 than ``MINOR_1_PROJECT_ID``, we will raise "Request not acceptable."
244 exception to the user.
245
246 .. code:: python
247
248 def allow_project_id():
249 # v2.1 added project_id for arq patch
250 return api.request.version.minor >= versions.MINOR_1_PROJECT_ID
251
252 Adding a new API method
253 ~~~~~~~~~~~~~~~~~~~~~~~
254
255 In the controller class:
256
257 .. code:: python
258
259 def my_api_method(self, req, id):
260 if not utils.allow_project_id():
261 raise exception.NotAcceptable(_(
262 "Request not acceptable. The minimal required API "
263 "version should be %(base)s.%(opr)s") %
264 {'base': versions.BASE_VERSION,
265 'opr': versions.MINOR_1_PROJECT_ID})
266
267 This method would only be available if the caller had specified an
268 ``OpenStack-API-Version`` of >= ``accelerator 2.1``. If they had specified a
269 lower version (or not specified it and received the default of
270 ``accelerator 2.0``) the server would respond with ``HTTP/406``.
271
272 Other necessary changes
273 -----------------------
274
275 If you are adding a patch which adds a new microversion, it is
276 necessary to add changes to other places which describe your change:
277
278 * Define ``MINOR_*{int}_**`` in
279 ``cyborg/api/controllers/v2/versions.py``
280
281 * Update ``MINOR_MAX_VERSION`` to the defined ``MINOR_*{int}_**`` in
282 ``cyborg/api/controllers/v2/versions.py``
283
284 * Add a verbose description of what changed in the new version to
285 ``cyborg/api/rest_api_version_history.rst``.
286
287 * Add a :doc:`release note <releasenotes>` with a ``features``
288 section announcing the new or changed feature and the microversion.
289
290 * Update the expected versions in affected tests, for example in
291 ``cyborg.tests.unit.api.controllers.v2.test_arqs.TestARQsController#test_apply_patch_allow_project_id``.
292
293 * Make a new commit to python-cyborgclient and update corresponding
294 files to enable the newly added microversion API.
295
296 * Update the `API Reference`_ documentation as appropriate. The source
297 is located under ``api-ref/source/``.
298
299 .. _`API Reference`: https://docs.openstack.org/api-ref/accelerator/v2/index.html
300
301 If applicable, add functional sample tests under
302 ``cyborg_tempest_plugin/tests/api/``
303
304 Allocating a microversion
305 -------------------------
306
307 If you are adding a patch which adds a new microversion, it is
308 necessary to allocate the next microversion number. The minor number
309 of ``_MAX_API_VERSION`` will be incremented. This will also be the new
310 microversion number for the API change. Developers may need over time
311 to rebase their patch calculating a new version number as above based
312 on the updated value of ``_MAX_API_VERSION``.
313
314 Testing Microversioned API Methods
315 ----------------------------------
316
317 Testing a microversioned API method is very similar to a normal controller
318 method test, you just need to add the ``OpenStack-API-Version``
319 header, for example:
320
321 .. code:: python
322
323 req = fakes.HTTPRequest.blank('/testable/url/endpoint')
324 req.headers = {'OpenStack-API-Version': 'accelerator 2.1'}
325 req.api_version_request = api_version.APIVersionRequest('2.1')
326
327 controller = controller.TestableController()
328
329 res = controller.index(req)
330 ... assertions about the response ...
331
332 For many examples of testing, the canonical examples are in
333 ``cyborg.tests.unit.api.controllers.v2.test_arqs.TestARQsController#test_apply_patch_allow_project_id``.