Refactoring the usage of nova.exception.Duplicate

This commit is contained in:
Brian Waldon
2011-04-26 04:26:14 +00:00
committed by Tarmac
8 changed files with 45 additions and 24 deletions

View File

@@ -61,8 +61,7 @@ def _gen_key(context, user_id, key_name):
# creation before creating key_pair
try:
db.key_pair_get(context, user_id, key_name)
raise exception.Duplicate(_("The key_pair %s already exists")
% key_name)
raise exception.KeyPairExists(key_name=key_name)
except exception.NotFound:
pass
private_key, public_key, fingerprint = crypto.generate_key_pair()

View File

@@ -81,7 +81,7 @@ class DbDriver(object):
user_ref = db.user_create(context.get_admin_context(), values)
return self._db_user_to_auth_user(user_ref)
except exception.Duplicate, e:
raise exception.Duplicate(_('User %s already exists') % name)
raise exception.UserExists(user=name)
def _db_user_to_auth_user(self, user_ref):
return {'id': user_ref['id'],
@@ -132,8 +132,7 @@ class DbDriver(object):
try:
project = db.project_create(context.get_admin_context(), values)
except exception.Duplicate:
raise exception.Duplicate(_("Project can't be created because "
"project %s already exists") % name)
raise exception.ProjectExists(project=name)
for member in members:
db.project_add_member(context.get_admin_context(),

View File

@@ -171,7 +171,7 @@ class LdapDriver(object):
def create_user(self, name, access_key, secret_key, is_admin):
"""Create a user"""
if self.__user_exists(name):
raise exception.Duplicate(_("LDAP user %s already exists") % name)
raise exception.LDAPUserExists(user=name)
if FLAGS.ldap_user_modify_only:
if self.__ldap_user_exists(name):
# Retrieve user by name
@@ -226,8 +226,7 @@ class LdapDriver(object):
description=None, member_uids=None):
"""Create a project"""
if self.__project_exists(name):
raise exception.Duplicate(_("Project can't be created because "
"project %s already exists") % name)
raise exception.ProjectExists(project=name)
if not self.__user_exists(manager_uid):
raise exception.NotFound(_("Project can't be created because "
"manager %s doesn't exist")
@@ -471,8 +470,7 @@ class LdapDriver(object):
description, member_uids=None):
"""Create a group"""
if self.__group_exists(group_dn):
raise exception.Duplicate(_("Group can't be created because "
"group %s already exists") % name)
raise exception.LDAPGroupExists(group=name)
members = []
if member_uids is not None:
for member_uid in member_uids:
@@ -512,8 +510,7 @@ class LdapDriver(object):
raise exception.NotFound(_("The group at dn %s doesn't exist") %
group_dn)
if self.__is_in_group(uid, group_dn):
raise exception.Duplicate(_("User %(uid)s is already a member of "
"the group %(group_dn)s") % locals())
raise exception.LDAPMembershipExists(uid=uid, group_dn=group_dn)
attr = [(self.ldap.MOD_ADD, 'member', self.__uid_to_dn(uid))]
self.conn.modify_s(group_dn, attr)

View File

@@ -71,10 +71,6 @@ class VolumeNotFound(NotFound):
super(VolumeNotFound, self).__init__(message)
class Duplicate(Error):
pass
class NotAuthorized(Error):
pass
@@ -228,3 +224,37 @@ class InvalidVLANPortGroup(Invalid):
class ImageUnacceptable(Invalid):
message = _("Image %(image_id)s is unacceptable") + ": %(reason)s"
#TODO(bcwaldon): EOL this exception!
class Duplicate(NovaException):
pass
class KeyPairExists(Duplicate):
message = _("Key pair %(key_name)s already exists.")
class UserExists(Duplicate):
message = _("User %(user)s already exists.")
class LDAPUserExists(UserExists):
message = _("LDAP user %(user)s already exists.")
class LDAPGroupExists(Duplicate):
message = _("LDAP group %(group)s already exists.")
class LDAPMembershipExists(Duplicate):
message = _("User %(uid)s is already a member of "
"the group %(group_dn)s")
class ProjectExists(Duplicate):
message = _("Project %(project)s already exists.")
class InstanceExists(Duplicate):
message = _("Instance %(name)s already exists.")

View File

@@ -143,8 +143,7 @@ class HyperVConnection(driver.ComputeDriver):
""" Create a new VM and start it."""
vm = self._lookup(instance.name)
if vm is not None:
raise exception.Duplicate(_('Attempt to create duplicate vm %s') %
instance.name)
raise exception.InstanceExists(name=instance.name)
user = manager.AuthManager().get_user(instance['user_id'])
project = manager.AuthManager().get_project(instance['project_id'])

View File

@@ -100,8 +100,7 @@ class VMWareVMOps(object):
"""
vm_ref = self._get_vm_ref_from_the_name(instance.name)
if vm_ref:
raise exception.Duplicate(_("Attempted to create a VM with a name"
" %s, but that already exists on the host") % instance.name)
raise exception.InstanceExists(name=instance.name)
client_factory = self._session._get_vim().client.factory
service_content = self._session._get_vim().get_service_content()

View File

@@ -643,8 +643,7 @@ class VMHelper(HelperBase):
if n == 0:
return None
elif n > 1:
raise exception.Duplicate(_('duplicate name found: %s') %
name_label)
raise exception.InstanceExists(name=name_label)
else:
return vm_refs[0]

View File

@@ -127,8 +127,7 @@ class VMOps(object):
instance_name = instance.name
vm_ref = VMHelper.lookup(self._session, instance_name)
if vm_ref is not None:
raise exception.Duplicate(_('Attempted to create'
' non-unique name %s') % instance_name)
raise exception.InstanceExists(name=instance_name)
#ensure enough free memory is available
if not VMHelper.ensure_free_mem(self._session, instance):