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