Class: WavefrontCli::Base
- Inherits:
-
Object
- Object
- WavefrontCli::Base
- Includes:
- Wavefront::Validators, Constants
- Defined in:
- lib/wavefront-cli/base.rb
Overview
Parent of all the CLI classes. This class uses metaprogramming techniques to try to make adding new CLI commands and sub-commands as simple as possible.
To define a subcommand ‘cmd’, you only need add it to the docopt description in the relevant section, and create a method ‘do_cmd’. The WavefrontCli::Base::dispatch() method will find it, and call it. If your subcommand has multiple words, like ‘delete tag’, your do method would be called do_delete_tag. The do_ methods are able to access the Wavefront SDK object as wf, and all docopt options as options.
Direct Known Subclasses
Alert, ApiToken, CloudIntegration, Config, Dashboard, DerivedMetric, Event, ExternalLink, Integration, MaintenanceWindow, Message, Metric, Notificant, Proxy, Query, SavedSearch, ServiceAccount, Settings, Source, User, UserGroup, Webhook, Write
Constant Summary
Constants included from Constants
Constants::ALL_PAGE_SIZE, Constants::DEFAULT_CONFIG, Constants::DEFAULT_OPTS, Constants::EVENT_STATE_DIR, Constants::HUMAN_TIME_FORMAT, Constants::HUMAN_TIME_FORMAT_MS, Constants::SEARCH_SPLIT
Instance Attribute Summary collapse
-
#klass ⇒ Object
Returns the value of attribute klass.
-
#klass_word ⇒ Object
Returns the value of attribute klass_word.
-
#options ⇒ Object
Returns the value of attribute options.
-
#wf ⇒ Object
Returns the value of attribute wf.
Instance Method Summary collapse
-
#_sdk_class ⇒ Object
Normally we map the class name to a similar one in the SDK.
-
#cannot_noop! ⇒ Object
Operations which do require multiple operations cannot be perormed as a no-op.
- #check_response_blocks(data) ⇒ Object
- #check_status(status) ⇒ Object
- #cli_output_class(output_format) ⇒ Object
-
#conds_to_query(conds) ⇒ Array[Hash]
Turn a list of search conditions into an API query.
-
#dispatch ⇒ nil
Works out the user’s command by matching any options docopt has set to ‘true’ with any ‘do_’ method in the class.
-
#display(data, method) ⇒ Object
Display a Ruby object as JSON, YAML, or human-readable.
-
#display_api_error(status) ⇒ Object
Classes can provide methods which give the user information on a given error code.
- #display_no_api_response(data, method) ⇒ Object
- #do_delete ⇒ Object
- #do_describe ⇒ Object
- #do_dump ⇒ Object
- #do_import ⇒ Object
-
#do_list ⇒ Object
Below here are common methods.
- #do_search(cond = ) ⇒ Object
- #do_set ⇒ Object
- #do_undelete ⇒ Object
- #dump_json ⇒ Object
- #dump_yaml ⇒ Object
-
#extract_values(obj, key, aggr = []) ⇒ Array
A recursive function which fetches list of values from a nested hash.
- #failed_validation_message(input) ⇒ Object
-
#format_var ⇒ Symbol
To allow a user to default to different output formats for different object types, we are able to define a format for each class.
-
#handle_error(method, code) ⇒ Object
This gives us a chance to catch different errors in WavefrontDisplay classes.
- #handle_response(resp, format, method) ⇒ Object
- #hcl_fields ⇒ Object
- #import_message(obj, resp) ⇒ Object
- #import_object(raw) ⇒ Object
-
#import_to_create(raw) ⇒ Object
Most things will re-import with the POST method if you remove the ID.
- #import_update(raw) ⇒ Object
-
#initialize(options) ⇒ Base
constructor
A new instance of Base.
-
#item_dump_call ⇒ Object
Broken out into its own method because ‘users’ does not use pagination.
- #load_display_class ⇒ Object
-
#load_file(path) ⇒ Hash
Give it a path to a file (as a string) and it will return the contents of that file as a Ruby object.
-
#load_from_stdin ⇒ Object
Read STDIN and return a Ruby object, assuming that STDIN is valid JSON or YAML.
- #load_json(file) ⇒ Object
- #load_yaml(file) ⇒ Object
-
#matching_method(cond) ⇒ Hash
rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/MethodLength.
-
#method_word_list ⇒ Object
Take a list of do_ methods, remove the ‘do_’ from their name, and break them into arrays of ‘_’ separated words.
-
#mk_creds ⇒ Hash
Make a wavefront-sdk credentials object from standard options.
-
#mk_opts ⇒ Hash
Make a common wavefront-sdk options object from standard CLI options.
- #name_of_do_method(word_list) ⇒ Object
-
#no_api_response ⇒ Array[String]
Some subcommands don’t make an API call, so they don’t return a Wavefront::Response object.
-
#ok_exit(message) ⇒ Object
Print a message and exit 0.
-
#one_or_all ⇒ Object
Return a detailed description of one item, if an ID has been given, or all items if it has not.
- #options_and_exit ⇒ Object
- #parseable_output(output_format, resp) ⇒ Object
-
#range_hash ⇒ Object
If the user has specified –all, override any limit and offset values.
- #run ⇒ Object
-
#search_key ⇒ Object
The search URI pattern doesn’t always match the command name, or class name.
-
#smart_delete(object_type = klass_word) ⇒ Object
Some objects support soft deleting.
- #smart_delete_message(object_type) ⇒ Object
- #status_error_handler(data, method) ⇒ Object
- #unsupported_format_message(output_format) ⇒ Object
- #validate_id ⇒ Object
- #validate_input ⇒ Object
-
#validate_opts ⇒ Object
There are things we need to have.
- #validate_tags(key = :'<tag>') ⇒ Object
- #validator_exception ⇒ Object
-
#validator_method ⇒ Object
We normally validate with a predictable method name.
- #warning_message(status) ⇒ Object
Constructor Details
#initialize(options) ⇒ Base
Returns a new instance of Base.
30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/wavefront-cli/base.rb', line 30 def initialize() = sdk_class = _sdk_class @klass_word = sdk_class.split('::').last.downcase validate_input if [:help] require File.join('wavefront-sdk', @klass_word) @klass = Object.const_get(sdk_class) send(:post_initialize, ) if respond_to?(:post_initialize) end |
Instance Attribute Details
#klass ⇒ Object
Returns the value of attribute klass.
25 26 27 |
# File 'lib/wavefront-cli/base.rb', line 25 def klass @klass end |
#klass_word ⇒ Object
Returns the value of attribute klass_word.
25 26 27 |
# File 'lib/wavefront-cli/base.rb', line 25 def klass_word @klass_word end |
#options ⇒ Object
Returns the value of attribute options.
25 26 27 |
# File 'lib/wavefront-cli/base.rb', line 25 def end |
#wf ⇒ Object
Returns the value of attribute wf.
25 26 27 |
# File 'lib/wavefront-cli/base.rb', line 25 def wf @wf end |
Instance Method Details
#_sdk_class ⇒ Object
Normally we map the class name to a similar one in the SDK. Overriding his method lets you map to something else.
47 48 49 |
# File 'lib/wavefront-cli/base.rb', line 47 def _sdk_class self.class.name.sub(/Cli/, '') end |
#cannot_noop! ⇒ Object
Operations which do require multiple operations cannot be perormed as a no-op. Drop in a call to this method for those things. The exception is caught in controller.rb
617 618 619 |
# File 'lib/wavefront-cli/base.rb', line 617 def cannot_noop! raise WavefrontCli::Exception::UnsupportedNoop if [:noop] end |
#check_response_blocks(data) ⇒ Object
238 239 240 241 242 |
# File 'lib/wavefront-cli/base.rb', line 238 def check_response_blocks(data) i[status response].each do |b| abort "no #{b} block in API response" unless data.respond_to?(b) end end |
#check_status(status) ⇒ Object
274 275 276 |
# File 'lib/wavefront-cli/base.rb', line 274 def check_status(status) status.respond_to?(:result) && status.result == 'OK' end |
#cli_output_class(output_format) ⇒ Object
304 305 306 307 308 |
# File 'lib/wavefront-cli/base.rb', line 304 def cli_output_class(output_format) require_relative File.join('output', output_format.to_s) Object.const_get(format('WavefrontOutput::%<class>s', class: output_format.to_s.capitalize)) end |
#conds_to_query(conds) ⇒ Array[Hash]
Turn a list of search conditions into an API query
552 553 554 555 556 557 |
# File 'lib/wavefront-cli/base.rb', line 552 def conds_to_query(conds) conds.map do |cond| key, value = cond.split(SEARCH_SPLIT, 2) { key: key, value: value }.merge(matching_method(cond)) end end |
#dispatch ⇒ nil
Works out the user’s command by matching any options docopt has set to ‘true’ with any ‘do_’ method in the class. Then calls that method, and displays whatever it returns.
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/wavefront-cli/base.rb', line 172 def dispatch # Look through each deconstructed method name and see if the # user supplied an option for each component. Call the first # one that matches. The order will ensure we match # "do_delete_tags" before we match "do_delete". # method_word_list.reverse_each do |w_list| if w_list.reject { |w| [w.to_sym] }.empty? method = name_of_do_method(w_list) return display(public_send(method), method) end end if respond_to?(:do_default) return display(public_send(:do_default), :do_default) end raise WavefrontCli::Exception::UnhandledCommand end |
#display(data, method) ⇒ Object
Display a Ruby object as JSON, YAML, or human-readable. We provide a default method to format human-readable output, but you can override it by creating your own humanize_command_output method control how its output is handled by setting the response instance variable.
218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/wavefront-cli/base.rb', line 218 def display(data, method) if no_api_response.include?(method) return display_no_api_response(data, method) end exit if [:noop] check_response_blocks(data) (data.status) status_error_handler(data, method) handle_response(data.response, format_var, method) end |
#display_api_error(status) ⇒ Object
Classes can provide methods which give the user information on a given error code. They are named #handle_errcode_xxx, and return a string.
250 251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/wavefront-cli/base.rb', line 250 def display_api_error(status) method = format('handle_errcode_%<code>s', code: status.code).to_sym msg = if respond_to?(method) send(method, status) else status. || 'No further information' end abort format('ERROR: API code %<code>s. %<message>s.', code: status.code, message: msg.chomp('.')).fold(TW, 7) end |
#display_no_api_response(data, method) ⇒ Object
270 271 272 |
# File 'lib/wavefront-cli/base.rb', line 270 def display_no_api_response(data, method) handle_response(data, format_var, method) end |
#do_delete ⇒ Object
478 479 480 |
# File 'lib/wavefront-cli/base.rb', line 478 def do_delete wf.delete([:'<id>']) end |
#do_describe ⇒ Object
409 410 411 |
# File 'lib/wavefront-cli/base.rb', line 409 def do_describe wf.describe([:'<id>']) end |
#do_dump ⇒ Object
413 414 415 416 417 418 419 420 421 422 423 424 |
# File 'lib/wavefront-cli/base.rb', line 413 def do_dump cannot_noop! if [:format] == 'yaml' ok_exit dump_yaml elsif [:format] == 'json' ok_exit dump_json else abort format("Dump format must be 'json' or 'yaml'. " \ "(Tried '%<format>s')", ) end end |
#do_import ⇒ Object
441 442 443 444 445 446 447 448 449 450 451 452 453 454 |
# File 'lib/wavefront-cli/base.rb', line 441 def do_import raw = load_file([:'<file>']) errs = 0 [raw].flatten.each do |obj| resp = import_object(obj) next if [:noop] errs += 1 unless resp.ok? puts (obj, resp) end exit errs end |
#do_list ⇒ Object
Below here are common methods. Most are used by most classes, but if they don’t match a command described in the docopt text, the dispatcher will never call them. So, there’s no harm inheriting unneeded things. Some classes override them.
399 400 401 402 403 404 405 406 407 |
# File 'lib/wavefront-cli/base.rb', line 399 def do_list list = if [:all] wf.list(ALL_PAGE_SIZE, :all) else wf.list([:offset] || 0, [:limit] || 100) end respond_to?(:list_filter) ? list_filter(list) : list end |
#do_search(cond = ) ⇒ Object
510 511 512 513 514 515 |
# File 'lib/wavefront-cli/base.rb', line 510 def do_search(cond = [:'<condition>']) require 'wavefront-sdk/search' wfs = Wavefront::Search.new(mk_creds, mk_opts) query = conds_to_query(cond) wfs.search(search_key, query, range_hash) end |
#do_set ⇒ Object
504 505 506 507 508 |
# File 'lib/wavefront-cli/base.rb', line 504 def do_set cannot_noop! k, v = [:'<key=value>'].split('=', 2) wf.update([:'<id>'], k => v) end |
#do_undelete ⇒ Object
500 501 502 |
# File 'lib/wavefront-cli/base.rb', line 500 def do_undelete wf.undelete([:'<id>']) end |
#dump_json ⇒ Object
430 431 432 |
# File 'lib/wavefront-cli/base.rb', line 430 def dump_json item_dump_call.to_json end |
#dump_yaml ⇒ Object
426 427 428 |
# File 'lib/wavefront-cli/base.rb', line 426 def dump_yaml JSON.parse(item_dump_call.to_json).to_yaml end |
#extract_values(obj, key, aggr = []) ⇒ Array
A recursive function which fetches list of values from a nested hash. Used by WavefrontCli::Dashboard#do_queries rubocop:disable Metrics/MethodLength
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 |
# File 'lib/wavefront-cli/base.rb', line 629 def extract_values(obj, key, aggr = []) if obj.is_a?(Hash) obj.each_pair do |k, v| if k == key && !v.to_s.empty? aggr.<< v else extract_values(v, key, aggr) end end elsif obj.is_a?(Array) obj.each { |e| extract_values(e, key, aggr) } end aggr end |
#failed_validation_message(input) ⇒ Object
116 117 118 119 120 |
# File 'lib/wavefront-cli/base.rb', line 116 def (input) format("'%<value>s' is not a valid %<thing>s ID.", value: input, thing: klass_word) end |
#format_var ⇒ Symbol
To allow a user to default to different output formats for different object types, we are able to define a format for each class. instance, alertformat or proxyformat. This method returns such a symbol appropriate for the inheriting class.
158 159 160 161 162 |
# File 'lib/wavefront-cli/base.rb', line 158 def format_var [:format].to_sym rescue NoMethodError :human end |
#handle_error(method, code) ⇒ Object
This gives us a chance to catch different errors in WavefrontDisplay classes. If nothing catches, them abort.
281 282 283 284 |
# File 'lib/wavefront-cli/base.rb', line 281 def handle_error(method, code) k = load_display_class k.new({}, ).run_error([method, code].join('_')) end |
#handle_response(resp, format, method) ⇒ Object
286 287 288 289 290 291 292 293 |
# File 'lib/wavefront-cli/base.rb', line 286 def handle_response(resp, format, method) if format == :human k = load_display_class k.new(resp, ).run(method) else parseable_output(format, resp) end end |
#hcl_fields ⇒ Object
315 316 317 |
# File 'lib/wavefront-cli/base.rb', line 315 def hcl_fields [] end |
#import_message(obj, resp) ⇒ Object
456 457 458 459 460 461 |
# File 'lib/wavefront-cli/base.rb', line 456 def (obj, resp) format('%-15<id>s %-10<status>s %<message>s', id: obj[:id] || obj[:url], status: resp.ok? ? 'IMPORTED' : 'FAILED', message: resp.status.) end |
#import_object(raw) ⇒ Object
463 464 465 466 467 468 469 470 471 472 |
# File 'lib/wavefront-cli/base.rb', line 463 def import_object(raw) raw = preprocess_rawfile(raw) if respond_to?(:preprocess_rawfile) prepped = import_to_create(raw) if [:update] import_update(raw) else wf.create(prepped) end end |
#import_to_create(raw) ⇒ Object
Most things will re-import with the POST method if you remove the ID.
588 589 590 591 592 593 594 595 |
# File 'lib/wavefront-cli/base.rb', line 588 def import_to_create(raw) raw.each_with_object({}) do |(k, v), a| a[k.to_sym] = v unless k == :id end rescue StandardError => e puts e if [:debug] raise WavefrontCli::Exception::UnparseableInput end |
#import_update(raw) ⇒ Object
474 475 476 |
# File 'lib/wavefront-cli/base.rb', line 474 def import_update(raw) wf.update(raw[:id], raw, false) end |
#item_dump_call ⇒ Object
Broken out into its own method because ‘users’ does not use pagination
437 438 439 |
# File 'lib/wavefront-cli/base.rb', line 437 def item_dump_call wf.list(ALL_PAGE_SIZE, :all).response.items end |
#load_display_class ⇒ Object
319 320 321 322 |
# File 'lib/wavefront-cli/base.rb', line 319 def load_display_class require_relative File.join('display', klass_word) Object.const_get(klass.name.sub('Wavefront', 'WavefrontDisplay')) end |
#load_file(path) ⇒ Hash
Give it a path to a file (as a string) and it will return the contents of that file as a Ruby object. Automatically detects JSON and YAML. Raises an exception if it doesn’t look like either. If path is ‘-’ then it will read STDIN.
351 352 353 354 355 356 357 358 359 360 361 362 363 |
# File 'lib/wavefront-cli/base.rb', line 351 def load_file(path) return load_from_stdin if path == '-' file = Pathname.new(path) extname = file.extname.downcase raise WavefrontCli::Exception::FileNotFound unless file.exist? return load_json(file) if extname == '.json' return load_yaml(file) if %w[.yaml .yml].include?(extname) raise WavefrontCli::Exception::UnsupportedFileFormat end |
#load_from_stdin ⇒ Object
Read STDIN and return a Ruby object, assuming that STDIN is valid JSON or YAML. This is a dumb method, it does no buffering, so STDIN must be a single block of data. This appears to be a valid assumption for use-cases of this CLI.
382 383 384 385 386 387 388 389 390 391 392 |
# File 'lib/wavefront-cli/base.rb', line 382 def load_from_stdin raw = STDIN.read if raw.start_with?('---') read_yaml(raw) else read_json(raw) end rescue RuntimeError raise Wavefront::Exception::UnparseableInput end |
#load_json(file) ⇒ Object
365 366 367 |
# File 'lib/wavefront-cli/base.rb', line 365 def load_json(file) read_json(IO.read(file)) end |
#load_yaml(file) ⇒ Object
369 370 371 |
# File 'lib/wavefront-cli/base.rb', line 369 def load_yaml(file) read_yaml(IO.read(file)) end |
#matching_method(cond) ⇒ Hash
rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/MethodLength
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 |
# File 'lib/wavefront-cli/base.rb', line 564 def matching_method(cond) case cond when /^\w+~/ { matchingMethod: 'CONTAINS', negated: false } when /^\w+!~/ { matchingMethod: 'CONTAINS', negated: true } when /^\w+=/ { matchingMethod: 'EXACT', negated: false } when /^\w+!=/ { matchingMethod: 'EXACT', negated: true } when /^\w+\^/ { matchingMethod: 'STARTSWITH', negated: false } when /^\w+!\^/ { matchingMethod: 'STARTSWITH', negated: true } else raise(WavefrontCli::Exception::UnparseableSearchPattern, cond) end end |
#method_word_list ⇒ Object
Take a list of do_ methods, remove the ‘do_’ from their name, and break them into arrays of ‘_’ separated words. The array is sorted by length, longest first.
200 201 202 203 |
# File 'lib/wavefront-cli/base.rb', line 200 def method_word_list do_methods = methods.select { |m| m.to_s.start_with?('do_') } do_methods.map { |m| m.to_s.split('_')[1..-1] }.sort_by(&:length) end |
#mk_creds ⇒ Hash
Make a wavefront-sdk credentials object from standard options.
127 128 129 130 131 |
# File 'lib/wavefront-cli/base.rb', line 127 def mk_creds { token: [:token], endpoint: [:endpoint], agent: "wavefront-cli-#{WF_CLI_VERSION}" } end |
#mk_opts ⇒ Hash
Make a common wavefront-sdk options object from standard CLI options. We force verbosity on for a noop, otherwise we get no output.
139 140 141 142 143 144 145 146 147 |
# File 'lib/wavefront-cli/base.rb', line 139 def mk_opts ret = { debug: [:debug], noop: [:noop] } ret[:verbose] = [:noop] ? true : [:verbose] ret.merge!() if respond_to?(:extra_options) ret end |
#name_of_do_method(word_list) ⇒ Object
192 193 194 |
# File 'lib/wavefront-cli/base.rb', line 192 def name_of_do_method(word_list) (%w[do] + word_list).join('_') end |
#no_api_response ⇒ Array[String]
Some subcommands don’t make an API call, so they don’t return a Wavefront::Response object. You can override this method with something which returns an array of methods like that. They will bypass the usual response checking.
response
59 60 61 |
# File 'lib/wavefront-cli/base.rb', line 59 def no_api_response [] end |
#ok_exit(message) ⇒ Object
Print a message and exit 0
69 70 71 72 |
# File 'lib/wavefront-cli/base.rb', line 69 def ok_exit() puts exit 0 end |
#one_or_all ⇒ Object
Return a detailed description of one item, if an ID has been given, or all items if it has not.
600 601 602 603 604 605 606 607 608 609 610 611 |
# File 'lib/wavefront-cli/base.rb', line 600 def one_or_all if [:'<id>'] resp = wf.describe([:'<id>']) data = [resp.response] else [:all] = true resp = do_list data = resp.response.items end [resp, data] end |
#options_and_exit ⇒ Object
63 64 65 |
# File 'lib/wavefront-cli/base.rb', line 63 def ok_exit() end |
#parseable_output(output_format, resp) ⇒ Object
295 296 297 298 299 300 301 302 |
# File 'lib/wavefront-cli/base.rb', line 295 def parseable_output(output_format, resp) [:class] = klass_word [:hcl_fields] = hcl_fields cli_output_class(output_format).new(resp, ).run rescue LoadError raise(WavefrontCli::Exception::UnsupportedOutput, (output_format)) end |
#range_hash ⇒ Object
If the user has specified –all, override any limit and offset values
rubocop:disable Metrics/MethodLength
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 |
# File 'lib/wavefront-cli/base.rb', line 521 def range_hash offset_key = :offset if [:all] limit = :all offset = ALL_PAGE_SIZE elsif [:cursor] offset_key = :cursor limit = [:limit] offset = [:cursor] else limit = [:limit] offset = [:offset] end { limit: limit, offset_key => offset } end |
#run ⇒ Object
74 75 76 77 |
# File 'lib/wavefront-cli/base.rb', line 74 def run @wf = klass.new(mk_creds, mk_opts) dispatch end |
#search_key ⇒ Object
The search URI pattern doesn’t always match the command name, or class name. Override this method if this is the case.
543 544 545 |
# File 'lib/wavefront-cli/base.rb', line 543 def search_key klass_word end |
#smart_delete(object_type = klass_word) ⇒ Object
Some objects support soft deleting. To handle that, call this method from do_delete
485 486 487 488 489 |
# File 'lib/wavefront-cli/base.rb', line 485 def smart_delete(object_type = klass_word) cannot_noop! puts (object_type) wf.delete([:'<id>']) end |
#smart_delete_message(object_type) ⇒ Object
491 492 493 494 495 496 497 498 |
# File 'lib/wavefront-cli/base.rb', line 491 def (object_type) desc = wf.describe([:'<id>']) word = desc.ok? ? 'Soft' : 'Permanently' format("%<soft_or_hard>s deleting %<object>s '%<id>s'", soft_or_hard: word, object: object_type, id: [:'<id>']) end |
#status_error_handler(data, method) ⇒ Object
231 232 233 234 235 236 |
# File 'lib/wavefront-cli/base.rb', line 231 def status_error_handler(data, method) return if check_status(data.status) handle_error(method, data.status.code) if format_var == :human display_api_error(data.status) end |
#unsupported_format_message(output_format) ⇒ Object
310 311 312 313 |
# File 'lib/wavefront-cli/base.rb', line 310 def (output_format) format("The '%<command>s' command does not support '%<format>s' output.", command: [:class], format: output_format) end |
#validate_id ⇒ Object
110 111 112 113 114 |
# File 'lib/wavefront-cli/base.rb', line 110 def validate_id send(validator_method, [:'<id>']) rescue validator_exception abort ([:'<id>']) end |
#validate_input ⇒ Object
93 94 95 96 97 |
# File 'lib/wavefront-cli/base.rb', line 93 def validate_input validate_id if [:'<id>'] if [:'<tag>'] extra_validation if respond_to?(:extra_validation) end |
#validate_opts ⇒ Object
There are things we need to have. If we don’t have them, stop the user right now. Also, if we’re in debug mode, print out a hash of options, which can be very useful when doing actual debugging. Some classes may have to override this method. The writer, for instance, uses a proxy and has no token.
330 331 332 333 334 335 336 337 338 |
# File 'lib/wavefront-cli/base.rb', line 330 def validate_opts unless [:token] raise(WavefrontCli::Exception::CredentialError, 'Missing API token.') end return true if [:endpoint] raise(WavefrontCli::Exception::CredentialError, 'Missing API endpoint.') end |
#validate_tags(key = :'<tag>') ⇒ Object
99 100 101 102 103 104 105 106 107 108 |
# File 'lib/wavefront-cli/base.rb', line 99 def (key = :'<tag>') Array([key]).each do |t| begin send(:wf_tag?, t) rescue Wavefront::Exception::InvalidTag raise(WavefrontCli::Exception::InvalidInput, "'#{t}' is not a valid tag.") end end end |
#validator_exception ⇒ Object
87 88 89 90 91 |
# File 'lib/wavefront-cli/base.rb', line 87 def validator_exception Object.const_get( "Wavefront::Exception::Invalid#{klass_word.capitalize}Id" ) end |
#validator_method ⇒ Object
We normally validate with a predictable method name. Alert IDs are validated with #wf_alert_id? etc. If you need to change that, override this method.
83 84 85 |
# File 'lib/wavefront-cli/base.rb', line 83 def validator_method "wf_#{klass_word}_id?".to_sym end |
#warning_message(status) ⇒ Object
264 265 266 267 268 |
# File 'lib/wavefront-cli/base.rb', line 264 def (status) return unless status.status.between?(201, 299) puts format("API WARNING: '%<message>s'.", message: status.) end |