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.

Instance Attribute Summary collapse

Attributes inherited from Base

#klass, #klass_word, #options, #wf

Instance Method Summary collapse

Methods inherited from Base

#check_status, #dispatch, #display, #do_delete, #do_describe, #do_import, #do_search, #do_tag_add, #do_tag_clear, #do_tag_delete, #do_tag_set, #do_tags, #do_undelete, #format_var, #handle_error, #handle_response, #import_to_create, #initialize, #load_display_class, #load_file, #mk_creds, #mk_opts, #run, #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.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/wavefront-cli/event.rb', line 80

def do_close(id = nil)
  id ||= options[:'<id>']
  ev_file = nil

  ev = if !id
         pop_event
       elsif id =~ /^\d{13}:.+/
         ev_file = state_dir + id
         id
       else
         pop_event(id)
       end

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

  res = wf.close(ev)
  ev_file.unlink if ev_file && 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.



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
# File 'lib/wavefront-cli/event.rb', line 41

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

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

  t_start = parse_time(opts[:start], true)
  id = [t_start, opts[:'<event>']].join(':')

  body = { name:        opts[:'<event>'],
           startTime:   t_start,
           id:          id,
           annotations: {} }

  body[:annotations][:details] = opts[:desc] if opts[:desc]
  body[:annotations][:severity] = opts[:severity] if opts[:severity]
  body[:annotations][:type] = opts[:type] if opts[:type]
  body[:hosts] = opts[:host] if opts[:host]

  if opts[:instant]
    body[:endTime] = t_start + 1
  elsif opts[:end]
    body[:endTime] = parse_time(opts[:end], true)
  end

  resp = wf.create(body)

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

  resp
end

#do_listObject



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

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

#do_showObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/wavefront-cli/event.rb', line 100

def do_show
  begin
    events = state_dir.children
  rescue Errno::ENOENT
    raise 'There is no event state directory on this host.'
  end

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

  exit
end

#do_updateObject



33
34
35
36
# File 'lib/wavefront-cli/event.rb', line 33

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

#do_wrapObject



116
117
118
119
120
121
122
123
124
# File 'lib/wavefront-cli/event.rb', line 116

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
20
21
22
23
24
# File 'lib/wavefront-cli/event.rb', line 16

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

  create_state_dir
end