diff --git a/instances/views.py b/instances/views.py index 6eaa1d3..cf2070e 100644 --- a/instances/views.py +++ b/instances/views.py @@ -217,18 +217,26 @@ def instance(request, compute_id, vname): index += 1 return free_names - def check_user_quota(): - # TODO: count cpus, memory correctly - userinstances = UserInstance.objects.filter(user__id=request.user.id) - instances_count = len(userinstances) - cpus_count = instances_count - memory_count = instances_count * 2048 + def check_user_quota(instance, cpu, memory): + user_instances = UserInstance.objects.filter(user_id=request.user.id) + instance += len(user_instances) + for usr_inst in user_instances: + if connection_manager.host_is_up(usr_inst.instance.compute.type, + usr_inst.instance.compute.hostname): + conn = wvmHostDetails(usr_inst.instance.compute, + usr_inst.instance.compute.login, + usr_inst.instance.compute.password, + usr_inst.instance.compute.type) + wvm_instance = conn.get_user_instances(usr_inst.instance.name) + cpu += int(wvm_instance['vcpu']) + memory += int(wvm_instance['memory'])/1024 + ua = request.user.userattributes - if ua.max_instances > 0 and instances_count > ua.max_instances: + if ua.max_instances > 0 and instance > ua.max_instances: return "instance" - if ua.max_cpus > 0 and cpus_count > ua.max_cpus: + if ua.max_cpus > 0 and cpu > ua.max_cpus: return "cpu" - if ua.max_memory > 0 and memory_count > ua.max_memory: + if ua.max_memory > 0 and memory > ua.max_memory: return "memory" return "" @@ -393,10 +401,15 @@ def instance(request, compute_id, vname): if input_disk_size > disk['size']+(64<<20): disk['size_new'] = input_disk_size disks_new.append(disk) - conn.resize(cur_memory, memory, cur_vcpu, vcpu, disks_new) - msg = _("Resize") - addlogmsg(request.user.username, instance.name, msg) - return HttpResponseRedirect(request.get_full_path() + '#resize') + quota_msg = check_user_quota(0, vcpu, memory) + if not request.user.is_superuser and quota_msg: + msg = _("User %s quota reached, cannot resize '%s'!" % (quota_msg, instance.name)) + error_messages.append(msg) + else: + conn.resize(cur_memory, memory, cur_vcpu, vcpu, disks_new) + msg = _("Resize") + addlogmsg(request.user.username, instance.name, msg) + return HttpResponseRedirect(request.get_full_path() + '#resize') if 'umount_iso' in request.POST: image = request.POST.get('path', '') @@ -556,7 +569,7 @@ def instance(request, compute_id, vname): clone_data = {} clone_data['name'] = request.POST.get('name', '') - quota_msg = check_user_quota() + quota_msg = check_user_quota(1, conn.get_vcpu(), conn.get_memory()) check_instance = Instance.objects.filter(name=clone_data['name']) if not request.user.is_superuser and quota_msg: