1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-02-23 21:57:43 +03:00

feature #203: added default and unlimited quota limits

This commit is contained in:
Javi Fontan 2010-07-13 12:32:11 +02:00
parent 47dddf34d5
commit b624debb22
3 changed files with 51 additions and 11 deletions

View File

@ -14,17 +14,29 @@
# limitations under the License. #
#--------------------------------------------------------------------------- #
require 'one_usage'
# Quota functionality for auth driver. Stores in database limits for each
# user and using OneUsage is able to retrieve resource usage from
# OpenNebula daemon and check if it is below limits
class Quota
attr_accessor :defaults
TABLE_NAME=:quotas
# 'db' is a Sequel database where to store user limits and client
# is OpenNebula::Client used to connect to OpenNebula daemon
def initialize(db, client)
def initialize(db, client, conf={})
@db=db
@client=client
@conf={
:defaults => {
:cpu => nil,
:memory => nil
}
}.merge(conf)
@defaults=@conf[:defaults]
@usage=OneUsage.new(@client)
@ -45,18 +57,30 @@ class Quota
end
# Adds new user limits
def add(uid, cpu, memory, num_vms)
@table.insert(
:uid => uid,
def set(uid, cpu, memory, num_vms)
data={
:cpu => cpu,
:memory => memory,
:num_vms => num_vms
)
}
quotas=@table.filter(:uid => uid)
if quotas.first
quotas.update(data)
else
@table.insert(data.merge!(:uid => uid))
end
end
# Gets user limits
def get(uid)
@table.filter(:uid => uid).first
limit=@table.filter(:uid => uid).first
if limit
limit
else
@defaults
end
end
# Checks if the user is below resource limits. If new_vm is defined
@ -68,7 +92,9 @@ class Quota
usage.cpu+=new_vm.cpu
usage.memory+=new_vm.memory
end
usage.cpu<=user_quota[:cpu] && usage.memory<=user_quota[:memory]
(!user_quota[:cpu] || usage.cpu<=user_quota[:cpu]) &&
(!user_quota[:memory] || usage.memory<=user_quota[:memory])
end
# Updates user resource consuption

View File

@ -16,6 +16,7 @@
require 'spec_common'
require 'client_mock'
require 'quota'
def check_quota(uid, cpu, memory, num_vms)
@ -45,15 +46,15 @@ describe 'Quota' do
end
it 'should let add and retrieve quotas' do
@quota.add(0, 10.0, 1024, 10)
@quota.add(1, 20.0, 2048, 20)
@quota.add(2, 40.0, 4096, 40)
@quota.set(0, 10.0, 1024, 10)
@quota.set(1, 20.0, 2048, 20)
@quota.set(2, 40.0, 4096, 40)
check_quota(0, 10.0, 1024, 10)
check_quota(1, 20.0, 2048, 20)
check_quota(2, 40.0, 4096, 40)
@quota.get(3).should == nil
@quota.get(3).should == @quota.defaults
end
it 'should check for quotas' do
@ -77,6 +78,18 @@ describe 'Quota' do
@quota.check(1).should == true
end
it 'should let update limits' do
@quota.set(0, nil, nil, nil)
check_quota(0, nil, nil, nil)
end
it 'should understand unlimited quotas' do
vms=@quota.get_user(0)
vms[7]=VmUsage.new(9999999999.0, 99999999999)
@quota.check(0).should == true
@quota.check(0, VmUsage.new(999999999.0, 99999999)).should == true
end
end

View File

@ -19,6 +19,7 @@ require 'pp'
require 'db_helpers'
$: << '../oca/ruby'
$: << './spec'