From 66fe7f73d2a517e34430471039150bf9faf8fba2 Mon Sep 17 00:00:00 2001 From: Josh Cooper Date: Mon, 9 Jul 2018 20:33:53 -0700 Subject: Use Puppet::Type.newtype Most of this change is whitespace indentation. --- lib/puppet/type/host.rb | 132 ++++++++++++++++++++++++------------------------ 1 file changed, 65 insertions(+), 67 deletions(-) diff --git a/lib/puppet/type/host.rb b/lib/puppet/type/host.rb index 7979619..e348969 100644 --- a/lib/puppet/type/host.rb +++ b/lib/puppet/type/host.rb @@ -1,99 +1,97 @@ require 'puppet/property/ordered_list' -module Puppet - Type.newtype(:host) do - ensurable - - newproperty(:ip) do - desc "The host's IP address, IPv4 or IPv6." - - def valid_v4?(addr) - if %r{^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$} =~ addr - $LAST_MATCH_INFO.captures.all? do |i| - i = i.to_i - i >= 0 && i <= 255 - end - else - false - end - end +Puppet::Type.newtype(:host) do + ensurable - def valid_v6?(addr) - # http://forums.dartware.com/viewtopic.php?t=452 - # ...and, yes, it is this hard. Doing it programmatically is harder. - return true if addr =~ %r{^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$} + newproperty(:ip) do + desc "The host's IP address, IPv4 or IPv6." + def valid_v4?(addr) + if %r{^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$} =~ addr + $LAST_MATCH_INFO.captures.all? do |i| + i = i.to_i + i >= 0 && i <= 255 + end + else false end + end - def valid_newline?(addr) - return false if addr =~ %r{\n} || addr =~ %r{\r} - true - end + def valid_v6?(addr) + # http://forums.dartware.com/viewtopic.php?t=452 + # ...and, yes, it is this hard. Doing it programmatically is harder. + return true if addr =~ %r{^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$} - validate do |value| - return true if (valid_v4?(value) || valid_v6?(value)) && valid_newline?(value) - raise Puppet::Error, _('Invalid IP address %{value}') % { value: value.inspect } - end + false end - # for now we use OrderedList to indicate that the order does matter. - newproperty(:host_aliases, parent: Puppet::Property::OrderedList) do - desc "Any aliases the host might have. Multiple values must be + def valid_newline?(addr) + return false if addr =~ %r{\n} || addr =~ %r{\r} + true + end + + validate do |value| + return true if (valid_v4?(value) || valid_v6?(value)) && valid_newline?(value) + raise Puppet::Error, _('Invalid IP address %{value}') % { value: value.inspect } + end + end + + # for now we use OrderedList to indicate that the order does matter. + newproperty(:host_aliases, parent: Puppet::Property::OrderedList) do + desc "Any aliases the host might have. Multiple values must be specified as an array." - def delimiter - ' ' - end + def delimiter + ' ' + end - def inclusive? - true - end + def inclusive? + true + end - validate do |value| - # This regex already includes newline check. - raise Puppet::Error, _('Host aliases cannot include whitespace') if value =~ %r{\s} - raise Puppet::Error, _('Host aliases cannot be an empty string. Use an empty array to delete all host_aliases ') if value =~ %r{^\s*$} - end + validate do |value| + # This regex already includes newline check. + raise Puppet::Error, _('Host aliases cannot include whitespace') if value =~ %r{\s} + raise Puppet::Error, _('Host aliases cannot be an empty string. Use an empty array to delete all host_aliases ') if value =~ %r{^\s*$} end + end - newproperty(:comment) do - desc 'A comment that will be attached to the line with a # character.' - validate do |value| - raise Puppet::Error, _('Comment cannot include newline') if value =~ %r{\n} || value =~ %r{\r} - end + newproperty(:comment) do + desc 'A comment that will be attached to the line with a # character.' + validate do |value| + raise Puppet::Error, _('Comment cannot include newline') if value =~ %r{\n} || value =~ %r{\r} end + end - newproperty(:target) do - desc "The file in which to store service information. Only used by + newproperty(:target) do + desc "The file in which to store service information. Only used by those providers that write to disk. On most systems this defaults to `/etc/hosts`." - defaultto do - if @resource.class.defaultprovider.ancestors.include?(Puppet::Provider::ParsedFile) - @resource.class.defaultprovider.default_target - else - nil - end + defaultto do + if @resource.class.defaultprovider.ancestors.include?(Puppet::Provider::ParsedFile) + @resource.class.defaultprovider.default_target + else + nil end end + end - newparam(:name) do - desc 'The host name.' + newparam(:name) do + desc 'The host name.' - isnamevar + isnamevar - validate do |value| - value.split('.').each do |hostpart| - unless hostpart =~ %r{^([\w]+|[\w][\w\-]+[\w])$} - raise Puppet::Error, _('Invalid host name') - end + validate do |value| + value.split('.').each do |hostpart| + unless hostpart =~ %r{^([\w]+|[\w][\w\-]+[\w])$} + raise Puppet::Error, _('Invalid host name') end - raise Puppet::Error, _('Hostname cannot include newline') if value =~ %r{\n} || value =~ %r{\r} end + raise Puppet::Error, _('Hostname cannot include newline') if value =~ %r{\n} || value =~ %r{\r} end + end - @doc = "Installs and manages host entries. For most systems, these + @doc = "Installs and manages host entries. For most systems, these entries will just be in `/etc/hosts`, but some systems (notably OS X) will have different solutions." - end end -- cgit v1.2.3