Revision 8301885b lib/cmdlib.py
b/lib/cmdlib.py | ||
---|---|---|
11798 | 11798 |
return result |
11799 | 11799 |
|
11800 | 11800 |
|
11801 |
def PrepareContainerMods(mods, private_fn): |
|
11802 |
"""Prepares a list of container modifications by adding a private data field. |
|
11803 |
|
|
11804 |
@type mods: list of tuples; (operation, index, parameters) |
|
11805 |
@param mods: List of modifications |
|
11806 |
@type private_fn: callable or None |
|
11807 |
@param private_fn: Callable for constructing a private data field for a |
|
11808 |
modification |
|
11809 |
@rtype: list |
|
11810 |
|
|
11811 |
""" |
|
11812 |
if private_fn is None: |
|
11813 |
fn = lambda: None |
|
11814 |
else: |
|
11815 |
fn = private_fn |
|
11816 |
|
|
11817 |
return [(op, idx, params, fn()) for (op, idx, params) in mods] |
|
11818 |
|
|
11819 |
|
|
11820 |
def ApplyContainerMods(kind, container, chgdesc, mods, |
|
11821 |
create_fn, modify_fn, remove_fn): |
|
11822 |
"""Applies descriptions in C{mods} to C{container}. |
|
11823 |
|
|
11824 |
@type kind: string |
|
11825 |
@param kind: One-word item description |
|
11826 |
@type container: list |
|
11827 |
@param container: Container to modify |
|
11828 |
@type chgdesc: None or list |
|
11829 |
@param chgdesc: List of applied changes |
|
11830 |
@type mods: list |
|
11831 |
@param mods: Modifications as returned by L{PrepareContainerMods} |
|
11832 |
@type create_fn: callable |
|
11833 |
@param create_fn: Callback for creating a new item (L{constants.DDM_ADD}); |
|
11834 |
receives absolute item index, parameters, list of applied changes and |
|
11835 |
private data object as added by L{PrepareContainerMods}, returns new item |
|
11836 |
@type modify_fn: callable |
|
11837 |
@param modify_fn: Callback for modifying an existing item |
|
11838 |
(L{constants.DDM_MODIFY}); receives absolute item index, item, parameters, |
|
11839 |
list of applied changes and private data object as added by |
|
11840 |
L{PrepareContainerMods} |
|
11841 |
@type remove_fn: callable |
|
11842 |
@param remove_fn: Callback on removing item; receives absolute item index, |
|
11843 |
item, list of applied changes and private data object as added by |
|
11844 |
L{PrepareContainerMods} |
|
11845 |
|
|
11846 |
""" |
|
11847 |
for (op, idx, params, private) in mods: |
|
11848 |
if idx == -1: |
|
11849 |
# Append |
|
11850 |
absidx = len(container) - 1 |
|
11851 |
elif idx < 0: |
|
11852 |
raise IndexError("Not accepting negative indices") |
|
11853 |
else: |
|
11854 |
absidx = idx |
|
11855 |
|
|
11856 |
if op == constants.DDM_ADD: |
|
11857 |
if create_fn is None: |
|
11858 |
item = params |
|
11859 |
else: |
|
11860 |
item = create_fn(absidx + 1, params, chgdesc, private) |
|
11861 |
|
|
11862 |
if idx == -1: |
|
11863 |
container.append(item) |
|
11864 |
else: |
|
11865 |
assert idx >= 0 |
|
11866 |
# list.insert does so before the specified index |
|
11867 |
container.insert(idx, item) |
|
11868 |
else: |
|
11869 |
# Retrieve existing item |
|
11870 |
try: |
|
11871 |
item = container[absidx] |
|
11872 |
except IndexError: |
|
11873 |
raise IndexError("Invalid %s index %s" % (kind, idx)) |
|
11874 |
|
|
11875 |
if op == constants.DDM_REMOVE: |
|
11876 |
assert not params |
|
11877 |
|
|
11878 |
if remove_fn is not None: |
|
11879 |
remove_fn(absidx, item, chgdesc, private) |
|
11880 |
|
|
11881 |
if chgdesc is not None: |
|
11882 |
chgdesc.append(("%s/%s" % (kind, absidx), "remove")) |
|
11883 |
|
|
11884 |
assert container[absidx] == item |
|
11885 |
del container[absidx] |
|
11886 |
elif op == constants.DDM_MODIFY: |
|
11887 |
if modify_fn is not None: |
|
11888 |
modify_fn(absidx, item, params, chgdesc, private) |
|
11889 |
else: |
|
11890 |
raise errors.ProgrammerError("Unhandled operation '%s'" % op) |
|
11891 |
|
|
11892 |
|
|
11801 | 11893 |
class LUInstanceSetParams(LogicalUnit): |
11802 | 11894 |
"""Modifies an instances's parameters. |
11803 | 11895 |
|
Also available in: Unified diff