Class: WavefrontCli::Event

Inherits:
Base
  • Object
show all
Includes:
Wavefront::Mixins
Defined in:
lib/wavefront-cli/event.rb

Overview

CLI coverage for the v2 ‘event’ API.

Constant Summary

Constants included from Constants

Constants::ALL_PAGE_SIZE, Constants::DEFAULT_CONFIG, Constants::DEFAULT_OPTS, Constants::HUMAN_TIME_FORMAT, Constants::HUMAN_TIME_FORMAT_MS

Instance Attribute Summary collapse

Attributes inherited from Base

#klass, #klass_word, #options, #wf

Instance Method Summary collapse

Methods inherited from Base

#_sdk_class, #cannot_noop!, #check_status, #conds_to_query, #dispatch, #display, #display_api_error, #display_no_api_response, #do_delete, #do_describe, #do_import, #do_search, #do_tag_add, #do_tag_clear, #do_tag_delete, #do_tag_set, #do_tags, #do_undelete, #extract_values, #failed_validation_message, #format_var, #handle_error, #handle_response, #hcl_fields, #import_to_create, #initialize, #load_display_class, #load_file, #load_from_stdin, #mk_creds, #mk_opts, #no_api_response, #ok_exit, #one_or_all, #options_and_exit, #parseable_output, #range_hash, #run, #search_key, #validate_id, #validate_opts, #validate_tags, #validator_exception, #validator_method

Constructor Details

This class inherits a constructor from WavefrontCli::Base

Instance Attribute Details

#state_dirObject (readonly)

Returns the value of attribute state_dir.



13
14
15
# File 'lib/wavefront-cli/event.rb', line 13

def state_dir
  @state_dir
end

Instance Method Details

#do_close(id = nil) ⇒ Object

The user doesn’t have to give us an event ID. If no event name is given, we’ll pop the last event off the stack. If an event name is given and it doesn’t look like a full WF event name, we’ll look for something on the stack. If it does look like a real event, we’ll make and API call straight away.

rubocop:disable Metrics/AbcSize



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/wavefront-cli/event.rb', line 63

def do_close(id = nil)
  id ||= options[:'<id>']
  ev_file = id =~ /^\d{13}:.+/ ? state_dir + id : nil
  ev = local_event(id)

  abort "No locally stored event matches '#{id}'." unless ev

  res = wf.close(ev)
  ev_file.unlink if ev_file&.exist? && res.status.code == 200
  res
end

#do_create(opts = nil) ⇒ Object

You can override the options generated by docopt. This is how #wrap() works.

rubocop:disable Metrics/AbcSize



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/wavefront-cli/event.rb', line 37

def do_create(opts = nil)
  opts ||= options

  opts[:start] = Time.now unless opts[:start]

  t_start = parse_time(opts[:start], true)

  body = create_body(opts, t_start)

  resp = wf.create(body)

  unless opts[:nostate] || opts[:end] || opts[:instant]
    create_state_file(resp.response[:id], opts[:host])
  end

  resp
end

#do_listObject



21
22
23
24
25
26
# File 'lib/wavefront-cli/event.rb', line 21

def do_list
  wf.list(options[:start]  || Time.now - 600,
          options[:end]    || Time.now,
          options[:limit]  || 100,
          options[:cursor] || nil)
end

#do_showObject

rubocop:enable Metrics/AbcSize



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/wavefront-cli/event.rb', line 76

def do_show
  events = local_event_list

  if events.size.zero?
    puts 'No open events.'
  else
    events.sort.reverse_each { |e| puts e.basename }
  end

  exit
end

#do_updateObject



28
29
30
31
# File 'lib/wavefront-cli/event.rb', line 28

def do_update
  k, v = options[:'<key=value>'].split('=')
  wf.update(options[:'<id>'], k => v)
end

#do_wrapObject



88
89
90
91
92
93
94
95
96
# File 'lib/wavefront-cli/event.rb', line 88

def do_wrap
  create_opts = options
  create_opts[:desc] ||= create_opts[:command]
  event_id = do_create(create_opts).response.id
  exit_code = run_wrapped_cmd(options[:command])
  do_close(event_id)
  puts "Command exited #{exit_code}"
  exit exit_code
end

#post_initialize(_options) ⇒ Object



16
17
18
19
# File 'lib/wavefront-cli/event.rb', line 16

def post_initialize(_options)
  @state_dir = EVENT_STATE_DIR + (Etc.getlogin || 'notty')
  create_state_dir
end