mirror of
https://github.com/OpenNebula/one.git
synced 2025-03-22 18:50:08 +03:00
Merge branch 'feature-1484'
This commit is contained in:
commit
be2ff24801
@ -14,6 +14,8 @@
|
||||
# limitations under the License. #
|
||||
#--------------------------------------------------------------------------- #
|
||||
|
||||
require 'csv'
|
||||
|
||||
module CLIHelper
|
||||
LIST = {
|
||||
:name => "list",
|
||||
@ -23,6 +25,12 @@ module CLIHelper
|
||||
:description => "Selects columns to display with list command"
|
||||
}
|
||||
|
||||
CSV = {
|
||||
:name => "csv",
|
||||
:large => "--csv",
|
||||
:description => "Write table in csv format"
|
||||
}
|
||||
|
||||
#ORDER = {
|
||||
# :name => "order",
|
||||
# :short => "-o x,y,z",
|
||||
@ -56,7 +64,7 @@ module CLIHelper
|
||||
}
|
||||
|
||||
#OPTIONS = [LIST, ORDER, FILTER, HEADER, DELAY]
|
||||
OPTIONS = [LIST, DELAY, FILTER]
|
||||
OPTIONS = [LIST, DELAY, FILTER, CSV]
|
||||
|
||||
# Sets bold font
|
||||
def CLIHelper.scr_bold
|
||||
@ -154,6 +162,8 @@ module CLIHelper
|
||||
class ShowTable
|
||||
require 'yaml'
|
||||
|
||||
attr_reader :default_columns
|
||||
|
||||
def initialize(conf=nil, ext=nil, &block)
|
||||
@columns = Hash.new
|
||||
@default_columns = Array.new
|
||||
@ -241,7 +251,7 @@ module CLIHelper
|
||||
private
|
||||
|
||||
def print_table(data, options)
|
||||
CLIHelper.print_header(header_str)
|
||||
CLIHelper.print_header(header_str) if !options[:csv]
|
||||
data ? print_data(data, options) : puts
|
||||
end
|
||||
|
||||
@ -257,17 +267,24 @@ module CLIHelper
|
||||
end
|
||||
|
||||
begin
|
||||
res_data.each{|l|
|
||||
puts (0..ncolumns-1).collect{ |i|
|
||||
dat=l[i]
|
||||
col=@default_columns[i]
|
||||
if options[:csv]
|
||||
CSV($stdout, :write_headers => true,
|
||||
:headers => @default_columns) do |csv|
|
||||
res_data.each {|l| csv << l }
|
||||
end
|
||||
else
|
||||
res_data.each{|l|
|
||||
puts (0..ncolumns-1).collect{ |i|
|
||||
dat=l[i]
|
||||
col=@default_columns[i]
|
||||
|
||||
str=format_str(col, dat)
|
||||
str=CLIHelper.color_state(str) if i==stat_column
|
||||
str=format_str(col, dat)
|
||||
str=CLIHelper.color_state(str) if i==stat_column
|
||||
|
||||
str
|
||||
}.join(' ').rstrip
|
||||
}
|
||||
str
|
||||
}.join(' ').rstrip
|
||||
}
|
||||
end
|
||||
rescue Errno::EPIPE
|
||||
end
|
||||
end
|
||||
|
@ -100,6 +100,10 @@ class AcctHelper < OpenNebulaHelper::OneHelper
|
||||
|
||||
|
||||
ACCT_TABLE = CLIHelper::ShowTable.new("oneacct.yaml", nil) do
|
||||
column :UID, "User ID", :size=>4 do |d|
|
||||
d["UID"]
|
||||
end
|
||||
|
||||
column :VID, "Virtual Machine ID", :size=>4 do |d|
|
||||
d["OID"]
|
||||
end
|
||||
|
@ -44,7 +44,7 @@ cmd = CommandParser::CmdParser.new(ARGV) do
|
||||
end
|
||||
|
||||
option AcctHelper::ACCT_OPTIONS + CommandParser::OPTIONS +
|
||||
[OpenNebulaHelper::DESCRIBE, CLIHelper::LIST] +
|
||||
[OpenNebulaHelper::DESCRIBE, CLIHelper::LIST, CLIHelper::CSV] +
|
||||
OpenNebulaHelper::CLIENT_OPTIONS
|
||||
|
||||
main do
|
||||
@ -87,7 +87,10 @@ cmd = CommandParser::CmdParser.new(ARGV) do
|
||||
else
|
||||
order_by = Hash.new
|
||||
order_by[:order_by_1] = 'VM/UID'
|
||||
order_by[:order_by_2] = 'VM/ID' if options[:split]
|
||||
|
||||
if options[:split] && !options[:csv]
|
||||
order_by[:order_by_2] = 'VM/ID'
|
||||
end
|
||||
|
||||
acct_hash = pool.accounting(filter_flag,
|
||||
common_opts.merge(order_by))
|
||||
@ -96,6 +99,21 @@ cmd = CommandParser::CmdParser.new(ARGV) do
|
||||
exit -1
|
||||
end
|
||||
|
||||
if options[:csv]
|
||||
a=Array.new
|
||||
acct_hash.each do |user_id, value|
|
||||
value['HISTORY_RECORDS']['HISTORY'].each do |l|
|
||||
l['UID']=user_id
|
||||
a << l
|
||||
end
|
||||
end
|
||||
|
||||
cols=AcctHelper::ACCT_TABLE.default_columns
|
||||
AcctHelper::ACCT_TABLE.default(:UID, *cols)
|
||||
|
||||
AcctHelper::ACCT_TABLE.show(a, options)
|
||||
exit(0)
|
||||
end
|
||||
|
||||
if ( start_time != -1 or end_time != -1 )
|
||||
AcctHelper.print_start_end_time_header(start_time, end_time)
|
||||
|
Loading…
x
Reference in New Issue
Block a user