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

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.

Parameters:

  • v (String)

    the alert ID to validate

Returns:

  • (Boolean)

    True if the alert ID is valid

Raises:

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



208
209
210
211
212
# File 'lib/wavefront-sdk/validators.rb', line 208

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

Parameters:

  • v (String)

    severity

Returns:

  • (Boolean)

    true if valid

Raises:

  • Wavefront::Exceptions::InvalidAlertSeverity if not valid



294
295
296
297
# File 'lib/wavefront-sdk/validators.rb', line 294

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

Parameters:

  • v (String)

    the integration name to validate

Returns:

  • (Boolean)

    True if the integration name is valid

Raises:

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



222
223
224
225
226
227
228
229
230
# File 'lib/wavefront-sdk/validators.rb', line 222

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.

Parameters:

  • v (String)

    the dashboard ID to validate

Returns:

  • (Boolean)

    true if the dashboard ID is valid

Raises:

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



241
242
243
244
# File 'lib/wavefront-sdk/validators.rb', line 241

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.

Parameters:

  • v (String, Integer)

Returns:

  • (Boolean)

    True if the timestamp is valid

Raises:

  • Wavefront::Exception::InvalidMaintenanceWindow



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

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.

Parameters:

  • v (String)

    the event ID to validate

Returns:

  • (Boolean)

    true if the event ID is valid

Raises:

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



255
256
257
258
# File 'lib/wavefront-sdk/validators.rb', line 255

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

Parameters:

  • v (String)

    granularity

Returns:

  • (Boolean)

    true if valid

Raises:

  • Wavefront::Exceptions::InvalidGranularity if not valid



318
319
320
321
# File 'lib/wavefront-sdk/validators.rb', line 318

def wf_granularity?(v)
  return true if %w(d h m s).include?(v.to_s)
  raise Wavefront::Exception::InvalidGranularity
end

#wf_integration_id?(v) ⇒ Boolean

Ensure the given argument is a valid Wavefront integration ID. These appear to be lower-case strings.

Parameters:

  • v (String)

    the integration name to validate

Returns:

  • (Boolean)

    True if the integration name is valid

Raises:

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



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

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

Ensure the given argument is a valid external Link ID

Parameters:

  • v (String)

    the external link ID to validate

Returns:

  • (Boolean)

    True if the link ID is valid

Raises:

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



267
268
269
270
# File 'lib/wavefront-sdk/validators.rb', line 267

def wf_link_id?(v)
  return true if v.is_a?(String) && v =~ /^\w{16}$/
  raise Wavefront::Exception::InvalidExternalLinkId
end

Ensure the given argument is a valid external link template

Returns:

  • (Boolean)

    true if it is valid

Raises:

  • Wavefront::Exception::InvalidTemplate if not



20
21
22
23
24
25
26
27
# File 'lib/wavefront-sdk/validators.rb', line 20

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.

Parameters:

  • v (String, Integer)

Returns:

  • (Boolean)

    True if the ID is valid

Raises:

  • Wavefront::Exception::InvalidMaintenanceWindowId



280
281
282
283
284
285
# File 'lib/wavefront-sdk/validators.rb', line 280

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

Parameters:

  • v (String)

    severity

Returns:

  • (Boolean)

    true if valid

Raises:

  • Wavefront::Exceptions::InvalidMessageId if not valid



306
307
308
309
# File 'lib/wavefront-sdk/validators.rb', line 306

def wf_message_id?(v)
  return true if v.is_a?(String) && v =~ /^\w+::\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.

Parameters:

  • v (String)

    the metric name to validate

Returns:

  • (Boolean)

    True if the metric name is valid

Raises:

  • Wavefront::Exception::InvalidMetricName if metric name



37
38
39
40
41
42
43
44
45
# File 'lib/wavefront-sdk/validators.rb', line 37

def wf_metric_name?(v)
  if v.is_a?(String) && v.size < 1024 &&
     (v.match(/^#{DELTA}?[\w\-\.]+$/) ||
      v.match(%r{^\"#{DELTA}?[\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?

Parameters:

  • v (Integer)

    the timestamp name to validate

Returns:

  • (Boolean)

    True if the value is valid

Raises:

  • Wavefront::Exception::InvalidTimestamp



96
97
98
99
# File 'lib/wavefront-sdk/validators.rb', line 96

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

Parameters:

  • v (String)

    the name to validate

Returns:

  • (Boolean)

    true if the name is valid

Raises:



54
55
56
57
# File 'lib/wavefront-sdk/validators.rb', line 54

def wf_name?(v)
  return true if v.is_a?(String) && v.size < 1024 && v =~ /^\w+$/
  raise Wavefront::Exception::InvalidName
end

#wf_notificant_id?(v) ⇒ Boolean

Ensure the given argument is a valid Wavefront notificant ID.

Parameters:

  • v (String)

    the notificant name to validate

Returns:

  • (Boolean)

    True if the notificant name is valid

Raises:

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



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

def wf_notificant_id?(v)
  return true if v.is_a?(String) && v =~ /^\w{16}$/
  raise Wavefront::Exception::InvalidNotificantId
end

#wf_point?(v) ⇒ Boolean

Validate a point so it conforms to the standard described in community.wavefront.com/docs/DOC-1031

Parameters:

  • v (Hash)

    description of point

Returns:

  • (Boolean)

    true if valie

Raises:

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



394
395
396
397
398
399
400
401
# File 'lib/wavefront-sdk/validators.rb', line 394

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

#wf_point_tag?(k, v) ⇒ Boolean

Validate a single point tag, probably on behalf of #wf_point_tags?

Parameters:

  • k (String)

    tag key

  • v (String)

    tag value

Returns:

  • (Boolean)

    nil

Raises:

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



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

def wf_point_tag?(k, v)
  return if  k && v && (k.size + v.size < 254) && k =~ /^[\w\-\.:]+$/
  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.

Parameters:

  • tags (Hash)

    a hash of key:value tags

Returns:

  • (Boolean)

    True if all tags are valid

Raises:

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



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

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

Ensure the given argument is a valid Wavefront proxy ID

Parameters:

  • v (String)

    the proxy ID to validate

Returns:

  • (Boolean)

    True if the proxy ID is valid

Raises:

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



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

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.

Parameters:

  • v (String)

    entity type

Returns:

  • (Boolean)

    true if valid

Raises:

  • Wavefront::Exceptions::InvalidSavedSearchEntity if not valid



341
342
343
344
345
346
# File 'lib/wavefront-sdk/validators.rb', line 341

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.

Parameters:

  • v (String)

    saved search ID

Returns:

  • (Boolean)

    true if valid

Raises:

  • Wavefront::Exceptions::InvalidSavedSearchId if not valid



329
330
331
332
# File 'lib/wavefront-sdk/validators.rb', line 329

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

Parameters:

  • v (String)

    the source name to validate

Returns:

  • (Boolean)

    True if the source name is valid

Raises:

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



355
356
357
358
359
360
361
# File 'lib/wavefront-sdk/validators.rb', line 355

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.

Parameters:

  • v (String)

    the string name to validate

Returns:

  • (Boolean)

    True if the string is valid

Raises:

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



65
66
67
68
69
70
71
72
73
74
# File 'lib/wavefront-sdk/validators.rb', line 65

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

Parameters:

  • v (String, Array)

    a tag or list of tags

Returns:

  • (Boolean)

    True if all tags are valid

Raises:

  • Wavefront::Exception::InvalidTag



122
123
124
125
126
127
128
129
130
# File 'lib/wavefront-sdk/validators.rb', line 122

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

Parameters:

  • v (DateTime)

    the timestamp name to validate

Returns:

  • (Boolean)

    True if the value is valid

Raises:

  • Wavefront::Exception::InvalidTimestamp



82
83
84
85
# File 'lib/wavefront-sdk/validators.rb', line 82

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.

Parameters:

  • v (String)

    user identifier

Returns:

  • (Boolean)

    true if valid

Raises:

  • Wavefront::Exceptions::InvalidUserId if not valid



369
370
371
372
373
# File 'lib/wavefront-sdk/validators.rb', line 369

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.

Parameters:

  • v (Numeric)

    the source name to validate

Returns:

  • (Boolean)

    True if the value is valid

Raises:

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



139
140
141
142
# File 'lib/wavefront-sdk/validators.rb', line 139

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

Parameters:

  • v (Integer)

    the version number to validate

Returns:

  • (Boolean)

    True if the version is valid

Raises:

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



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

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.

Parameters:

  • v (String)

    webhook ID

Returns:

  • (Boolean)

    true if valid

Raises:

  • Wavefront::Exceptions::InvalidWebhook if not valid



381
382
383
384
# File 'lib/wavefront-sdk/validators.rb', line 381

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