Update stdlib
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / validate_domain_name.rb
1 module Puppet::Parser::Functions
2   newfunction(:validate_domain_name, :doc => <<-ENDHEREDOC
3     Validate that all values passed are syntactically correct domain names.
4     Fail compilation if any value fails this check.
5
6     The following values will pass:
7
8         $my_domain_name = 'server.domain.tld'
9         validate_domain_name($my_domain_name)
10         validate_domain_name('domain.tld', 'puppet.com', $my_domain_name)
11
12     The following values will fail, causing compilation to abort:
13
14         validate_domain_name(1)
15         validate_domain_name(true)
16         validate_domain_name('invalid domain')
17         validate_domain_name('-foo.example.com')
18         validate_domain_name('www.example.2com')
19
20     ENDHEREDOC
21   ) do |args|
22
23     rescuable_exceptions = [ArgumentError]
24
25     if args.empty?
26       raise Puppet::ParseError, "validate_domain_name(): wrong number of arguments (#{args.length}; must be > 0)"
27     end
28
29     args.each do |arg|
30       raise Puppet::ParseError, "#{arg.inspect} is not a string." unless arg.is_a?(String)
31
32       begin
33         raise Puppet::ParseError, "#{arg.inspect} is not a syntactically correct domain name" unless function_is_domain_name([arg])
34       rescue *rescuable_exceptions
35         raise Puppet::ParseError, "#{arg.inspect} is not a syntactically correct domain name"
36       end
37     end
38   end
39 end