commit 1cfd2f1b48889a384756cb983dd0fa0529cf1a2c
Author: Ryan McCabe <rmccabe(a)redhat.com>
Date: Thu Feb 23 18:10:49 2012 -0500
Error out if inline resource names inside service groups collide
with global resource names and vice versa.
luci/lib/ClusterConf/ModelBuilder.py | 4 ++--
luci/lib/ClusterConf/TagObject.py | 17 +++++++++++++----
luci/validation/validate_resource.py | 14 ++++++--------
3 files changed, 21 insertions(+), 14 deletions(-)
---
diff --git a/luci/lib/ClusterConf/ModelBuilder.py b/luci/lib/ClusterConf/ModelBuilder.py
index 92d1197..a4e7d9f 100644
--- a/luci/lib/ClusterConf/ModelBuilder.py
+++ b/luci/lib/ClusterConf/ModelBuilder.py
@@ -1197,8 +1197,8 @@ class ModelBuilder:
if found_one is True:
break
- def searchObjectTree(self, tagtype):
+ def searchObjectTree(self, tagtype, subclass=False):
objlist = list()
- self.object_tree.searchTree(objlist, tagtype)
+ self.object_tree.searchTree(objlist, tagtype, subclass)
return objlist
diff --git a/luci/lib/ClusterConf/TagObject.py b/luci/lib/ClusterConf/TagObject.py
index 0f32b59..2d61caa 100644
--- a/luci/lib/ClusterConf/TagObject.py
+++ b/luci/lib/ClusterConf/TagObject.py
@@ -157,14 +157,23 @@ class TagObject(object):
def isRefObject(self):
return False
- def searchTree(self, objlist, tagtype):
- if self.TAG_NAME == tagtype:
- objlist.append(self)
+ def searchTree(self, objlist, tagtype, subclass=False):
+ if subclass is not True:
+ if type(tagtype) in (str, unicode):
+ if self.TAG_NAME == tagtype:
+ objlist.append(self)
+ else:
+ if type(self) == tagtype:
+ objlist.append(self)
+ else:
+ if issubclass(type(self), tagtype):
+ objlist.append(self)
+
if len(self.children) > 0:
for child in self.children:
if child is None:
continue
- child.searchTree(objlist, tagtype)
+ child.searchTree(objlist, tagtype, subclass)
def replaceChild(self, oldchild, newchild):
"""Looks up oldchild and replace it with newchild."""
diff --git a/luci/validation/validate_resource.py b/luci/validation/validate_resource.py
index 7f546b6..bdb4168 100644
--- a/luci/validation/validate_resource.py
+++ b/luci/validation/validate_resource.py
@@ -522,13 +522,11 @@ def create_resource(res_type, model, **kw):
rname = kw['resourcename'].strip()
res.addAttribute('name', rname)
+ existing_res_names = map(lambda x: x.getName(), model.searchObjectTree(BaseResource,
subclass=True))
+
if not resource_edit:
- try:
- dummy = getResourceForEdit(model, rname)
- if dummy:
- raise Exception, _('A resource named "%s" already exists.') %
rname
- except:
- pass
+ if rname in existing_res_names:
+ raise Exception, _('A resource named "%s" already exists.') % rname
errors = resource_table[res_type][0](res, rname, model, **kw)
if resource_edit and oldname != res.getName():
@@ -758,7 +756,7 @@ def validate_clusvc_form(model, **kw):
cur_res_name = 'resource "%s"' % cur_name
except Exception, e:
resObj = None
- errors.append(_('Error adding resource type %s: %s') % (res_type, str(e)))
+ errors.append(_('Error adding resource type "%s": %s') % (res_type,
str(e)))
log.exception('Error validating %s resource "%r"' % (res_type,
dummy_form))
if not resObj:
@@ -865,6 +863,6 @@ def validate_resource_form(model, **kw):
res = create_resource(res_type, model, **kw)
model.getResourcesPtr().addChild(res)
except Exception, e:
- errors.append(_('Error adding resource type %s: %s') % (res_type, str(e)))
+ errors.append(_('Error adding resource type "%s": %s') % (res_type,
str(e)))
return (False, {'errors': errors})
return (True, {})