Class: Morpheus::Cli::InvoicesCommand
- Inherits:
-
Object
- Object
- Morpheus::Cli::InvoicesCommand
- Includes:
- CliCommand
- Defined in:
- lib/morpheus/cli/invoices_command.rb
Instance Attribute Summary
Attributes included from CliCommand
Instance Method Summary collapse
- #_get(id, options) ⇒ Object
- #connect(opts) ⇒ Object
- #get(args) ⇒ Object
- #handle(args) ⇒ Object
- #list(args) ⇒ Object
Methods included from CliCommand
#apply_options, #build_common_options, #build_option_type_options, #build_standard_add_options, #build_standard_get_options, #build_standard_list_options, #build_standard_remove_options, #build_standard_update_options, #command_name, #default_refresh_interval, #default_subcommand, #establish_remote_appliance_connection, #full_command_usage, #handle_subcommand, included, #interactive?, #my_help_command, #my_terminal, #my_terminal=, #parse_bytes_param, #parse_id_list, #parse_list_options, #parse_list_subtitles, #parse_payload, #parse_query_options, #print, #print_error, #println, #puts, #puts_error, #raise_command_error, #render_with_format, #run_command_for_each_arg, #subcommand_aliases, #subcommand_usage, #subcommands, #usage, #verify_access_token!
Instance Method Details
#_get(id, options) ⇒ Object
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 |
# File 'lib/morpheus/cli/invoices_command.rb', line 261 def _get(id, ) params = {} if [:show_raw_data] params['rawData'] = true end begin @invoices_interface.setopts() if [:dry_run] print_dry_run @invoices_interface.dry.get(id, params) return end json_response = @invoices_interface.get(id, params) invoice = json_response['invoice'] render_result = render_with_format(json_response, , 'invoice') return 0 if render_result print_h1 "Invoice Details" print cyan description_cols = { "Invoice ID" => lambda {|it| it['id'] }, "Type" => lambda {|it| format_invoice_ref_type(it) }, "Ref ID" => lambda {|it| it['refId'] }, "Ref Name" => lambda {|it| it['refName'] }, "Plan" => lambda {|it| it['plan'] ? it['plan']['name'] : '' }, "Power State" => lambda {|it| format_server_power_state(it) }, "Account" => lambda {|it| it['account'] ? it['account']['name'] : '' }, "Active" => lambda {|it| format_boolean(it['active']) }, "Period" => lambda {|it| format_invoice_period(it) }, #"Interval" => lambda {|it| it['interval'] }, "Start" => lambda {|it| format_date(it['startDate']) }, "End" => lambda {|it| it['endDate'] ? format_date(it['endDate']) : '' }, "Estimate" => lambda {|it| format_boolean(it['estimate']) }, "Created" => lambda {|it| format_local_dt(it['dateCreated']) }, "Updated" => lambda {|it| format_local_dt(it['lastUpdated']) } } # remove columns that do not apply if !invoice['plan'] description_cols.delete("Plan") end if !['ComputeServer','Instance','Container'].include?(invoice['refType']) description_cols.delete("Power State") end print_description_list(description_cols, invoice) =begin print_h2 "Costs" cost_columns = { "Compute" => lambda {|it| format_money(it['computeCost']) }, "Memory" => lambda {|it| format_money(it['memoryCost']) }, "Storage" => lambda {|it| format_money(it['storageCost']) }, "Network" => lambda {|it| format_money(it['networkCost']) }, "License" => lambda {|it| format_money(it['licenseCost']) }, "Other" => lambda {|it| format_money(it['extraCost']) }, "Running" => lambda {|it| format_money(it['runningCost']) }, "Total Cost" => lambda {|it| format_money(it['totalCost']) }, } print as_pretty_table([invoice], cost_columns, options) print_h2 "Prices" price_columns = { "Compute" => lambda {|it| format_money(it['computePrice']) }, "Memory" => lambda {|it| format_money(it['memoryPrice']) }, "Storage" => lambda {|it| format_money(it['storagePrice']) }, "Network" => lambda {|it| format_money(it['networkPrice']) }, "License" => lambda {|it| format_money(it['licensePrice']) }, "Other" => lambda {|it| format_money(it['extraPrice']) }, "Running" => lambda {|it| format_money(it['runningPrice']) }, "Total Price" => lambda {|it| format_money(it['totalPrice']) }, } print as_pretty_table([invoice], price_columns, options) =end # current_date = Time.now # current_period = "#{current_date.year}#{current_date.month.to_s.rjust(2, '0')}" print "\n" # print_h2 "Costs" cost_rows = [ {label: 'Usage Price'.upcase, compute: invoice['computePrice'], memory: invoice['memoryPrice'], storage: invoice['storagePrice'], network: invoice['networkPrice'], license: invoice['licensePrice'], extra: invoice['extraPrice'], running: invoice['runningPrice'], total: invoice['totalPrice']}, {label: 'Usage Cost'.upcase, compute: invoice['computeCost'], memory: invoice['memoryCost'], storage: invoice['storageCost'], network: invoice['networkCost'], license: invoice['licenseCost'], extra: invoice['extraCost'], running: invoice['runningCost'], total: invoice['totalCost']}, {label: 'Actual Cost'.upcase, compute: invoice['actualComputeCost'], memory: invoice['actualMemoryCost'], storage: invoice['actualStorageCost'], network: invoice['actualNetworkCost'], license: invoice['actualLicenseCost'], extra: invoice['actualExtraCost'], running: invoice['actualRunningCost'], total: invoice['actualTotalCost']}, ] cost_columns = { "" => lambda {|it| it[:label] }, "Compute".upcase => lambda {|it| format_money(it[:compute]) }, "Memory".upcase => lambda {|it| format_money(it[:memory]) }, "Storage".upcase => lambda {|it| format_money(it[:storage]) }, "Network".upcase => lambda {|it| format_money(it[:network]) }, "License".upcase => lambda {|it| format_money(it[:license]) }, "Other".upcase => lambda {|it| format_money(it[:extra]) }, "MTD" => lambda {|it| format_money(it[:running]) }, "Total".upcase => lambda {|it| if invoice['runningMultiplier'] && invoice['runningMultiplier'].to_i != 1 && it[:total].to_f.to_f > 0 format_money(it[:total]) + " (Projected)" else format_money(it[:total]) end }, } # remove columns that rarely have data... if cost_rows.sum { |it| it[:memory].to_f } == 0 cost_columns.delete("Memory".upcase) end if cost_rows.sum { |it| it[:license].to_f } == 0 cost_columns.delete("License".upcase) end if cost_rows.sum { |it| it[:extra].to_f } == 0 cost_columns.delete("Other".upcase) end print as_pretty_table(cost_rows, cost_columns, ) if [:show_raw_data] print_h2 "Raw Data" puts invoice['rawData'] end print reset,"\n" return 0 rescue RestClient::Exception => e print_rest_exception(e, ) return 1 end end |
#connect(opts) ⇒ Object
11 12 13 14 |
# File 'lib/morpheus/cli/invoices_command.rb', line 11 def connect(opts) @api_client = establish_remote_appliance_connection(opts) @invoices_interface = @api_client.invoices end |
#get(args) ⇒ Object
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/morpheus/cli/invoices_command.rb', line 239 def get(args) = {} optparse = Morpheus::Cli::OptionParser.new do |opts| opts. = subcommand_usage("[id]") opts.on('--raw-data', '--raw-data', "Display Raw Data, the cost data from the cloud provider's API.") do |val| [:show_raw_data] = true end (opts, ) opts. = "Get details about a specific invoice." end optparse.parse!(args) if args.count < 1 puts optparse return 1 end connect() id_list = parse_id_list(args) return run_command_for_each_arg(id_list) do |arg| _get(arg, ) end end |
#handle(args) ⇒ Object
16 17 18 |
# File 'lib/morpheus/cli/invoices_command.rb', line 16 def handle(args) handle_subcommand(args) end |
#list(args) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/morpheus/cli/invoices_command.rb', line 20 def list(args) = {} params = {} ref_ids = [] optparse = Morpheus::Cli::OptionParser.new do |opts| opts. = subcommand_usage() opts.on('-a', '--all', "Display all costs, prices and raw data" ) do [:show_actual_costs] = true [:show_costs] = true [:show_prices] = true [:show_raw_data] = true end opts.on('--actuals', '--actuals', "Display all actual costs: Compute, Memory, Storage, etc." ) do [:show_actual_costs] = true end opts.on('--costs', '--costs', "Display all costs: Compute, Memory, Storage, etc." ) do [:show_costs] = true end opts.on('--prices', '--prices', "Display prices: Total, Compute, Memory, Storage, etc." ) do [:show_prices] = true end opts.on('--type TYPE', String, "Filter by Ref Type eg. ComputeSite (Group), ComputeZone (Cloud), ComputeServer (Host), Instance, Container, User") do |val| if val.to_s.downcase == 'cloud' || val.to_s.downcase == 'zone' params['refType'] = 'ComputeZone' elsif val.to_s.downcase == 'instance' params['refType'] = 'Instance' elsif val.to_s.downcase == 'server' || val.to_s.downcase == 'host' params['refType'] = 'ComputeServer' elsif val.to_s.downcase == 'cluster' params['refType'] = 'ComputeServerGroup' elsif val.to_s.downcase == 'group' params['refType'] = 'ComputeSite' elsif val.to_s.downcase == 'user' params['refType'] = 'User' else params['refType'] = val end end opts.on('--id ID', String, "Filter by Ref ID") do |val| ref_ids << val end opts.on('--ref-id ID', String, "Filter by Ref ID") do |val| ref_ids << val end opts.add_hidden_option('--ref-id') opts.on('--group ID', String, "Filter by Group") do |val| params['siteId'] ||= [] params['siteId'] << val end opts.on('--cloud ID', String, "Filter by Cloud") do |val| params['zoneId'] ||= [] params['zoneId'] << val end opts.on('--instance ID', String, "Filter by Instance") do |val| params['instanceId'] ||= [] params['instanceId'] << val end opts.on('--container ID', String, "Filter by Container") do |val| params['containerId'] ||= [] params['containerId'] << val end opts.on('--server ID', String, "Filter by Server (Host)") do |val| params['serverId'] ||= [] params['serverId'] << val end opts.on('--user ID', String, "Filter by User") do |val| params['userId'] ||= [] params['userId'] << val end # opts.on('--cluster ID', String, "Filter by Cluster") do |val| # params['clusterId'] ||= [] # params['clusterId'] << val # end opts.on('--start DATE', String, "Start date in the format YYYY-MM-DD.") do |val| params['startDate'] = val #parse_time(val).utc.iso8601 end opts.on('--end DATE', String, "End date in the format YYYY-MM-DD. Default is now.") do |val| params['endDate'] = val #parse_time(val).utc.iso8601 end opts.on('--period PERIOD', String, "Period in the format YYYYMM. This can be used instead of start/end.") do |val| params['period'] = parse_period(val) end opts.on('--active [true|false]',String, "Filter by active.") do |val| params['active'] = (val.to_s != 'false' && val.to_s != 'off') end opts.on('--estimate [true|false]',String, "Filter by estimate.") do |val| params['estimate'] = (val.to_s != 'false' && val.to_s != 'off') end opts.on('--tenant ID', String, "View invoices for a tenant. Default is your own account.") do |val| params['accountId'] = val end opts.on('--raw-data', '--raw-data', "Display Raw Data, the cost data from the cloud provider's API.") do |val| [:show_raw_data] = true end (opts, ) opts. = "List invoices." end optparse.parse!(args) connect() if args.count > 0 print_error Morpheus::Terminal.angry_prompt puts_error "wrong number of arguments, expected 0 and got (#{args.count}) #{args.join(', ')}\n#{optparse}" return 1 end begin params['rawData'] = true if [:show_raw_data] params['refId'] = ref_ids unless ref_ids.empty? params.merge!(()) @invoices_interface.setopts() if [:dry_run] print_dry_run @invoices_interface.dry.list(params) return end json_response = @invoices_interface.list(params) render_result = render_with_format(json_response, , 'invoices') return 0 if render_result invoices = json_response['invoices'] title = "Morpheus Invoices" subtitles = [] if params['status'] subtitles << "Status: #{params['status']}" end if params['alarmStatus'] == 'acknowledged' subtitles << "(Acknowledged)" end if params['startDate'] subtitles << "Start Date: #{params['startDate']}" end if params['endDate'] subtitles << "End Date: #{params['endDate']}" end subtitles += parse_list_subtitles() print_h1 title, subtitles if invoices.empty? print cyan,"No invoices found.",reset,"\n" else # current_date = Time.now # current_period = "#{current_date.year}#{current_date.month.to_s.rjust(2, '0')}" columns = [ {"INVOICE ID" => lambda {|it| it['id'] } }, {"TYPE" => lambda {|it| format_invoice_ref_type(it) } }, {"REF ID" => lambda {|it| it['refId'] } }, {"REF NAME" => lambda {|it| it['refName'] } }, #{"INTERVAL" => lambda {|it| it['interval'] } }, {"CLOUD" => lambda {|it| it['cloud'] ? it['cloud']['name'] : '' } }, {"ACCOUNT" => lambda {|it| it['account'] ? it['account']['name'] : '' } }, {"ACTIVE" => lambda {|it| format_boolean(it['active']) } }, #{"ESTIMATE" => lambda {|it| format_boolean(it['estimate']) } }, {"PERIOD" => lambda {|it| format_invoice_period(it) } }, {"START" => lambda {|it| format_date(it['startDate']) } }, {"END" => lambda {|it| it['endDate'] ? format_date(it['endDate']) : '' } }, {"MTD" => lambda {|it| format_money(it['runningCost']) } }, {"TOTAL" => lambda {|it| if it['runningMultiplier'] && it['runningMultiplier'].to_i != 1 && it['totalCost'].to_f > 0 format_money(it['totalCost']) + " (Projected)" else format_money(it['totalCost']) end } }, {"ACTUAL MTD" => lambda {|it| format_money(it['actualRunningCost']) } }, {"ACTUAL TOTAL" => lambda {|it| if it['runningMultiplier'] && it['runningMultiplier'].to_i != 1 && it['actualTotalCost'].to_f > 0 format_money(it['actualTotalCost']) + " (Projected)" else format_money(it['actualTotalCost']) end } } ] if [:show_costs] columns += [ {"COMPUTE" => lambda {|it| format_money(it['computeCost']) } }, # {"MEMORY" => lambda {|it| format_money(it['memoryCost']) } }, {"STORAGE" => lambda {|it| format_money(it['storageCost']) } }, {"NETWORK" => lambda {|it| format_money(it['networkCost']) } }, {"OTHER" => lambda {|it| format_money(it['extraCost']) } }, ] end if [:show_actual_costs] columns += [ {"ACTUAL COMPUTE" => lambda {|it| format_money(it['actualComputeCost']) } }, # {"ACTUAL MEMORY" => lambda {|it| format_money(it['actualMemoryCost']) } }, {"ACTUAL STORAGE" => lambda {|it| format_money(it['actualStorageCost']) } }, {"ACTUAL NETWORK" => lambda {|it| format_money(it['actualNetworkCost']) } }, {"ACTUAL OTHER" => lambda {|it| format_money(it['actualExtraCost']) } }, ] end if [:show_prices] columns += [ {"COMPUTE PRICE" => lambda {|it| format_money(it['computePrice']) } }, # {"MEMORY PRICE" => lambda {|it| format_money(it['memoryPrice']) } }, {"STORAGE PRICE" => lambda {|it| format_money(it['storagePrice']) } }, {"NETWORK PRICE" => lambda {|it| format_money(it['networkPrice']) } }, {"OTHER PRICE" => lambda {|it| format_money(it['extraPrice']) } }, {"MTD PRICE" => lambda {|it| format_money(it['runningPrice']) } }, {"TOTAL PRICE" => lambda {|it| if it['runningMultiplier'] && it['runningMultiplier'].to_i != 1 && it['totalPrice'].to_f > 0 format_money(it['totalPrice']) + " (Projected)" else format_money(it['totalPrice']) end } } ] end if [:show_raw_data] columns += [{"RAW DATA" => lambda {|it| truncate_string(it['rawData'].to_s, 10) } }] end print as_pretty_table(invoices, columns, ) print_results_pagination(json_response, {:label => "invoice", :n_label => "invoices"}) end print reset,"\n" rescue RestClient::Exception => e print_rest_exception(e, ) return 1 end end |