aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorJosh Cooper <josh@puppet.com>2018-07-09 21:57:33 -0700
committerJosh Cooper <josh@puppet.com>2018-07-10 11:03:14 -0700
commitc8d6404fb8263691578cc731cc2b5978834cf626 (patch)
treea05f49dca045ab7e59f7c0768237678ef2d08c26 /lib
parent53c12ae853476c6bb9f4c1ab761ddf88413e339b (diff)
downloadpuppet-hosts_core-c8d6404fb8263691578cc731cc2b5978834cf626.tar.gz
puppet-hosts_core-c8d6404fb8263691578cc731cc2b5978834cf626.tar.bz2
Don't use global matchdata
Simplify logic for validating each IPv4 tuple. Remove explicit return based on the result of the conditional.
Diffstat (limited to 'lib')
-rw-r--r--lib/puppet/type/host.rb27
1 files changed, 11 insertions, 16 deletions
diff --git a/lib/puppet/type/host.rb b/lib/puppet/type/host.rb
index 52f0e87..03059b9 100644
--- a/lib/puppet/type/host.rb
+++ b/lib/puppet/type/host.rb
@@ -11,27 +11,18 @@ Puppet::Type.newtype(:host) 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
+ data = addr.match(%r{^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$})
+ data && data.captures.map(&:to_i).all? { |i| i >= 0 && i <= 255 }
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}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}:){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*$}
end
def valid_newline?(addr)
- return false if addr =~ %r{\n} || addr =~ %r{\r}
- true
+ addr !~ %r{\n} && addr !~ %r{\r}
end
validate do |value|
@@ -63,7 +54,9 @@ Puppet::Type.newtype(:host) do
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}
+ if value =~ %r{\n} || value =~ %r{\r}
+ raise Puppet::Error, _('Comment cannot include newline')
+ end
end
end
@@ -87,11 +80,13 @@ Puppet::Type.newtype(:host) do
validate do |value|
value.split('.').each do |hostpart|
- unless hostpart =~ %r{^([\w]+|[\w][\w\-]+[\w])$}
+ if hostpart !~ %r{^([\w]+|[\w][\w\-]+[\w])$}
raise Puppet::Error, _('Invalid host name')
end
end
- raise Puppet::Error, _('Hostname cannot include newline') if value =~ %r{\n} || value =~ %r{\r}
+ if value =~ %r{\n} || value =~ %r{\r}
+ raise Puppet::Error, _('Hostname cannot include newline')
+ end
end
end
end