summaryrefslogtreecommitdiff
path: root/nova/context.py
diff options
context:
space:
mode:
authorDan Smith <dansmith@redhat.com>2017-04-20 12:18:10 -0700
committerDan Smith <dansmith@redhat.com>2017-05-23 08:12:46 -0700
commita5f800b02477e767883ed661e4b0f827d6debe54 (patch)
tree9712ba29f15e650b12c9d5dfe4ef096ac20503c9 /nova/context.py
parentbb7c6ebb5648150430363c12ebc25a1c405ca79f (diff)
downloadnova-a5f800b02477e767883ed661e4b0f827d6debe54.tar.gz
Make target_cell() yield a new context
This makes target_cell() do the thing it was originally intended to do, which is yield a new context that is targeted at the proper cell. With this, any objects you create inside the target context manager (assuming you use the yielded context) will remain targeted after the thing returns. Related to blueprint cells-aware-api Change-Id: Iccdf6b80f5fc8adcc8a89ce6ece3f37b6cbcaee2
Diffstat (limited to 'nova/context.py')
-rw-r--r--nova/context.py20
1 files changed, 7 insertions, 13 deletions
diff --git a/nova/context.py b/nova/context.py
index a22185dcbf..42fff61e8e 100644
--- a/nova/context.py
+++ b/nova/context.py
@@ -406,22 +406,16 @@ def set_target_cell(context, cell_mapping):
@contextmanager
def target_cell(context, cell_mapping):
- """Temporarily adds database connection information to the context
- for communicating with the given target cell.
+ """Yields a new context with connection information for a specific cell.
- This context manager makes a temporary change to the context
- and restores it when complete.
+ This function yields a copy of the provided context, which is targeted to
+ the referenced cell for MQ and DB connections.
- Passing None for cell_mapping will untarget the context temporarily.
+ Passing None for cell_mapping will yield an untargetd copy of the context.
:param context: The RequestContext to add connection information
:param cell_mapping: An objects.CellMapping object or None
"""
- original_db_connection = context.db_connection
- original_mq_connection = context.mq_connection
- set_target_cell(context, cell_mapping)
- try:
- yield context
- finally:
- context.db_connection = original_db_connection
- context.mq_connection = original_mq_connection
+ cctxt = copy.copy(context)
+ set_target_cell(cctxt, cell_mapping)
+ yield cctxt