PageRenderTime 39ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/puppet/parser/functions/validate_ipv6_address.rb

https://github.com/hunner/puppetlabs-stdlib
Ruby | 49 lines | 36 code | 13 blank | 0 comment | 2 complexity | 4699238e4cad60e7e1428905523eaeb7 MD5 | raw file
Possible License(s): Apache-2.0
  1. module Puppet::Parser::Functions
  2. newfunction(:validate_ipv6_address, :doc => <<-ENDHEREDOC
  3. Validate that all values passed are valid IPv6 addresses.
  4. Fail compilation if any value fails this check.
  5. The following values will pass:
  6. $my_ip = "3ffe:505:2"
  7. validate_ipv6_address(1)
  8. validate_ipv6_address($my_ip)
  9. validate_bool("fe80::baf6:b1ff:fe19:7507", $my_ip)
  10. The following values will fail, causing compilation to abort:
  11. $some_array = [ true, false, "garbage string", "1.2.3.4" ]
  12. validate_ipv6_address($some_array)
  13. ENDHEREDOC
  14. ) do |args|
  15. require "ipaddr"
  16. rescuable_exceptions = [ ArgumentError ]
  17. if defined?(IPAddr::InvalidAddressError)
  18. rescuable_exceptions << IPAddr::InvalidAddressError
  19. end
  20. unless args.length > 0 then
  21. raise Puppet::ParseError, ("validate_ipv6_address(): wrong number of arguments (#{args.length}; must be > 0)")
  22. end
  23. args.each do |arg|
  24. unless arg.is_a?(String)
  25. raise Puppet::ParseError, "#{arg.inspect} is not a string."
  26. end
  27. begin
  28. unless IPAddr.new(arg).ipv6?
  29. raise Puppet::ParseError, "#{arg.inspect} is not a valid IPv6 address."
  30. end
  31. rescue *rescuable_exceptions
  32. raise Puppet::ParseError, "#{arg.inspect} is not a valid IPv6 address."
  33. end
  34. end
  35. end
  36. end