Revision c36934a7 api/servers.py

b/api/servers.py
32 32
    elif request.method == 'POST':
33 33
        return create_server(request)
34 34
    else:
35
        return HttpResponse(status=404)
35
        fault = BadRequest()
36
        return render_fault(request, fault)
36 37

  
37 38
def server_demux(request, server_id):
38 39
    if request.method == 'GET':
......
42 43
    elif request.method == 'DELETE':
43 44
        return delete_server(request, server_id)
44 45
    else:
45
        return HttpResponse(status=404)
46
        fault = BadRequest()
47
        return render_fault(request, fault)
46 48

  
47
def server_dict(vm, detail=False):
48
    d = dict(id=vm.id, name=vm.name)
49

  
50
def server_to_dict(server, detail=False):
51
    d = dict(id=server.id, name=server.name)
49 52
    if detail:
50
        d['status'] = vm.rsapi_state
51
        d['progress'] = 100 if vm.rsapi_state == 'ACTIVE' else 0
52
        d['hostId'] = vm.hostid
53
        d['updated'] = vm.updated.isoformat()
54
        d['created'] = vm.created.isoformat()
55
        d['flavorId'] = vm.flavor.id            # XXX Should use flavorRef instead?
56
        d['imageId'] = vm.sourceimage.id        # XXX Should use imageRef instead?
57
        d['description'] = vm.description       # XXX Not in OpenStack docs
53
        d['status'] = server.rsapi_state
54
        d['progress'] = 100 if server.rsapi_state == 'ACTIVE' else 0
55
        d['hostId'] = server.hostid
56
        d['updated'] = server.updated.isoformat()
57
        d['created'] = server.created.isoformat()
58
        d['flavorId'] = server.flavor.id            # XXX Should use flavorRef instead?
59
        d['imageId'] = server.sourceimage.id        # XXX Should use imageRef instead?
60
        d['description'] = server.description       # XXX Not in OpenStack docs
58 61
        
59
        vm_meta = vm.virtualmachinemetadata_set.all()
60
        metadata = dict((meta.meta_key, meta.meta_value) for meta in vm_meta)
62
        server_meta = server.virtualmachinemetadata_set.all()
63
        metadata = dict((meta.meta_key, meta.meta_value) for meta in server_meta)
61 64
        if metadata:
62 65
            d['metadata'] = dict(values=metadata)
63 66
        
64
        public_addrs = [dict(version=4, addr=vm.ipfour), dict(version=6, addr=vm.ipsix)]
67
        public_addrs = [dict(version=4, addr=server.ipfour), dict(version=6, addr=server.ipsix)]
65 68
        d['addresses'] = {'values': []}
66 69
        d['addresses']['values'].append({'id': 'public', 'values': public_addrs})
67 70
    return d
68 71

  
69
def render_server(server, request, status=200):
72
def render_server(request, serverdict, status=200):
70 73
    if request.type == 'xml':
71
        mimetype = 'application/xml'
72
        data = render_to_string('server.xml', dict(server=server, is_root=True))
74
        data = render_to_string('server.xml', dict(server=serverdict, is_root=True))
73 75
    else:
74
        mimetype = 'application/json'
75
        data = json.dumps({'server': server})
76
    return HttpResponse(data, mimetype=mimetype, status=status)    
76
        data = json.dumps({'server': serverdict})
77
    return HttpResponse(data, status=status)
77 78

  
78 79

  
79
@api_method
80
@api_method('GET')
80 81
def list_servers(request, detail=False):
81 82
    # Normal Response Codes: 200, 203
82 83
    # Error Response Codes: computeFault (400, 500),
......
84 85
    #                       unauthorized (401),
85 86
    #                       badRequest (400),
86 87
    #                       overLimit (413)
88
    
87 89
    owner = get_user()
88
    vms = VirtualMachine.objects.filter(owner=owner, deleted=False)
89
    servers = [server_dict(vm, detail) for vm in vms]
90
    user_servers = VirtualMachine.objects.filter(owner=owner, deleted=False)
91
    servers = [server_to_dict(server, detail) for server in user_servers]
92
    
90 93
    if request.type == 'xml':
91
        mimetype = 'application/xml'
92 94
        data = render_to_string('list_servers.xml', dict(servers=servers, detail=detail))
93 95
    else:
94
        mimetype = 'application/json'
95
        data = json.dumps({'servers': servers})
96
    return HttpResponse(data, mimetype=mimetype, status=200)
96
        data = json.dumps({'servers': {'values': servers}})
97
    
98
    return HttpResponse(data, status=200)
97 99

  
98
@api_method
100
@api_method('POST')
99 101
def create_server(request):
100 102
    # Normal Response Code: 202
101 103
    # Error Response Codes: computeFault (400, 500),
......
121 123
    except Flavor.DoesNotExist:
122 124
        raise ItemNotFound
123 125
    
124
    vm = VirtualMachine.objects.create(
126
    server = VirtualMachine.objects.create(
125 127
        name=name,
126 128
        owner=get_user(),
127 129
        sourceimage=sourceimage,
......
133 135
        name = 'test-server'
134 136
        dry_run = True
135 137
    else:
136
        name = vm.backend_id
138
        name = server.backend_id
137 139
        dry_run = False
138 140
    
139 141
    jobId = rapi.CreateInstance(
......
149 151
        dry_run=dry_run,
150 152
        beparams=dict(auto_balance=True, vcpus=flavor.cpu, memory=flavor.ram))
151 153
    
152
    vm.save()
154
    server.save()
153 155
        
154 156
    log.info('created vm with %s cpus, %s ram and %s storage' % (flavor.cpu, flavor.ram, flavor.disk))
155 157
    
156
    server = server_dict(vm, detail=True)
157
    server['status'] = 'BUILD'
158
    server['adminPass'] = random_password()
159
    return render_server(server, request, status=202)
158
    serverdict = server_to_dict(server, detail=True)
159
    serverdict['status'] = 'BUILD'
160
    serverdict['adminPass'] = random_password()
161
    return render_server(request, serverdict, status=202)
160 162

  
161
@api_method
163
@api_method('GET')
162 164
def get_server_details(request, server_id):
165
    # Normal Response Codes: 200, 203
166
    # Error Response Codes: computeFault (400, 500),
167
    #                       serviceUnavailable (503),
168
    #                       unauthorized (401),
169
    #                       badRequest (400),
170
    #                       itemNotFound (404),
171
    #                       overLimit (413)
172
    
163 173
    try:
164
        vm = VirtualMachine.objects.get(id=int(server_id))
174
        server_id = int(server_id)
175
        server = VirtualMachine.objects.get(id=server_id)
165 176
    except VirtualMachine.DoesNotExist:
166
        raise NotFound
177
        raise ItemNotFound
167 178
    
168
    server = server_dict(vm, detail=True)
169
    return render_server(server, request)
179
    serverdict = server_to_dict(server, detail=True)
180
    return render_server(request, serverdict)
170 181

  
171
@api_method
172
def update_server_name(request, server_id):    
182
@api_method('PUT')
183
def update_server_name(request, server_id):
184
    # Normal Response Code: 204
185
    # Error Response Codes: computeFault (400, 500),
186
    #                       serviceUnavailable (503),
187
    #                       unauthorized (401),
188
    #                       badRequest (400),
189
    #                       badMediaType(415),
190
    #                       itemNotFound (404),
191
    #                       buildInProgress (409),
192
    #                       overLimit (413)
193
    
173 194
    req = get_request_dict(request)
174 195
    
175 196
    try:
176 197
        name = req['server']['name']
177
        vm = VirtualMachine.objects.get(id=int(server_id))
198
        server_id = int(server_id)
199
        server = VirtualMachine.objects.get(id=server_id)
178 200
    except KeyError:
179 201
        raise BadRequest
180 202
    except VirtualMachine.DoesNotExist:
181
        raise NotFound
203
        raise ItemNotFound
182 204
    
183
    vm.name = name
184
    vm.save()
205
    server.name = name
206
    server.save()
185 207
    
186 208
    return HttpResponse(status=204)
187 209

  
188
@api_method
210
@api_method('DELETE')
189 211
def delete_server(request, server_id):
212
    # Normal Response Codes: 204
213
    # Error Response Codes: computeFault (400, 500),
214
    #                       serviceUnavailable (503),
215
    #                       unauthorized (401),
216
    #                       itemNotFound (404),
217
    #                       unauthorized (401),
218
    #                       buildInProgress (409),
219
    #                       overLimit (413)
220
    
190 221
    try:
191
        vm = VirtualMachine.objects.get(id=int(server_id))
222
        server_id = int(server_id)
223
        server = VirtualMachine.objects.get(id=server_id)
192 224
    except VirtualMachine.DoesNotExist:
193
        raise NotFound
225
        raise ItemNotFound
194 226
    
195
    vm.start_action('DESTROY')
196
    rapi.DeleteInstance(vm.backend_id)
197
    vm.state = 'DESTROYED'
198
    vm.save()
227
    server.start_action('DESTROY')
228
    rapi.DeleteInstance(server.backend_id)
199 229
    return HttpResponse(status=204)

Also available in: Unified diff