def vm_edit_view(req): dicts = {} arg_vmid = req.GET.get('vmid') if req.method == 'POST': arg_cpu = req.POST.get('cpu') arg_mem = req.POST.get('mem') status_res = api_vm_status({'req_user': req.user, 'uuid': arg_vmid}) if status_res['res'] and status_res['status'] != 1: if arg_cpu.isdigit() and arg_mem.isdigit(): edit_res = api_vm_edit({ 'req_user': req.user, 'uuid': arg_vmid, 'vcpu': arg_cpu, 'mem': arg_mem }) if not edit_res['res']: dicts['warning'] = '修改失败。' else: dicts['warning'] = '只能修改处于关机状态的虚拟机。' obj_res = api_vm_get({'req_user': req.user, 'uuid': arg_vmid}) if obj_res['res']: obj = obj_res['info'] else: return HttpResponseRedirect('../list/') dicts['vmobj'] = obj host_res = api_host_get({'req_user': req.user, 'host_id': obj['host_id']}) if host_res['res']: host = host_res['info'] dicts['remain_mem'] = host['mem_total'] - \ host['mem_allocated'] - \ host['mem_reserved'] if dicts['remain_mem'] < 0: dicts['remain_mem'] = 0 dicts['remain_cpu'] = host['vcpu_total'] - host['vcpu_allocated'] if dicts['remain_cpu'] < 0: dicts['remain_cpu'] = 0 return render_to_response('vmadmin_edit.html', dicts, context_instance=RequestContext(req))
def vm_edit_view(req): dicts = {} arg_vmid = req.GET.get('vmid') if req.method == 'POST': arg_cpu = req.POST.get('cpu') arg_mem = req.POST.get('mem') status_res = api_vm_status({'req_user': req.user, 'uuid': arg_vmid}) if status_res['res'] and status_res['status'] != 1: if arg_cpu.isdigit() and arg_mem.isdigit(): edit_res = api_vm_edit({'req_user': req.user, 'uuid': arg_vmid, 'vcpu': arg_cpu, 'mem': arg_mem}) if not edit_res['res']: dicts['warning'] = '修改失败。' else: dicts['warning'] = '只能修改处于关机状态的虚拟机。' obj_res = api_vm_get({'req_user': req.user, 'uuid': arg_vmid}) if obj_res['res']: obj = obj_res['info'] else: return HttpResponseRedirect('../list/') dicts['vmobj'] = obj host_res = api_host_get({'req_user': req.user, 'host_id': obj['host_id']}) if host_res['res']: host = host_res['info'] dicts['remain_mem'] = host['mem_total'] - \ host['mem_allocated'] - \ host['mem_reserved'] if dicts['remain_mem'] < 0: dicts['remain_mem'] = 0 dicts['remain_cpu'] = host['vcpu_total'] - host['vcpu_allocated'] if dicts['remain_cpu'] < 0: dicts['remain_cpu'] = 0 return render_to_response('vmadmin_edit.html', dicts, context_instance=RequestContext(req))
def _get_host_by_id(user, host_id): host_info = api_host_get({'req_user': user, 'host_id': host_id}) if host_info['res']: return host_info['info'] return None