From 0d9c0cdc304efc9b6f0d1454900f51d2d914d30f Mon Sep 17 00:00:00 2001 From: Anton Arefiev Date: Thu, 9 Jun 2016 10:11:15 +0300 Subject: [PATCH] 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 --- doc/source/http-api.rst | 8 +++++- ironic_inspector/main.py | 28 ++++++++++++------- ironic_inspector/test/unit/test_main.py | 22 +++++++++++++++ ...es-endpoint-response-d60984c40d927c1f.yaml | 10 +++++++ 4 files changed, 57 insertions(+), 11 deletions(-) create mode 100644 releasenotes/notes/fix-rules-endpoint-response-d60984c40d927c1f.yaml diff --git a/doc/source/http-api.rst b/doc/source/http-api.rst index aeff7ae6e..095f52ef0 100644 --- a/doc/source/http-api.rst +++ b/doc/source/http-api.rst @@ -131,7 +131,8 @@ authentication. Response - * 200 - OK + * 200 - OK for API version < 1.6 + * 201 - OK for API version 1.6 and higher * 400 - bad request 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 and maximum API versions supported by the server. + .. note:: + Maximum is server API version used by default. + + API Discovery ~~~~~~~~~~~~~ @@ -328,3 +333,4 @@ Version History * **1.3** endpoint for canceling running introspection * **1.4** endpoint for reapplying the introspection over stored data. * **1.5** support for Ironic node names. +* **1.6** endpoint for rules creating returns 201 instead of 200 on success. diff --git a/ironic_inspector/main.py b/ironic_inspector/main.py index de913fff2..05d054ebe 100644 --- a/ironic_inspector/main.py +++ b/ironic_inspector/main.py @@ -47,15 +47,26 @@ app = flask.Flask(__name__) LOG = utils.getProcessingLogger(__name__) MINIMUM_API_VERSION = (1, 0) -CURRENT_API_VERSION = (1, 5) +CURRENT_API_VERSION = (1, 6) _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): 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): @@ -86,13 +97,7 @@ def convert_exceptions(func): @app.before_request def check_api_version(): - requested = flask.request.headers.get(conf.VERSION_HEADER, - _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) + requested = _get_version() if requested < MINIMUM_API_VERSION or requested > CURRENT_API_VERSION: return error_response(_('Unsupported API version %(requested)s, ' @@ -279,7 +284,10 @@ def api_rules(): actions_json=body.get('actions', []), uuid=body.get('uuid'), 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/', methods=['GET', 'DELETE']) diff --git a/ironic_inspector/test/unit/test_main.py b/ironic_inspector/test/unit/test_main.py index daa77d7fb..0906e03c3 100644 --- a/ironic_inspector/test/unit/test_main.py +++ b/ironic_inspector/test/unit/test_main.py @@ -369,6 +369,28 @@ class TestApiRules(BaseAPITest): **{'as_dict.return_value': exp}) 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) create_mock.assert_called_once_with(conditions_json='cond', actions_json='act', diff --git a/releasenotes/notes/fix-rules-endpoint-response-d60984c40d927c1f.yaml b/releasenotes/notes/fix-rules-endpoint-response-d60984c40d927c1f.yaml new file mode 100644 index 000000000..1cf1379ee --- /dev/null +++ b/releasenotes/notes/fix-rules-endpoint-response-d60984c40d927c1f.yaml @@ -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.