Puppet Function: validate_domain_name

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

Summary

Validate that all values passed are syntactically correct domain names. Fail compilation if any value fails this check.

Overview

validate_domain_name()Any

Examples:

*Usage*


The following values will pass:

    $my_domain_name = 'server.domain.tld'
    validate_domain_name($my_domain_name)
    validate_domain_name('domain.tld', 'puppet.com', $my_domain_name)

The following values will fail, causing compilation to abort:

    validate_domain_name(1)
    validate_domain_name(true)
    validate_domain_name('invalid domain')
    validate_domain_name('-foo.example.com')
    validate_domain_name('www.example.2com')

Returns:

  • (Any)

    passes when the given values are syntactically correct domain names or raise an error when they are not and fails compilation



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
# File 'vendor_modules/stdlib/lib/puppet/parser/functions/validate_domain_name.rb', line 7

newfunction(:validate_domain_name, doc: <<-DOC
  @summary
    Validate that all values passed are syntactically correct domain names.
    Fail compilation if any value fails this check.

  @return
    passes when the given values are syntactically correct domain names or raise an error when they are not and fails compilation

  @example **Usage**

    The following values will pass:

        $my_domain_name = 'server.domain.tld'
        validate_domain_name($my_domain_name)
        validate_domain_name('domain.tld', 'puppet.com', $my_domain_name)

    The following values will fail, causing compilation to abort:

        validate_domain_name(1)
        validate_domain_name(true)
        validate_domain_name('invalid domain')
        validate_domain_name('-foo.example.com')
        validate_domain_name('www.example.2com')
  DOC
) do |args|
  rescuable_exceptions = [ArgumentError]

  if args.empty?
    raise Puppet::ParseError, "validate_domain_name(): wrong number of arguments (#{args.length}; must be > 0)"
  end

  args.each do |arg|
    raise Puppet::ParseError, "#{arg.inspect} is not a string." unless arg.is_a?(String)

    begin
      raise Puppet::ParseError, "#{arg.inspect} is not a syntactically correct domain name" unless function_is_domain_name([arg])
    rescue *rescuable_exceptions
      raise Puppet::ParseError, "#{arg.inspect} is not a syntactically correct domain name"
    end
  end
end