From f8fbbd07265763b2df4903133e1f73b049b6834d Mon Sep 17 00:00:00 2001 From: Zane Bitter Date: Tue, 12 Jun 2012 16:23:35 +0200 Subject: [PATCH] Raise NotFound exceptions from database 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 --- heat/db/sqlalchemy/api.py | 19 ++++++++++--------- heat/engine/cloud_watch.py | 2 +- heat/engine/resources.py | 14 ++++++-------- 3 files changed, 17 insertions(+), 18 deletions(-) diff --git a/heat/db/sqlalchemy/api.py b/heat/db/sqlalchemy/api.py index f382d1d6b2..401aff1985 100644 --- a/heat/db/sqlalchemy/api.py +++ b/heat/db/sqlalchemy/api.py @@ -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) diff --git a/heat/engine/cloud_watch.py b/heat/engine/cloud_watch.py index 5470b263f1..c405b59322 100644 --- a/heat/engine/cloud_watch.py +++ b/heat/engine/cloud_watch.py @@ -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): diff --git a/heat/engine/resources.py b/heat/engine/resources.py index 75f6c288a3..f21e709c7f 100644 --- a/heat/engine/resources.py +++ b/heat/engine/resources.py @@ -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)