]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Gracefully handle fetching nonexistent rule
authorJohn Schwarz <jschwarz@redhat.com>
Mon, 3 Aug 2015 13:56:27 +0000 (16:56 +0300)
committerIhar Hrachyshka <ihrachys@redhat.com>
Mon, 3 Aug 2015 16:16:54 +0000 (16:16 +0000)
Currently, if we invoke the API for 'show rule' but the rule does not
exist, an exception is raised from deep within Neutron. This in turns
causes an uncaught exception and the user will see 'ServerFault'. This
patch proposes a fix for this scenario - the case where the rule does
not exist is handled and a NeutronException is caused, causing a proper
'NotFound' error on the client side instead.

Partially-Implements: blueprint quantum-qos-api
Change-Id: Ic703a0865d1cfa057ab1ad5290b793b22df06af6

neutron/common/exceptions.py
neutron/services/qos/qos_plugin.py
neutron/tests/api/test_qos.py

index 7dc39bf4800ccba5713d5b0c7698df746c2e92aa..40bb50e7c4a5685df022e6381f90c221fb30548a 100644 (file)
@@ -97,6 +97,11 @@ class QosPolicyNotFound(NotFound):
     message = _("QoS policy %(policy_id)s could not be found")
 
 
+class QosRuleNotFound(NotFound):
+    message = _("QoS rule %(rule_id)s for policy %(policy_id)s "
+                "could not be found")
+
+
 class PortNotFoundOnNetwork(NotFound):
     message = _("Port %(port_id)s could not be found "
                 "on network %(net_id)s")
index d66acc2685cf5055f6c06023a5bcf6cd01f300ae..8a11499e59df4b28b090df7a7ff60c41b6479ba5 100644 (file)
@@ -128,8 +128,10 @@ class QoSPlugin(qos.QoSPluginBase):
                                         policy_id, fields=None):
         # validate that we have access to the policy
         self._get_policy_obj(context, policy_id)
-        return rule_object.QosBandwidthLimitRule.get_by_id(context,
-                                                           rule_id)
+        rule = rule_object.QosBandwidthLimitRule.get_by_id(context, rule_id)
+        if not rule:
+            raise n_exc.QosRuleNotFound(policy_id=policy_id, rule_id=rule_id)
+        return rule
 
     @db_base_plugin_common.filter_fields
     @db_base_plugin_common.convert_result_to_dict
index f476ecf1da7a9bd9e3e32772631f517b99b9c5aa..9a043148f2d1d9a728d17d2cf31e7d9e883b2ca3 100644 (file)
@@ -294,7 +294,7 @@ class QosBandwidthLimitRuleTestJSON(base.BaseAdminNetworkTest):
         self.assertEqual(rule['id'], retrieved_policy['id'])
 
         self.admin_client.delete_bandwidth_limit_rule(policy['id'], rule['id'])
-        self.assertRaises(exceptions.ServerFault,
+        self.assertRaises(exceptions.NotFound,
                           self.admin_client.show_bandwidth_limit_rule,
                           policy['id'], rule['id'])