From 259350ba7e04ce82971bad09155412cb689ec062 Mon Sep 17 00:00:00 2001 From: Mehdi Abaakouk Date: Fri, 27 Sep 2013 16:32:21 +0200 Subject: [PATCH] api: return 404 if a alarm is not found The patch return 404 instead of 400 if a alarm is not found Fixes bug #1232041 Change-Id: I797546cf650f6d78a3103867bcb3ac7c024ac08f --- ceilometer/api/controllers/v2.py | 25 +++++++++----------- tests/api/v2/test_alarm_scenarios.py | 7 ++++++ tests/api/v2/test_app.py | 34 ++++++++++++++-------------- 3 files changed, 35 insertions(+), 31 deletions(-) diff --git a/ceilometer/api/controllers/v2.py b/ceilometer/api/controllers/v2.py index 64dfa0ed..c4b82aa2 100644 --- a/ceilometer/api/controllers/v2.py +++ b/ceilometer/api/controllers/v2.py @@ -76,6 +76,15 @@ state_kind = ["ok", "alarm", "insufficient data"] operation_kind = wtypes.Enum(str, 'lt', 'le', 'eq', 'ne', 'ge', 'gt') +class EntityNotFound(Exception): + code = 404 + + def __init__(self, entity, id): + super(EntityNotFound, self).__init__( + _("%(entity)s %(id)s Not Found") % {'entity': entity, + 'id': id}) + + class BoundedInt(wtypes.UserType): basetype = int name = 'bounded int' @@ -866,14 +875,6 @@ class Resource(_Base): ) -class ResourceNotFound(Exception): - code = 404 - - def __init__(self, id): - super(ResourceNotFound, self).__init__( - _("Resource %s Not Found") % id) - - class ResourcesController(rest.RestController): """Works on resources.""" @@ -897,7 +898,7 @@ class ResourcesController(rest.RestController): resources = list(pecan.request.storage_conn.get_resources( resource=resource_id, project=authorized_project)) if not resources: - raise ResourceNotFound(resource_id) + raise EntityNotFound(_('Resource'), resource_id) return Resource.from_db_and_links(resources[0], self._resource_links(resource_id)) @@ -1229,12 +1230,8 @@ class AlarmController(rest.RestController): auth_project = acl.get_limited_to_project(pecan.request.headers) alarms = list(self.conn.get_alarms(alarm_id=self._id, project=auth_project)) - # FIXME (flwang): Need to change this to return a 404 error code when - # we get a release of WSME that supports it. if len(alarms) < 1: - error = _("Unknown alarm") - pecan.response.translatable_error = error - raise wsme.exc.ClientSideError(unicode(error)) + raise EntityNotFound(_('Alarm'), self._id) return alarms[0] def _record_change(self, data, now, on_behalf_of=None, type=None): diff --git a/tests/api/v2/test_alarm_scenarios.py b/tests/api/v2/test_alarm_scenarios.py index 9d429900..8b027bbf 100644 --- a/tests/api/v2/test_alarm_scenarios.py +++ b/tests/api/v2/test_alarm_scenarios.py @@ -167,6 +167,13 @@ class TestAlarms(FunctionalTest, for r in data if 'combination_rule' in r), set(['or'])) + def test_get_not_existing_alarm(self): + resp = self.get_json('/alarms/alarm-id-3', expect_errors=True) + self.assertEqual(resp.status_code, 404) + self.assertEqual(jsonutils.loads(resp.body)['error_message'] + ['faultstring'], + "Alarm alarm-id-3 Not Found") + def test_get_alarm(self): alarms = self.get_json('/alarms', q=[{'field': 'name', diff --git a/tests/api/v2/test_app.py b/tests/api/v2/test_app.py index 727dc7b4..768e2f3c 100644 --- a/tests/api/v2/test_app.py +++ b/tests/api/v2/test_app.py @@ -132,11 +132,11 @@ class TestApiMiddleware(FunctionalTest): # Ensure translated messages get placed properly into json faults self.stubs.Set(gettextutils, 'get_localized_message', self._fake_get_localized_message) - response = self.get_json('/alarms/-', - expect_errors=True, - headers={"Accept": - "application/json"} - ) + response = self.post_json('/alarms', params={}, + expect_errors=True, + headers={"Accept": + "application/json"} + ) self.assertEqual(response.status_int, 400) self.assertEqual(response.content_type, "application/json") self.assertTrue(response.json['error_message']) @@ -167,11 +167,11 @@ class TestApiMiddleware(FunctionalTest): self.stubs.Set(gettextutils, 'get_localized_message', self._fake_get_localized_message) - response = self.get_json('/alarms/-', - expect_errors=True, - headers={"Accept": - "application/xml,*/*"} - ) + response = self.post_json('/alarms', params={}, + expect_errors=True, + headers={"Accept": + "application/xml,*/*"} + ) self.assertEqual(response.status_int, 400) self.assertEqual(response.content_type, "application/xml") self.assertEqual(response.xml.tag, 'error_message') @@ -184,13 +184,13 @@ class TestApiMiddleware(FunctionalTest): self.stubs.Set(gettextutils, 'get_localized_message', self._fake_get_localized_message) - response = self.get_json('/alarms/-', - expect_errors=True, - headers={"Accept": - "application/xml,*/*", - "Accept-Language": - "en-US"} - ) + response = self.post_json('/alarms', params={}, + expect_errors=True, + headers={"Accept": + "application/xml,*/*", + "Accept-Language": + "en-US"} + ) self.assertEqual(response.status_int, 400) self.assertEqual(response.content_type, "application/xml") self.assertEqual(response.xml.tag, 'error_message')