Class: Morpheus::Cli::InvoicesCommand

Inherits:
Object
  • Object
show all
Includes:
CliCommand
Defined in:
lib/morpheus/cli/invoices_command.rb

Instance Attribute Summary

Attributes included from CliCommand

#no_prompt

Instance Method Summary collapse

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



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
# File 'lib/morpheus/cli/invoices_command.rb', line 166

def _get(id, options)

  begin
    @invoices_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @invoices_interface.dry.get(id)
      return
    end
    json_response = @invoices_interface.get(id)
    invoice = json_response['invoice']
    render_result = render_with_format(json_response, options, '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'] : '' },
      "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_local_dt(it['startDate']) },
      "End" => lambda {|it| it['endDate'] ? format_local_dt(it['endDate']) : '' },
      "Estimate" => lambda {|it| format_boolean(it['estimate']) },
      "Price" => lambda {|it| format_money(it['totalPrice']) },
      "Cost" => lambda {|it| format_money(it['totalCost']) },
      # "Created" => lambda {|it| format_local_dt(it['dateCreated']) },
      # "Updated" => lambda {|it| format_local_dt(it['lastUpdated']) }
    }
    print_description_list(description_cols, invoice)

    if invoice['rawData'] && !invoice['rawData'].empty?
      print_h2 "Raw Data"
      puts invoice['rawData']
    end
    

    print reset,"\n"
    return 0
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    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



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/morpheus/cli/invoices_command.rb', line 147

def get(args)
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[id]")
    build_standard_get_options(opts, options)
    opts.footer = "Get details about a specific invoice."
  end
  optparse.parse!(args)
  if args.count < 1
    puts optparse
    return 1
  end
  connect(options)
  id_list = parse_id_list(args)
  return run_command_for_each_arg(id_list) do |arg|
    _get(arg, options)
  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
# File 'lib/morpheus/cli/invoices_command.rb', line 20

def list(args)
  options = {}
  params = {}
  ref_ids = []
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage()
    opts.on('--type TYPE', String, "Find invoices for a 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('--ref-id ID', String, "Find invoices for a Ref ID") do |val|
      ref_ids << val
    end
    opts.on('--group ID', String, "Find invoices for a Group") do |val|
      # params['siteId'] = val
      params['refType'] = 'ComputeSite'
      ref_ids << val
    end
    opts.on('--cloud ID', String, "Find invoices for a Cloud") do |val|
      # params['zoneId'] = val
      params['refType'] = 'ComputeZone'
      ref_ids << val
    end
    opts.on('--instance ID', String, "Find invoices for an Instance") do |val|
      # params['instanceId'] = val
      params['refType'] = 'Instance'
      ref_ids << val
    end
    opts.on('--container ID', String, "Find invoices for a Container") do |val|
      # params['instanceId'] = val
      params['refType'] = 'Container'
      ref_ids << val
    end
    opts.on('--server ID', String, "Find invoices for a Server (Host)") do |val|
      # params['serverId'] = val
      params['refType'] = 'ComputeServer'
      ref_ids << val
    end
    opts.on('--user ID', String, "Find invoices for a User") do |val|
      # params['userId'] = val
      params['refType'] = 'User'
      ref_ids << val
    end
    # opts.on('--cluster ID', String, "Filter by Cluster") do |val|
    #   # params['clusterId'] = val
    #   params['refType'] = 'ComputeServerGroup'
    #   params['refId'] = val.to_i
    # 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('--tenant ID', String, "View invoices for a tenant. Default is your own account.") do |val|
      params['accountId'] = val
    end
    build_standard_list_options(opts, options)
    opts.footer = "List invoices."
  end
  optparse.parse!(args)
  connect(options)
  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['refId'] = ref_ids unless ref_ids.empty?
    params.merge!(parse_list_options(options))
    @invoices_interface.setopts(options)
    if options[: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, options, 'invoice')
    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(options)
    print_h1 title, subtitles
    if invoices.empty?
      print cyan,"No invoices found.",reset,"\n"
    else
      print_invoices_table(invoices, options)
      print_results_pagination(json_response, {:label => "invoice", :n_label => "invoices"})
    end
    print reset,"\n"
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    return 1
  end
end