Revision cc7c0f44 snf-cyclades-app/synnefo/api/servers.py

b/snf-cyclades-app/synnefo/api/servers.py
31 31
# interpreted as representing official policies, either expressed
32 32
# or implied, of GRNET S.A.
33 33

  
34
import random
35

  
36 34
from base64 import b64decode
37 35
from logging import getLogger
38 36

  
......
49 47
from synnefo.logic.backend import create_instance, delete_instance
50 48
from synnefo.logic.utils import get_rsapi_state
51 49
from synnefo.util.rapi import GanetiApiError
50
from synnefo.logic.backend_allocator import BackendAllocator
51

  
52
from django.utils import importlib
53

  
52 54

  
53 55

  
54 56
log = getLogger('synnefo.api')
......
196 198
    #                       badRequest (400),
197 199
    #                       serverCapacityUnavailable (503),
198 200
    #                       overLimit (413)
199

  
200 201
    req = util.get_request_dict(request)
201 202
    log.debug('create_server %s', req)
202 203

  
......
253 254
    if count >= vms_limit_for_user:
254 255
        raise faults.OverLimit("Server count limit exceeded for your account.")
255 256

  
256
    backend = random.choice(Backend.objects.all())
257
    # We must save the VM instance now, so that it gets a
258
    # valid vm.backend_vm_id.
257
    backend_allocator = BackendAllocator()
258
    backend = backend_allocator.allocate(flavor)
259
    if backend is None:
260
        raise Exception
261

  
262
    # We must save the VM instance now, so that it gets a valid
263
    # vm.backend_vm_id.
259 264
    vm = VirtualMachine.objects.create(
260 265
        name=name,
261 266
        backend=backend,

Also available in: Unified diff