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

Instance Method Details

#uuid?(str) ⇒ Bool

Is the given string a UUID? These are used for various item IDs.

Parameters:

Returns:



21
22
23
# File 'lib/wavefront-sdk/validators.rb', line 21

def uuid?(str)
  str.is_a?(String) && str =~ /([a-f\d]{8}(-[a-f\d]{4}){3}-[a-f\d]{12})/
end

#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.

Raises:

  • Wavefront::Exception::InvalidAlertId if the alert ID is not valid

Parameters:

  • the alert ID to validate

Returns:

  • True if the alert ID is valid



218
219
220
221
222
# File 'lib/wavefront-sdk/validators.rb', line 218

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

Raises:

  • Wavefront::Exceptions::InvalidAlertSeverity if not valid

Parameters:

  • severity

Returns:

  • true if valid



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_apitoken_id?(id) ⇒ Boolean

Ensure the given argument is a valid API token ID

Raises:

  • Wavefront::Exception::InvalidApiTokenId if the count is not valid

Parameters:

Returns:



511
512
513
514
# File 'lib/wavefront-sdk/validators.rb', line 511

def wf_apitoken_id?(id)
  return true if uuid?(id)
  raise Wavefront::Exception::InvalidApiTokenId
end

#wf_cloudintegration_id?(id) ⇒ Boolean

Ensure the given argument is a valid Wavefront cloud integration ID

Raises:

  • Wavefront::Exception::InvalidCloudIntegrationId if the integration ID is not valid

Parameters:

  • the integration name to validate

Returns:

  • True if the integration name is valid



232
233
234
235
# File 'lib/wavefront-sdk/validators.rb', line 232

def wf_cloudintegration_id?(id)
  return true if uuid?(id)
  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.

Raises:

  • Wavefront::Exception::InvalidDashboardID if the dashboard ID is not valid

Parameters:

  • the dashboard ID to validate

Returns:

  • true if the dashboard ID is valid



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.

Raises:

  • Wavefront::Exception::InvalidDerivedMetricId

Parameters:

Returns:

  • True if the ID is valid



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

Raises:

  • whichever exception is thrown first when validating each component of the distribution.

Parameters:

  • description of distribution

Returns:

  • true if valid



438
439
440
441
442
443
444
445
# File 'lib/wavefront-sdk/validators.rb', line 438

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]
  wf_point_tags?(dist[:tags]) if dist[:tags]
  true
end

#wf_distribution_count?(count) ⇒ Boolean

Ensure the given argument is a valid distribution count.

Raises:

  • Wavefront::Exception::InvalidDistributionCount if the count is not valid

Parameters:

Returns:



501
502
503
504
# File 'lib/wavefront-sdk/validators.rb', line 501

def wf_distribution_count?(count)
  return true if count.is_a?(Integer) && count.positive?
  raise Wavefront::Exception::InvalidDistributionCount
end

#wf_distribution_interval?(interval) ⇒ Boolean

Ensure the given argument is a valid distribution interval.

Raises:

  • Wavefront::Exception::InvalidDistributionInterval if the interval is not valid

Parameters:

Returns:



491
492
493
494
# File 'lib/wavefront-sdk/validators.rb', line 491

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

Raises:

  • whichever exception is thrown first when validating each component of the distribution.

Parameters:

  • count, value

Returns:

  • true if valid



453
454
455
456
457
458
459
# File 'lib/wavefront-sdk/validators.rb', line 453

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.

Raises:

  • Wavefront::Exception::InvalidMaintenanceWindow

Parameters:

Returns:

  • True if the timestamp is valid



120
121
122
123
# File 'lib/wavefront-sdk/validators.rb', line 120

def wf_epoch?(timestamp)
  return true if timestamp.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.

Raises:

  • Wavefront::Exception::InvalidEventID if the event ID is not valid

Parameters:

  • the event ID to validate

Returns:

  • true if the event ID is valid



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

Raises:

  • Wavefront::Exceptions::InvalidGranularity if not valid

Parameters:

  • granularity

Returns:

  • true if valid



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.

Raises:

  • Wavefront::Exception::InvalidIntegrationId if the integration ID is not valid

Parameters:

  • the integration ID to validate

Returns:

  • True if the integration name is valid



481
482
483
484
# File 'lib/wavefront-sdk/validators.rb', line 481

def wf_integration_id?(id)
  return true if id.is_a?(String) && id =~ /^[a-z0-9]+$/
  raise Wavefront::Exception::InvalidIntegrationId
end

Ensure the given argument is a valid external Link ID

Raises:

  • Wavefront::Exception::InvalidExternalLinkId if the link ID is not valid

Parameters:

  • the external link ID to validate

Returns:

  • True if the link ID is valid



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

Ensure the given argument is a valid external link template

Raises:

  • Wavefront::Exception::InvalidTemplate if not

Returns:

  • true if it is valid



30
31
32
33
34
35
36
37
# File 'lib/wavefront-sdk/validators.rb', line 30

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.

Raises:

  • Wavefront::Exception::InvalidMaintenanceWindowId

Parameters:

Returns:

  • True if the ID is valid



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

Raises:

  • Wavefront::Exceptions::InvalidMessageId if not valid

Parameters:

  • message ID

Returns:

  • true if valid



324
325
326
327
# File 'lib/wavefront-sdk/validators.rb', line 324

def wf_message_id?(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.

Raises:

  • Wavefront::Exception::InvalidMetricName if metric name

Parameters:

  • the metric name to validate

Returns:

  • True if the metric name is valid



47
48
49
50
51
52
53
54
55
# File 'lib/wavefront-sdk/validators.rb', line 47

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?

Raises:

  • Wavefront::Exception::InvalidTimestamp

Parameters:

  • the timestamp name to validate

Returns:

  • True if the value is valid



108
109
110
111
# File 'lib/wavefront-sdk/validators.rb', line 108

def wf_ms_ts?(timestamp)
  return true if timestamp.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

Raises:

Parameters:

  • the name to validate

Returns:

  • true if the name is valid



64
65
66
67
# File 'lib/wavefront-sdk/validators.rb', line 64

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.

Raises:

  • Wavefront::Exception::InvalidNotificantId if the notificant ID is not valid

Parameters:

  • the notificant ID to validate

Returns:

  • True if the notificant ID is valid



468
469
470
471
# File 'lib/wavefront-sdk/validators.rb', line 468

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

Raises:

  • whichever exception is thrown first when validating each component of the point.

Parameters:

  • description of point

Returns:

  • true if valid



423
424
425
426
427
428
429
430
# File 'lib/wavefront-sdk/validators.rb', line 423

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]
  wf_point_tags?(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?

Raises:

  • Wavefront::Exception::InvalidTag if any tag is not valid

Parameters:

  • tag key

  • tag value

Returns:

  • nil



188
189
190
191
192
193
194
195
# File 'lib/wavefront-sdk/validators.rb', line 188

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.

Raises:

  • Wavefront::Exception::InvalidTag if any tags in the has do not validate

Parameters:

  • a hash of key:value tags

Returns:

  • True if all tags are valid



177
178
179
180
# File 'lib/wavefront-sdk/validators.rb', line 177

def wf_point_tags?(tags)
  raise Wavefront::Exception::InvalidTag unless tags.is_a?(Hash)
  tags.each { |k, v| wf_point_tag?(k, v) }
end

#wf_proxy_id?(id) ⇒ Boolean

Ensure the given argument is a valid Wavefront proxy ID

Raises:

  • Wavefront::Exception::InvalidProxyId if the proxy ID is not valid

Parameters:

  • the proxy ID to validate

Returns:

  • True if the proxy ID is valid



204
205
206
207
# File 'lib/wavefront-sdk/validators.rb', line 204

def wf_proxy_id?(id)
  return true if uuid?(id)
  raise Wavefront::Exception::InvalidProxyId
end

#wf_savedsearch_entity?(id) ⇒ Boolean

Ensure the given argument is a valid saved search entity type.

Raises:

  • Wavefront::Exceptions::InvalidSavedSearchEntity if not valid

Parameters:

  • entity type

Returns:

  • true if valid



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.

Raises:

  • Wavefront::Exceptions::InvalidSavedSearchId if not valid

Parameters:

  • saved search ID

Returns:

  • true if valid



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

Raises:

  • Wavefront::Exception::InvalidSourceId if the source name is not valid

Parameters:

  • the source name to validate

Returns:

  • True if the source name is valid



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.

Raises:

  • Wavefront::Exception::InvalidString if string is not valid.

Parameters:

  • the string name to validate

Returns:

  • True if the string is valid



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/wavefront-sdk/validators.rb', line 75

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

Raises:

  • Wavefront::Exception::InvalidTag

Parameters:

  • a tag or list of tags

Returns:

  • True if all tags are valid



134
135
136
137
138
139
140
141
142
# File 'lib/wavefront-sdk/validators.rb', line 134

def wf_tag?(*tags)
  Array(*tags).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

Raises:

  • Wavefront::Exception::InvalidTimestamp

Parameters:

  • the timestamp name to validate

Returns:

  • True if the value is valid



94
95
96
97
# File 'lib/wavefront-sdk/validators.rb', line 94

def wf_ts?(timestamp)
  return true if timestamp.is_a?(Time) || timestamp.is_a?(Date)
  raise Wavefront::Exception::InvalidTimestamp
end

#wf_user_id?(user) ⇒ Boolean

Ensure the given argument is a valid user.

Raises:

  • Wavefront::Exceptions::InvalidUserId if not valid

Parameters:

  • user identifier

Returns:

  • true if valid



388
389
390
391
# File 'lib/wavefront-sdk/validators.rb', line 388

def wf_user_id?(user)
  return true if user.is_a?(String) && user.length < 256 && !user.empty?
  raise Wavefront::Exception::InvalidUserId
end

#wf_usergroup_id?(gid) ⇒ Boolean

Ensure the given argument is a valid user group.

Raises:

  • Wavefront::Exceptions::InvalidUserGroupId if not valid

Parameters:

  • user group identiier

Returns:

  • true if valid



399
400
401
402
# File 'lib/wavefront-sdk/validators.rb', line 399

def wf_usergroup_id?(gid)
  return true if uuid?(gid)
  raise Wavefront::Exception::InvalidUserGroupId
end

#wf_value?(value) ⇒ Boolean

Ensure the given argument is a valid Wavefront value. Can be any form of Numeric, including standard notation.

Raises:

  • Wavefront::Exception::InvalidValue if the value is not valid

Parameters:

  • the source name to validate

Returns:

  • True if the value is valid



151
152
153
154
# File 'lib/wavefront-sdk/validators.rb', line 151

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

Raises:

  • Wavefront::Exception::InvalidVersion if the alert ID is not valid

Parameters:

  • the version number to validate

Returns:

  • True if the version is valid



163
164
165
166
167
# File 'lib/wavefront-sdk/validators.rb', line 163

def wf_version?(version)
  version = version.to_i if version.is_a?(String) && version =~ /^\d+$/
  return true if version.is_a?(Integer) && version.positive?
  raise Wavefront::Exception::InvalidVersion
end

#wf_webhook_id?(id) ⇒ Boolean

Ensure the given argument is a valid webhook ID.

Raises:

  • Wavefront::Exceptions::InvalidWebhook if not valid

Parameters:

  • webhook ID

Returns:

  • true if valid



410
411
412
413
# File 'lib/wavefront-sdk/validators.rb', line 410

def wf_webhook_id?(id)
  return true if id.is_a?(String) && id =~ /^[a-zA-Z0-9]{16}$/
  raise Wavefront::Exception::InvalidWebhookId
end