Add a persistence backend fetching context manager
Allow the persistence backends to be fetched using a new helper method that can be used as a context manager, it will fetch the backend, ensure it's upgraded and upon context manager exit will close the backend automatically. Change-Id: I1bf8e43dcce25c02823cca92e3e7ed3ef254a847
This commit is contained in:
committed by
Thomas Goirand
parent
fbfdb5fb84
commit
e630907563
@@ -14,6 +14,7 @@
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import contextlib
|
||||
import logging
|
||||
import re
|
||||
|
||||
@@ -53,3 +54,14 @@ def fetch(conf, namespace=BACKEND_NAMESPACE, **kwargs):
|
||||
return mgr.driver
|
||||
except RuntimeError as e:
|
||||
raise exc.NotFound("Could not find backend %s: %s" % (backend_name, e))
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def backend(conf, namespace=BACKEND_NAMESPACE, **kwargs):
|
||||
"""Fetches a persistence backend, ensures that it is upgraded and upon
|
||||
context manager completion closes the backend.
|
||||
"""
|
||||
with contextlib.closing(fetch(conf, namespace=namespace, **kwargs)) as be:
|
||||
with contextlib.closing(be.get_connection()) as conn:
|
||||
conn.upgrade()
|
||||
yield be
|
||||
|
||||
Reference in New Issue
Block a user