Class: WavefrontCli::Event

Inherits:
Base
  • Object
show all
Includes:
Wavefront::Mixins, Mixin::Tag
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 included from Mixin::Tag

#do_tag_add, #do_tag_clear, #do_tag_delete, #do_tag_set, #do_tags

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_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, #smart_delete, #smart_delete_message, #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.



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

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



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

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



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

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



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

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



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

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



30
31
32
33
# File 'lib/wavefront-cli/event.rb', line 30

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

#do_wrapObject



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

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



18
19
20
21
# File 'lib/wavefront-cli/event.rb', line 18

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