diff options
author | Rick Harris <rconradharris@gmail.com> | 2013-01-28 23:25:23 +0000 |
---|---|---|
committer | Rick Harris <rconradharris@gmail.com> | 2013-01-30 18:00:08 +0000 |
commit | 8ab6a0fd9e16f650e202c8cf631fa03004451d9f (patch) | |
tree | 945e7265d3e4a05c6a6bfaa077afc7696436cc7a /nova/utils.py | |
parent | 05751bb7861892b949dedeccc283ce48abee0d16 (diff) | |
download | nova-8ab6a0fd9e16f650e202c8cf631fa03004451d9f.tar.gz nova-8ab6a0fd9e16f650e202c8cf631fa03004451d9f.tar.xz nova-8ab6a0fd9e16f650e202c8cf631fa03004451d9f.zip |
Code cleanup for rebuild block device mapping
* Removes extra call to get block-device-mappings when detaching during
recreate.
* Untangle network handling from block-device handling in rebuild
* DRYs up evacuate hosts tests
* Temporary mutation supports dicts as well as objs
* Make instance recreate exceptions more specific to aid testing
Change-Id: I242f9f6a7d329f27b06b7e81b9a418e01682c82e
Diffstat (limited to 'nova/utils.py')
-rw-r--r-- | nova/utils.py | 29 |
1 files changed, 25 insertions, 4 deletions
diff --git a/nova/utils.py b/nova/utils.py index 52d4868c9..7ceb16563 100644 --- a/nova/utils.py +++ b/nova/utils.py @@ -1087,21 +1087,42 @@ def temporary_mutation(obj, **kwargs): with temporary_mutation(context, read_deleted="yes"): do_something_that_needed_deleted_objects() """ + def is_dict_like(thing): + return hasattr(thing, 'has_key') + + def get(thing, attr, default): + if is_dict_like(thing): + return thing.get(attr, default) + else: + return getattr(thing, attr, default) + + def set_value(thing, attr, val): + if is_dict_like(thing): + thing[attr] = val + else: + setattr(thing, attr, val) + + def delete(thing, attr): + if is_dict_like(thing): + del thing[attr] + else: + delattr(thing, attr) + NOT_PRESENT = object() old_values = {} for attr, new_value in kwargs.items(): - old_values[attr] = getattr(obj, attr, NOT_PRESENT) - setattr(obj, attr, new_value) + old_values[attr] = get(obj, attr, NOT_PRESENT) + set_value(obj, attr, new_value) try: yield finally: for attr, old_value in old_values.items(): if old_value is NOT_PRESENT: - del obj[attr] + delete(obj, attr) else: - setattr(obj, attr, old_value) + set_value(obj, attr, old_value) def generate_mac_address(): |