]> review.fuel-infra Code Review - openstack-build/heat-build.git/commitdiff
Raise NotFound exceptions from database
authorZane Bitter <zbitter@redhat.com>
Tue, 12 Jun 2012 14:23:35 +0000 (16:23 +0200)
committerZane Bitter <zbitter@redhat.com>
Fri, 15 Jun 2012 09:36:33 +0000 (11:36 +0200)
Raise a specific exception (NotFound) if something is not found in the
database. Then we can match on this exception, rather than searching the
error message (which effectively makes the text of error messages part of
the API).

Change-Id: I22673ffb1407cf95b1fe24eaec8824039c1dda4e
Signed-off-by: Zane Bitter <zbitter@redhat.com>
heat/db/sqlalchemy/api.py
heat/engine/cloud_watch.py
heat/engine/resources.py

index f382d1d6b28d00069febd49394c12ad283b8c80b..401aff19856436c3494382dfcf2732567aa6d96a 100644 (file)
@@ -16,6 +16,7 @@
 '''Implementation of SQLAlchemy backend.'''
 from sqlalchemy.orm.session import Session
 
+from heat.common.exception import NotFound
 from heat.db.sqlalchemy import models
 from heat.db.sqlalchemy.session import get_session
 
@@ -36,7 +37,7 @@ def raw_template_get(context, template_id):
                         filter_by(id=template_id).first()
 
     if not result:
-        raise Exception("raw template with id %s not found" % template_id)
+        raise NotFound("raw template with id %s not found" % template_id)
 
     return result
 
@@ -45,7 +46,7 @@ def raw_template_get_all(context):
     results = model_query(context, models.RawTemplate).all()
 
     if not results:
-        raise Exception('no raw templates were found')
+        raise NotFound('no raw templates were found')
 
     return results
 
@@ -67,7 +68,7 @@ def parsed_template_get_all(context):
     results = model_query(context, models.ParsedTemplate).all()
 
     if not results:
-        raise Exception('no parsed templates were found')
+        raise NotFound('no parsed templates were found')
 
     return results
 
@@ -84,7 +85,7 @@ def resource_get(context, resource_id):
                         filter_by(id=resource_id).first()
 
     if not result:
-        raise Exception("resource with id %s not found" % resource_id)
+        raise NotFound("resource with id %s not found" % resource_id)
 
     return result
 
@@ -101,7 +102,7 @@ def resource_get_all(context):
     results = model_query(context, models.Resource).all()
 
     if not results:
-        raise Exception('no resources were found')
+        raise NotFound('no resources were found')
 
     return results
 
@@ -118,7 +119,7 @@ def resource_get_all_by_stack(context, stack_id):
                 filter_by(stack_id=stack_id).all()
 
     if not results:
-        raise Exception("no resources for stack_id %s were found" % stack_id)
+        raise NotFound("no resources for stack_id %s were found" % stack_id)
 
     return results
 
@@ -153,7 +154,7 @@ def stack_create(context, values):
 def stack_delete(context, stack_name):
     s = stack_get(context, stack_name)
     if not s:
-        raise Exception('Attempt to delete a stack with id: %s %s' %
+        raise NotFound('Attempt to delete a stack with id: %s %s' %
                         (stack_name, 'that does not exist'))
 
     session = Session.object_session(s)
@@ -243,7 +244,7 @@ def watch_rule_delete(context, watch_name):
                         filter_by(name=watch_name).first()
 
     if not wr:
-        raise Exception('Attempt to delete a watch_rule with name: %s %s' %
+        raise NotFound('Attempt to delete a watch_rule with name: %s %s' %
                         (watch_name, 'that does not exist'))
 
     session = Session.object_session(wr)
@@ -274,7 +275,7 @@ def watch_data_delete(context, watch_name):
                      filter_by(name=watch_name).all()
 
     if not ds:
-        raise Exception('Attempt to delete watch_data with name: %s %s' %
+        raise NotFound('Attempt to delete watch_data with name: %s %s' %
                         (watch_name, 'that does not exist'))
 
     session = Session.object_session(ds)
index 5470b263f152f22e208a67417fb6c826e630dd11..c405b593226546522efe56b9829129badc12a157 100644 (file)
@@ -74,7 +74,7 @@ class CloudWatchAlarm(Resource):
     def handle_delete(self):
         try:
             db_api.watch_rule_delete(self.stack.context, self.name)
-        except Exception as ex:
+        except exception.NotFound:
             pass
 
     def FnGetRefId(self):
index 75f6c288a339618646068804bbd89b2237c6d323..f21e709c7fd8485b03e3dfd2eeb478c6841a1eb1 100644 (file)
@@ -20,9 +20,6 @@ import logging
 from novaclient.v1_1 import client as nc
 from keystoneclient.v2_0 import client as kc
 
-from novaclient.exceptions import BadRequest
-from novaclient.exceptions import NotFound
-
 from heat.common import exception
 from heat.common.config import HeatEngineConfigOpts
 from heat.db import api as db_api
@@ -196,13 +193,14 @@ class Resource(object):
         else:
             try:
                 db_api.resource_get(self.stack.context, self.id).delete()
-            except Exception as ex:
+            except exception.NotFound:
                 # Don't fail on delete if the db entry has
                 # not been created yet.
-                if 'not found' not in str(ex):
-                    self.state_set(self.DELETE_FAILED)
-                    logger.exception('Delete %s from DB' % str(self))
-                    return str(ex)
+                pass
+            except Exception as ex:
+                self.state_set(self.DELETE_FAILED)
+                logger.exception('Delete %s from DB' % str(self))
+                return str(ex)
 
             self.state_set(self.DELETE_COMPLETE)