diff --git a/ceilometer/api/controllers/v2.py b/ceilometer/api/controllers/v2.py index 54c8e7474..64dfa0edf 100644 --- a/ceilometer/api/controllers/v2.py +++ b/ceilometer/api/controllers/v2.py @@ -2,10 +2,12 @@ # # Copyright © 2012 New Dream Network, LLC (DreamHost) # Copyright 2013 IBM Corp. +# Copyright © 2013 eNovance # -# Author: Doug Hellmann -# Angus Salkeld -# Eoghan Glynn +# Authors: Doug Hellmann +# Angus Salkeld +# Eoghan Glynn +# Julien Danjou # # Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain @@ -864,6 +866,14 @@ 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.""" @@ -886,14 +896,8 @@ class ResourcesController(rest.RestController): authorized_project = acl.get_limited_to_project(pecan.request.headers) resources = list(pecan.request.storage_conn.get_resources( resource=resource_id, project=authorized_project)) - # FIXME (flwang): Need to change this to return a 404 error code when - # we get a release of WSME that supports it. if not resources: - error = _("Unknown resource") - pecan.response.translatable_error = error - raise wsme.exc.InvalidInput("resource_id", - resource_id, - unicode(error)) + raise ResourceNotFound(resource_id) return Resource.from_db_and_links(resources[0], self._resource_links(resource_id)) diff --git a/ceilometer/api/middleware.py b/ceilometer/api/middleware.py index ad35b1e82..5a936f28f 100644 --- a/ceilometer/api/middleware.py +++ b/ceilometer/api/middleware.py @@ -125,7 +125,7 @@ class ParsableErrorMiddleware(object): fault['faultstring'] = ( gettextutils.get_localized_message( error, user_locale)) - body = [json.dumps({'error_message': json.dumps(fault)})] + body = [json.dumps({'error_message': fault})] except ValueError as err: body = [json.dumps({'error_message': '\n'.join(app_iter)})] state['headers'].append(('Content-Type', 'application/json')) diff --git a/tests/api/v2/test_alarm_scenarios.py b/tests/api/v2/test_alarm_scenarios.py index 7e51a2630..9d4299008 100644 --- a/tests/api/v2/test_alarm_scenarios.py +++ b/tests/api/v2/test_alarm_scenarios.py @@ -243,9 +243,8 @@ class TestAlarms(FunctionalTest, resp = self.post_json('/alarms', params=json, expect_errors=True, status=400, headers=self.auth_headers) self.assertEqual( - resp.json['error_message'], - '{"debuginfo": null, "faultcode": "Client", "faultstring": ' - '"%s is mandatory"}' % field) + resp.json['error_message']['faultstring'], + '%s is mandatory' % field) alarms = list(self.conn.get_alarms()) self.assertEqual(4, len(alarms)) @@ -323,10 +322,9 @@ class TestAlarms(FunctionalTest, alarms = list(self.conn.get_alarms()) self.assertEqual(4, len(alarms)) self.assertEqual( - resp.json['error_message'], - '{"debuginfo": null, "faultcode": "Client", "faultstring": ' - '"threshold_rule and combination_rule cannot ' - 'be set at the same time"}') + resp.json['error_message']['faultstring'], + 'threshold_rule and combination_rule cannot ' + 'be set at the same time') def test_post_alarm_defaults(self): to_check = { diff --git a/tests/api/v2/test_app.py b/tests/api/v2/test_app.py index 8033891e2..727dc7b4c 100644 --- a/tests/api/v2/test_app.py +++ b/tests/api/v2/test_app.py @@ -18,7 +18,6 @@ # under the License. """Test basic ceilometer-api app """ -import json import os from oslo.config import cfg @@ -141,8 +140,8 @@ class TestApiMiddleware(FunctionalTest): self.assertEqual(response.status_int, 400) self.assertEqual(response.content_type, "application/json") self.assertTrue(response.json['error_message']) - fault = json.loads(response.json['error_message']) - self.assertEqual(fault['faultstring'], self.no_lang_translated_error) + self.assertEqual(response.json['error_message']['faultstring'], + self.no_lang_translated_error) def test_xml_parsable_error_middleware_404(self): response = self.get_json('/invalid_path', diff --git a/tests/api/v2/test_list_resources_scenarios.py b/tests/api/v2/test_list_resources_scenarios.py index 6cf8c47d1..7c869dda3 100644 --- a/tests/api/v2/test_list_resources_scenarios.py +++ b/tests/api/v2/test_list_resources_scenarios.py @@ -19,6 +19,7 @@ """ import datetime +import json import logging import testscenarios @@ -249,7 +250,10 @@ class TestListResources(FunctionalTest, self.assertEqual(resp2["resource_id"], "resource-id-2") resp3 = self.get_json('/resources/resource-id-3', expect_errors=True) - self.assertEqual(resp3.status_code, 400) + self.assertEqual(resp3.status_code, 404) + self.assertEqual(json.loads(resp3.body)['error_message'] + ['faultstring'], + "Resource resource-id-3 Not Found") def test_with_user(self): sample1 = sample.Sample(