Commit ee91464b authored by Christos Stavrakakis's avatar Christos Stavrakakis
Browse files

Use common 'api_method' decorator to cyclades api

Update Cyclades API methods to use the new common 'api_method'
decorator. Also, update all the relevant tests.
parent 9694bd0b
......@@ -38,6 +38,7 @@ from django.http import HttpResponse
from django.template.loader import render_to_string
from django.utils import simplejson as json
from snf_django.lib import api
from synnefo.api import util
from synnefo.db.models import Flavor
......@@ -63,7 +64,7 @@ def flavor_to_dict(flavor, detail=True):
return d
@util.api_method('GET')
@api.api_method(http_method='GET', user_required=True, logger=log)
def list_flavors(request, detail=False):
# Normal Response Codes: 200, 203
# Error Response Codes: computeFault (400, 500),
......@@ -87,7 +88,7 @@ def list_flavors(request, detail=False):
return HttpResponse(data, status=200)
@util.api_method('GET')
@api.api_method(http_method='GET', user_required=True, logger=log)
def get_flavor_details(request, flavor_id):
# Normal Response Codes: 200, 203
# Error Response Codes: computeFault (400, 500),
......
......@@ -33,23 +33,20 @@
from logging import getLogger
import dateutil.parser
from dateutil.parser import parse as date_parse
from django.conf.urls.defaults import patterns
from django.http import HttpResponse
from django.template.loader import render_to_string
from django.utils import simplejson as json
from contextlib import contextmanager
from snf_django.lib.api import faults
from snf_django.lib import api
from snf_django.lib.api import faults, utils
from synnefo.api import util
from synnefo.api.common import method_not_allowed
from synnefo.api.util import api_method, isoformat, isoparse
from synnefo.plankton.backend import ImageBackend
from synnefo.plankton.utils import image_backend
log = getLogger('synnefo.api')
log = getLogger(__name__)
urlpatterns = patterns(
'synnefo.api.images',
......@@ -67,7 +64,7 @@ def demux(request):
elif request.method == 'POST':
return create_image(request)
else:
return method_not_allowed(request)
return api.method_not_allowed(request)
def image_demux(request, image_id):
......@@ -76,7 +73,7 @@ def image_demux(request, image_id):
elif request.method == 'DELETE':
return delete_image(request, image_id)
else:
return method_not_allowed(request)
return api.method_not_allowed(request)
def metadata_demux(request, image_id):
......@@ -85,7 +82,7 @@ def metadata_demux(request, image_id):
elif request.method == 'POST':
return update_metadata(request, image_id)
else:
return method_not_allowed(request)
return api.method_not_allowed(request)
def metadata_item_demux(request, image_id, key):
......@@ -96,14 +93,14 @@ def metadata_item_demux(request, image_id, key):
elif request.method == 'DELETE':
return delete_metadata_item(request, image_id, key)
else:
return method_not_allowed(request)
return api.method_not_allowed(request)
def image_to_dict(image, detail=True):
d = dict(id=image['id'], name=image['name'])
if detail:
d['updated'] = isoformat(dateutil.parser.parse(image['updated_at']))
d['created'] = isoformat(dateutil.parser.parse(image['created_at']))
d['updated'] = utils.isoformat(date_parse(image['updated_at']))
d['created'] = utils.isoformat(date_parse(image['created_at']))
d['status'] = 'DELETED' if image['deleted_at'] else 'ACTIVE'
d['progress'] = 100 if image['status'] == 'available' else 0
if image['properties']:
......@@ -111,16 +108,7 @@ def image_to_dict(image, detail=True):
return d
@contextmanager
def image_backend(userid):
backend = ImageBackend(userid)
try:
yield backend
finally:
backend.close()
@api_method('GET')
@api.api_method("GET", user_required=True, logger=log)
def list_images(request, detail=False):
# Normal Response Codes: 200, 203
# Error Response Codes: computeFault (400, 500),
......@@ -131,11 +119,11 @@ def list_images(request, detail=False):
log.debug('list_images detail=%s', detail)
with image_backend(request.user_uniq) as backend:
since = isoparse(request.GET.get('changes-since'))
since = utils.isoparse(request.GET.get('changes-since'))
if since:
images = []
for image in backend.iter():
updated = dateutil.parser.parse(image['updated_at'])
updated = date_parse(image['updated_at'])
if updated >= since:
images.append(image)
if not images:
......@@ -155,7 +143,7 @@ def list_images(request, detail=False):
return HttpResponse(data, status=200)
@api_method('POST')
@api.api_method('POST', user_required=True, logger=log)
def create_image(request):
# Normal Response Code: 202
# Error Response Codes: computeFault (400, 500),
......@@ -173,7 +161,7 @@ def create_image(request):
raise faults.NotImplemented('Not supported.')
@api_method('GET')
@api.api_method('GET', user_required=True, logger=log)
def get_image_details(request, image_id):
# Normal Response Codes: 200, 203
# Error Response Codes: computeFault (400, 500),
......@@ -195,7 +183,7 @@ def get_image_details(request, image_id):
return HttpResponse(data, status=200)
@api_method('DELETE')
@api.api_method('DELETE', user_required=True, logger=log)
def delete_image(request, image_id):
# Normal Response Code: 204
# Error Response Codes: computeFault (400, 500),
......@@ -211,7 +199,7 @@ def delete_image(request, image_id):
return HttpResponse(status=204)
@api_method('GET')
@api.api_method('GET', user_required=True, logger=log)
def list_metadata(request, image_id):
# Normal Response Codes: 200, 203
# Error Response Codes: computeFault (400, 500),
......@@ -226,7 +214,7 @@ def list_metadata(request, image_id):
return util.render_metadata(request, metadata, use_values=True, status=200)
@api_method('POST')
@api.api_method('POST', user_required=True, logger=log)
def update_metadata(request, image_id):
# Normal Response Code: 201
# Error Response Codes: computeFault (400, 500),
......@@ -237,7 +225,7 @@ def update_metadata(request, image_id):
# badMediaType(415),
# overLimit (413)
req = util.get_request_dict(request)
req = utils.get_request_dict(request)
log.info('update_image_metadata %s %s', image_id, req)
image = util.get_image(image_id, request.user_uniq)
try:
......@@ -255,7 +243,7 @@ def update_metadata(request, image_id):
return util.render_metadata(request, properties, status=201)
@api_method('GET')
@api.api_method('GET', user_required=True, logger=log)
def get_metadata_item(request, image_id, key):
# Normal Response Codes: 200, 203
# Error Response Codes: computeFault (400, 500),
......@@ -273,7 +261,7 @@ def get_metadata_item(request, image_id, key):
return util.render_meta(request, {key: val}, status=200)
@api_method('PUT')
@api.api_method('PUT', user_required=True, logger=log)
def create_metadata_item(request, image_id, key):
# Normal Response Code: 201
# Error Response Codes: computeFault (400, 500),
......@@ -285,7 +273,7 @@ def create_metadata_item(request, image_id, key):
# badMediaType(415),
# overLimit (413)
req = util.get_request_dict(request)
req = utils.get_request_dict(request)
log.info('create_image_metadata_item %s %s %s', image_id, key, req)
try:
metadict = req['meta']
......@@ -306,7 +294,7 @@ def create_metadata_item(request, image_id, key):
return util.render_meta(request, {key: val}, status=201)
@api_method('DELETE')
@api.api_method('DELETE', user_required=True, logger=log)
def delete_metadata_item(request, image_id, key):
# Normal Response Code: 204
# Error Response Codes: computeFault (400, 500),
......
......@@ -31,27 +31,27 @@
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.
from logging import getLogger
from django.conf.urls.defaults import patterns
from django.conf import settings
from django.db.models import Q
from django.conf.urls.defaults import patterns
from django.db import transaction
from django.db.models import Q
from django.http import HttpResponse
from django.template.loader import render_to_string
from django.utils import simplejson as json
from snf_django.lib.api import faults
from snf_django.lib import api
from snf_django.lib.api import faults, utils
from synnefo.api import util
from synnefo.api.actions import network_actions
from synnefo.api.common import method_not_allowed
from synnefo import quotas
from synnefo.db.models import Network
from synnefo.db.pools import EmptyPool
from synnefo.logic import backend
log = getLogger('synnefo.api')
from logging import getLogger
log = getLogger(__name__)
urlpatterns = patterns(
'synnefo.api.networks',
......@@ -68,7 +68,7 @@ def demux(request):
elif request.method == 'POST':
return create_network(request)
else:
return method_not_allowed(request)
return api.method_not_allowed(request)
def network_demux(request, network_id):
......@@ -79,7 +79,7 @@ def network_demux(request, network_id):
elif request.method == 'DELETE':
return delete_network(request, network_id)
else:
return method_not_allowed(request)
return api.method_not_allowed(request)
def network_to_dict(network, user_id, detail=True):
......@@ -91,8 +91,8 @@ def network_to_dict(network, user_id, detail=True):
d['gateway6'] = network.gateway6
d['dhcp'] = network.dhcp
d['type'] = network.flavor
d['updated'] = util.isoformat(network.updated)
d['created'] = util.isoformat(network.created)
d['updated'] = utils.isoformat(network.updated)
d['created'] = utils.isoformat(network.created)
d['status'] = network.state
d['public'] = network.public
......@@ -112,7 +112,7 @@ def render_network(request, networkdict, status=200):
return HttpResponse(data, status=status)
@util.api_method('GET')
@api.api_method(http_method='GET', user_required=True, logger=log)
def list_networks(request, detail=False):
# Normal Response Codes: 200, 203
# Error Response Codes: computeFault (400, 500),
......@@ -122,7 +122,7 @@ def list_networks(request, detail=False):
# overLimit (413)
log.debug('list_networks detail=%s', detail)
since = util.isoparse(request.GET.get('changes-since'))
since = utils.isoparse(request.GET.get('changes-since'))
user_networks = Network.objects.filter(Q(userid=request.user_uniq) |
Q(public=True))
......@@ -146,7 +146,7 @@ def list_networks(request, detail=False):
return HttpResponse(data, status=200)
@util.api_method('POST')
@api.api_method(http_method='POST', user_required=True, logger=log)
@quotas.uses_commission
@transaction.commit_manually
def create_network(serials, request):
......@@ -160,7 +160,7 @@ def create_network(serials, request):
# overLimit (413)
try:
req = util.get_request_dict(request)
req = utils.get_request_dict(request)
log.info('create_network %s', req)
user_id = request.user_uniq
......@@ -242,7 +242,7 @@ def create_network(serials, request):
return response
@util.api_method('GET')
@api.api_method(http_method='GET', user_required=True, logger=log)
def get_network_details(request, network_id):
# Normal Response Codes: 200, 203
# Error Response Codes: computeFault (400, 500),
......@@ -258,7 +258,7 @@ def get_network_details(request, network_id):
return render_network(request, netdict)
@util.api_method('PUT')
@api.api_method(http_method='PUT', user_required=True, logger=log)
def update_network_name(request, network_id):
# Normal Response Code: 204
# Error Response Codes: computeFault (400, 500),
......@@ -270,7 +270,7 @@ def update_network_name(request, network_id):
# itemNotFound (404),
# overLimit (413)
req = util.get_request_dict(request)
req = utils.get_request_dict(request)
log.info('update_network_name %s', network_id)
try:
......@@ -288,7 +288,7 @@ def update_network_name(request, network_id):
return HttpResponse(status=204)
@util.api_method('DELETE')
@api.api_method(http_method='DELETE', user_required=True, logger=log)
@transaction.commit_on_success
def delete_network(request, network_id):
# Normal Response Code: 204
......@@ -317,9 +317,9 @@ def delete_network(request, network_id):
return HttpResponse(status=204)
@util.api_method('POST')
@api.api_method(http_method='POST', user_required=True, logger=log)
def network_action(request, network_id):
req = util.get_request_dict(request)
req = utils.get_request_dict(request)
log.debug('network_action %s %s', network_id, req)
if len(req) != 1:
raise faults.BadRequest('Malformed request.')
......
# Copyright 2011-2012 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
......@@ -31,8 +31,6 @@
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.
from base64 import b64decode
from django import dispatch
from django.conf import settings
from django.conf.urls.defaults import patterns
......@@ -41,10 +39,10 @@ from django.http import HttpResponse
from django.template.loader import render_to_string
from django.utils import simplejson as json
from snf_django.lib.api import faults
from snf_django.lib import api
from snf_django.lib.api import faults, utils
from synnefo.api import util
from synnefo.api.actions import server_actions
from synnefo.api.common import method_not_allowed
from synnefo.db.models import (VirtualMachine, VirtualMachineMetadata,
NetworkInterface)
from synnefo.logic.backend import create_instance, delete_instance
......@@ -57,7 +55,7 @@ from synnefo import quotas
server_created = dispatch.Signal(providing_args=["created_vm_params"])
from logging import getLogger
log = getLogger('synnefo.api')
log = getLogger(__name__)
urlpatterns = patterns(
'synnefo.api.servers',
......@@ -80,7 +78,7 @@ def demux(request):
elif request.method == 'POST':
return create_server(request)
else:
return method_not_allowed(request)
return api.method_not_allowed(request)
def server_demux(request, server_id):
......@@ -91,7 +89,7 @@ def server_demux(request, server_id):
elif request.method == 'DELETE':
return delete_server(request, server_id)
else:
return method_not_allowed(request)
return api.method_not_allowed(request)
def metadata_demux(request, server_id):
......@@ -100,7 +98,7 @@ def metadata_demux(request, server_id):
elif request.method == 'POST':
return update_metadata(request, server_id)
else:
return method_not_allowed(request)
return api.method_not_allowed(request)
def metadata_item_demux(request, server_id, key):
......@@ -111,7 +109,7 @@ def metadata_item_demux(request, server_id, key):
elif request.method == 'DELETE':
return delete_metadata_item(request, server_id, key)
else:
return method_not_allowed(request)
return api.method_not_allowed(request)
def nic_to_dict(nic):
......@@ -133,8 +131,8 @@ def vm_to_dict(vm, detail=False):
d['progress'] = 100 if get_rsapi_state(vm) == 'ACTIVE' \
else vm.buildpercentage
d['hostId'] = vm.hostid
d['updated'] = util.isoformat(vm.updated)
d['created'] = util.isoformat(vm.created)
d['updated'] = utils.isoformat(vm.updated)
d['created'] = utils.isoformat(vm.created)
d['flavorRef'] = vm.flavor.id
d['imageRef'] = vm.imageid
d['suspended'] = vm.suspended
......@@ -166,11 +164,11 @@ def diagnostics_to_dict(diagnostics):
# format source date if set
formatted_source_date = None
if diagnostic.source_date:
formatted_source_date = util.isoformat(diagnostic.source_date)
formatted_source_date = utils.isoformat(diagnostic.source_date)
entry = {
'source': diagnostic.source,
'created': util.isoformat(diagnostic.created),
'created': utils.isoformat(diagnostic.created),
'message': diagnostic.message,
'details': diagnostic.details,
'level': diagnostic.level,
......@@ -201,7 +199,7 @@ def render_diagnostics(request, diagnostics_dict, status=200):
return HttpResponse(json.dumps(diagnostics_dict), status=status)
@util.api_method('GET')
@api.api_method(http_method='GET', user_required=True, logger=log)
def get_server_diagnostics(request, server_id):
"""
Virtual machine diagnostics api view.
......@@ -212,7 +210,7 @@ def get_server_diagnostics(request, server_id):
return render_diagnostics(request, diagnostics)
@util.api_method('GET')
@api.api_method(http_method='GET', user_required=True, logger=log)
def list_servers(request, detail=False):
# Normal Response Codes: 200, 203
# Error Response Codes: computeFault (400, 500),
......@@ -224,7 +222,7 @@ def list_servers(request, detail=False):
log.debug('list_servers detail=%s', detail)
user_vms = VirtualMachine.objects.filter(userid=request.user_uniq)
since = util.isoparse(request.GET.get('changes-since'))
since = utils.isoparse(request.GET.get('changes-since'))
if since:
user_vms = user_vms.filter(updated__gte=since)
......@@ -246,7 +244,7 @@ def list_servers(request, detail=False):
return HttpResponse(data, status=200)
@util.api_method('POST')
@api.api_method(http_method='POST', user_required=True, logger=log)
# Use manual transactions. Backend and IP pool allocations need exclusive
# access (SELECT..FOR UPDATE). Running create_server with commit_on_success
# would result in backends and public networks to be locked until the job is
......@@ -264,7 +262,7 @@ def create_server(serials, request):
# serverCapacityUnavailable (503),
# overLimit (413)
try:
req = util.get_request_dict(request)
req = utils.get_request_dict(request)
log.info('create_server %s', req)
user_id = request.user_uniq
......@@ -398,7 +396,7 @@ def create_server(serials, request):
return respsone
@util.api_method('GET')
@api.api_method(http_method='GET', user_required=True, logger=log)
def get_server_details(request, server_id):
# Normal Response Codes: 200, 203
# Error Response Codes: computeFault (400, 500),
......@@ -414,7 +412,7 @@ def get_server_details(request, server_id):
return render_server(request, server)
@util.api_method('PUT')
@api.api_method(http_method='PUT', user_required=True, logger=log)
def update_server_name(request, server_id):
# Normal Response Code: 204
# Error Response Codes: computeFault (400, 500),
......@@ -426,7 +424,7 @@ def update_server_name(request, server_id):
# buildInProgress (409),
# overLimit (413)
req = util.get_request_dict(request)
req = utils.get_request_dict(request)
log.info('update_server_name %s %s', server_id, req)
try:
......@@ -442,7 +440,7 @@ def update_server_name(request, server_id):
return HttpResponse(status=204)
@util.api_method('DELETE')
@api.api_method(http_method='DELETE', user_required=True, logger=log)
@transaction.commit_on_success
def delete_server(request, server_id):
# Normal Response Codes: 204
......@@ -466,9 +464,9 @@ def delete_server(request, server_id):
ARBITRARY_ACTIONS = ['console', 'firewallProfile']
@util.api_method('POST')
@api.api_method(http_method='POST', user_required=True, logger=log)
def server_action(request, server_id):
req = util.get_request_dict(request)
req = utils.get_request_dict(request)
log.debug('server_action %s %s', server_id, req)
if len(req) != 1:
......@@ -528,7 +526,7 @@ def start_action(vm, action):
vm.save()
@util.api_method('GET')
@api.api_method(http_method='GET', user_required=True, logger=log)
def list_addresses(request, server_id):
# Normal Response Codes: 200, 203
# Error Response Codes: computeFault (400, 500),
......@@ -549,7 +547,7 @@ def list_addresses(request, server_id):
return HttpResponse(data, status=200)
@util.api_method('GET')
@api.api_method(http_method='GET', user_required=True, logger=log)
def list_addresses_by_network(request, server_id, network_id):
# Normal Response Codes: 200, 203
# Error Response Codes: computeFault (400, 500),
......@@ -573,7 +571,7 @@ def list_addresses_by_network(request, server_id, network_id):
return HttpResponse(data, status=200)
@util.api_method('GET')
@api.api_method(http_method='GET', user_required=True, logger=log)
def list_metadata(request, server_id):
# Normal Response Codes: 200, 203
# Error Response Codes: computeFault (400, 500),
......@@ -588,7 +586,7 @@ def list_metadata(request, server_id):
return util.render_metadata(request, metadata, use_values=True, status=200)
@util.api_method('POST')
@api.api_method(http_method='POST', user_required=True, logger=log)
def update_metadata(request, server_id):
# Normal Response Code: 201
# Error Response Codes: computeFault (400, 500),
......@@ -599,7 +597,7 @@ def update_metadata(request, server_id):
# badMediaType(415),
# overLimit (413)
req = util.get_request_dict(request)
req = utils.get_request_dict(request)
log.info('update_server_metadata %s %s', server_id, req)
vm = util.get_vm(server_id, request.user_uniq, non_suspended=True)
try:
......@@ -618,7 +616,7 @@ def update_metadata(request, server_id):
return util.render_metadata(request, vm_meta, status=201)
@util.api_method('GET')
@api.api_method(http_method='GET', user_required=True, logger=log)
def get_metadata_item(request, server_id, key):
# Normal Response Codes: 200, 203
# Error Response Codes: computeFault (400, 500),
......@@ -635,7 +633,7 @@ def get_metadata_item(request, server_id, key):
return util.render_meta(request, d, status=200)
@util.api_method('PUT')
@api.api_method(http_method='PUT', user_required=True, logger=log)
@transaction.commit_on_success
def create_metadata_item(request, server_id, key):
# Normal Response Code: 201
......@@ -648,7 +646,7 @@ def create_metadata_item(request, server_id, key):
# badMediaType(415),
# overLimit (413)
req = util.get_request_dict(request)
req = utils.get_request_dict(request)
log.info('create_server_metadata_item %s %s %s', server_id, key, req)
vm = util.get_vm(server_id, request.user_uniq, non_suspended=True)
try:
......@@ -670,7 +668,7 @@ def create_metadata_item(request, server_id, key):
return util.render_meta(request, d, status=201)
@util.api_method('DELETE')
@api.api_method(http_method='DELETE'