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.
-
#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 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
88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/wavefront/cli/sources.rb', line 88 def add_tag_handler(hosts, ) hosts ||= [Socket.gethostname] hosts.each do |h| .each do |t| puts "Tagging '#{h}' with '#{t}'" 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
103 104 105 106 107 108 109 110 111 112 |
# File 'lib/wavefront/cli/sources.rb', line 103 def delete_tag_handler(hosts, ) hosts ||= [Socket.gethostname] hosts.each do |h| .each do |t| puts "Removing tag '#{t}' from '#{h}'" wf.delete_tag(h, t) end end end |
#describe_handler(hosts, desc) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/wavefront/cli/sources.rb', line 61 def describe_handler(hosts, desc) hosts = [Socket.gethostname] if hosts.empty? 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
127 128 129 130 131 132 133 134 135 |
# File 'lib/wavefront/cli/sources.rb', line 127 def display_data(result, method) 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
137 138 139 140 141 142 143 144 145 146 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 |
# File 'lib/wavefront/cli/sources.rb', line 137 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['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
180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/wavefront/cli/sources.rb', line 180 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
47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/wavefront/cli/sources.rb', line 47 def list_source_handler(pattern, start = false, limit = false) limit ||= 100 q = { desc: false, limit: limit, pattern: pattern } q[:lastEntityId] = start if start display_data(wf.show_sources(q), 'list_source') end |
#run ⇒ Object
17 18 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 |
# File 'lib/wavefront/cli/sources.rb', line 17 def run setup_wf @out_format = [:format] @show_hidden = [:all] = [:tags] 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 |
# File 'lib/wavefront/cli/sources.rb', line 12 def setup_wf @wf = Wavefront::Metadata.new([:token], [:endpoint], false, { verbose: [:verbose] }) end |
#show_source_handler(sources) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/wavefront/cli/sources.rb', line 114 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
79 80 81 82 83 84 85 86 |
# File 'lib/wavefront/cli/sources.rb', line 79 def untag_handler(hosts) hosts ||= [Socket.gethostname] hosts.each do |h| puts "Removing all tags from '#{h}'" wf.(h) end end |