Go to file
Chris Dent e346558d5f Improve Version.matches() method
Use of Version within the placement service has made it clear
that the Version object knowing the min and max versions is
useful when making Version.matches comparisons.

Therefore the matches method has been updated to refer to
min_version and max_version attributes if they are not set
in the matches() request. The extract_version function now
sets those attributes based on the version_list that is
passed to it.

If min and max are not set on the Version object, they default
to negative versions, which are not possible in microversions
and mean matches() (without override args) will be false. The
use of explicit values (instead of None) is so python3 is happy
(and it is better anyway).

Change-Id: I71d091d037abcd068601b32722a09094ae74e658
2018-03-20 17:23:26 +00:00
2016-03-22 15:29:38 +00:00
2016-03-22 15:29:38 +00:00
2018-03-15 18:15:25 +00:00

microversion_parse

A small set of functions to manage OpenStack microversion headers that can be used in middleware, application handlers and decorators to effectively manage microversions.

get_version

A simple parser for OpenStack microversion headers:

import microversion_parse

# headers is a dict of headers with folded (comma-separated
# values) or a list of header, value tuples
version = microversion_parse.get_version(
    headers, service_type='compute',
    legacy_headers=['x-openstack-nova-api-version'])

# If headers are not already available, a dict of headers
# can be extracted from the WSGI environ
headers = microversion_parse.headers_from_wsgi_environ(environ)
version = microversion_parse.get_version(
    headers, service_type='placement')

It processes microversion headers with the standard form:

OpenStack-API-Version: compute 2.1

In that case, the response will be '2.1'.

If provided with a legacy_headers argument, this is treated as a list of additional headers to check for microversions. Some examples of headers include:

OpenStack-telemetry-api-version: 2.1
OpenStack-nova-api-version: 2.1
X-OpenStack-nova-api-version: 2.1

If a version string cannot be found, None will be returned. If the input is incorrect usual Python exceptions (ValueError, TypeError) are allowed to raise to the caller.

parse_version_string

A function to turn a version string into a Version, a comparable namedtuple:

version_tuple = microversion_parse.parse_version_string('2.1')

If the provided string is not a valid microversion string, TypeError is raised.

extract_version

Combines get_version and parse_version_string to find and validate a microversion for a given service type in a collection of headers:

version_tuple = microversion_parse.extract_version(
    headers,  # a representation of headers, as accepted by get_version
    service_type,  # service type identify to match in headers
    versions_list,  # an ordered list of strings of version numbers that
                    # are the valid versions presented by this service
)

latest will be translated to whatever the max version is in versions_list.

If the found version is not in versions_list a ValueError is raised.

Note that extract_version does not support legacy_headers.

Description
Simple library for parsing OpenStack microversion headers.
Readme 387 KiB
Languages
Python 97.2%
Makefile 2.8%