Class: Wavefront::Cli::Sources

Inherits:
Wavefront::Cli show all
Defined in:
lib/wavefront/cli/sources.rb

Overview

Turn CLI input, from the ‘sources’ command, into metadata API calls

Instance Attribute Summary collapse

Attributes inherited from Wavefront::Cli

#arguments, #noop, #options

Instance Method Summary collapse

Methods inherited from Wavefront::Cli

#initialize, #validate_opts

Constructor Details

This class inherits a constructor from Wavefront::Cli

Instance Attribute Details

#out_formatObject

Returns the value of attribute out_format.



10
11
12
# File 'lib/wavefront/cli/sources.rb', line 10

def out_format
  @out_format
end

#show_hiddenObject

Returns the value of attribute show_hidden.



10
11
12
# File 'lib/wavefront/cli/sources.rb', line 10

def show_hidden
  @show_hidden
end

#show_tagsObject

Returns the value of attribute show_tags.



10
11
12
# File 'lib/wavefront/cli/sources.rb', line 10

def show_tags
  @show_tags
end

#verboseObject

Returns the value of attribute verbose.



10
11
12
# File 'lib/wavefront/cli/sources.rb', line 10

def verbose
  @verbose
end

#wfObject

Returns the value of attribute wf.



10
11
12
# File 'lib/wavefront/cli/sources.rb', line 10

def wf
  @wf
end

Instance Method Details

#add_tag_handler(hosts, tags) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/wavefront/cli/sources.rb', line 95

def add_tag_handler(hosts, tags)
  hosts ||= Socket.gethostname
  hosts = [hosts] if hosts.is_a?(String)

  hosts.each do |h|
    tags.each do |t|
      puts "Tagging '#{h}' with '#{t}'" if verbose
      begin
        wf.set_tag(h, t)
      rescue Wavefront::Exception::InvalidString
        puts 'ERROR: tag contains invalid characters.'
      end
    end
  end
end

#delete_tag_handler(hosts, tags) ⇒ Object



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

def delete_tag_handler(hosts, tags)
  hosts ||= Socket.gethostname
  hosts = [hosts] if hosts.is_a?(String)

  hosts.each do |h|
    tags.each do |t|
      puts "Removing tag '#{t}' from '#{h}'" if verbose
      wf.delete_tag(h, t)
    end
  end
end

#describe_handler(hosts, desc) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/wavefront/cli/sources.rb', line 66

def describe_handler(hosts, desc)
  hosts = [Socket.gethostname] if hosts.empty?
  hosts = [hosts] if hosts.is_a?(String)

  hosts.each do |h|
    if desc.empty?
      puts "clearing description of '#{h}'"
    else
      puts "setting '#{h}' description to '#{desc}'"
    end

    begin
      wf.set_description(h, desc)
    rescue Wavefront::Exception::InvalidString
      puts 'ERROR: description contains invalid characters.'
    end
  end
end

#display_data(result, method) ⇒ Object



136
137
138
139
140
141
142
143
144
145
# File 'lib/wavefront/cli/sources.rb', line 136

def display_data(result, method)
  return if noop
  if out_format == 'human'
    puts public_send('humanize_' + method, result)
  elsif out_format == 'json'
    puts result.to_json
  else
    pp result
  end
end

#humanize_list_source(result) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/wavefront/cli/sources.rb', line 147

def humanize_list_source(result)
  hdr = format('%-25s %-30s %s', 'HOSTNAME', 'DESCRIPTION', 'TAGS')

  ret = result['sources'].each_with_object([hdr]) do |s, aggr|
    if s.include?('userTags') && s['userTags'].include?('hidden') &&
       !show_hidden
      next
    end

    if options[:tagged]
      skip = false
      options[:tagged].each do |t|
        unless s.key?('userTags') && s['userTags'].include?(t)
          skip = true
          break
        end
      end
      next if skip
    end

    if s['description']
      desc = s['description']
      desc = desc[0..27] + '...' if desc.length > 30
    else
      desc = ''
    end

    tags = s['userTags'] ? s['userTags'].join(', ') : ''

    aggr.<< format('%-25s %-30s %s', s['hostname'], desc, tags)
  end

  if show_tags
    ret.<< ['', format('%-25s%s', 'TAG', 'COUNT')]

    result['counts'].each do |tag, count|
      ret.<< format('%-25s%s', tag, count)
    end
  end

  ret.join("\n")
end

#humanize_show_source(data) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/wavefront/cli/sources.rb', line 190

def humanize_show_source(data)
  ret = [data['hostname']]

  if data['description']
    ret.<< format('  %-15s%s', 'description', data['description'])
  end

  if data['userTags']
    ret.<< format('  %-15s%s', 'tags', data['userTags'].shift)
    data['userTags'].each { |t| ret.<< format('  %-15s%s', '', t) }
  end

  ret.join("\n")
end

#list_source_handler(pattern, start = false, limit = false) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/wavefront/cli/sources.rb', line 50

def list_source_handler(pattern, start = false, limit = false)
  limit ||= 100

  q = {
    desc:         false,
    limit:        limit.to_i,
    pattern:      pattern
  }

  q[:lastEntityId] = start if start

  res = wf.show_sources(q)
  return if noop
  display_data(res, 'list_source')
end

#runObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/wavefront/cli/sources.rb', line 19

def run
  setup_wf
  @out_format = options[:sourceformat].to_s
  @show_hidden = options[:all]
  @show_tags = options[:tags]
  @verbose = options[:verbose]

  begin
    if options[:list]
      list_source_handler(options[:'<pattern>'], options[:start],
                          options[:limit])
    elsif options[:show]
      show_source_handler(options[:'<host>'])
    elsif options[:tag] && options[:add]
      add_tag_handler(options[:host], options[:'<tag>'])
    elsif options[:tag] && options[:delete]
      delete_tag_handler(options[:host], options[:'<tag>'])
    elsif options[:describe]
      describe_handler(options[:host], options[:'<description>'])
    elsif options[:undescribe]
      describe_handler(options[:'<host>'], '')
    elsif options[:untag]
      untag_handler(options[:'<host>'])
    else
      fail 'undefined sources error'
    end
  rescue Wavefront::Exception::InvalidSource
    abort 'ERROR: invalid source name.'
  end
end

#setup_wfObject



12
13
14
15
16
17
# File 'lib/wavefront/cli/sources.rb', line 12

def setup_wf
  @wf = Wavefront::Metadata.new(options[:token], options[:endpoint],
                                options[:debug],
                                { verbose: options[:verbose],
                                  noop:    options[:noop]})
end

#show_source_handler(sources) ⇒ Object



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

def show_source_handler(sources)
  sources.each do |s|
    begin
      result = wf.show_source(s)
    rescue RestClient::ResourceNotFound
      puts "Source '#{s}' not found."
      next
    end

    display_data(result, 'show_source')
  end
end

#untag_handler(hosts) ⇒ Object



85
86
87
88
89
90
91
92
93
# File 'lib/wavefront/cli/sources.rb', line 85

def untag_handler(hosts)
  hosts ||= Socket.gethostname
  hosts = [hosts] if hosts.is_a?(String)

  hosts.each do |h|
    puts "Removing all tags from '#{h}'" if verbose
    wf.delete_tags(h)
  end
end