39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
# File 'lib/softlayer/cli.rb', line 39
def report
client_opts = {}
client_opts[:username] = options['username'] if options['username']
client_opts[:api_key] = options['api_key'] if options['api_key']
SoftLayer::Client.default_client = SoftLayer::Client.new(client_opts)
search_opts = options['search'] ? {}.merge(options['search']) : {}
search_opts.symbolize_keys!
search_opts[:object_mask] = OBJECT_MASK
search_opts[:result_limit] = { offset: 0, limit: 150 }
column_opts = COLUMNS
column_opts = options['columns'] if options['columns'].is_a? Array
rows = []
%w[BareMetalServer VirtualServer].each do |class_name|
loop do
servers = SoftLayer.const_get(class_name).find_servers(search_opts)
servers.each do |server|
next if server.respond_to?(:status) && server.status == 'DISCONNECTED'
cancellation_date = server['billingItem'] && server['billingItem']['cancellationDate']
next unless cancellation_date.nil? || cancellation_date.strip.empty?
row = []
row << server.id if column_opts.include? 'id'
row << class_name if column_opts.include? 'type'
row << server.fqdn if column_opts.include? 'hostname'
row << (server['primaryIpAddress'] || server['primaryBackendIpAddress']) if column_opts.include? 'ip'
row << server['datacenter']['longName'] if column_opts.include? 'datacenter'
if server['billingItem']
cost = server['billingItem']['recurringFee'].to_f
if (server['billingItem']['hourlyRecurringFee'] &&
!server['billingItem']['hourlyRecurringFee'].strip.empty?)
cost = server['billingItem']['hourlyRecurringFee'].to_f * 24 * 30
end
server['billingItem']['activeAssociatedChildren'].each do |child|
if child['hourlyRecurringFee'] && !child['hourlyRecurringFee'].strip.empty?
cost += (child['hourlyRecurringFee'].to_f * 24 * 30)
else
cost += child['recurringFee'].to_f
end
end
row << cost.round(2) if column_opts.include? 'cost'
user_record = server['billingItem']['orderItem']['order']['userRecord']
row << user_record['username'] if column_opts.include? 'username'
row << "#{user_record['firstName']} #{user_record['lastName']}" if column_opts.include? 'name'
row << user_record['email'] if column_opts.include? 'email'
else
row << '' if column_opts.include? 'cost'
row << '' if column_opts.include? 'username'
row << '' if column_opts.include? 'name'
row << '' if column_opts.include? 'email'
end
row << server['provisionDate'] if column_opts.include? 'provision_date'
rows << row
end
break if servers.size < search_opts[:result_limit][:limit]
search_opts[:result_limit][:offset] += search_opts[:result_limit][:limit]
end
end
CLIFormatter.format(
rows,
options['format'],
COLUMNS.reject { |c| !column_opts.include?(c) },
options['output_file']
)
end
|