diff options
author | TP Honey <tphoney@users.noreply.github.com> | 2017-07-31 16:23:11 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-07-31 16:23:11 +0100 |
commit | 48a8f762343b6a85fa68508dafb11c76f560b3df (patch) | |
tree | cbf45f13875e2a176413077351d5635af6011e6c /lib/puppet/parser/functions | |
parent | 0c8697477255f2ca6c477c0a960cd151478729e1 (diff) | |
parent | fe7ccd8b89556cc6cc1f5ea7f58b5ac2aedb23ec (diff) |
Merge pull request #753 from frapex/master
Add validate_domain_name function
Diffstat (limited to 'lib/puppet/parser/functions')
-rw-r--r-- | lib/puppet/parser/functions/validate_domain_name.rb | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/lib/puppet/parser/functions/validate_domain_name.rb b/lib/puppet/parser/functions/validate_domain_name.rb new file mode 100644 index 0000000..c3fad78 --- /dev/null +++ b/lib/puppet/parser/functions/validate_domain_name.rb @@ -0,0 +1,39 @@ +module Puppet::Parser::Functions + newfunction(:validate_domain_name, :doc => <<-ENDHEREDOC + Validate that all values passed are syntactically correct domain names. + Fail compilation if any value fails this check. + + The following values will pass: + + $my_domain_name = 'server.domain.tld' + validate_domain_name($my_domain_name) + validate_domain_name('domain.tld', 'puppet.com', $my_domain_name) + + The following values will fail, causing compilation to abort: + + validate_domain_name(1) + validate_domain_name(true) + validate_domain_name('invalid domain') + validate_domain_name('-foo.example.com') + validate_domain_name('www.example.2com') + + ENDHEREDOC + ) do |args| + + rescuable_exceptions = [ArgumentError] + + if args.empty? + raise Puppet::ParseError, "validate_domain_name(): wrong number of arguments (#{args.length}; must be > 0)" + end + + args.each do |arg| + raise Puppet::ParseError, "#{arg.inspect} is not a string." unless arg.is_a?(String) + + begin + raise Puppet::ParseError, "#{arg.inspect} is not a syntactically correct domain name" unless function_is_domain_name([arg]) + rescue *rescuable_exceptions + raise Puppet::ParseError, "#{arg.inspect} is not a syntactically correct domain name" + end + end + end +end |