Fix response code for rule creating API

This change introduces new return code (201 instead of 200) for
POST /v1/rules endpoint on success rule creation.

API less 1.6 continues returning 200.

Default API version was changed from minimum to maximum
which Inspector can support.

Change-Id: I911c7c241d16b9948ee4b6db92b127c7f8f374ae
This commit is contained in:
Anton Arefiev 2016-06-09 10:11:15 +03:00
parent 457c1277de
commit 0d9c0cdc30
4 changed files with 57 additions and 11 deletions

View File

@ -131,7 +131,8 @@ authentication.
Response Response
* 200 - OK * 200 - OK for API version < 1.6
* 201 - OK for API version 1.6 and higher
* 400 - bad request * 400 - bad request
Response body: JSON dictionary with introspection rule representation (the Response body: JSON dictionary with introspection rule representation (the
@ -282,6 +283,10 @@ major version and is always ``1`` for now, ``Y`` is a minor version.
``X-OpenStack-Ironic-Inspector-API-Maximum-Version`` headers with minimum ``X-OpenStack-Ironic-Inspector-API-Maximum-Version`` headers with minimum
and maximum API versions supported by the server. and maximum API versions supported by the server.
.. note::
Maximum is server API version used by default.
API Discovery API Discovery
~~~~~~~~~~~~~ ~~~~~~~~~~~~~
@ -328,3 +333,4 @@ Version History
* **1.3** endpoint for canceling running introspection * **1.3** endpoint for canceling running introspection
* **1.4** endpoint for reapplying the introspection over stored data. * **1.4** endpoint for reapplying the introspection over stored data.
* **1.5** support for Ironic node names. * **1.5** support for Ironic node names.
* **1.6** endpoint for rules creating returns 201 instead of 200 on success.

View File

@ -47,15 +47,26 @@ app = flask.Flask(__name__)
LOG = utils.getProcessingLogger(__name__) LOG = utils.getProcessingLogger(__name__)
MINIMUM_API_VERSION = (1, 0) MINIMUM_API_VERSION = (1, 0)
CURRENT_API_VERSION = (1, 5) CURRENT_API_VERSION = (1, 6)
_LOGGING_EXCLUDED_KEYS = ('logs',) _LOGGING_EXCLUDED_KEYS = ('logs',)
def _get_version():
ver = flask.request.headers.get(conf.VERSION_HEADER,
_DEFAULT_API_VERSION)
try:
requested = tuple(int(x) for x in ver.split('.'))
except (ValueError, TypeError):
return error_response(_('Malformed API version: expected string '
'in form of X.Y'), code=400)
return requested
def _format_version(ver): def _format_version(ver):
return '%d.%d' % ver return '%d.%d' % ver
_DEFAULT_API_VERSION = _format_version(MINIMUM_API_VERSION) _DEFAULT_API_VERSION = _format_version(CURRENT_API_VERSION)
def error_response(exc, code=500): def error_response(exc, code=500):
@ -86,13 +97,7 @@ def convert_exceptions(func):
@app.before_request @app.before_request
def check_api_version(): def check_api_version():
requested = flask.request.headers.get(conf.VERSION_HEADER, requested = _get_version()
_DEFAULT_API_VERSION)
try:
requested = tuple(int(x) for x in requested.split('.'))
except (ValueError, TypeError):
return error_response(_('Malformed API version: expected string '
'in form of X.Y'), code=400)
if requested < MINIMUM_API_VERSION or requested > CURRENT_API_VERSION: if requested < MINIMUM_API_VERSION or requested > CURRENT_API_VERSION:
return error_response(_('Unsupported API version %(requested)s, ' return error_response(_('Unsupported API version %(requested)s, '
@ -279,7 +284,10 @@ def api_rules():
actions_json=body.get('actions', []), actions_json=body.get('actions', []),
uuid=body.get('uuid'), uuid=body.get('uuid'),
description=body.get('description')) description=body.get('description'))
return flask.jsonify(rule_repr(rule, short=False))
response_code = (200 if _get_version() < (1, 6) else 201)
return flask.make_response(
flask.jsonify(rule_repr(rule, short=False)), response_code)
@app.route('/v1/rules/<uuid>', methods=['GET', 'DELETE']) @app.route('/v1/rules/<uuid>', methods=['GET', 'DELETE'])

View File

@ -369,6 +369,28 @@ class TestApiRules(BaseAPITest):
**{'as_dict.return_value': exp}) **{'as_dict.return_value': exp})
res = self.app.post('/v1/rules', data=json.dumps(data)) res = self.app.post('/v1/rules', data=json.dumps(data))
self.assertEqual(201, res.status_code)
create_mock.assert_called_once_with(conditions_json='cond',
actions_json='act',
uuid=self.uuid,
description=None)
self.assertEqual(exp, json.loads(res.data.decode('utf-8')))
@mock.patch.object(rules, 'create', autospec=True)
def test_create_api_less_1_6(self, create_mock):
data = {'uuid': self.uuid,
'conditions': 'cond',
'actions': 'act'}
exp = data.copy()
exp['description'] = None
create_mock.return_value = mock.Mock(spec=rules.IntrospectionRule,
**{'as_dict.return_value': exp})
headers = {conf.VERSION_HEADER:
main._format_version((1, 5))}
res = self.app.post('/v1/rules', data=json.dumps(data),
headers=headers)
self.assertEqual(200, res.status_code) self.assertEqual(200, res.status_code)
create_mock.assert_called_once_with(conditions_json='cond', create_mock.assert_called_once_with(conditions_json='cond',
actions_json='act', actions_json='act',

View File

@ -0,0 +1,10 @@
---
upgrade:
- API "POST /v1/rules" returns 201 response code instead of
200 on creating success. API version was bumped to 1.6.
API less than 1.6 continues to return 200.
- Default API version was changed from minimum to maximum
which Inspector can support.
fixes:
- Fix response return code for rule creating endpoint, it
returns 201 now instead of 200 on success.