Update stdlib and concat to 6.1.0 both
[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     @summary
7       Validate that all values passed are syntactically correct domain names.
8       Fail compilation if any value fails this check.
9
10     @return
11       passes when the given values are syntactically correct domain names or raise an error when they are not and fails compilation
12
13     @example **Usage**
14
15       The following values will pass:
16
17           $my_domain_name = 'server.domain.tld'
18           validate_domain_name($my_domain_name)
19           validate_domain_name('domain.tld', 'puppet.com', $my_domain_name)
20
21       The following values will fail, causing compilation to abort:
22
23           validate_domain_name(1)
24           validate_domain_name(true)
25           validate_domain_name('invalid domain')
26           validate_domain_name('-foo.example.com')
27           validate_domain_name('www.example.2com')
28     DOC
29              ) do |args|
30
31     rescuable_exceptions = [ArgumentError]
32
33     if args.empty?
34       raise Puppet::ParseError, "validate_domain_name(): wrong number of arguments (#{args.length}; must be > 0)"
35     end
36
37     args.each do |arg|
38       raise Puppet::ParseError, "#{arg.inspect} is not a string." unless arg.is_a?(String)
39
40       begin
41         raise Puppet::ParseError, "#{arg.inspect} is not a syntactically correct domain name" unless function_is_domain_name([arg])
42       rescue *rescuable_exceptions
43         raise Puppet::ParseError, "#{arg.inspect} is not a syntactically correct domain name"
44       end
45     end
46   end
47 end