Puppet Function: is_domain_name

Defined in:
modules/stdlib/lib/puppet/parser/functions/is_domain_name.rb
Function type:
Ruby 3.x API

Overview

is_domain_name()Boolean

> *Note: *Deprecated* Will be removed in a future version of stdlib. See [`validate_legacy`](#validate_legacy).

Returns:

  • (Boolean)

    Returns `true` or `false`



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'modules/stdlib/lib/puppet/parser/functions/is_domain_name.rb', line 5

newfunction(:is_domain_name, :type => :rvalue, :doc => <<-DOC
  @summary
    **Deprecated:** Returns true if the string passed to this function is
    a syntactically correct domain name.

  @return [Boolean]
    Returns `true` or `false`

  > **Note:* **Deprecated** Will be removed in a future version of stdlib. See
  [`validate_legacy`](#validate_legacy).
  DOC
           ) do |arguments|

  if arguments.size != 1
    raise(Puppet::ParseError, "is_domain_name(): Wrong number of arguments given #{arguments.size} for 1")
  end

  # Only allow string types
  return false unless arguments[0].is_a?(String)

  domain = arguments[0].dup

  # Limits (rfc1035, 3.1)
  domain_max_length = 255
  label_min_length = 1
  label_max_length = 63

  # Allow ".", it is the top level domain
  return true if domain == '.'

  # Remove the final dot, if present.
  domain.chomp!('.')

  # Check the whole domain
  return false if domain.empty?
  return false if domain.length > domain_max_length

  # The top level domain must be alphabetic if there are multiple labels.
  # See rfc1123, 2.1
  return false if domain.include?('.') && !%r{\.[A-Za-z]+$}.match(domain)

  # Check each label in the domain
  labels = domain.split('.')
  vlabels = labels.each do |label|
    break if label.length < label_min_length
    break if label.length > label_max_length
    break if label[-1..-1] == '-'
    break if label[0..0] == '-'
    break unless %r{^[a-z\d-]+$}i =~ label
  end
  return vlabels == labels
end