Change the behaviour of addattr/setattr parameters.

setattr and addattr can now be used both to set all values of
ANY attribute. the last setattr always resets the attribute to
the specified value and all addattr append to it.

Examples:
user-mod testuser --setattr=title=msc
  title: msc
user-mod testuser --setattr=title=msb
  title: msb
user-mod testuser --addattr=title=msc
  title: msb, msc
user-mod testuser --setattr=title=
  title:
user-mod testuser --setattr=title=msc --addattr=msb
  title: msc, msb
user-mod testuser --setattr=title=ing --addattr=bc
  title: ing, bc
user-mod testuser --setattr=title=doc
  title: doc

It's not very user friendly, but it's going to be used very very
rarely in special conditions in the CLI and we can use it to save
lots of JSON-RPC roundtrips in the webUI.

This version includes calling the validation of Params during the setting of the attrs.
This commit is contained in:
Adam Young 2010-08-13 16:20:41 -04:00
parent f15758dbea
commit 030b5dab93
2 changed files with 40 additions and 35 deletions

View File

@ -519,11 +519,12 @@ class Command(HasParam):
if len(value) == 0:
# None means "delete this attribute"
value = None
if attr not in self.params:
if append and attr in newdict:
newdict[attr].append(value)
else:
newdict[attr] = [value]
if attr in self.params:
value = self.params[attr](value)
if append and attr in newdict:
newdict[attr].append(value)
else:
newdict[attr] = [value]
return newdict
def __attributes_2_entry(self, kw):
@ -540,7 +541,11 @@ class Command(HasParam):
adddict = self.__convert_2_dict(kw['setattr'], append=False)
if kw.get('addattr'):
adddict.update(self.__convert_2_dict(kw['addattr']))
for (k, v) in self.__convert_2_dict(kw['addattr']).iteritems():
if k in adddict:
adddict[k] += v
else:
adddict[k] = v
for name in adddict:
value = adddict[name]

View File

@ -419,6 +419,35 @@ class LDAPUpdate(LDAPQuery, crud.Update):
entry_attrs = self.args_options_2_entry(**options)
"""
Some special handling is needed because we need to update the
values here rather than letting ldap.update_entry() do the work. We
have to do the work of adding new values to an existing attribute
because if we pass just what is addded only the new values get
set.
"""
if 'addattr' in options:
setset = set(get_attributes(options.get('setattr', [])))
addset = set(get_attributes(options.get('addattr', [])))
difflist = list(addset.difference(setset))
if difflist:
try:
(dn, old_entry) = ldap.get_entry(
dn, difflist, normalize=self.obj.normalize_dn
)
except errors.ExecutionError, e:
try:
(dn, old_entry) = self._call_exc_callbacks(
keys, options, e, ldap.get_entry, dn, attrs_list,
normalize=self.obj.normalize_dn
)
except errors.NotFound:
self.obj.handle_not_found(*keys)
for a in old_entry:
if not isinstance(entry_attrs[a], (list, tuple)):
entry_attrs[a] = [entry_attrs[a]]
entry_attrs[a] += old_entry[a]
if options.get('all', False):
attrs_list = ['*']
else:
@ -436,35 +465,6 @@ class LDAPUpdate(LDAPQuery, crud.Update):
self, ldap, dn, entry_attrs, attrs_list, *keys, **options
)
"""
Some special handling is needed because we need to update the
values here rather than letting ldap.update_entry() do the work. We
have to do the work of adding new values to an existing attribute
because if we pass just what is addded only the new values get
set.
"""
if 'addattr' in options:
try:
(dn, old_entry) = ldap.get_entry(
dn, attrs_list, normalize=self.obj.normalize_dn
)
except errors.ExecutionError, e:
try:
(dn, old_entry) = self._call_exc_callbacks(
keys, options, e, ldap.get_entry, dn, attrs_list,
normalize=self.obj.normalize_dn
)
except errors.NotFound:
self.obj.handle_not_found(*keys)
attrlist = get_attributes(options['addattr'])
for attr in attrlist:
if attr in old_entry:
if type(entry_attrs[attr]) in (tuple,list):
entry_attrs[attr] = old_entry[attr] + entry_attrs[attr]
else:
old_entry[attr].append(entry_attrs[attr])
entry_attrs[attr] = old_entry[attr]
try:
ldap.update_entry(dn, entry_attrs, normalize=self.obj.normalize_dn)
except errors.ExecutionError, e: