Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / plankton / utils.py @ 24ea227b

History | View | Annotate | Download (2.1 kB)

1 cda71050 Christos Stavrakakis
# Copyright 2011-2013 GRNET S.A. All rights reserved.
2 d9d1763e Christos Stavrakakis
#
3 d9d1763e Christos Stavrakakis
# Redistribution and use in source and binary forms, with or
4 d9d1763e Christos Stavrakakis
# without modification, are permitted provided that the following
5 d9d1763e Christos Stavrakakis
# conditions are met:
6 d9d1763e Christos Stavrakakis
#
7 d9d1763e Christos Stavrakakis
#   1. Redistributions of source code must retain the above
8 d9d1763e Christos Stavrakakis
#      copyright notice, this list of conditions and the following
9 d9d1763e Christos Stavrakakis
#      disclaimer.
10 d9d1763e Christos Stavrakakis
#
11 d9d1763e Christos Stavrakakis
#   2. Redistributions in binary form must reproduce the above
12 d9d1763e Christos Stavrakakis
#      copyright notice, this list of conditions and the following
13 d9d1763e Christos Stavrakakis
#      disclaimer in the documentation and/or other materials
14 d9d1763e Christos Stavrakakis
#      provided with the distribution.
15 d9d1763e Christos Stavrakakis
#
16 d9d1763e Christos Stavrakakis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 d9d1763e Christos Stavrakakis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 d9d1763e Christos Stavrakakis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 d9d1763e Christos Stavrakakis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 d9d1763e Christos Stavrakakis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 d9d1763e Christos Stavrakakis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 d9d1763e Christos Stavrakakis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 d9d1763e Christos Stavrakakis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 d9d1763e Christos Stavrakakis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 d9d1763e Christos Stavrakakis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 d9d1763e Christos Stavrakakis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 d9d1763e Christos Stavrakakis
# POSSIBILITY OF SUCH DAMAGE.
28 d9d1763e Christos Stavrakakis
#
29 d9d1763e Christos Stavrakakis
# The views and conclusions contained in the software and
30 d9d1763e Christos Stavrakakis
# documentation are those of the authors and should not be
31 d9d1763e Christos Stavrakakis
# interpreted as representing official policies, either expressed
32 d9d1763e Christos Stavrakakis
# or implied, of GRNET S.A.
33 d9d1763e Christos Stavrakakis
34 d9d1763e Christos Stavrakakis
from contextlib import contextmanager
35 cda71050 Christos Stavrakakis
from synnefo.plankton import backend
36 cda71050 Christos Stavrakakis
from snf_django.lib.api import faults
37 d9d1763e Christos Stavrakakis
38 d9d1763e Christos Stavrakakis
39 d9d1763e Christos Stavrakakis
@contextmanager
40 d9d1763e Christos Stavrakakis
def image_backend(user_id):
41 cda71050 Christos Stavrakakis
    """Context manager for ImageBackend.
42 cda71050 Christos Stavrakakis

43 cda71050 Christos Stavrakakis
    Context manager for using ImageBackend in API methods. Handles
44 cda71050 Christos Stavrakakis
    opening and closing a connection to Pithos and converting backend
45 cda71050 Christos Stavrakakis
    erros to cloud faults.
46 cda71050 Christos Stavrakakis

47 cda71050 Christos Stavrakakis
    """
48 9393d4ee Kostas Papadimitriou
    image_backend = backend.get_backend()(user_id)
49 d9d1763e Christos Stavrakakis
    try:
50 cda71050 Christos Stavrakakis
        yield image_backend
51 cda71050 Christos Stavrakakis
    except backend.Forbidden:
52 cda71050 Christos Stavrakakis
        raise faults.Forbidden
53 cda71050 Christos Stavrakakis
    except backend.ImageNotFound:
54 cda71050 Christos Stavrakakis
        raise faults.ItemNotFound
55 d9d1763e Christos Stavrakakis
    finally:
56 cda71050 Christos Stavrakakis
        image_backend.close()