Revision 1af851fd snf-cyclades-app/synnefo/api/actions.py

b/snf-cyclades-app/synnefo/api/actions.py
41 41
from django.utils import simplejson as json
42 42

  
43 43
from snf_django.lib.api import faults
44
from synnefo.api import util
44 45
from synnefo.api.util import (random_password, get_vm, get_nic_from_index,
45 46
                              get_network_free_address)
46 47
from synnefo.db.models import NetworkInterface
......
168 169
    #                       serverCapacityUnavailable (503),
169 170
    #                       overLimit (413),
170 171
    #                       resizeNotAllowed (403)
172
    log.debug("resize %s: %s", vm, args)
173
    flavorRef = args.get("flavorRef", None)
174
    if flavorRef is None:
175
        raise faults.BadRequest("Missing 'flavorRef' attribute")
176

  
177
    new_flavor = util.get_flavor(flavor_id=flavorRef, include_deleted=False)
178
    old_flavor = vm.flavor
179
    # User requested the same flavor
180
    if old_flavor.id == new_flavor.id:
181
        return HttpResponse(status=200)
182
    # Check that resize can be performed
183
    if old_flavor.disk != new_flavor.disk:
184
        raise faults.BadRequest("Can not resize instance disk")
185
    if old_flavor.disk_template != new_flavor.disk_template:
186
        raise faults.BadRequest("Can not change instance disk template")
187

  
188
    if not vm_is_stopped(vm):
189
        raise faults.BadRequest("Can not resize running instance")
190

  
191
    vcpus, memory = new_flavor.cpu, new_flavor.ram
192
    jobId = backend.resize_instance(vm, vcpus=vcpus, memory=memory)
193

  
194
    log.info("User '%s' resized VM from flavor '%s' to '%s', job %s",
195
              request.user_uniq, old_flavor, new_flavor, jobId)
196

  
197
    # Save operstate now, since you don't want any other action when an
198
    # an instance is resizing
199
    vm.operstate = "RESIZE"
200
    util.start_action(vm, "RESIZE", jobId)
201

  
202
    vm.save()
203
    return HttpResponse(status=202)
171 204

  
172
    raise faults.NotImplemented('Resize not supported.')
205

  
206
def vm_is_stopped(vm):
207
    """Check if a VirtualMachine is currently stopped.
208

  
209
    A server is stopped if it's operstate is 'STOPPED'. Also, you must check
210
    that no other job is currently running, because this job may start the
211
    instance
212
    """
213
    return vm.operstate == "STOPPED" and vm.backendjobstatus == "success"
173 214

  
174 215

  
175 216
@server_action('confirmResize')

Also available in: Unified diff