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 1f13234e45..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) @@ -242,9 +273,9 @@ class Quota pool = get_pool(res, user_id) base_xpath = "/#{res}_POOL/#{resource}" Quota.const_get("#{res}_USAGE".to_sym).each { |key, params| + usage[key] ||= 0 pool.each_xpath("#{base_xpath}/#{params[:xpath]}") { |elem| if elem - usage[key] ||= 0 if params[:count] usage[key] += 1 else diff --git a/src/authm_mad/remotes/quota/test/quota_spec.rb b/src/authm_mad/remotes/quota/test/quota_spec.rb index ff80f0c6c8..cf58842847 100644 --- a/src/authm_mad/remotes/quota/test/quota_spec.rb +++ b/src/authm_mad/remotes/quota/test/quota_spec.rb @@ -31,7 +31,7 @@ describe "Quota testing" do