Class: Morpheus::Cli::UsageCommand

Inherits:
Object
  • Object
show all
Includes:
CliCommand, OptionSourceHelper
Defined in:
lib/morpheus/cli/commands/usage_command.rb

Overview

CLI command usages UI is Costing - Usage API is /usage and returns usages

Instance Attribute Summary

Attributes included from CliCommand

#no_prompt

Instance Method Summary collapse

Methods included from OptionSourceHelper

#find_available_user_option, #find_cloud_option, #find_group_option, #find_tenant_option, #get_available_user_options, #get_cloud_options, #get_group_options, #get_tenant_options, included, #load_option_source_data, #options_interface, #parse_cloud_id_list, #parse_group_id_list, #parse_option_source_id_list, #parse_project_id_list, #parse_tenant_id_list, #parse_user_id_list

Methods included from CliCommand

#add_query_parameter, #apply_options, #build_common_options, #build_get_options, #build_list_options, #build_option_type_options, #build_standard_add_many_options, #build_standard_add_options, #build_standard_api_options, #build_standard_delete_options, #build_standard_get_options, #build_standard_list_options, #build_standard_post_options, #build_standard_put_options, #build_standard_remove_options, #build_standard_update_options, #command_description, #command_name, #confirm, #confirm!, #default_refresh_interval, #default_sigdig, #default_subcommand, #establish_remote_appliance_connection, #execute_api, #execute_api_payload, #execute_api_request, #find_all, #find_all_json, #find_by_id, #find_by_name, #find_by_name_or_id, #find_record, #find_record_json, #full_command_usage, #get_interface, #get_list_key, #get_object_key, #get_subcommand_description, #handle_each_payload, #handle_subcommand, included, #interactive?, #my_help_command, #my_terminal, #my_terminal=, #parse_array, #parse_bytes_param, #parse_get_options!, #parse_id_list, #parse_labels, #parse_list_options, #parse_list_options!, #parse_list_subtitles, #parse_options, #parse_parameter_as_resource_id!, #parse_passed_options, #parse_payload, #parse_query_options, #print, #print_error, #println, #prog_name, #puts, #puts_error, #raise_args_error, #raise_command_error, #render_response, #run_command_for_each_arg, #subcommand_aliases, #subcommand_description, #subcommand_usage, #subcommands, #usage, #validate_outfile, #verify_args!, #visible_subcommands

Instance Method Details

#_get(id, params, options) ⇒ Object



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
# File 'lib/morpheus/cli/commands/usage_command.rb', line 123

def _get(id, params, options)
  usage = nil
  @usage_interface.setopts(options)
  if options[:dry_run]
    print_dry_run @usage_interface.dry.get(id, params)
    return
  end
  json_response = @usage_interface.get(id, params)
  usage = json_response[usage_object_key]
  render_response(json_response, options, usage_object_key) do
    print_h1 "Usage Details", [], options
    print cyan
    show_columns = {
      "ID" => 'id',
      "Cloud" => 'zoneName',
      "Type" => lambda {|it| format_usage_type(it) },
      "Name" => 'name',
      "Plan" => 'planName',
      "Start Date" => lambda {|it| format_local_dt(it['startDate']) },
      "End Date" => lambda {|it| format_local_dt(it['endDate']) },
      "Usage Status" => lambda {|it| format_usage_status(it) },
      "Usage Cost" => lambda {|it| format_money(it['costDetails']['cost'], it['currency'] || 'USD', {sigdig: (options[:sigdig] || 5)}) },
      "Usage Price" => lambda {|it| format_money(it['price'], it['currency'] || 'USD', {sigdig: (options[:sigdig] || 5)}) },
    }
    print_description_list(show_columns, usage)

    # print_h2 "Applicable Prices"
    
    print reset,"\n"
  end
  return 0, nil
end

#connect(opts) ⇒ Object



14
15
16
17
# File 'lib/morpheus/cli/commands/usage_command.rb', line 14

def connect(opts)
  @api_client = establish_remote_appliance_connection(opts)
  @usage_interface = @api_client.usage
end

#get(args) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/morpheus/cli/commands/usage_command.rb', line 103

def get(args)
  params = {}
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[usage]")
    build_standard_get_options(opts, options)
    opts.footer = <<-EOT
Get details about a specific usage.
[usage] is required. This is the id of a usage record.
EOT
  end
  optparse.parse!(args)
  verify_args!(args:args, optparse:optparse, min:1)
  connect(options)
  id_list = parse_id_list(args)
  return run_command_for_each_arg(id_list) do |arg|
    _get(arg, params, options)
  end
end

#handle(args) ⇒ Object



19
20
21
# File 'lib/morpheus/cli/commands/usage_command.rb', line 19

def handle(args)
  handle_subcommand(args)
end

#list(args) ⇒ Object



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
# File 'lib/morpheus/cli/commands/usage_command.rb', line 23

def list(args)
  options = {}
  params = {}
  ref_ids = []
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[search]")
    opts.on( '-t', '--type TYPE', "Filter by type" ) do |val|
      params['type'] = parse_usage_type(val)
    end
    opts.on( '-c', '--cloud CLOUD', "Filter by cloud" ) do |val|
      options[:cloud] = 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 the current date.") do |val|
      params['endDate'] = val # parse_time(val).utc.iso8601
    end
    opts.on('--sigdig DIGITS', "Significant digits when rounding cost values for display as currency. Default is 5.") do |val|
      options[:sigdig] = val.to_i
    end
    build_standard_list_options(opts, options)
    opts.footer = "List usages."
  end
  optparse.parse!(args)
  connect(options)
  # verify_args!(args:args, optparse:optparse, count:0)
  if args.count > 0
    options[:phrase] = args.join(" ")
  end
  params.merge!(parse_list_options(options))
  # --cloud
  if options[:cloud]
    params['cloud'] = parse_id_list(options[:cloud]).collect {|cloud_id|
      if cloud_id.to_s =~ /\A\d{1,}\Z/
        cloud_id
      else
        cloud = find_cloud_option(cloud_id)
        return 1 if cloud.nil?
        cloud['id']
      end
    }
  end

  @usage_interface.setopts(options)
  if options[:dry_run]
    print_dry_run @usage_interface.dry.list(params)
    return
  end
  json_response = @usage_interface.list(params)
  usages = json_response[usage_list_key]
  render_response(json_response, options, usage_list_key) do
    print_h1 "Morpheus Usages", parse_list_subtitles(options), options
    if usages.empty?
      print cyan,"No usages found.",reset,"\n"
    else
      list_columns = {
        "ID" => 'id',
        "Cloud" => 'zoneName',
        "Type" => lambda {|it| format_usage_type(it) },
        "Name" => 'name',
        "Plan" => 'planName',
        "Start Date" => lambda {|it| format_local_dt(it['startDate']) },
        "End Date" => lambda {|it| format_local_dt(it['endDate']) },
        "Usage Status" => lambda {|it| format_usage_status(it) },
        "Usage Cost" => lambda {|it| format_money(it['costDetails']['cost'], it['currency'] || 'USD', {sigdig: (options[:sigdig] || 5)}) },
        "Usage Price" => lambda {|it| format_money(it['price'], it['currency'] || 'USD', {sigdig: (options[:sigdig] || 5)}) },
      }
      print as_pretty_table(usages, list_columns.upcase_keys!, options)
      print_results_pagination(json_response)
    end
    print reset,"\n"
  end
  if usages.empty?
    return 1, "no usages found"
  else
    return 0, nil
  end
end