From faa3a8b8c687afee7feda3372878106352624dff Mon Sep 17 00:00:00 2001 From: Daniel Molina Date: Fri, 14 Oct 2011 16:47:56 +0200 Subject: [PATCH] feature #801: Add onequota unset --- src/authm_mad/remotes/quota/onequota | 24 ++++++++++- src/authm_mad/remotes/quota/quota.rb | 41 ++++++++++++++++--- .../remotes/quota/test/quota_spec.rb | 26 ++++++------ 3 files changed, 71 insertions(+), 20 deletions(-) diff --git a/src/authm_mad/remotes/quota/onequota b/src/authm_mad/remotes/quota/onequota index a6fd24e669..0638a299c5 100755 --- a/src/authm_mad/remotes/quota/onequota +++ b/src/authm_mad/remotes/quota/onequota @@ -99,7 +99,7 @@ cmd=CommandParser::CmdParser.new(ARGV) do arg_list.map! {|a| a.to_i } [0, arg_list] end - + set :format, :userid, OpenNebulaHelper.rname_to_id_desc("USER") do |arg| OpenNebulaHelper.rname_to_id(arg, "USER") end @@ -130,6 +130,26 @@ cmd=CommandParser::CmdParser.new(ARGV) do exit_with_code 0 end + ######################################################################## + unset_desc = <<-EOT.unindent + Unset a quota for a given user. + Examples: + onequota unset 3 cpu + onequota unset 4 cpu,memory,storage + EOT + + command :unset, unset_desc, :userid, :quota_list do + user_id, keys = args + + values_hash = Hash.new + keys.each_with_index { |k,i| + values_hash[k.to_sym] = 0 + } + + quota.set_quota(user_id, values_hash) + exit_with_code 0 + end + ######################################################################## delete_desc = "Delete the defined quotas for the given user" @@ -140,7 +160,7 @@ cmd=CommandParser::CmdParser.new(ARGV) do ######################################################################## show_desc = "Show the user's quota and usage. (usage/quota)" - + FORCE={ :name => "force", :short => "-f", diff --git a/src/authm_mad/remotes/quota/quota.rb b/src/authm_mad/remotes/quota/quota.rb index 466cfe5013..805eff1d6e 100644 --- a/src/authm_mad/remotes/quota/quota.rb +++ b/src/authm_mad/remotes/quota/quota.rb @@ -154,6 +154,19 @@ class Quota set(QUOTA_TABLE, uid, quota) end + # Retrieves quota information for a given user + # + # @param [Integer, nil] uid the user id from which get the quota + # information, if nil will retrieve the quotas for all users. + # @return [Hash] Hash containing the quota information and the user id + # + # { + # :uid => 4, + # :cpu => 8, + # :memory => 8064, + # :num_vms => 4, + # :storage => 1240019 + # } def get_quota(uid=nil) limit = get(QUOTA_TABLE, uid) limit ? limit : @conf[:defaults].merge!(:uid => uid) @@ -192,14 +205,16 @@ class Quota msg = "" separator = "" info.each { |qname, quota_requested| - unless quota[qname] + unless quota[qname] || quota[qname]==0 next end - used = send(DB_QUOTA_SCHEMA[qname].name.to_sym, total[qname]) - request = send(DB_QUOTA_SCHEMA[qname].name.to_sym, quota_requested) - limit = send(DB_QUOTA_SCHEMA[qname].name.to_sym, quota[qname]) - spent = used + request + type = DB_QUOTA_SCHEMA[qname].name.to_sym + + used = send(type, total[qname]) + request = send(type, quota_requested) + limit = send(type, quota[qname]) + spent = used + request if spent > limit msg << separator @@ -228,6 +243,22 @@ class Quota ########################################################################### # Usage ########################################################################### + # Retrieves usage information for a given user + # + # @param [Integer] uid the user id from which get the usage information. + # @param ["VM", "IMAGE"] resource kind of resource. If nil will return + # the usage for all kinds of resources + # @param [true, false] force If true will force the usage calculation + # instead of retrieving it from the cache + # @return [Hash] Hash containing the usage information and the user id + # + # { + # :uid => 4, + # :cpu => 8, + # :memory => 8064, + # :num_vms => 4, + # :storage => 1240019 + # } def get_usage(user_id, resource=nil, force=false) if force if RESOURCES.include?(resource) diff --git a/src/authm_mad/remotes/quota/test/quota_spec.rb b/src/authm_mad/remotes/quota/test/quota_spec.rb index ff80f0c6c8..fd6d656f18 100644 --- a/src/authm_mad/remotes/quota/test/quota_spec.rb +++ b/src/authm_mad/remotes/quota/test/quota_spec.rb @@ -80,10 +80,10 @@ describe "Quota testing" do it "should check default cache (force)" do usage1force = @quota.get_usage(@uid1, nil, true) usage1force[:uid].should eql(0) - usage1force[:num_vms].should eql(nil) - usage1force[:cpu].should eql(nil) - usage1force[:memory].should eql(nil) - usage1force[:storage].should eql(nil) + usage1force[:num_vms].should eql(0) + usage1force[:cpu].should eql(0) + usage1force[:memory].should eql(0) + usage1force[:storage].should eql(0) end it "should authorize the user because there is no quota defined" do @@ -106,10 +106,10 @@ describe "Quota testing" do it "should check the usage cache is not updated" do usage1cache = @quota.get_usage(@uid1) usage1cache[:uid].should eql(0) - usage1cache[:num_vms].should eql(nil) - usage1cache[:cpu].should eql(nil) - usage1cache[:memory].should eql(nil) - usage1cache[:storage].should eql(nil) + usage1cache[:num_vms].should eql(0) + usage1cache[:cpu].should eql(0.0) + usage1cache[:memory].should eql(0) + usage1cache[:storage].should eql(0) end it "should check the cache (force)" do @@ -118,7 +118,7 @@ describe "Quota testing" do usage1force[:num_vms].should eql(1) usage1force[:cpu].should eql(2.0) usage1force[:memory].should eql(128) - usage1force[:storage].should eql(nil) + usage1force[:storage].should eql(0) end it "should check the usage cache is updated and contains the last usage" do @@ -127,7 +127,7 @@ describe "Quota testing" do usage1cache[:num_vms].should eql(1) usage1cache[:cpu].should eql(2.0) usage1cache[:memory].should eql(128) - usage1cache[:storage].should eql(nil) + usage1cache[:storage].should eql(0) end it "should add a new Image" do @@ -146,7 +146,7 @@ describe "Quota testing" do usage1cache[:num_vms].should eql(1) usage1cache[:cpu].should eql(2.0) usage1cache[:memory].should eql(128) - usage1cache[:storage].should eql(nil) + usage1cache[:storage].should eql(0) end it "should check the cache (force)" do @@ -252,7 +252,7 @@ describe "Quota testing" do end it "should not authorize the user because the vm quota is spent" do - err_msg = "CPU quota exceeded (Quota: 2.4, Used: 4.0, Asked: 2.0)." + err_msg = "CPU quota exceeded (Quota: 2.4, Used: 4.0, Requested: 2.0)" @quota.authorize(@uid1, @acl_vm_create).should eql(err_msg) @quota.authorize(@uid1, @acl_template_instantiate).should eql(err_msg) @@ -320,7 +320,7 @@ describe "Quota testing" do @quota.authorize(@uid1, @acl_vm_create).should eql(false) @quota.authorize(@uid1, @acl_template_instantiate).should eql(false) - err_msg = "STORAGE quota exceeded (Quota: 0, Used: 2000, Asked: 1474)." + err_msg = "STORAGE quota exceeded (Quota: 0, Used: 2000, Requested: 271)" @quota.authorize(@uid1, @acl_image_create).should eql(err_msg) end