4 module Puppet::Parser::Functions
5 newfunction(:is_domain_name, :type => :rvalue, :doc => <<-DOC
6 Returns true if the string passed to this function is a syntactically correct domain name.
10 if arguments.size != 1
11 raise(Puppet::ParseError, "is_domain_name(): Wrong number of arguments given #{arguments.size} for 1")
14 # Only allow string types
15 return false unless arguments[0].is_a?(String)
17 domain = arguments[0].dup
19 # Limits (rfc1035, 3.1)
20 domain_max_length = 255
24 # Allow ".", it is the top level domain
25 return true if domain == '.'
27 # Remove the final dot, if present.
30 # Check the whole domain
31 return false if domain.empty?
32 return false if domain.length > domain_max_length
34 # The top level domain must be alphabetic if there are multiple labels.
36 return false if domain.include?('.') && !%r{\.[A-Za-z]+$}.match(domain)
38 # Check each label in the domain
39 labels = domain.split('.')
40 vlabels = labels.each do |label|
41 break if label.length < label_min_length
42 break if label.length > label_max_length
43 break if label[-1..-1] == '-'
44 break if label[0..0] == '-'
45 break unless %r{^[a-z\d-]+$}i =~ label
47 return vlabels == labels
51 # vim: set ts=2 sw=2 et :