commit c36934a7f2cb6c1b32b249a61b0eeced582ca1b2
Author: Giorgos Verigakis <verigak@gmail.com>
Date:   Mon Mar 21 18:09:31 2011 +0200

    Add image and flavor support to REDUX API
    
    Refs #301

diff --git a/api/servers.py b/api/servers.py
index 885371b..4bd68ef 100644
--- a/api/servers.py
+++ b/api/servers.py
@@ -32,7 +32,8 @@ def demux(request):
     elif request.method == 'POST':
         return create_server(request)
     else:
-        return HttpResponse(status=404)
+        fault = BadRequest()
+        return render_fault(request, fault)
 
 def server_demux(request, server_id):
     if request.method == 'GET':
@@ -42,41 +43,41 @@ def server_demux(request, server_id):
     elif request.method == 'DELETE':
         return delete_server(request, server_id)
     else:
-        return HttpResponse(status=404)
+        fault = BadRequest()
+        return render_fault(request, fault)
 
-def server_dict(vm, detail=False):
-    d = dict(id=vm.id, name=vm.name)
+
+def server_to_dict(server, detail=False):
+    d = dict(id=server.id, name=server.name)
     if detail:
-        d['status'] = vm.rsapi_state
-        d['progress'] = 100 if vm.rsapi_state == 'ACTIVE' else 0
-        d['hostId'] = vm.hostid
-        d['updated'] = vm.updated.isoformat()
-        d['created'] = vm.created.isoformat()
-        d['flavorId'] = vm.flavor.id            # XXX Should use flavorRef instead?
-        d['imageId'] = vm.sourceimage.id        # XXX Should use imageRef instead?
-        d['description'] = vm.description       # XXX Not in OpenStack docs
+        d['status'] = server.rsapi_state
+        d['progress'] = 100 if server.rsapi_state == 'ACTIVE' else 0
+        d['hostId'] = server.hostid
+        d['updated'] = server.updated.isoformat()
+        d['created'] = server.created.isoformat()
+        d['flavorId'] = server.flavor.id            # XXX Should use flavorRef instead?
+        d['imageId'] = server.sourceimage.id        # XXX Should use imageRef instead?
+        d['description'] = server.description       # XXX Not in OpenStack docs
         
-        vm_meta = vm.virtualmachinemetadata_set.all()
-        metadata = dict((meta.meta_key, meta.meta_value) for meta in vm_meta)
+        server_meta = server.virtualmachinemetadata_set.all()
+        metadata = dict((meta.meta_key, meta.meta_value) for meta in server_meta)
         if metadata:
             d['metadata'] = dict(values=metadata)
         
-        public_addrs = [dict(version=4, addr=vm.ipfour), dict(version=6, addr=vm.ipsix)]
+        public_addrs = [dict(version=4, addr=server.ipfour), dict(version=6, addr=server.ipsix)]
         d['addresses'] = {'values': []}
         d['addresses']['values'].append({'id': 'public', 'values': public_addrs})
     return d
 
-def render_server(server, request, status=200):
+def render_server(request, serverdict, status=200):
     if request.type == 'xml':
-        mimetype = 'application/xml'
-        data = render_to_string('server.xml', dict(server=server, is_root=True))
+        data = render_to_string('server.xml', dict(server=serverdict, is_root=True))
     else:
-        mimetype = 'application/json'
-        data = json.dumps({'server': server})
-    return HttpResponse(data, mimetype=mimetype, status=status)    
+        data = json.dumps({'server': serverdict})
+    return HttpResponse(data, status=status)
 
 
-@api_method
+@api_method('GET')
 def list_servers(request, detail=False):
     # Normal Response Codes: 200, 203
     # Error Response Codes: computeFault (400, 500),
@@ -84,18 +85,19 @@ def list_servers(request, detail=False):
     #                       unauthorized (401),
     #                       badRequest (400),
     #                       overLimit (413)
+    
     owner = get_user()
-    vms = VirtualMachine.objects.filter(owner=owner, deleted=False)
-    servers = [server_dict(vm, detail) for vm in vms]
+    user_servers = VirtualMachine.objects.filter(owner=owner, deleted=False)
+    servers = [server_to_dict(server, detail) for server in user_servers]
+    
     if request.type == 'xml':
-        mimetype = 'application/xml'
         data = render_to_string('list_servers.xml', dict(servers=servers, detail=detail))
     else:
-        mimetype = 'application/json'
-        data = json.dumps({'servers': servers})
-    return HttpResponse(data, mimetype=mimetype, status=200)
+        data = json.dumps({'servers': {'values': servers}})
+    
+    return HttpResponse(data, status=200)
 
-@api_method
+@api_method('POST')
 def create_server(request):
     # Normal Response Code: 202
     # Error Response Codes: computeFault (400, 500),
@@ -121,7 +123,7 @@ def create_server(request):
     except Flavor.DoesNotExist:
         raise ItemNotFound
     
-    vm = VirtualMachine.objects.create(
+    server = VirtualMachine.objects.create(
         name=name,
         owner=get_user(),
         sourceimage=sourceimage,
@@ -133,7 +135,7 @@ def create_server(request):
         name = 'test-server'
         dry_run = True
     else:
-        name = vm.backend_id
+        name = server.backend_id
         dry_run = False
     
     jobId = rapi.CreateInstance(
@@ -149,51 +151,79 @@ def create_server(request):
         dry_run=dry_run,
         beparams=dict(auto_balance=True, vcpus=flavor.cpu, memory=flavor.ram))
     
-    vm.save()
+    server.save()
         
     log.info('created vm with %s cpus, %s ram and %s storage' % (flavor.cpu, flavor.ram, flavor.disk))
     
-    server = server_dict(vm, detail=True)
-    server['status'] = 'BUILD'
-    server['adminPass'] = random_password()
-    return render_server(server, request, status=202)
+    serverdict = server_to_dict(server, detail=True)
+    serverdict['status'] = 'BUILD'
+    serverdict['adminPass'] = random_password()
+    return render_server(request, serverdict, status=202)
 
-@api_method
+@api_method('GET')
 def get_server_details(request, server_id):
+    # Normal Response Codes: 200, 203
+    # Error Response Codes: computeFault (400, 500),
+    #                       serviceUnavailable (503),
+    #                       unauthorized (401),
+    #                       badRequest (400),
+    #                       itemNotFound (404),
+    #                       overLimit (413)
+    
     try:
-        vm = VirtualMachine.objects.get(id=int(server_id))
+        server_id = int(server_id)
+        server = VirtualMachine.objects.get(id=server_id)
     except VirtualMachine.DoesNotExist:
-        raise NotFound
+        raise ItemNotFound
     
-    server = server_dict(vm, detail=True)
-    return render_server(server, request)
+    serverdict = server_to_dict(server, detail=True)
+    return render_server(request, serverdict)
 
-@api_method
-def update_server_name(request, server_id):    
+@api_method('PUT')
+def update_server_name(request, server_id):
+    # Normal Response Code: 204
+    # Error Response Codes: computeFault (400, 500),
+    #                       serviceUnavailable (503),
+    #                       unauthorized (401),
+    #                       badRequest (400),
+    #                       badMediaType(415),
+    #                       itemNotFound (404),
+    #                       buildInProgress (409),
+    #                       overLimit (413)
+    
     req = get_request_dict(request)
     
     try:
         name = req['server']['name']
-        vm = VirtualMachine.objects.get(id=int(server_id))
+        server_id = int(server_id)
+        server = VirtualMachine.objects.get(id=server_id)
     except KeyError:
         raise BadRequest
     except VirtualMachine.DoesNotExist:
-        raise NotFound
+        raise ItemNotFound
     
-    vm.name = name
-    vm.save()
+    server.name = name
+    server.save()
     
     return HttpResponse(status=204)
 
-@api_method
+@api_method('DELETE')
 def delete_server(request, server_id):
+    # Normal Response Codes: 204
+    # Error Response Codes: computeFault (400, 500),
+    #                       serviceUnavailable (503),
+    #                       unauthorized (401),
+    #                       itemNotFound (404),
+    #                       unauthorized (401),
+    #                       buildInProgress (409),
+    #                       overLimit (413)
+    
     try:
-        vm = VirtualMachine.objects.get(id=int(server_id))
+        server_id = int(server_id)
+        server = VirtualMachine.objects.get(id=server_id)
     except VirtualMachine.DoesNotExist:
-        raise NotFound
+        raise ItemNotFound
     
-    vm.start_action('DESTROY')
-    rapi.DeleteInstance(vm.backend_id)
-    vm.state = 'DESTROYED'
-    vm.save()
+    server.start_action('DESTROY')
+    rapi.DeleteInstance(server.backend_id)
     return HttpResponse(status=204)
