Class: Wavefront::Cli::Sources
- Inherits:
-
Wavefront::Cli
- Object
- Wavefront::Cli
- Wavefront::Cli::Sources
- Defined in:
- lib/wavefront/cli/sources.rb
Overview
Turn CLI input, from the ‘sources’ command, into metadata API calls
Instance Attribute Summary collapse
-
#out_format ⇒ Object
Returns the value of attribute out_format.
-
#show_hidden ⇒ Object
Returns the value of attribute show_hidden.
-
#show_tags ⇒ Object
Returns the value of attribute show_tags.
-
#verbose ⇒ Object
Returns the value of attribute verbose.
-
#wf ⇒ Object
Returns the value of attribute wf.
Attributes inherited from Wavefront::Cli
Instance Method Summary collapse
- #add_tag_handler(hosts, tags) ⇒ Object
- #delete_tag_handler(hosts, tags) ⇒ Object
- #describe_handler(hosts, desc) ⇒ Object
- #display_data(result, method) ⇒ Object
- #humanize_list_source(result) ⇒ Object
- #humanize_show_source(data) ⇒ Object
- #list_source_handler(pattern, start = false, limit = false) ⇒ Object
- #run ⇒ Object
- #setup_wf ⇒ Object
- #show_source_handler(sources) ⇒ Object
- #untag_handler(hosts) ⇒ Object
Methods inherited from Wavefront::Cli
Constructor Details
This class inherits a constructor from Wavefront::Cli
Instance Attribute Details
#out_format ⇒ Object
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_hidden ⇒ Object
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_tags ⇒ Object
Returns the value of attribute show_tags.
10 11 12 |
# File 'lib/wavefront/cli/sources.rb', line 10 def @show_tags end |
#verbose ⇒ Object
Returns the value of attribute verbose.
10 11 12 |
# File 'lib/wavefront/cli/sources.rb', line 10 def verbose @verbose end |
#wf ⇒ Object
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, ) hosts ||= Socket.gethostname hosts = [hosts] if hosts.is_a?(String) hosts.each do |h| .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, ) hosts ||= Socket.gethostname hosts = [hosts] if hosts.is_a?(String) hosts.each do |h| .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 [:tagged] skip = false [: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 = s['userTags'] ? s['userTags'].join(', ') : '' aggr.<< format('%-25s %-30s %s', s['hostname'], desc, ) end if 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 |
#run ⇒ Object
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 = [:sourceformat].to_s @show_hidden = [:all] @show_tags = [:tags] @verbose = [:verbose] begin if [:list] list_source_handler([:'<pattern>'], [:start], [:limit]) elsif [:show] show_source_handler([:'<host>']) elsif [:tag] && [:add] add_tag_handler([:host], [:'<tag>']) elsif [:tag] && [:delete] delete_tag_handler([:host], [:'<tag>']) elsif [:describe] describe_handler([:host], [:'<description>']) elsif [:undescribe] describe_handler([:'<host>'], '') elsif [:untag] untag_handler([:'<host>']) else fail 'undefined sources error' end rescue Wavefront::Exception::InvalidSource abort 'ERROR: invalid source name.' end end |
#setup_wf ⇒ Object
12 13 14 15 16 17 |
# File 'lib/wavefront/cli/sources.rb', line 12 def setup_wf @wf = Wavefront::Metadata.new([:token], [:endpoint], [:debug], { verbose: [:verbose], noop: [: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.(h) end end |