Class: Wavefront::Cli::Dashboards

Inherits:
Wavefront::Cli show all
Includes:
Wavefront::Constants, Mixins
Defined in:
lib/wavefront/cli/dashboards.rb

Constant Summary

Constants included from Wavefront::Constants

Wavefront::Constants::ALERT_FORMATS, Wavefront::Constants::DASH_FORMATS, Wavefront::Constants::DEFAULT_ALERT_FORMAT, Wavefront::Constants::DEFAULT_DASH_FORMAT, Wavefront::Constants::DEFAULT_FORMAT, Wavefront::Constants::DEFAULT_HOST, Wavefront::Constants::DEFAULT_INFILE_FORMAT, Wavefront::Constants::DEFAULT_OBSOLETE_METRICS, Wavefront::Constants::DEFAULT_OPTS, Wavefront::Constants::DEFAULT_PERIOD_SECONDS, Wavefront::Constants::DEFAULT_PREFIX_LENGTH, Wavefront::Constants::DEFAULT_PROXY, Wavefront::Constants::DEFAULT_PROXY_PORT, Wavefront::Constants::DEFAULT_SOURCE_FORMAT, Wavefront::Constants::DEFAULT_STRICT, Wavefront::Constants::EVENT_LEVELS, Wavefront::Constants::EVENT_STATE_DIR, Wavefront::Constants::FORMATS, Wavefront::Constants::GRANULARITIES, Wavefront::Constants::SOURCE_FORMATS

Instance Attribute Summary collapse

Attributes inherited from Wavefront::Cli

#arguments, #noop, #options

Instance Method Summary collapse

Methods included from Mixins

#call_delete, #call_get, #call_post, #hash_to_qs, #interpolate_schema, #load_file, #parse_time, #time_to_ms, #uri_concat

Methods inherited from Wavefront::Cli

#initialize, #validate_opts

Constructor Details

This class inherits a constructor from Wavefront::Cli

Instance Attribute Details

#wfdObject

Returns the value of attribute wfd.



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

def wfd
  @wfd
end

Instance Method Details

#clone_dashObject



36
37
38
39
40
41
42
# File 'lib/wavefront/cli/dashboards.rb', line 36

def clone_dash
  wfd.clone(options[:'<source_id>'], options[:'<new_id>'],
            options[:'<new_name>'], options[:version])
  puts 'Dashboard cloned' unless options[:noop]
rescue RestClient::BadRequest
  raise '400 error: either target exists or source does not'
end

#create_dashObject



70
71
72
73
74
75
# File 'lib/wavefront/cli/dashboards.rb', line 70

def create_dash
  wfd.create(options[:'<dashboard_id>'], options[:'<name>'])
  puts 'dashboard created' unless options[:noop]
rescue RestClient::BadRequest
  raise '400 error: dashboard probably exists'
end

#delete_dashObject



63
64
65
66
67
68
# File 'lib/wavefront/cli/dashboards.rb', line 63

def delete_dash
  wfd.delete(options[:'<dashboard_id>'])
  puts 'dashboard deleted' unless options[:noop]
rescue RestClient::ResourceNotFound
  raise 'Dashboard does not exist'
end

#display_resp(resp, human_method = nil) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/wavefront/cli/dashboards.rb', line 89

def display_resp(resp, human_method = nil)
  return if options[:noop]

  case options[:dashformat].to_sym
  when :json
    if resp.is_a?(String)
      puts resp
    else
      puts resp.to_json
    end
  when :yaml
    puts resp.to_yaml
  when :human
    unless human_method
      raise 'human output format is not supported by this subcommand'
    end

    send(human_method, JSON.parse(resp))
  else
    raise 'unsupported output format'
  end
end

#export_dashObject



77
78
79
80
81
# File 'lib/wavefront/cli/dashboards.rb', line 77

def export_dash
  resp = wfd.export(options[:'<dashboard_id>'], options[:version] || nil)
  options[:dashformat] = :json if options[:dashformat] == :human
  display_resp(resp)
end

#history_dashObject



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/wavefront/cli/dashboards.rb', line 44

def history_dash
  begin
    resp = wfd.history(options[:'<dashboard_id>'],
                       options[:start] || 100,
                       options[:limit] || nil)
  rescue RestClient::ResourceNotFound
    raise 'Dashboard does not exist'
  end

  display_resp(resp, :human_history)
end

#human_history(resp) ⇒ Object



112
113
114
115
116
117
118
119
120
121
# File 'lib/wavefront/cli/dashboards.rb', line 112

def human_history(resp)
  resp.each do |rev|
    puts format('%-4s%s (%s)', rev['version'],
                Time.at(rev['update_time'].to_i / 1000),
                rev['update_user'])

    next unless rev['change_description']
    rev['change_description'].each { |desc| puts '      ' + desc }
  end
end

#human_list(resp) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/wavefront/cli/dashboards.rb', line 123

def human_list(resp)
  #
  # Simply list the dashboards we have. If the user wants more
  #
  max_id_width = resp.map { |s| s['url'].size }.max

  puts format("%-#{max_id_width + 1}s%s", 'ID', 'NAME')

  resp.each do |dash|
    next if !options[:all] && dash['isTrash']
    line = format("%-#{max_id_width + 1}s%s", dash['url'], dash['name'])
    line.<< ' (in trash)' if dash['isTrash']
    puts line
  end
end

#import_dashObject



29
30
31
32
33
34
# File 'lib/wavefront/cli/dashboards.rb', line 29

def import_dash
  wfd.import(load_file(options[:'<file>']).to_json, options[:force])
  puts 'Dashboard imported' unless options[:noop]
rescue RestClient::BadRequest
  raise '400 error: dashboard probably exists, and force not used'
end

#list_dashboardsObject



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

def list_dashboards
  resp = wfd.list({ private: options[:privatetag],
                    shared: options[:sharedtag] })
  display_resp(resp, :human_list)
end

#runObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/wavefront/cli/dashboards.rb', line 13

def run
  @wfd = Wavefront::Dashboards.new(
    options[:token], options[:endpoint], options[:debug],
    noop: options[:noop], verbose: options[:verbose]
  )

  list_dashboards if options[:list]
  export_dash if options[:export]
  create_dash if options[:create]
  delete_dash if options[:delete]
  undelete_dash if options[:undelete]
  history_dash if options[:history]
  clone_dash if options[:clone]
  import_dash if options[:import]
end

#undelete_dashObject



56
57
58
59
60
61
# File 'lib/wavefront/cli/dashboards.rb', line 56

def undelete_dash
  wfd.undelete(options[:'<dashboard_id>'])
  puts 'dashboard undeleted' unless options[:noop]
rescue RestClient::ResourceNotFound
  raise 'Dashboard does not exist'
end