Commit 9694bd0b authored by Christos Stavrakakis's avatar Christos Stavrakakis
Browse files

Use the common 'api_method' decorator in plankton

Use the common 'api_method' decorator in all plankton API methods.
Modify the 'plankton_method' decorator to only create and close
the ImageBackend.
parent 0334f184
......@@ -68,13 +68,11 @@ def astakos_user(user):
.... make api calls ....
"""
def dummy_get_user(request, *args, **kwargs):
request.user = {'username': user, 'groups': []}
request.user_uniq = user
with patch('synnefo.plankton.util.get_user') as m:
m.side_effect = dummy_get_user
yield
with patch("snf_django.lib.api.get_token") as get_token:
get_token.return_value = "DummyToken"
with patch('astakosclient.AstakosClient.get_user_info') as m:
m.return_value = {"uuid": user}
yield
class BaseAPITest(TestCase):
......@@ -190,7 +188,7 @@ def assert_backend_closed(func):
return wrapper
@patch("synnefo.plankton.util.ImageBackend")
@patch("synnefo.plankton.utils.ImageBackend")
class PlanktonTest(BaseAPITest):
@assert_backend_closed
def test_list_images(self, backend):
......
# Copyright 2011 GRNET S.A. All rights reserved.
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
# conditions are met:
#
# 1. Redistributions of source code must retain the above
# copyright notice, this list of conditions and the following
# disclaimer.
#
# 2. Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials
# provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and
# documentation are those of the authors and should not be
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.
from functools import wraps
from synnefo.plankton.backend import ImageBackend
from contextlib import contextmanager
def plankton_method(func):
"""Decorator function for API methods using ImageBackend.
Decorator function that creates and closes an ImageBackend, needed
by all API methods that handle images.
"""
@wraps(func)
def wrapper(request, *args, **kwargs):
with image_backend(request.user_uniq) as backend:
request.backend = backend
return func(request, *args, **kwargs)
return wrapper
@contextmanager
def image_backend(user_id):
"""Context manager for ImageBackend"""
backend = ImageBackend(user_id)
try:
yield backend
finally:
backend.close()
# Copyright 2011 GRNET S.A. All rights reserved.
# Copyright 2011-2013 GRNET S.A. All rights reserved.
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
......@@ -40,7 +40,9 @@ from urllib import unquote
from django.conf import settings
from django.http import (HttpResponse, HttpResponseNotFound,
HttpResponseBadRequest)
from synnefo.plankton.util import plankton_method
from snf_django.lib import api
from synnefo.plankton.utils import plankton_method
FILTERS = ('name', 'container_format', 'disk_format', 'status', 'size_min',
......@@ -114,7 +116,8 @@ def _get_image_headers(request):
return headers
@plankton_method('POST')
@api.api_method(http_method="POST", user_required=True, logger=log)
@plankton_method
def add_image(request):
"""Add a new virtual machine image
......@@ -155,7 +158,8 @@ def add_image(request):
return _create_image_response(image)
@plankton_method("DELETE")
@api.api_method(http_method="DELETE", user_required=True, logger=log)
@plankton_method
def delete_image(request, image_id):
"""Delete an Image.
......@@ -174,7 +178,8 @@ def delete_image(request, image_id):
return HttpResponse(status=204)
@plankton_method('PUT')
@api.api_method(http_method="PUT", user_required=True, logger=log)
@plankton_method
def add_image_member(request, image_id, member):
"""Add a member to an image
......@@ -190,7 +195,8 @@ def add_image_member(request, image_id, member):
return HttpResponse(status=204)
@plankton_method('GET')
@api.api_method(http_method="GET", user_required=True, logger=log)
@plankton_method
def get_image(request, image_id):
"""Retrieve a virtual machine image
......@@ -216,7 +222,8 @@ def get_image(request, image_id):
return HttpResponse(status=501) # Not Implemented
@plankton_method('HEAD')
@api.api_method(http_method="HEAD", user_required=True, logger=log)
@plankton_method
def get_image_meta(request, image_id):
"""Return detailed metadata on a specific image
......@@ -230,7 +237,8 @@ def get_image_meta(request, image_id):
return _create_image_response(image)
@plankton_method('GET')
@api.api_method(http_method="GET", user_required=True, logger=log)
@plankton_method
def list_image_members(request, image_id):
"""List image memberships
......@@ -244,7 +252,8 @@ def list_image_members(request, image_id):
return HttpResponse(data)
@plankton_method('GET')
@api.api_method(http_method="GET", user_required=True, logger=log)
@plankton_method
def list_images(request, detail=False):
"""Return a list of available images.
......@@ -297,7 +306,8 @@ def list_images(request, detail=False):
return HttpResponse(data)
@plankton_method('GET')
@api.api_method(http_method="GET", user_required=True, logger=log)
@plankton_method
def list_shared_images(request, member):
"""Request shared images
......@@ -320,7 +330,8 @@ def list_shared_images(request, member):
return HttpResponse(data)
@plankton_method('DELETE')
@api.api_method(http_method="DELETE", user_required=True, logger=log)
@plankton_method
def remove_image_member(request, image_id, member):
"""Remove a member from an image
......@@ -333,7 +344,8 @@ def remove_image_member(request, image_id, member):
return HttpResponse(status=204)
@plankton_method('PUT')
@api.api_method(http_method="PUT", user_required=True, logger=log)
@plankton_method
def update_image(request, image_id):
"""Update an image
......@@ -355,7 +367,8 @@ def update_image(request, image_id):
return _create_image_response(image)
@plankton_method('PUT')
@api.api_method(http_method="PUT", user_required=True, logger=log)
@plankton_method
def update_image_members(request, image_id):
"""Replace a membership list for an image
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment