Module: Wavefront::Validators
- Included in:
- Base
- 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.
Instance Method Summary collapse
-
#wf_alert_id?(v) ⇒ Boolean
Ensure the given argument is a valid Wavefront alert ID.
-
#wf_alert_severity?(v) ⇒ Boolean
Ensure the given argument is a valid alert severity.
-
#wf_cloudintegration_id?(v) ⇒ Boolean
Ensure the given argument is a valid Wavefront cloud integration ID.
-
#wf_dashboard_id?(v) ⇒ Boolean
There doesn’t seem to be a public statement on what’s allowed in a dashboard name.
-
#wf_epoch?(v) ⇒ Boolean
Ensure the given argument is a valid epoch timestamp.
-
#wf_event_id?(v) ⇒ Boolean
Ensure the given argument is a valid event ID.
-
#wf_granularity?(v) ⇒ Boolean
Ensure the given argument is a valid query granularity.
-
#wf_link_id?(v) ⇒ Boolean
Ensure the given argument is a valid external Link ID.
-
#wf_link_template?(v) ⇒ Boolean
Ensure the given argument is a valid external link template.
-
#wf_maintenance_window_id?(v) ⇒ Boolean
Ensure the given argument is a valid maintenance window ID.
-
#wf_message_id?(v) ⇒ Boolean
Ensure the given argument is a valid message ID.
-
#wf_metric_name?(v) ⇒ Boolean
Ensure the given argument is a valid Wavefront metric name, or path.
-
#wf_ms_ts?(v) ⇒ Boolean
Ensure the given argument is a valid millisecond epoch timestamp.
-
#wf_name?(v) ⇒ Boolean
Ensure the given argument is a valid name, for instance for an event.
-
#wf_point?(v) ⇒ Boolean
Validate a point so it conforms to the standard described in community.wavefront.com/docs/DOC-1031.
-
#wf_point_tags?(tags) ⇒ Boolean
Ensure a hash of key:value point tags are value.
-
#wf_proxy_id?(v) ⇒ Boolean
Ensure the given argument is a valid Wavefront proxy ID.
-
#wf_savedsearch_entity?(v) ⇒ Boolean
Ensure the given argument is a valid saved search entity type.
-
#wf_savedsearch_id?(v) ⇒ Boolean
Ensure the given argument is a valid saved search ID.
-
#wf_source_id?(v) ⇒ Boolean
Ensure the given argument is a valid Wavefront source name.
-
#wf_string?(v) ⇒ Boolean
Ensure the given argument is a valid string, for a tag name.
-
#wf_tag?(*v) ⇒ Boolean
Ensure one, or an array, of tags are valid.
-
#wf_ts?(v) ⇒ Boolean
Ensure the given argument is a valid timestamp.
-
#wf_user_id?(v) ⇒ Boolean
Ensure the given argument is a valid user.
-
#wf_value?(v) ⇒ Boolean
Ensure the given argument is a valid Wavefront value.
-
#wf_version?(v) ⇒ Boolean
Ensure the given argument is a valid version number.
-
#wf_webhook_id?(v) ⇒ Boolean
Ensure the given argument is a valid webhook ID.
Instance Method Details
#wf_alert_id?(v) ⇒ Boolean
Ensure the given argument is a valid Wavefront alert ID. Alerts are identified by the epoch-nanosecond at which they were created.
200 201 202 203 204 |
# File 'lib/wavefront-sdk/validators.rb', line 200 def wf_alert_id?(v) v = v.to_s if v.is_a?(Numeric) return true if v.is_a?(String) && v.match(/^\d{13}$/) raise Wavefront::Exception::InvalidAlertId end |
#wf_alert_severity?(v) ⇒ Boolean
Ensure the given argument is a valid alert severity
286 287 288 289 |
# File 'lib/wavefront-sdk/validators.rb', line 286 def wf_alert_severity?(v) return true if %w(INFO SMOKE WARN SEVERE).include?(v) raise Wavefront::Exception::InvalidAlertSeverity end |
#wf_cloudintegration_id?(v) ⇒ Boolean
Ensure the given argument is a valid Wavefront cloud integration ID
214 215 216 217 218 219 220 221 222 |
# File 'lib/wavefront-sdk/validators.rb', line 214 def wf_cloudintegration_id?(v) if v.is_a?(String) && v.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?(v) ⇒ 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.
233 234 235 236 |
# File 'lib/wavefront-sdk/validators.rb', line 233 def wf_dashboard_id?(v) return true if v.is_a?(String) && v.size < 256 && v.match(/^[\w\-]+$/) raise Wavefront::Exception::InvalidDashboardId end |
#wf_epoch?(v) ⇒ Boolean
Ensure the given argument is a valid epoch timestamp. Again, no range checking.
106 107 108 109 |
# File 'lib/wavefront-sdk/validators.rb', line 106 def wf_epoch?(v) return true if v.is_a?(Numeric) raise Wavefront::Exception::InvalidTimestamp end |
#wf_event_id?(v) ⇒ 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.
247 248 249 250 |
# File 'lib/wavefront-sdk/validators.rb', line 247 def wf_event_id?(v) return true if v.is_a?(String) && v =~ /^\d{13}:.+/ raise Wavefront::Exception::InvalidEventId end |
#wf_granularity?(v) ⇒ Boolean
Ensure the given argument is a valid query granularity
310 311 312 313 |
# File 'lib/wavefront-sdk/validators.rb', line 310 def wf_granularity?(v) return true if %w(d h m s).include?(v.to_s) raise Wavefront::Exception::InvalidGranularity end |
#wf_link_id?(v) ⇒ Boolean
Ensure the given argument is a valid external Link ID
259 260 261 262 |
# File 'lib/wavefront-sdk/validators.rb', line 259 def wf_link_id?(v) return true if v.is_a?(String) && v =~ /^\w{16}$/ raise Wavefront::Exception::InvalidExternalLinkId end |
#wf_link_template?(v) ⇒ Boolean
Ensure the given argument is a valid external link template
19 20 21 22 23 24 25 26 |
# File 'lib/wavefront-sdk/validators.rb', line 19 def wf_link_template?(v) if v.is_a?(String) && (v.start_with?('http://') || v.start_with?('https://')) return true end raise Wavefront::Exception::InvalidLinkTemplate end |
#wf_maintenance_window_id?(v) ⇒ Boolean
Ensure the given argument is a valid maintenance window ID. IDs are the millisecond epoch timestamp at which the window was created.
272 273 274 275 276 277 |
# File 'lib/wavefront-sdk/validators.rb', line 272 def wf_maintenance_window_id?(v) v = v.to_s if v.is_a?(Numeric) return true if v.is_a?(String) && v =~ /^\d{13}$/ raise Wavefront::Exception::InvalidMaintenanceWindowId end |
#wf_message_id?(v) ⇒ Boolean
Ensure the given argument is a valid message ID
298 299 300 301 |
# File 'lib/wavefront-sdk/validators.rb', line 298 def (v) return true if v.is_a?(String) && v =~ /^\w+$/ raise Wavefront::Exception::InvalidMessageId end |
#wf_metric_name?(v) ⇒ Boolean
Ensure the given argument is a valid Wavefront metric name, or path.
is not valid.
36 37 38 39 40 41 42 43 |
# File 'lib/wavefront-sdk/validators.rb', line 36 def wf_metric_name?(v) if v.is_a?(String) && v.size < 1024 && (v.match(/^[\w\-\.]+$/) || v.match(%r{^\"[\w\-\.\/,]+\"$})) return true end raise Wavefront::Exception::InvalidMetricName end |
#wf_ms_ts?(v) ⇒ 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?
94 95 96 97 |
# File 'lib/wavefront-sdk/validators.rb', line 94 def wf_ms_ts?(v) return true if v.is_a?(Numeric) raise Wavefront::Exception::InvalidTimestamp end |
#wf_name?(v) ⇒ 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
52 53 54 55 |
# File 'lib/wavefront-sdk/validators.rb', line 52 def wf_name?(v) return true if v.is_a?(String) && v.size < 1024 && v =~ /^\w+$/ raise Wavefront::Exception::InvalidName end |
#wf_point?(v) ⇒ Boolean
Validate a point so it conforms to the standard described in community.wavefront.com/docs/DOC-1031
386 387 388 389 390 391 392 393 |
# File 'lib/wavefront-sdk/validators.rb', line 386 def wf_point?(v) wf_metric_name?(v[:path]) wf_value?(v[:value]) wf_epoch?(v[:ts]) if v[:ts] wf_source_id?(v[:source]) if v[:source] (v[:tags]) if v[:tags] true end |
#wf_point_tags?(tags) ⇒ Boolean
Ensure a hash of key:value point tags are value. Not to be confused with source tags.
163 164 165 166 167 168 169 170 171 172 |
# File 'lib/wavefront-sdk/validators.rb', line 163 def () raise Wavefront::Exception::InvalidTag unless .is_a?(Hash) .each do |k, v| unless (k.size + v.size < 254) && k.match(/^[\w\-\.:]+$/) raise Wavefront::Exception::InvalidTag end end true end |
#wf_proxy_id?(v) ⇒ Boolean
Ensure the given argument is a valid Wavefront proxy ID
181 182 183 184 185 186 187 188 189 |
# File 'lib/wavefront-sdk/validators.rb', line 181 def wf_proxy_id?(v) if v.is_a?(String) && v.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?(v) ⇒ Boolean
Ensure the given argument is a valid saved search entity type.
333 334 335 336 337 338 |
# File 'lib/wavefront-sdk/validators.rb', line 333 def wf_savedsearch_entity?(v) return true if %w(DASHBOARD ALERT MAINTENANCE_WINDOW NOTIFICANT EVENT SOURCE EXTERNAL_LINK AGENT CLOUD_INTEGRATION).include?(v) raise Wavefront::Exception::InvalidSavedSearchEntity end |
#wf_savedsearch_id?(v) ⇒ Boolean
Ensure the given argument is a valid saved search ID.
321 322 323 324 |
# File 'lib/wavefront-sdk/validators.rb', line 321 def wf_savedsearch_id?(v) return true if v.is_a?(String) && v =~ /^\w{8}$/ raise Wavefront::Exception::InvalidSavedSearchId end |
#wf_source_id?(v) ⇒ Boolean
Ensure the given argument is a valid Wavefront source name
347 348 349 350 351 352 353 |
# File 'lib/wavefront-sdk/validators.rb', line 347 def wf_source_id?(v) if v.is_a?(String) && v.match(/^[\w\.\-]+$/) && v.size < 1024 return true end raise Wavefront::Exception::InvalidSourceId end |
#wf_string?(v) ⇒ Boolean
Ensure the given argument is a valid string, for a tag name.
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/wavefront-sdk/validators.rb', line 63 def wf_string?(v) # # 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. # return true if v.is_a?(String) && v.size < 1024 && v =~ /^[\-\w \.,]*$/ raise Wavefront::Exception::InvalidString end |
#wf_tag?(*v) ⇒ 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
120 121 122 123 124 125 126 127 128 |
# File 'lib/wavefront-sdk/validators.rb', line 120 def wf_tag?(*v) Array(*v).each do |t| unless t.is_a?(String) && t.size < 255 && t =~ /^[\w:\-\.]+$/ raise Wavefront::Exception::InvalidTag end end true end |
#wf_ts?(v) ⇒ Boolean
Ensure the given argument is a valid timestamp
80 81 82 83 |
# File 'lib/wavefront-sdk/validators.rb', line 80 def wf_ts?(v) return true if v.is_a?(Time) || v.is_a?(Date) raise Wavefront::Exception::InvalidTimestamp end |
#wf_user_id?(v) ⇒ Boolean
Ensure the given argument is a valid user.
361 362 363 364 365 |
# File 'lib/wavefront-sdk/validators.rb', line 361 def wf_user_id?(v) return true if v.is_a?(String) && v =~ /\A([\w+\-].?)+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i raise Wavefront::Exception::InvalidUserId end |
#wf_value?(v) ⇒ Boolean
Ensure the given argument is a valid Wavefront value. Can be any form of Numeric, including standard notation.
137 138 139 140 |
# File 'lib/wavefront-sdk/validators.rb', line 137 def wf_value?(v) return true if v.is_a?(Numeric) raise Wavefront::Exception::InvalidMetricValue end |
#wf_version?(v) ⇒ Boolean
Ensure the given argument is a valid version number
149 150 151 152 153 |
# File 'lib/wavefront-sdk/validators.rb', line 149 def wf_version?(v) v = v.to_i if v.is_a?(String) && v =~ /^\d+$/ return true if v.is_a?(Integer) && v > 0 raise Wavefront::Exception::InvalidVersion end |
#wf_webhook_id?(v) ⇒ Boolean
Ensure the given argument is a valid webhook ID.
373 374 375 376 |
# File 'lib/wavefront-sdk/validators.rb', line 373 def wf_webhook_id?(v) return true if v.is_a?(String) && v =~ /^[a-zA-Z0-9]{16}$/ raise Wavefront::Exception::InvalidWebhookId end |