Revision 64bca363
b/snf-cyclades-app/synnefo/logic/servers.py | ||
---|---|---|
56 | 56 |
return |
57 | 57 |
|
58 | 58 |
|
59 |
def server_command(action): |
|
59 |
def server_command(action, action_fields=None):
|
|
60 | 60 |
"""Handle execution of a server action. |
61 | 61 |
|
62 | 62 |
Helper function to validate and execute a server action, handle quota |
... | ... | |
85 | 85 |
|
86 | 86 |
commission_name = "client: api, resource: %s" % vm |
87 | 87 |
quotas.handle_resource_commission(vm, action=action, |
88 |
action_fields=action_fields, |
|
88 | 89 |
commission_name=commission_name) |
89 | 90 |
vm.save() |
90 | 91 |
|
... | ... | |
275 | 276 |
return backend.reboot_instance(vm, reboot_type.lower()) |
276 | 277 |
|
277 | 278 |
|
278 |
@server_command("RESIZE") |
|
279 | 279 |
def resize(vm, flavor): |
280 |
action_fields = {"beparams": {"vcpus": flavor.cpu, |
|
281 |
"maxmem": flavor.ram}} |
|
282 |
comm = server_command("RESIZE", action_fields=action_fields) |
|
283 |
return comm(_resize)(vm, flavor) |
|
284 |
|
|
285 |
|
|
286 |
def _resize(vm, flavor): |
|
280 | 287 |
old_flavor = vm.flavor |
281 | 288 |
# User requested the same flavor |
282 | 289 |
if old_flavor.id == flavor.id: |
283 | 290 |
raise faults.BadRequest("Server '%s' flavor is already '%s'." |
284 | 291 |
% (vm, flavor)) |
285 |
return None |
|
286 | 292 |
# Check that resize can be performed |
287 | 293 |
if old_flavor.disk != flavor.disk: |
288 | 294 |
raise faults.BadRequest("Cannot resize instance disk.") |
... | ... | |
290 | 296 |
raise faults.BadRequest("Cannot change instance disk template.") |
291 | 297 |
|
292 | 298 |
log.info("Resizing VM from flavor '%s' to '%s", old_flavor, flavor) |
293 |
commission_info = {"cyclades.cpu": flavor.cpu - old_flavor.cpu, |
|
294 |
"cyclades.ram": 1048576 * (flavor.ram - old_flavor.ram)} |
|
295 |
# Save serial to VM, since it is needed by server_command decorator |
|
296 |
vm.serial = quotas.issue_commission(user=vm.userid, |
|
297 |
source=quotas.DEFAULT_SOURCE, |
|
298 |
provisions=commission_info, |
|
299 |
name="resource: %s. resize" % vm) |
|
300 | 299 |
return backend.resize_instance(vm, vcpus=flavor.cpu, memory=flavor.ram) |
301 | 300 |
|
302 | 301 |
|
b/snf-cyclades-app/synnefo/logic/tests/servers.py | ||
---|---|---|
151 | 151 |
vm = mfactory.VirtualMachineFactory(operstate="STOPPED") |
152 | 152 |
self.assertRaises(faults.BadRequest, servers.stop, vm) |
153 | 153 |
vm = mfactory.VirtualMachineFactory(operstate="STARTED") |
154 |
self.assertRaises(faults.BadRequest, servers.resize, vm) |
|
154 |
flavor = mfactory.FlavorFactory() |
|
155 |
self.assertRaises(faults.BadRequest, servers.resize, vm, flavor) |
|
155 | 156 |
# Check that connect/disconnect is allowed only in STOPPED vms |
156 | 157 |
# if hotplug is disabled. |
157 | 158 |
vm = mfactory.VirtualMachineFactory(operstate="STARTED") |
b/snf-cyclades-app/synnefo/quotas/__init__.py | ||
---|---|---|
350 | 350 |
|
351 | 351 |
def handle_resource_commission(resource, action, commission_name, |
352 | 352 |
commission_info=None, force=False, |
353 |
auto_accept=False): |
|
353 |
auto_accept=False, action_fields=None):
|
|
354 | 354 |
"""Handle a issuing of a commission for a resource. |
355 | 355 |
|
356 | 356 |
Create a new commission for a resource based on the action that |
... | ... | |
364 | 364 |
# Check if action is quotable and issue the corresponding commission |
365 | 365 |
serial = None |
366 | 366 |
if commission_info is None: |
367 |
commission_info = get_commission_info(resource, action=action) |
|
367 |
commission_info = get_commission_info( |
|
368 |
resource, action=action, action_fields=action_fields) |
|
368 | 369 |
if commission_info is not None: |
369 | 370 |
# Issue new commission, associate it with the resource |
370 | 371 |
if commission_name is None: |
Also available in: Unified diff