Class: WavefrontCli::Alert

Inherits:
Base
  • Object
show all
Defined in:
lib/wavefront-cli/alert.rb

Overview

CLI coverage for the v2 ‘alert’ 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

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_import, #do_list, #do_search, #do_tag_add, #do_tag_clear, #do_tag_delete, #do_tag_set, #do_tags, #do_undelete, #do_update, #extract_values, #failed_validation_message, #format_var, #handle_error, #handle_response, #hcl_fields, #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_input, #validate_opts, #validate_tags, #validator_exception, #validator_method

Constructor Details

This class inherits a constructor from WavefrontCli::Base

Instance Method Details

#do_currentlyObject



43
44
45
46
47
48
49
50
51
# File 'lib/wavefront-cli/alert.rb', line 43

def do_currently
  state = options[:'<state>'].to_s

  if wf.respond_to?(state)
    in_state(state)
  else
    abort format("'%s' is not a valid alert state.", state)
  end
end

#do_deleteObject

rubocop:disable Metrics/AbcSize



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/wavefront-cli/alert.rb', line 21

def do_delete
  cannot_noop!

  word = if wf.describe(options[:'<id>']).status.code == 200
           'Soft'
         else
           'Permanently'
         end

  puts "#{word} deleting alert '#{options[:'<id>']}'."
  wf.delete(options[:'<id>'])
end

#do_describeObject



8
9
10
# File 'lib/wavefront-cli/alert.rb', line 8

def do_describe
  wf.describe(options[:'<id>'], options[:version])
end

#do_firingObject



53
54
55
# File 'lib/wavefront-cli/alert.rb', line 53

def do_firing
  in_state(:firing)
end

#do_historyObject



39
40
41
# File 'lib/wavefront-cli/alert.rb', line 39

def do_history
  wf.history(options[:'<id>'], options[:offset], options[:limit])
end

#do_installObject



71
72
73
# File 'lib/wavefront-cli/alert.rb', line 71

def do_install
  wf.install(options[:'<id>'])
end

#do_queriesObject



61
62
63
64
65
66
67
68
69
# File 'lib/wavefront-cli/alert.rb', line 61

def do_queries
  resp, data = one_or_all

  resp.tap do |r|
    r.response.items = data.map do |a|
      { id: a.id, condition: a.condition }
    end
  end
end

#do_snoozeObject



12
13
14
# File 'lib/wavefront-cli/alert.rb', line 12

def do_snooze
  wf.snooze(options[:'<id>'], options[:time])
end

#do_snoozedObject



57
58
59
# File 'lib/wavefront-cli/alert.rb', line 57

def do_snoozed
  in_state(:snoozed)
end

#do_summaryObject

rubocop:enable Metrics/AbcSize



35
36
37
# File 'lib/wavefront-cli/alert.rb', line 35

def do_summary
  wf.summary
end

#do_uninstallObject



75
76
77
# File 'lib/wavefront-cli/alert.rb', line 75

def do_uninstall
  wf.uninstall(options[:'<id>'])
end

#do_unsnoozeObject



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

def do_unsnooze
  wf.unsnooze(options[:'<id>'])
end

#find_in_state(status) ⇒ Object

Does the work for #in_state

Parameters:

  • status (Symbol, String)

    the alert status you wish to find

Returns:

  • Wavefront::Response



94
95
96
97
98
99
100
101
102
# File 'lib/wavefront-cli/alert.rb', line 94

def find_in_state(status)
  search = do_search([format('status=%s', status)])

  items = search.response.items.map do |i|
    { name: i.name, id: i.id, time: state_time(i) }
  end

  search.tap { |s| s.response[:items] = items }
end

#import_to_create(raw) ⇒ Object

Take a previously exported alert, and construct a hash which create() can use to re-create it.

Parameters:

  • raw (Hash)

    Ruby hash of imported data



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/wavefront-cli/alert.rb', line 120

def import_to_create(raw)
  ret = %w[name condition minutes target severity displayExpression
           additionalInformation].each_with_object({}) do |k, aggr|
    aggr[k.to_sym] = raw[k]
  end

  if raw.key?('resolveAfterMinutes')
    ret[:resolveMinutes] = raw['resolveAfterMinutes']
  end

  if raw.key?('customerTagsWithCounts')
    ret[:sharedTags] = raw['customerTagsWithCounts'].keys
  end
  ret
end

#in_state(status) ⇒ Object

How many alerts are in the given state? If none, say so, rather than just printing nothing.



82
83
84
85
86
87
# File 'lib/wavefront-cli/alert.rb', line 82

def in_state(status)
  options[:all] = true
  ret = find_in_state(status)
  ok_exit(format('No alerts are currently %s.', status)) if ret.empty?
  ret
end

#state_time(item) ⇒ Integer

Snoozed alerts don’t have a start time, they have a “snoozed” time. This is -1 if they are snoozed forever: the formatting methods know what to do with that.

Returns:

  • (Integer)


109
110
111
112
113
# File 'lib/wavefront-cli/alert.rb', line 109

def state_time(item)
  return item[:event][:startTime] if item.key?(:event)
  return item[:snoozed] if item.key?(:snoozed)
  nil
end