lib/cloudstack-cli/commands/capacity.rb in cloudstack-cli-0.3.4 vs lib/cloudstack-cli/commands/capacity.rb in cloudstack-cli-0.3.5

- old
+ new

@@ -1,17 +1,17 @@ class Capacity < CloudstackCli::Base CAPACITY_TYPES = { - 0 => "Memory", - 1 => "CPU", - 2 => "Storage", - 3 => "Storage Allocated", - 4 => "Public IP's", - 5 => "Private IP's", - 6 => "Secondary Storage", - 7 => "VLAN", - 8 => "Direct Attached Public IP's", - 9 => "Local Storage" + 0 => {name: "Memory", unit: "GB", divider: 1024.0**3}, + 1 => {name: "CPU", unit: "GHz", divider: 1000.0}, + 2 => {name: "Storage", unit: "TB", divider: 1024.0**4}, + 3 => {name: "Primary Storage", unit: "TB", divider: 1024.0**4}, + 4 => {name: "Public IP's"}, + 5 => {name: "Private IP's"}, + 6 => {name: "Secondary Storage", unit: "TB", divider: 1024.0**4}, + 7 => {name: "VLAN"}, + 8 => {name: "Direct Attached Public IP's"}, + 9 => {name: "Local Storage", unit: "TB", divider: 1024.0**4} } desc "list", "list system capacity" option :zone def list @@ -19,16 +19,29 @@ table = [] header = ["Zone", "Type", "Capacity Used", "Capacity Total", "Used"] capacities.each do |c| table << [ c['zonename'], - CAPACITY_TYPES[c['type']], - c['capacityused'], - c['capacitytotal'], + CAPACITY_TYPES[c['type']][:name], + capacity_to_s(c, 'capacityused'), + capacity_to_s(c, 'capacitytotal'), "#{c['percentused']}%" ] end table = table.sort {|a, b| [a[0], a[1]] <=> [b[0], b[1]]}.insert(0, header) print_table table + end + + no_commands do + + def capacity_to_s(capacity, entity) + value = CAPACITY_TYPES[capacity['type']][:divider] ? + (capacity[entity] / CAPACITY_TYPES[capacity['type']][:divider]).round(1) : + capacity[entity] + CAPACITY_TYPES[capacity['type']][:unit] ? + "#{value}#{CAPACITY_TYPES[capacity['type']][:unit]}" : + value.to_s + end + end end \ No newline at end of file