Module: Wavefront::Validators
- Included in:
- CoreApi, Write, Writer::Core
- Defined in:
- lib/wavefront-sdk/validators.rb
Overview
A module of mixins to validate input. The Wavefront documentation lays down restrictions on types and sizes of various inputs, which we will check on the user’s behalf. Most of the information used in this file comes from community.wavefront.com/docs/DOC-1031 Some comes from the Swagger API documentation, some has come directly from Wavefront engineers.
rubocop:disable Metrics/ModuleLength
Instance Method Summary collapse
-
#wf_alert_id?(id) ⇒ Boolean
Ensure the given argument is a valid Wavefront alert ID.
-
#wf_alert_severity?(severity) ⇒ Boolean
Ensure the given argument is a valid alert severity.
-
#wf_cloudintegration_id?(id) ⇒ Boolean
Ensure the given argument is a valid Wavefront cloud integration ID.
-
#wf_dashboard_id?(id) ⇒ Boolean
There doesn’t seem to be a public statement on what’s allowed in a dashboard name.
-
#wf_derivedmetric_id?(id) ⇒ Boolean
Ensure the given argument is a valid derived metric ID.
-
#wf_distribution?(dist) ⇒ Boolean
Validate a distribution description.
-
#wf_distribution_count?(count) ⇒ Boolean
Ensure the given argument is a valid distribution count.
-
#wf_distribution_interval?(interval) ⇒ Boolean
Ensure the given argument is a valid distribution interval.
-
#wf_distribution_values?(vals) ⇒ Boolean
Validate an array of distribution values.
-
#wf_epoch?(timestamp) ⇒ Boolean
Ensure the given argument is a valid epoch timestamp.
-
#wf_event_id?(id) ⇒ Boolean
Ensure the given argument is a valid event ID.
-
#wf_granularity?(granularity) ⇒ Boolean
Ensure the given argument is a valid query granularity.
-
#wf_integration_id?(id) ⇒ Boolean
Ensure the given argument is a valid Wavefront integration ID.
-
#wf_link_id?(id) ⇒ Boolean
Ensure the given argument is a valid external Link ID.
-
#wf_link_template?(template) ⇒ Boolean
Ensure the given argument is a valid external link template.
-
#wf_maintenance_window_id?(id) ⇒ Boolean
Ensure the given argument is a valid maintenance window ID.
-
#wf_message_id?(id) ⇒ Boolean
Ensure the given argument is a valid message ID.
-
#wf_metric_name?(metric) ⇒ Boolean
Ensure the given argument is a valid Wavefront metric name, or path.
-
#wf_ms_ts?(timestamp) ⇒ Boolean
Ensure the given argument is a valid millisecond epoch timestamp.
-
#wf_name?(name) ⇒ Boolean
Ensure the given argument is a valid name, for instance for an event.
-
#wf_notificant_id?(id) ⇒ Boolean
Ensure the given argument is a valid Wavefront notificant ID.
-
#wf_point?(point) ⇒ Boolean
Validate a point so it conforms to the standard described in community.wavefront.com/docs/DOC-1031.
-
#wf_point_tag?(key, val) ⇒ Boolean
Validate a single point tag, probably on behalf of #wf_point_tags?.
-
#wf_point_tags?(tags) ⇒ Boolean
Ensure a hash of key:value point tags are value.
-
#wf_proxy_id?(id) ⇒ Boolean
Ensure the given argument is a valid Wavefront proxy ID.
-
#wf_savedsearch_entity?(id) ⇒ Boolean
Ensure the given argument is a valid saved search entity type.
-
#wf_savedsearch_id?(id) ⇒ Boolean
Ensure the given argument is a valid saved search ID.
-
#wf_source_id?(source) ⇒ Boolean
Ensure the given argument is a valid Wavefront source name.
-
#wf_string?(str) ⇒ Boolean
Ensure the given argument is a valid string, for a tag name.
-
#wf_tag?(*tags) ⇒ Boolean
Ensure one, or an array, of tags are valid.
-
#wf_ts?(timestamp) ⇒ Boolean
Ensure the given argument is a valid timestamp.
-
#wf_user_id?(user) ⇒ Boolean
Ensure the given argument is a valid user.
-
#wf_value?(value) ⇒ Boolean
Ensure the given argument is a valid Wavefront value.
-
#wf_version?(version) ⇒ Boolean
Ensure the given argument is a valid version number.
-
#wf_webhook_id?(id) ⇒ Boolean
Ensure the given argument is a valid webhook ID.
Instance Method Details
#wf_alert_id?(id) ⇒ Boolean
Ensure the given argument is a valid Wavefront alert ID. Alerts are identified by the epoch-nanosecond at which they were created.
213 214 215 216 217 |
# File 'lib/wavefront-sdk/validators.rb', line 213 def wf_alert_id?(id) id = id.to_s if id.is_a?(Numeric) return true if id.is_a?(String) && id.match(/^\d{13}$/) raise Wavefront::Exception::InvalidAlertId end |
#wf_alert_severity?(severity) ⇒ Boolean
Ensure the given argument is a valid alert severity
313 314 315 316 |
# File 'lib/wavefront-sdk/validators.rb', line 313 def wf_alert_severity?(severity) return true if %w[INFO SMOKE WARN SEVERE].include?(severity) raise Wavefront::Exception::InvalidAlertSeverity end |
#wf_cloudintegration_id?(id) ⇒ Boolean
Ensure the given argument is a valid Wavefront cloud integration ID
227 228 229 230 231 232 233 234 235 |
# File 'lib/wavefront-sdk/validators.rb', line 227 def wf_cloudintegration_id?(id) if id.is_a?(String) && id.match( /^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/ ) return true end raise Wavefront::Exception::InvalidCloudIntegrationId end |
#wf_dashboard_id?(id) ⇒ Boolean
There doesn’t seem to be a public statement on what’s allowed in a dashboard name. For now I’m going to assume up to 255 word characters.
246 247 248 249 |
# File 'lib/wavefront-sdk/validators.rb', line 246 def wf_dashboard_id?(id) return true if id.is_a?(String) && id.size < 256 && id.match(/^[\w\-]+$/) raise Wavefront::Exception::InvalidDashboardId end |
#wf_derivedmetric_id?(id) ⇒ Boolean
Ensure the given argument is a valid derived metric ID. IDs are the millisecond epoch timestamp at which the derived metric was created.
259 260 261 262 263 264 |
# File 'lib/wavefront-sdk/validators.rb', line 259 def wf_derivedmetric_id?(id) id = id.to_s if id.is_a?(Numeric) return true if id.is_a?(String) && id =~ /^\d{13}$/ raise Wavefront::Exception::InvalidDerivedMetricId end |
#wf_distribution?(dist) ⇒ Boolean
Validate a distribution description
431 432 433 434 435 436 437 438 |
# File 'lib/wavefront-sdk/validators.rb', line 431 def wf_distribution?(dist) wf_metric_name?(dist[:path]) wf_distribution_values?(dist[:value]) wf_epoch?(dist[:ts]) if dist[:ts] wf_source_id?(dist[:source]) if dist[:source] (dist[:tags]) if dist[:tags] true end |
#wf_distribution_count?(count) ⇒ Boolean
Ensure the given argument is a valid distribution count.
494 495 496 497 |
# File 'lib/wavefront-sdk/validators.rb', line 494 def wf_distribution_count?(count) return true if count.is_a?(Integer) && count > 0 raise Wavefront::Exception::InvalidDistributionCount end |
#wf_distribution_interval?(interval) ⇒ Boolean
Ensure the given argument is a valid distribution interval.
484 485 486 487 |
# File 'lib/wavefront-sdk/validators.rb', line 484 def wf_distribution_interval?(interval) return true if %i[m h d].include?(interval) raise Wavefront::Exception::InvalidDistributionInterval end |
#wf_distribution_values?(vals) ⇒ Boolean
Validate an array of distribution values
446 447 448 449 450 451 452 |
# File 'lib/wavefront-sdk/validators.rb', line 446 def wf_distribution_values?(vals) vals.each do |times, val| wf_distribution_count?(times) wf_value?(val) end true end |
#wf_epoch?(timestamp) ⇒ Boolean
Ensure the given argument is a valid epoch timestamp. Again, no range checking.
110 111 112 113 |
# File 'lib/wavefront-sdk/validators.rb', line 110 def wf_epoch?() return true if .is_a?(Numeric) raise Wavefront::Exception::InvalidTimestamp end |
#wf_event_id?(id) ⇒ Boolean
Ensure the given argument is a valid event ID. Event IDs are an epoch-millisecond timestamp followed by a : followed by the name of the event.
275 276 277 278 |
# File 'lib/wavefront-sdk/validators.rb', line 275 def wf_event_id?(id) return true if id.is_a?(String) && id =~ /^\d{13}:.+/ raise Wavefront::Exception::InvalidEventId end |
#wf_granularity?(granularity) ⇒ Boolean
Ensure the given argument is a valid query granularity
336 337 338 339 |
# File 'lib/wavefront-sdk/validators.rb', line 336 def wf_granularity?(granularity) return true if %w[d h m s].include?(granularity.to_s) raise Wavefront::Exception::InvalidGranularity end |
#wf_integration_id?(id) ⇒ Boolean
Ensure the given argument is a valid Wavefront integration ID. These appear to be lower-case strings.
474 475 476 477 |
# File 'lib/wavefront-sdk/validators.rb', line 474 def wf_integration_id?(id) return true if id.is_a?(String) && id =~ /^[a-z0-9]+$/ raise Wavefront::Exception::InvalidIntegrationId end |
#wf_link_id?(id) ⇒ Boolean
Ensure the given argument is a valid external Link ID
287 288 289 290 |
# File 'lib/wavefront-sdk/validators.rb', line 287 def wf_link_id?(id) return true if id.is_a?(String) && id =~ /^\w{16}$/ raise Wavefront::Exception::InvalidExternalLinkId end |
#wf_link_template?(template) ⇒ Boolean
Ensure the given argument is a valid external link template
20 21 22 23 24 25 26 27 |
# File 'lib/wavefront-sdk/validators.rb', line 20 def wf_link_template?(template) if template.is_a?(String) && template.start_with?('http://', 'https://') return true end raise Wavefront::Exception::InvalidLinkTemplate end |
#wf_maintenance_window_id?(id) ⇒ Boolean
Ensure the given argument is a valid maintenance window ID. IDs are the millisecond epoch timestamp at which the window was created.
300 301 302 303 304 305 |
# File 'lib/wavefront-sdk/validators.rb', line 300 def wf_maintenance_window_id?(id) id = id.to_s if id.is_a?(Numeric) return true if id.is_a?(String) && id =~ /^\d{13}$/ raise Wavefront::Exception::InvalidMaintenanceWindowId end |
#wf_message_id?(id) ⇒ Boolean
Ensure the given argument is a valid message ID
324 325 326 327 |
# File 'lib/wavefront-sdk/validators.rb', line 324 def (id) return true if id.is_a?(String) && id =~ /^\w+::\w+$/ raise Wavefront::Exception::InvalidMessageId end |
#wf_metric_name?(metric) ⇒ Boolean
Ensure the given argument is a valid Wavefront metric name, or path.
is not valid.
37 38 39 40 41 42 43 44 45 |
# File 'lib/wavefront-sdk/validators.rb', line 37 def wf_metric_name?(metric) if metric.is_a?(String) && metric.size < 1024 && (metric.match(/^#{DELTA}?[\w\-\.]+$/) || metric.match(%r{^\"#{DELTA}?[\w\-\.\/,]+\"$})) return true end raise Wavefront::Exception::InvalidMetricName end |
#wf_ms_ts?(timestamp) ⇒ Boolean
Ensure the given argument is a valid millisecond epoch timestamp. We do no checking of the value, because who am I to say that the user doesn’t want to send a point relating to 1ms after the epoch, or a thousand years in the future?
98 99 100 101 |
# File 'lib/wavefront-sdk/validators.rb', line 98 def wf_ms_ts?() return true if .is_a?(Numeric) raise Wavefront::Exception::InvalidTimestamp end |
#wf_name?(name) ⇒ Boolean
Ensure the given argument is a valid name, for instance for an event. Names can contain, AFAIK, word characters.
raise Wavefront::Exception::InvalidName if name is not valid
54 55 56 57 |
# File 'lib/wavefront-sdk/validators.rb', line 54 def wf_name?(name) return true if name.is_a?(String) && name.size < 1024 && name =~ /^\w+$/ raise Wavefront::Exception::InvalidName end |
#wf_notificant_id?(id) ⇒ Boolean
Ensure the given argument is a valid Wavefront notificant ID.
461 462 463 464 |
# File 'lib/wavefront-sdk/validators.rb', line 461 def wf_notificant_id?(id) return true if id.is_a?(String) && id =~ /^\w{16}$/ raise Wavefront::Exception::InvalidNotificantId end |
#wf_point?(point) ⇒ Boolean
Validate a point so it conforms to the standard described in community.wavefront.com/docs/DOC-1031
416 417 418 419 420 421 422 423 |
# File 'lib/wavefront-sdk/validators.rb', line 416 def wf_point?(point) wf_metric_name?(point[:path]) wf_value?(point[:value]) wf_epoch?(point[:ts]) if point[:ts] wf_source_id?(point[:source]) if point[:source] (point[:tags]) if point[:tags] true end |
#wf_point_tag?(key, val) ⇒ Boolean
Validate a single point tag, probably on behalf of #wf_point_tags?
178 179 180 181 182 183 184 185 |
# File 'lib/wavefront-sdk/validators.rb', line 178 def wf_point_tag?(key, val) if key && val && (key.size + val.size < 254) && key =~ /^[\w\-\.:]+$/ && val !~ /\\$/ return end raise Wavefront::Exception::InvalidTag end |
#wf_point_tags?(tags) ⇒ Boolean
Ensure a hash of key:value point tags are value. Not to be confused with source tags.
167 168 169 170 |
# File 'lib/wavefront-sdk/validators.rb', line 167 def () raise Wavefront::Exception::InvalidTag unless .is_a?(Hash) .each { |k, v| wf_point_tag?(k, v) } end |
#wf_proxy_id?(id) ⇒ Boolean
Ensure the given argument is a valid Wavefront proxy ID
194 195 196 197 198 199 200 201 202 |
# File 'lib/wavefront-sdk/validators.rb', line 194 def wf_proxy_id?(id) if id.is_a?(String) && id.match( /^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/ ) return true end raise Wavefront::Exception::InvalidProxyId end |
#wf_savedsearch_entity?(id) ⇒ Boolean
Ensure the given argument is a valid saved search entity type.
359 360 361 362 363 364 |
# File 'lib/wavefront-sdk/validators.rb', line 359 def wf_savedsearch_entity?(id) return true if %w[DASHBOARD ALERT MAINTENANCE_WINDOW NOTIFICANT EVENT SOURCE EXTERNAL_LINK AGENT CLOUD_INTEGRATION].include?(id) raise Wavefront::Exception::InvalidSavedSearchEntity end |
#wf_savedsearch_id?(id) ⇒ Boolean
Ensure the given argument is a valid saved search ID.
347 348 349 350 |
# File 'lib/wavefront-sdk/validators.rb', line 347 def wf_savedsearch_id?(id) return true if id.is_a?(String) && id =~ /^\w{8}$/ raise Wavefront::Exception::InvalidSavedSearchId end |
#wf_source_id?(source) ⇒ Boolean
Ensure the given argument is a valid Wavefront source name
373 374 375 376 377 378 379 380 |
# File 'lib/wavefront-sdk/validators.rb', line 373 def wf_source_id?(source) if source.is_a?(String) && source.match(/^[\w\.\-]+$/) && source.size < 1024 return true end raise Wavefront::Exception::InvalidSourceId end |
#wf_string?(str) ⇒ Boolean
Ensure the given argument is a valid string, for a tag name.
65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/wavefront-sdk/validators.rb', line 65 def wf_string?(str) # # Only allows PCRE "word" characters, spaces, full-stops and # commas in tags and descriptions. This might be too restrictive, # but if it is, this is the only place we need to change it. # if str.is_a?(String) && str.size < 1024 && str =~ /^[\-\w \.,]*$/ return true end raise Wavefront::Exception::InvalidString end |
#wf_tag?(*tags) ⇒ Boolean
Ensure one, or an array, of tags are valid. These tags are used as source tags, or tags for maintenance windows etc. They can contain letters, numbers, -, _ and :, and must be less than 256 characters long
124 125 126 127 128 129 130 131 132 |
# File 'lib/wavefront-sdk/validators.rb', line 124 def wf_tag?(*) Array(*).each do |tag| unless tag.is_a?(String) && tag.size < 255 && tag =~ /^[\w:\-\.]+$/ raise Wavefront::Exception::InvalidTag end end true end |
#wf_ts?(timestamp) ⇒ Boolean
Ensure the given argument is a valid timestamp
84 85 86 87 |
# File 'lib/wavefront-sdk/validators.rb', line 84 def wf_ts?() return true if .is_a?(Time) || .is_a?(Date) raise Wavefront::Exception::InvalidTimestamp end |
#wf_user_id?(user) ⇒ Boolean
Ensure the given argument is a valid user.
388 389 390 391 392 393 394 395 |
# File 'lib/wavefront-sdk/validators.rb', line 388 def wf_user_id?(user) if user.is_a?(String) && user =~ /\A([\w+\-].?)+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i return true end raise Wavefront::Exception::InvalidUserId end |
#wf_value?(value) ⇒ Boolean
Ensure the given argument is a valid Wavefront value. Can be any form of Numeric, including standard notation.
141 142 143 144 |
# File 'lib/wavefront-sdk/validators.rb', line 141 def wf_value?(value) return true if value.is_a?(Numeric) raise Wavefront::Exception::InvalidMetricValue end |
#wf_version?(version) ⇒ Boolean
Ensure the given argument is a valid version number
153 154 155 156 157 |
# File 'lib/wavefront-sdk/validators.rb', line 153 def wf_version?(version) version = version.to_i if version.is_a?(String) && version =~ /^\d+$/ return true if version.is_a?(Integer) && version > 0 raise Wavefront::Exception::InvalidVersion end |
#wf_webhook_id?(id) ⇒ Boolean
Ensure the given argument is a valid webhook ID.
403 404 405 406 |
# File 'lib/wavefront-sdk/validators.rb', line 403 def wf_webhook_id?(id) return true if id.is_a?(String) && id =~ /^[a-zA-Z0-9]{16}$/ raise Wavefront::Exception::InvalidWebhookId end |