33 |
33 |
|
34 |
34 |
import logging
|
35 |
35 |
|
|
36 |
from django.conf import settings
|
36 |
37 |
from django.conf.urls.defaults import patterns
|
37 |
38 |
from django.http import HttpResponse
|
38 |
39 |
from django.template.loader import render_to_string
|
39 |
40 |
from django.utils import simplejson as json
|
40 |
41 |
|
41 |
|
from synnefo.api import util
|
|
42 |
from synnefo.api import faults, util
|
42 |
43 |
from synnefo.api.actions import server_actions
|
43 |
44 |
from synnefo.api.common import method_not_allowed
|
44 |
|
from synnefo.api.faults import BadRequest, ItemNotFound, ServiceUnavailable
|
45 |
45 |
from synnefo.db.models import VirtualMachine, VirtualMachineMetadata
|
46 |
46 |
from synnefo.logic.backend import create_instance, delete_instance
|
47 |
47 |
from synnefo.logic.utils import get_rsapi_state
|
... | ... | |
199 |
199 |
image_id = server['imageRef']
|
200 |
200 |
flavor_id = server['flavorRef']
|
201 |
201 |
except (KeyError, AssertionError):
|
202 |
|
raise BadRequest('Malformed request.')
|
|
202 |
raise faults.BadRequest("Malformed request")
|
203 |
203 |
|
204 |
204 |
image = util.get_image(image_id, owner)
|
205 |
205 |
flavor = util.get_flavor(flavor_id)
|
206 |
206 |
password = util.random_password()
|
207 |
207 |
|
|
208 |
count = VirtualMachine.objects.filter(owner=owner, deleted=False).count()
|
|
209 |
if count >= settings.MAX_VMS_PER_USER:
|
|
210 |
raise faults.OverLimit("Maximum number of servers reached")
|
|
211 |
|
208 |
212 |
# We must save the VM instance now, so that it gets a valid vm.backend_id.
|
209 |
213 |
vm = VirtualMachine.objects.create(
|
210 |
214 |
name=name,
|
... | ... | |
216 |
220 |
create_instance(vm, flavor, image, password)
|
217 |
221 |
except GanetiApiError:
|
218 |
222 |
vm.delete()
|
219 |
|
raise ServiceUnavailable('Could not create server.')
|
|
223 |
raise faults.ServiceUnavailable("Could not create server")
|
220 |
224 |
|
221 |
225 |
for key, val in metadata.items():
|
222 |
226 |
VirtualMachineMetadata.objects.create(
|
... | ... | |
263 |
267 |
try:
|
264 |
268 |
name = req['server']['name']
|
265 |
269 |
except (TypeError, KeyError):
|
266 |
|
raise BadRequest('Malformed request.')
|
|
270 |
raise faults.BadRequest("Malformed request")
|
267 |
271 |
|
268 |
272 |
vm = util.get_vm(server_id, request.user)
|
269 |
273 |
vm.name = name
|
... | ... | |
291 |
295 |
vm = util.get_vm(server_id, request.user)
|
292 |
296 |
req = util.get_request_dict(request)
|
293 |
297 |
if len(req) != 1:
|
294 |
|
raise BadRequest('Malformed request.')
|
|
298 |
raise faults.BadRequest("Malformed request")
|
295 |
299 |
|
296 |
300 |
key = req.keys()[0]
|
297 |
301 |
val = req[key]
|
... | ... | |
300 |
304 |
assert isinstance(val, dict)
|
301 |
305 |
return server_actions[key](request, vm, req[key])
|
302 |
306 |
except KeyError:
|
303 |
|
raise BadRequest('Unknown action.')
|
|
307 |
raise faults.BadRequest("Unknown action")
|
304 |
308 |
except AssertionError:
|
305 |
|
raise BadRequest('Invalid argument.')
|
|
309 |
raise faults.BadRequest("Invalid argument")
|
306 |
310 |
|
307 |
311 |
@util.api_method('GET')
|
308 |
312 |
def list_addresses(request, server_id):
|
... | ... | |
376 |
380 |
metadata = req['metadata']
|
377 |
381 |
assert isinstance(metadata, dict)
|
378 |
382 |
except (KeyError, AssertionError):
|
379 |
|
raise BadRequest('Malformed request.')
|
|
383 |
raise faults.BadRequest("Malformed request")
|
380 |
384 |
|
381 |
385 |
updated = {}
|
382 |
386 |
|
... | ... | |
428 |
432 |
assert len(metadict) == 1
|
429 |
433 |
assert key in metadict
|
430 |
434 |
except (KeyError, AssertionError):
|
431 |
|
raise BadRequest('Malformed request.')
|
|
435 |
raise faults.BadRequest("Malformed request")
|
432 |
436 |
|
433 |
437 |
meta, created = VirtualMachineMetadata.objects.get_or_create(
|
434 |
438 |
meta_key=key,
|