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

Sort all API list_* responses

parent 6306ec09
......@@ -70,11 +70,12 @@ def list_flavors(request, detail=False):
# unauthorized (401),
# badRequest (400),
# overLimit (413)
log.debug('list_flavors detail=%s', detail)
active_flavors = Flavor.objects.exclude(deleted=True)
flavors = [flavor_to_dict(flavor, detail) for flavor in active_flavors]
flavors = [flavor_to_dict(flavor, detail)\
for flavor in active_flavors.order_by('id')]
if request.serialization == 'xml':
data = render_to_string('list_flavors.xml', {
'flavors': flavors,
......@@ -84,6 +85,7 @@ def list_flavors(request, detail=False):
return HttpResponse(data, status=200)
@util.api_method('GET')
def get_flavor_details(request, flavor_id):
# Normal Response Codes: 200, 203
......@@ -93,7 +95,7 @@ def get_flavor_details(request, flavor_id):
# badRequest (400),
# itemNotFound (404),
# overLimit (413)
log.debug('get_flavor_details %s', flavor_id)
flavor = util.get_flavor(flavor_id, include_deleted=True)
flavordict = flavor_to_dict(flavor, detail=True)
......
# Copyright 2011-2012 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
......@@ -25,7 +25,7 @@
# 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
......@@ -112,10 +112,10 @@ def list_images(request, detail=False):
# unauthorized (401),
# badRequest (400),
# overLimit (413)
log.debug('list_images detail=%s', detail)
backend = ImageBackend(request.user_uniq)
since = isoparse(request.GET.get('changes-since'))
if since:
images = []
......@@ -127,15 +127,16 @@ def list_images(request, detail=False):
return HttpResponse(status=304)
else:
images = backend.list()
images = sorted(images, key=lambda x: x['id'])
reply = [image_to_dict(image, detail) for image in images]
if request.serialization == 'xml':
data = render_to_string('list_images.xml',
dict(images=reply, detail=detail))
else:
data = json.dumps(dict(images={'values': reply}))
return HttpResponse(data, status=200)
......@@ -153,7 +154,7 @@ def create_image(request):
# resizeNotAllowed (403),
# backupOrResizeInProgress (409),
# overLimit (413)
raise ServiceUnavailable('Not supported.')
......@@ -166,16 +167,16 @@ def get_image_details(request, image_id):
# badRequest (400),
# itemNotFound (404),
# overLimit (413)
log.debug('get_image_details %s', image_id)
image = util.get_image(image_id, request.user_uniq)
reply = image_to_dict(image)
if request.serialization == 'xml':
data = render_to_string('image.xml', dict(image=reply))
else:
data = json.dumps(dict(image=reply))
return HttpResponse(data, status=200)
......@@ -187,7 +188,7 @@ def delete_image(request, image_id):
# unauthorized (401),
# itemNotFound (404),
# overLimit (413)
log.info('delete_image %s', image_id)
backend = ImageBackend(request.user_uniq)
backend.delete(image_id)
......@@ -204,7 +205,7 @@ def list_metadata(request, image_id):
# unauthorized (401),
# badRequest (400),
# overLimit (413)
log.debug('list_image_metadata %s', image_id)
image = util.get_image(image_id, request.user_uniq)
metadata = image['properties']
......@@ -221,7 +222,7 @@ def update_metadata(request, image_id):
# buildInProgress (409),
# badMediaType(415),
# overLimit (413)
req = util.get_request_dict(request)
log.info('update_image_metadata %s %s', image_id, req)
image = util.get_image(image_id, request.user_uniq)
......@@ -230,14 +231,14 @@ def update_metadata(request, image_id):
assert isinstance(metadata, dict)
except (KeyError, AssertionError):
raise BadRequest('Malformed request.')
properties = image['properties']
properties.update(metadata)
backend = ImageBackend(request.user_uniq)
backend.update(image_id, dict(properties=properties))
backend.close()
return util.render_metadata(request, properties, status=201)
......@@ -250,7 +251,7 @@ def get_metadata_item(request, image_id, key):
# itemNotFound (404),
# badRequest (400),
# overLimit (413)
log.debug('get_image_metadata_item %s %s', image_id, key)
image = util.get_image(image_id, request.user_uniq)
val = image['properties'].get(key)
......@@ -270,7 +271,7 @@ def create_metadata_item(request, image_id, key):
# buildInProgress (409),
# badMediaType(415),
# overLimit (413)
req = util.get_request_dict(request)
log.info('create_image_metadata_item %s %s %s', image_id, key, req)
try:
......@@ -285,11 +286,11 @@ def create_metadata_item(request, image_id, key):
image = util.get_image(image_id, request.user_uniq)
properties = image['properties']
properties[key] = val
backend = ImageBackend(request.user_uniq)
backend.update(image_id, dict(properties=properties))
backend.close()
return util.render_meta(request, {key: val}, status=201)
......@@ -304,14 +305,14 @@ def delete_metadata_item(request, image_id, key):
# buildInProgress (409),
# badMediaType(415),
# overLimit (413),
log.info('delete_image_metadata_item %s %s', image_id, key)
image = util.get_image(image_id, request.user_uniq)
properties = image['properties']
properties.pop(key, None)
backend = ImageBackend(request.user_uniq)
backend.update(image_id, dict(properties=properties))
backend.close()
return HttpResponse(status=204)
......@@ -96,7 +96,8 @@ def network_to_dict(network, user_id, detail=True):
d['public'] = network.public
attachments = [util.construct_nic_id(nic)
for nic in network.nics.filter(machine__userid=user_id)]
for nic in network.nics.filter(machine__userid=user_id)\
.order_by('machine')]
d['attachments'] = {'values': attachments}
return d
......@@ -131,7 +132,7 @@ def list_networks(request, detail=False):
user_networks = user_networks.filter(deleted=False)
networks = [network_to_dict(network, request.user_uniq, detail)
for network in user_networks]
for network in user_networks.order_by('id')]
if request.serialization == 'xml':
data = render_to_string('list_networks.xml', {
......
......@@ -137,7 +137,7 @@ def vm_to_dict(vm, detail=False):
if metadata:
d['metadata'] = {'values': metadata}
attachments = [nic_to_dict(nic) for nic in vm.nics.all()]
attachments = [nic_to_dict(nic) for nic in vm.nics.order_by('index')]
if attachments:
d['attachments'] = {'values': attachments}
......@@ -226,7 +226,8 @@ def list_servers(request, detail=False):
else:
user_vms = user_vms.filter(deleted=False)
servers = [vm_to_dict(server, detail) for server in user_vms]
servers = [vm_to_dict(server, detail)\
for server in user_vms.order_by('id')]
if request.serialization == 'xml':
data = render_to_string('list_servers.xml', {
......
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