comparison modules/stdlib/lib/puppet/parser/functions/is_domain_name.rb @ 0:956e484adc12

Initial public release of Puppet configs
author IBBoard <dev@ibboard.co.uk>
date Sat, 16 Aug 2014 19:47:38 +0000
parents
children addb0ea390a1
comparison
equal deleted inserted replaced
-1:000000000000 0:956e484adc12
1 #
2 # is_domain_name.rb
3 #
4
5 module Puppet::Parser::Functions
6 newfunction(:is_domain_name, :type => :rvalue, :doc => <<-EOS
7 Returns true if the string passed to this function is a syntactically correct domain name.
8 EOS
9 ) do |arguments|
10
11 if (arguments.size != 1) then
12 raise(Puppet::ParseError, "is_domain_name(): Wrong number of arguments "+
13 "given #{arguments.size} for 1")
14 end
15
16 domain = arguments[0]
17
18 # Limits (rfc1035, 3.1)
19 domain_max_length=255
20 label_min_length=1
21 label_max_length=63
22
23 # Allow ".", it is the top level domain
24 return true if domain == '.'
25
26 # Remove the final dot, if present.
27 domain.chomp!('.')
28
29 # Check the whole domain
30 return false if domain.empty?
31 return false if domain.length > domain_max_length
32
33 # Check each label in the domain
34 labels = domain.split('.')
35 vlabels = labels.each do |label|
36 break if label.length < label_min_length
37 break if label.length > label_max_length
38 break if label[-1..-1] == '-'
39 break if label[0..0] == '-'
40 break unless /^[a-z\d-]+$/i.match(label)
41 end
42 return vlabels == labels
43
44 end
45 end
46
47 # vim: set ts=2 sw=2 et :